diff --git a/TODO.txt b/TODO.txt index 6d22191ec..95750293b 100644 --- a/TODO.txt +++ b/TODO.txt @@ -214,9 +214,9 @@ * record game difficulty DONE 12/7/2024 * standardize game ids DONE 12/7/2024 * bufix: mini map doesn't load in time DONE 12/7/2024 -* bugfix: private game host game doesn't start -* record game winner +* bugfix: private game host game doesn't start DONE 12/8/2024 * record commit hash of game +* record game winner * store metadata in BigQuery * replay stored games * max price for units diff --git a/src/client/Transport.ts b/src/client/Transport.ts index 6fa98210d..cd15c5a87 100644 --- a/src/client/Transport.ts +++ b/src/client/Transport.ts @@ -123,16 +123,17 @@ export class Transport { private startPing() { if (this.isLocal || this.pingInterval) return; - - this.pingInterval = window.setInterval(() => { - if (this.socket != null && this.socket.readyState === WebSocket.OPEN) { - this.sendMsg(JSON.stringify(ClientPingMessageSchema.parse({ - type: 'ping', - clientID: this.clientID, - gameID: this.gameID, - }))) - } - }, 10000); + if (this.pingInterval == null) { + this.pingInterval = window.setInterval(() => { + if (this.socket != null && this.socket.readyState === WebSocket.OPEN) { + this.sendMsg(JSON.stringify(ClientPingMessageSchema.parse({ + type: 'ping', + clientID: this.clientID, + gameID: this.gameID, + }))) + } + }, 10000); + } } private stopPing() { @@ -157,12 +158,10 @@ export class Transport { private connectRemote(onconnect: () => void, onmessage: (message: ServerMessage) => void) { this.startPing() - const isFirstConnect = this.socket == null - if (isFirstConnect) { - const wsHost = process.env.WEBSOCKET_URL || window.location.host; - const wsProtocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'; - this.socket = new WebSocket(`${wsProtocol}//${wsHost}`) - } + this.maybeKillSocket() + const wsHost = process.env.WEBSOCKET_URL || window.location.host; + const wsProtocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'; + this.socket = new WebSocket(`${wsProtocol}//${wsHost}`) this.onconnect = onconnect this.onmessage = onmessage this.socket.onopen = () => { @@ -187,10 +186,6 @@ export class Transport { this.connect(onconnect, onmessage) } }; - if (!isFirstConnect) { - // Socket has already been opened, so simulate new connection. - onconnect() - } } joinGame(numTurns: number) { @@ -368,4 +363,22 @@ export class Transport { } } } + + private maybeKillSocket(): void { + if (this.socket == null) { + return + } + // Remove all event listeners + this.socket.onmessage = null; + this.socket.onopen = null; + this.socket.onclose = null; + this.socket.onerror = null; + + // Close the connection if it's still open + if (this.socket.readyState === WebSocket.OPEN) { + this.socket.close(); + } + this.socket = null + } + } \ No newline at end of file diff --git a/src/server/GameServer.ts b/src/server/GameServer.ts index 9b61cf434..81c47e7e2 100644 --- a/src/server/GameServer.ts +++ b/src/server/GameServer.ts @@ -1,4 +1,4 @@ -import { ClientMessage, ClientMessageSchema, GameConfig, GameRecordSchema, Intent, ServerStartGameMessage, ServerStartGameMessageSchema, ServerTurnMessageSchema, Turn } from "../core/Schemas"; +import { ClientMessage, ClientMessageSchema, GameConfig, GameRecordSchema, Intent, ServerPingMessageSchema, ServerStartGameMessage, ServerStartGameMessageSchema, ServerTurnMessageSchema, Turn } from "../core/Schemas"; import { Config } from "../core/configuration/Config"; import { Client } from "./Client"; import WebSocket from 'ws'; @@ -35,7 +35,7 @@ export class GameServer { private config: Config, private gameConfig: GameConfig, - ) { } + ) {} public updateGameConfig(gameConfig: GameConfig): void { if (gameConfig.gameMap != null) { @@ -103,11 +103,11 @@ export class GameServer { this._hasStarted = true this._startTime = Date.now() + this.endTurnIntervalID = setInterval(() => this.endTurn(), this.config.turnIntervalMs()); this.clients.forEach(c => { console.log(`game ${this.id} sending start message to ${c.id}`) this.sendStartGameMsg(c.ws, 0) }) - this.endTurnIntervalID = setInterval(() => this.endTurn(), this.config.turnIntervalMs()); } private addIntent(intent: Intent) {