Skip to content

Commit 1ae7ebc

Browse files
Simplify GameServer message creation (#1970)
## Description: Simplify `GameServer` message creation. ## Please complete the following: - [x] I have added screenshots for all UI updates - [x] I process any text displayed to the user through translateText() and I've added it to the en.json file - [x] I have added relevant tests to the test directory - [x] I confirm I have thoroughly tested these changes and take full responsibility for any bugs introduced
1 parent 2094249 commit 1ae7ebc

File tree

1 file changed

+8
-33
lines changed

1 file changed

+8
-33
lines changed

src/server/GameServer.ts

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ import {
77
GameStartInfoSchema,
88
Intent,
99
PlayerRecord,
10+
ServerDesyncMessage,
1011
ServerDesyncSchema,
1112
ServerErrorMessage,
13+
ServerPrestartMessage,
1214
ServerPrestartMessageSchema,
1315
ServerStartGameMessage,
1416
ServerTurnMessage,
@@ -258,22 +260,10 @@ export class GameServer {
258260
}
259261
this._hasPrestarted = true;
260262

261-
const prestartMsg = ServerPrestartMessageSchema.safeParse({
263+
const msg = JSON.stringify({
262264
gameMap: this.gameConfig.gameMap,
263265
type: "prestart",
264-
});
265-
266-
if (!prestartMsg.success) {
267-
console.error(
268-
`error creating prestart message for game ${this.id}, ${prestartMsg.error}`.substring(
269-
0,
270-
250,
271-
),
272-
);
273-
return;
274-
}
275-
276-
const msg = JSON.stringify(prestartMsg.data);
266+
} satisfies ServerPrestartMessage);
277267
this.activeClients.forEach((c) => {
278268
this.log.info("sending prestart message", {
279269
clientID: c.clientID,
@@ -293,7 +283,7 @@ export class GameServer {
293283
// if no client connects/pings.
294284
this.lastPingUpdate = Date.now();
295285

296-
const result = GameStartInfoSchema.safeParse({
286+
this.gameStartInfo = {
297287
config: this.gameConfig,
298288
gameID: this.id,
299289
players: this.activeClients.map((c) => ({
@@ -302,13 +292,7 @@ export class GameServer {
302292
pattern: c.pattern,
303293
username: c.username,
304294
})),
305-
});
306-
if (!result.success) {
307-
const error = z.prettifyError(result.error);
308-
this.log.error("Error parsing game start info", { message: error });
309-
return;
310-
}
311-
this.gameStartInfo = result.data satisfies GameStartInfo;
295+
};
312296

313297
this.endTurnIntervalID = setInterval(
314298
() => this.endTurn(),
@@ -622,23 +606,14 @@ export class GameServer {
622606
return;
623607
}
624608

625-
const serverDesync = ServerDesyncSchema.safeParse({
609+
const desyncMsg = JSON.stringify({
626610
clientsWithCorrectHash:
627611
this.activeClients.length - outOfSyncClients.length,
628612
correctHash: mostCommonHash,
629613
totalActiveClients: this.activeClients.length,
630614
turn: lastHashTurn,
631615
type: "desync",
632-
});
633-
if (!serverDesync.success) {
634-
this.log.warn("failed to create desync message", {
635-
error: serverDesync.error,
636-
gameID: this.id,
637-
});
638-
return;
639-
}
640-
641-
const desyncMsg = JSON.stringify(serverDesync.data);
616+
} satisfies ServerDesyncMessage);
642617
for (const c of outOfSyncClients) {
643618
this.outOfSyncClients.add(c.clientID);
644619
if (this.sentDesyncMessageClients.has(c.clientID)) {

0 commit comments

Comments
 (0)