send game hash to server each second

This commit is contained in:
Evan
2025-02-25 11:49:07 -08:00
parent 808107c9c3
commit e0938253df
11 changed files with 93 additions and 25 deletions
+11 -2
View File
@@ -48,7 +48,8 @@ export type ClientMessage =
| ClientPingMessage
| ClientIntentMessage
| ClientJoinMessage
| ClientLogMessage;
| ClientLogMessage
| ClientHashMessage;
export type ServerMessage =
| ServerSyncMessage
| ServerStartGameMessage
@@ -65,6 +66,7 @@ export type ClientPingMessage = z.infer<typeof ClientPingMessageSchema>;
export type ClientIntentMessage = z.infer<typeof ClientIntentMessageSchema>;
export type ClientJoinMessage = z.infer<typeof ClientJoinMessageSchema>;
export type ClientLogMessage = z.infer<typeof ClientLogMessageSchema>;
export type ClientHashMessage = z.infer<typeof ClientHashSchema>;
export type PlayerRecord = z.infer<typeof PlayerRecordSchema>;
export type GameRecord = z.infer<typeof GameRecordSchema>;
@@ -268,7 +270,7 @@ export const ServerMessageSchema = z.union([
// Client
const ClientBaseMessageSchema = z.object({
type: z.enum(["winner", "join", "intent", "ping", "log"]),
type: z.enum(["winner", "join", "intent", "ping", "log", "hash"]),
clientID: ID,
persistentID: SafeString.nullable(), // WARNING: persistent id is private.
gameID: ID,
@@ -279,6 +281,12 @@ export const ClientSendWinnerSchema = ClientBaseMessageSchema.extend({
winner: ID.nullable(),
});
export const ClientHashSchema = ClientBaseMessageSchema.extend({
type: z.literal("hash"),
hash: z.number(),
tick: z.number(),
});
export const ClientLogMessageSchema = ClientBaseMessageSchema.extend({
type: z.literal("log"),
severity: z.nativeEnum(LogSeverity),
@@ -308,6 +316,7 @@ export const ClientMessageSchema = z.union([
ClientIntentMessageSchema,
ClientJoinMessageSchema,
ClientLogMessageSchema,
ClientHashSchema,
]);
export const PlayerRecordSchema = z.object({
+18 -10
View File
@@ -241,21 +241,29 @@ export class GameImpl implements Game {
this.execs.push(...inited);
this.unInitExecs = unInited;
this._ticks++;
if (this._ticks % 100 == 0) {
let hash = 1;
this._players.forEach((p) => {
hash += p.hash();
});
consolex.log(`tick ${this._ticks}: hash ${hash}`);
}
for (const player of this._players.values()) {
// Players change each to so always add them
this.addUpdate(player.toUpdate());
}
if (this.ticks() % 10 == 0) {
this.addUpdate({
type: GameUpdateType.Hash,
tick: this.ticks(),
hash: this.hash(),
});
}
this._ticks++;
return this.updates;
}
private hash(): number {
let hash = 1;
this._players.forEach((p) => {
hash += p.hash();
});
return hash;
}
terraNullius(): TerraNullius {
return this._terraNullius;
}
@@ -494,14 +502,14 @@ export class GameImpl implements Game {
sendEmojiUpdate(msg: EmojiMessage): void {
this.addUpdate({
type: GameUpdateType.EmojiUpdate,
type: GameUpdateType.Emoji,
emoji: msg,
});
}
setWinner(winner: Player): void {
this.addUpdate({
type: GameUpdateType.WinUpdate,
type: GameUpdateType.Win,
winnerID: winner.smallID(),
});
}
+13 -5
View File
@@ -35,8 +35,9 @@ export enum GameUpdateType {
BrokeAlliance,
AllianceExpired,
TargetPlayer,
EmojiUpdate,
WinUpdate,
Emoji,
Win,
Hash,
}
export type GameUpdate =
@@ -50,7 +51,8 @@ export type GameUpdate =
| DisplayMessageUpdate
| TargetPlayerUpdate
| EmojiUpdate
| WinUpdate;
| WinUpdate
| HashUpdate;
export interface TileUpdateWrapper {
type: GameUpdateType.Tile;
@@ -135,7 +137,7 @@ export interface TargetPlayerUpdate {
}
export interface EmojiUpdate {
type: GameUpdateType.EmojiUpdate;
type: GameUpdateType.Emoji;
emoji: EmojiMessage;
}
@@ -147,6 +149,12 @@ export interface DisplayMessageUpdate {
}
export interface WinUpdate {
type: GameUpdateType.WinUpdate;
type: GameUpdateType.Win;
winnerID: number;
}
export interface HashUpdate {
type: GameUpdateType.Hash;
tick: Tick;
hash: number;
}
+1 -1
View File
@@ -135,7 +135,7 @@ export class UnitImpl implements Unit {
}
hash(): number {
return this.tile() + simpleHash(this.type());
return this.tile() + simpleHash(this.type()) * this._id;
}
toString(): string {