created prestart message: add 2 second delay before starting public games to allow all players to connect

This commit is contained in:
Evan
2025-04-03 15:42:25 -07:00
parent f09801fe3e
commit 950344c0bd
8 changed files with 105 additions and 33 deletions
+27 -9
View File
@@ -21,7 +21,7 @@ import {
WinUpdate,
} from "../core/game/GameUpdates";
import { GameView, PlayerView, UnitView } from "../core/game/GameView";
import { loadTerrainMap } from "../core/game/TerrainMapLoader";
import { loadTerrainMap, TerrainMapData } from "../core/game/TerrainMapLoader";
import { UserSettings } from "../core/game/UserSettings";
import { WorkerClient } from "../core/worker/WorkerClient";
import { InputHandler, MouseMoveEvent, MouseUpEvent } from "./InputHandler";
@@ -60,7 +60,8 @@ export interface LobbyConfig {
export function joinLobby(
lobbyConfig: LobbyConfig,
onjoin: () => void,
onPrestart: () => void,
onJoin: () => void,
): () => void {
const eventBus = new EventBus();
initRemoteSender(eventBus);
@@ -78,15 +79,28 @@ export function joinLobby(
consolex.log(`Joined game lobby ${lobbyConfig.gameID}`);
transport.joinGame(0);
};
let terrainLoad: Promise<TerrainMapData> | null = null;
const onmessage = (message: ServerMessage) => {
if (message.type == "prestart") {
consolex.log(`lobby: game prestarting: ${JSON.stringify(message)}`);
terrainLoad = loadTerrainMap(message.gameMap);
onPrestart();
}
if (message.type == "start") {
// Trigger prestart for singleplayer games
onPrestart();
consolex.log(`lobby: game started: ${JSON.stringify(message)}`);
onjoin();
onJoin();
// For multiplayer games, GameStartInfo is not known until game starts.
lobbyConfig.gameStartInfo = message.gameStartInfo;
createClientGame(lobbyConfig, eventBus, transport, userSettings).then(
(r) => r.start(),
);
createClientGame(
lobbyConfig,
eventBus,
transport,
userSettings,
terrainLoad,
).then((r) => r.start());
}
};
transport.connect(onconnect, onmessage);
@@ -101,15 +115,19 @@ export async function createClientGame(
eventBus: EventBus,
transport: Transport,
userSettings: UserSettings,
terrainLoad: Promise<TerrainMapData> | null,
): Promise<ClientGameRunner> {
const config = await getConfig(
lobbyConfig.gameStartInfo.config,
userSettings,
);
let gameMap: TerrainMapData | null = null;
const gameMap = await loadTerrainMap(
lobbyConfig.gameStartInfo.config.gameMap,
);
if (terrainLoad) {
gameMap = await terrainLoad;
} else {
gameMap = await loadTerrainMap(lobbyConfig.gameStartInfo.config.gameMap);
}
const worker = new WorkerClient(
lobbyConfig.gameStartInfo,
lobbyConfig.clientID,