feat: replay archived games, gamestate hash verification (#195)

create endpoint to load archived game. when joining game client first
checks if the game is active, if not it requests the game archive from
the server. the archive is sent to LocalServer to replay the game
locally. Every 10 ticks a hash is stored on the archive, and during
replay the LocalServer verifies this hash.
This commit is contained in:
evanpelle
2025-03-09 14:24:39 -07:00
committed by GitHub
parent 84951fed9f
commit 33292aec5c
16 changed files with 239 additions and 161 deletions
+21 -2
View File
@@ -255,7 +255,7 @@ export function onlyImages(html: string) {
});
}
export function CreateGameRecord(
export function createGameRecord(
id: GameID,
gameConfig: GameConfig,
// username does not need to be set.
@@ -278,7 +278,7 @@ export function CreateGameRecord(
};
for (const turn of turns) {
if (turn.intents.length != 0) {
if (turn.intents.length != 0 || turn.hash != undefined) {
record.turns.push(turn);
for (const intent of turn.intents) {
if (intent.type == "spawn") {
@@ -300,6 +300,25 @@ export function CreateGameRecord(
return record;
}
export function decompressGameRecord(gameRecord: GameRecord) {
const turns = [];
let lastTurnNum = 0;
for (const turn of gameRecord.turns) {
while (lastTurnNum < turn.turnNumber - 1) {
lastTurnNum++;
turns.push({
gameID: gameRecord.id,
turnNumber: lastTurnNum,
intents: [],
});
}
turns.push(turn);
lastTurnNum = turn.turnNumber;
}
gameRecord.turns = turns;
return gameRecord;
}
export function assertNever(x: never): never {
throw new Error("Unexpected value: " + x);
}