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
+36 -2
View File
@@ -13,6 +13,7 @@ import {
Intent,
PlayerRecord,
ServerDesyncSchema,
ServerPrestartMessageSchema,
ServerStartGameMessageSchema,
ServerTurnMessageSchema,
Turn,
@@ -55,6 +56,8 @@ export class GameServer {
private log: Logger;
private _hasPrestarted = false;
constructor(
public readonly id: string,
readonly log_: Logger,
@@ -219,7 +222,38 @@ export class GameServer {
}
}
public prestart() {
if (this.hasStarted()) {
return;
}
this._hasPrestarted = true;
const prestartMsg = ServerPrestartMessageSchema.safeParse({
type: "prestart",
gameMap: this.gameConfig.gameMap,
});
if (!prestartMsg.success) {
console.error(
`error creating prestart message for game ${this.id}, ${prestartMsg.error}`.substring(
0,
250,
),
);
return;
}
const msg = JSON.stringify(prestartMsg.data);
this.activeClients.forEach((c) => {
this.log.info(`${this.id}: sending prestart message to ${c.clientID}`);
c.ws.send(msg);
});
}
public start() {
if (this._hasStarted) {
return;
}
this._hasStarted = true;
this._startTime = Date.now();
// Set last ping to start so we don't immediately stop the game
@@ -424,7 +458,7 @@ export class GameServer {
}
hasStarted(): boolean {
return this._hasStarted;
return this._hasStarted || this._hasPrestarted;
}
public gameInfo(): GameInfo {
@@ -446,7 +480,7 @@ export class GameServer {
}
private handleSynchronization() {
if (this.activeClients.length < 1) {
if (this.activeClients.length <= 1) {
return;
}
if (this.turns.length % 10 != 0 || this.turns.length < 10) {