have server check hashes, crash game if out of sync

This commit is contained in:
Evan
2025-03-02 09:29:10 -08:00
parent d92a68e958
commit ff33c2db50
5 changed files with 112 additions and 3 deletions
+13 -2
View File
@@ -53,13 +53,15 @@ export type ClientMessage =
export type ServerMessage =
| ServerSyncMessage
| ServerStartGameMessage
| ServerPingMessage;
| ServerPingMessage
| ServerDesyncMessage;
export type ServerSyncMessage = z.infer<typeof ServerTurnMessageSchema>;
export type ServerStartGameMessage = z.infer<
typeof ServerStartGameMessageSchema
>;
export type ServerPingMessage = z.infer<typeof ServerPingMessageSchema>;
export type ServerDesyncMessage = z.infer<typeof ServerDesyncSchema>;
export type ClientSendWinnerMessage = z.infer<typeof ClientSendWinnerSchema>;
export type ClientPingMessage = z.infer<typeof ClientPingMessageSchema>;
@@ -242,7 +244,7 @@ export const TurnSchema = z.object({
// Server
const ServerBaseMessageSchema = z.object({
type: SafeString,
type: z.enum(["turn", "ping", "start", "desync"]),
});
export const ServerTurnMessageSchema = ServerBaseMessageSchema.extend({
@@ -261,10 +263,19 @@ export const ServerStartGameMessageSchema = ServerBaseMessageSchema.extend({
config: GameConfigSchema,
});
export const ServerDesyncSchema = ServerBaseMessageSchema.extend({
type: z.literal("desync"),
turn: z.number(),
correctHash: z.number().nullable(),
clientsWithCorrectHash: z.number(),
totalActiveClients: z.number(),
});
export const ServerMessageSchema = z.union([
ServerTurnMessageSchema,
ServerStartGameMessageSchema,
ServerPingMessageSchema,
ServerDesyncSchema,
]);
// Client
+1
View File
@@ -30,6 +30,7 @@ import { UnitImpl } from "./UnitImpl";
import { consolex } from "../Consolex";
import { GameMap, GameMapImpl, TileRef, TileUpdate } from "./GameMap";
import { DefenseGrid } from "./DefensePostGrid";
import { simpleHash } from "../Util";
export function createGame(
gameMap: GameMap,