From cb9b5a7a17e8766f24fba17a4ffe758885a917ba Mon Sep 17 00:00:00 2001 From: Scott Anderson Date: Wed, 7 May 2025 14:11:18 -0400 Subject: [PATCH] Remove clientID, persistentID, gameID from ClientBaseMessageSchema, TurnSchema (#666) ## Description: Remove the devils `clientID`, `persistentID`, and `gameID` from `ClientBaseMessageSchema`, as well as `gameID` from `TurnSchema`. These values are already known through the `Client` object that is hoisted into the relevant handler. ## Please complete the following: - [x] I have added screenshots for all UI updates - [x] I confirm I have thoroughly tested these changes and take full responsibility for any bugs introduced - [x] I understand that submitting code with bugs that could have been caught through manual testing blocks releases and new features for all contributors --------- Co-authored-by: Scott Anderson <662325+scottanderson@users.noreply.github.com> --- src/client/ClientGameRunner.ts | 1 - src/client/LocalServer.ts | 1 - src/core/Schemas.ts | 4 ---- src/server/GameServer.ts | 41 +++++++++------------------------- 4 files changed, 10 insertions(+), 37 deletions(-) diff --git a/src/client/ClientGameRunner.ts b/src/client/ClientGameRunner.ts index 1b8aefe22..941e07a46 100644 --- a/src/client/ClientGameRunner.ts +++ b/src/client/ClientGameRunner.ts @@ -275,7 +275,6 @@ export class ClientGameRunner { while (turn.turnNumber - 1 > this.turnsSeen) { this.worker.sendTurn({ turnNumber: this.turnsSeen, - gameID: turn.gameID, intents: [], }); this.turnsSeen++; diff --git a/src/client/LocalServer.ts b/src/client/LocalServer.ts index 8248a1603..42a777129 100644 --- a/src/client/LocalServer.ts +++ b/src/client/LocalServer.ts @@ -127,7 +127,6 @@ export class LocalServer { } const pastTurn: Turn = { turnNumber: this.turns.length, - gameID: this.lobbyConfig.gameStartInfo.gameID, intents: this.intents, }; this.turns.push(pastTurn); diff --git a/src/core/Schemas.ts b/src/core/Schemas.ts index fba00a877..61f3d1eea 100644 --- a/src/core/Schemas.ts +++ b/src/core/Schemas.ts @@ -290,7 +290,6 @@ const IntentSchema = z.union([ export const TurnSchema = z.object({ turnNumber: z.number(), - gameID: ID, intents: z.array(IntentSchema), // The hash of the game state at the end of the turn. hash: z.number().nullable().optional(), @@ -357,9 +356,6 @@ export const ServerMessageSchema = z.union([ const ClientBaseMessageSchema = z.object({ type: z.enum(["winner", "join", "intent", "ping", "log", "hash"]), - clientID: ID, - persistentID: SafeString.nullable(), // WARNING: persistent id is private. - gameID: ID, }); export const ClientSendWinnerSchema = ClientBaseMessageSchema.extend({ diff --git a/src/server/GameServer.ts b/src/server/GameServer.ts index df95296f9..49830cfef 100644 --- a/src/server/GameServer.ts +++ b/src/server/GameServer.ts @@ -128,10 +128,14 @@ export class GameServer { (c) => c.clientID == client.clientID, ); if (existing != null) { - if (client.persistentID != existing.persistentID) { - console.warn( - `client ${client.clientID} cannot rejoin game, persistent id mismatch: exist pid: ${existing.persistentID}, new pid: ${client.persistentID}`, - ); + if (client.persistentID !== existing.persistentID) { + this.log.error("persistent ids do not match", { + clientID: client.clientID, + clientIP: client.ip, + clientPersistentID: client.persistentID, + existingIP: existing.ip, + existingPersistentID: existing.persistentID, + }); return; } existing.ws.removeAllListeners("message"); @@ -154,34 +158,10 @@ export class GameServer { } catch (error) { throw Error(`error parsing schema for ${client.ip}`); } - if (this.allClients.has(clientMsg.clientID)) { - const client = this.allClients.get(clientMsg.clientID); - if (client.persistentID != clientMsg.persistentID) { - this.log.warn( - `Client ID ${clientMsg.clientID} sent incorrect id ${clientMsg.persistentID}, does not match persistent id ${client.persistentID}`, - { - clientID: clientMsg.clientID, - persistentID: clientMsg.persistentID, - }, - ); - return; - } - } - - // Clear out persistent id to make sure it doesn't get sent to other clients. - clientMsg.persistentID = null; - if (clientMsg.type == "intent") { - if (clientMsg.gameID != this.id) { - this.log.warn("client sent to wrong game", { - clientID: clientMsg.clientID, - persistentID: clientMsg.persistentID, - }); - return; - } - if (clientMsg.intent.clientID != clientMsg.clientID) { + if (clientMsg.intent.clientID != client.clientID) { this.log.warn( - `client id mismatch, client message: ${clientMsg.clientID}, intent client id ${clientMsg.intent.clientID}`, + `client id mismatch, client: ${client.clientID}, intent: ${clientMsg.intent.clientID}`, ); return; } @@ -335,7 +315,6 @@ export class GameServer { private endTurn() { const pastTurn: Turn = { turnNumber: this.turns.length, - gameID: this.id, intents: this.intents, }; this.turns.push(pastTurn);