mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-07-07 15:55:58 +00:00
created prestart message: add 2 second delay before starting public games to allow all players to connect
This commit is contained in:
@@ -64,11 +64,16 @@ export class GameManager {
|
||||
const phase = game.phase();
|
||||
if (phase == GamePhase.Active) {
|
||||
if (!game.hasStarted()) {
|
||||
try {
|
||||
game.start();
|
||||
} catch (error) {
|
||||
this.log.error(`error starting game ${id}: ${error}`);
|
||||
}
|
||||
// Prestart tells clients to start loading the game.
|
||||
game.prestart();
|
||||
// Start game on delay to allow time for clients to connect.
|
||||
setTimeout(() => {
|
||||
try {
|
||||
game.start();
|
||||
} catch (error) {
|
||||
this.log.error(`error starting game ${id}: ${error}`);
|
||||
}
|
||||
}, 2000);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -154,6 +154,10 @@ export function startWorker() {
|
||||
log.warn(`cannot update public game ${game.id}, ip: ${clientIP}`);
|
||||
return res.status(400);
|
||||
}
|
||||
if (game.hasStarted()) {
|
||||
log.warn(`cannot update game ${game.id} after it has started`);
|
||||
return res.status(400);
|
||||
}
|
||||
game.updateGameConfig({
|
||||
gameMap: req.body.gameMap,
|
||||
difficulty: req.body.difficulty,
|
||||
|
||||
Reference in New Issue
Block a user