Can select map on private lobbies

This commit is contained in:
evanpelle
2024-10-16 20:34:57 -07:00
parent d92dc9eba6
commit 206f6d3333
6 changed files with 55 additions and 14 deletions
+13 -3
View File
@@ -1,8 +1,9 @@
import {Config} from "../core/configuration/Config";
import {ClientID, GameID} from "../core/Schemas";
import {ClientID, GameConfig, GameID} from "../core/Schemas";
import {v4 as uuidv4} from 'uuid';
import {Client} from "./Client";
import {GamePhase, GameServer} from "./GameServer";
import {GameMap} from "../core/game/Game";
@@ -27,9 +28,18 @@ export class GameManager {
game.addClient(client, lastTurn)
}
updateGameConfig(gameID: GameID, gameConfig: GameConfig) {
const game = this.games.find(g => g.id == gameID)
if (game == null) {
console.warn(`game ${gameID} not found`)
return
}
game.updateGameConfig(gameConfig)
}
createPrivateGame(): string {
const id = genSmallGameID()
this.games.push(new GameServer(id, Date.now(), false, this.config))
this.games.push(new GameServer(id, Date.now(), false, this.config, {gameMap: GameMap.World}))
return id
}
@@ -58,7 +68,7 @@ export class GameManager {
if (now > this.lastNewLobby + this.config.gameCreationRate()) {
this.lastNewLobby = now
const id = uuidv4()
lobbies.push(new GameServer(id, now, true, this.config))
lobbies.push(new GameServer(id, now, true, this.config, {gameMap: GameMap.World}))
}
active.filter(g => !g.hasStarted() && g.isPublic).forEach(g => {
+12 -3
View File
@@ -1,4 +1,4 @@
import {ClientMessage, ClientMessageSchema, Intent, ServerStartGameMessage, ServerStartGameMessageSchema, ServerTurnMessageSchema, Turn} from "../core/Schemas";
import {ClientMessage, ClientMessageSchema, GameConfig, Intent, ServerStartGameMessage, ServerStartGameMessageSchema, ServerTurnMessageSchema, Turn} from "../core/Schemas";
import {Config} from "../core/configuration/Config";
import {Client} from "./Client";
import WebSocket from 'ws';
@@ -23,13 +23,21 @@ export class GameServer {
private endTurnIntervalID
constructor(
public readonly id: string,
public readonly createdAt: number,
public readonly isPublic: boolean,
private config: Config,
private gameConfig: GameConfig,
) { }
public updateGameConfig(gameConfig: GameConfig): void {
if (gameConfig.gameMap != null) {
this.gameConfig.gameMap = gameConfig.gameMap
}
}
public addClient(client: Client, lastTurn: number) {
console.log(`game ${this.id} adding client ${client.id}`)
slog('client_joined_game', `client ${client.id} (re)joining game ${this.id}`, {
@@ -93,7 +101,8 @@ export class GameServer {
ws.send(JSON.stringify(ServerStartGameMessageSchema.parse(
{
type: "start",
turns: this.turns.slice(lastTurn)
turns: this.turns.slice(lastTurn),
config: this.gameConfig
}
)))
}
@@ -137,7 +146,7 @@ export class GameServer {
if (!this.isPublic) {
if (this._hasStarted) {
if (this.clients.length == 0) {
console.log(`game ${this.id} is finisehd`)
console.log()
return GamePhase.Finished
} else {
return GamePhase.Active
+3 -3
View File
@@ -23,8 +23,6 @@ app.use(express.static(path.join(__dirname, '../../out')));
app.use(express.json())
const gm = new GameManager(getConfig())
const privateGames = new Map<string, GameServer>()
// New GET endpoint to list lobbies
app.get('/lobbies', (req, res) => {
const now = Date.now()
@@ -35,6 +33,7 @@ app.get('/lobbies', (req, res) => {
});
});
app.post('/private_lobby', (req, res) => {
const id = gm.createPrivateGame()
console.log('creating private lobby with id ${id}')
@@ -48,8 +47,9 @@ app.post('/start_private_lobby/:id', (req, res) => {
gm.startPrivateGame(req.params.id)
});
app.put('/private_lobby/:id', (req, res) => {
const lobbyID = req.params.id
gm.updateGameConfig(lobbyID, {gameMap: req.body.gameMap})
});
app.get('/lobby/:id/exists', (req, res) => {