Room codes also dodge orphaned files on disk
makeRoomCode checked only the in-memory map, which covers every live and restored room — but a file that failed to restore stays on disk with no map entry, and a new room taking its code would append a second game into the orphaned file, corrupting both. The generator now rerolls on disk presence too. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5
parent
bed29c4992
commit
28bc91c3ac
@@ -15,7 +15,7 @@ import {
|
||||
type GameView,
|
||||
type PlayerId,
|
||||
} from "@wizwar/engine";
|
||||
import { appendLine, ensureDataDir, readAllRooms, type RoomLine } from "./store";
|
||||
import { appendLine, ensureDataDir, readAllRooms, roomFileExists, type RoomLine } from "./store";
|
||||
import { recordRoom } from "./stats";
|
||||
|
||||
export interface LoggedCommand {
|
||||
@@ -72,7 +72,9 @@ function makeRoomCode(): string {
|
||||
for (let i = 0; i < 4; i++) {
|
||||
code += ROOM_CODE_ALPHABET[randomInt(ROOM_CODE_ALPHABET.length)];
|
||||
}
|
||||
return rooms.has(code) ? makeRoomCode() : code;
|
||||
// The disk check covers rooms that exist on file but failed to restore —
|
||||
// reusing such a code would append a new game into the orphaned file.
|
||||
return rooms.has(code) || roomFileExists(code) ? makeRoomCode() : code;
|
||||
}
|
||||
|
||||
export function roomCount(): number {
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
// command. Because the engine is deterministic, replaying a file rebuilds
|
||||
// the exact game state — server restarts lose nothing.
|
||||
|
||||
import { appendFileSync, mkdirSync, readdirSync, readFileSync } from "node:fs";
|
||||
import { appendFileSync, existsSync, mkdirSync, readdirSync, readFileSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
|
||||
export interface RoomMetaLine {
|
||||
@@ -60,6 +60,11 @@ export function ensureDataDir(): void {
|
||||
mkdirSync(DATA_DIR, { recursive: true });
|
||||
}
|
||||
|
||||
/** A room file may exist even when the room failed to restore into memory. */
|
||||
export function roomFileExists(roomId: string): boolean {
|
||||
return existsSync(fileFor(roomId));
|
||||
}
|
||||
|
||||
export function appendLine(roomId: string, line: RoomLine): void {
|
||||
appendFileSync(fileFor(roomId), JSON.stringify(line) + "\n", "utf8");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user