Seat tokens hashed on the ledger, inputs written as they arrive, codes checked against the disk
A seat line now carries the SHA-256 of its token, compared in constant time; the token itself goes once to the browser that earned it. Ledgers written before this are read once more by their raw token, and deploy/hash-tokens.ts rewrites them in place. Each submitted move is its own ledger line, so a half-written turn survives a restart. A room code is never reissued over an evicted room's ledger. Only the host seats a bot. The deploy gate compares against the gallery view, so no token leaves the ledger for it. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0141G6xqLeNRYEtviLWSB5Up
This commit is contained in:
1 parent
effe17c729
commit
966a36b4aa
6 files changed
+94
-33
No files matched your search
+32
-22
@@ -3,7 +3,7 @@
|
||||
// same place. Inputs wait in memory until every seat that must move has moved;
|
||||
// then the turn resolves at once, bots included.
|
||||
|
||||
import { randomBytes } from 'node:crypto';
|
||||
import { createHash, randomBytes, timingSafeEqual } from 'node:crypto';
|
||||
import { chooseBotTurn } from '../../src/lib/game/bot';
|
||||
import { resolveTurn } from '../../src/lib/game/resolve';
|
||||
import { GESTURES, type Gesture } from '../../src/lib/game/spells';
|
||||
@@ -23,10 +23,15 @@ const CHAT_KEEP = 200;
|
||||
export interface Seat {
|
||||
id: WizardId;
|
||||
name: string;
|
||||
token: string;
|
||||
/** SHA-256 of the seat's token, hex; empty for a bot. */
|
||||
tokenHash: string;
|
||||
bot: boolean;
|
||||
}
|
||||
|
||||
export function hashToken(token: string): string {
|
||||
return createHash('sha256').update(token).digest('hex');
|
||||
}
|
||||
|
||||
export interface Room {
|
||||
id: string;
|
||||
size: number;
|
||||
@@ -113,25 +118,26 @@ export class Rooms {
|
||||
return this.rooms.size;
|
||||
}
|
||||
|
||||
/** The seat a token unlocks. */
|
||||
/** The seat a token unlocks, compared by hash in constant time. */
|
||||
seatOf(room: Room, token: string | undefined): Seat {
|
||||
const seat = room.seats.find((s) => !s.bot && s.token === token);
|
||||
if (!token) throw new RoomError('That token opens no seat at this duel.', 403);
|
||||
const given = Buffer.from(hashToken(token), 'hex');
|
||||
const seat = room.seats.find((s) => !s.bot && s.tokenHash.length === given.length * 2 && timingSafeEqual(Buffer.from(s.tokenHash, 'hex'), given));
|
||||
if (!seat) throw new RoomError('That token opens no seat at this duel.', 403);
|
||||
return seat;
|
||||
}
|
||||
|
||||
create(name: string, size: number): { room: Room; seat: Seat } {
|
||||
create(name: string, size: number): { room: Room; seat: Seat; token: string } {
|
||||
if (!Number.isInteger(size) || size < 2 || size > MAX_SEATS) throw new RoomError(`A table seats 2 to ${MAX_SEATS} wizards.`);
|
||||
let id = newCode();
|
||||
while (this.rooms.has(id)) id = newCode();
|
||||
while (this.rooms.has(id) || this.store.exists(id)) id = newCode();
|
||||
const room: Room = { id, size, createdAt: Date.now(), seats: [], state: null, pending: {}, chat: [], seq: 0, updatedAt: Date.now() };
|
||||
this.rooms.set(id, room);
|
||||
this.commit(room, { t: 'room', id, seats: size, createdAt: room.createdAt });
|
||||
const seat = this.sit(room, name, false);
|
||||
return { room, seat };
|
||||
return { room, ...this.sit(room, name, false) };
|
||||
}
|
||||
|
||||
join(room: Room, name: string): Seat {
|
||||
join(room: Room, name: string): { seat: Seat; token: string } {
|
||||
if (room.state) throw new RoomError('The duel has begun; no more seats are taken.', 409);
|
||||
return this.sit(room, name, false);
|
||||
}
|
||||
@@ -145,23 +151,27 @@ export class Rooms {
|
||||
this.commit(room, { t: 'start', seed: newSeed(), rules: CURRENT_RULES });
|
||||
}
|
||||
|
||||
/** The host seats a bot in an empty chair. */
|
||||
addBot(room: Room, token: string | undefined): Seat {
|
||||
this.seatOf(room, token);
|
||||
const host = this.seatOf(room, token);
|
||||
if (host.id !== room.seats[0]?.id) throw new RoomError('Only the wizard who opened the table may seat a bot.', 403);
|
||||
if (room.state) throw new RoomError('The duel has begun; no more seats are taken.', 409);
|
||||
const taken = new Set(room.seats.map((s) => s.name.toLowerCase()));
|
||||
const pool = BOT_NAMES.filter((n) => !taken.has(n.toLowerCase()));
|
||||
const name = pool[Math.floor(Math.random() * pool.length)] ?? 'The Construct';
|
||||
return this.sit(room, name, true);
|
||||
return this.sit(room, name, true).seat;
|
||||
}
|
||||
|
||||
private sit(room: Room, rawName: string, bot: boolean): Seat {
|
||||
/** Take the next free seat. The token is returned once and never stored; the ledger keeps its hash. */
|
||||
private sit(room: Room, rawName: string, bot: boolean): { seat: Seat; token: string } {
|
||||
if (room.seats.length >= room.size) throw new RoomError('Every seat at this duel is taken.', 409);
|
||||
const name = cleanName(rawName);
|
||||
if (room.seats.some((s) => s.name.toLowerCase() === name.toLowerCase())) throw new RoomError('Another wizard here already has that name.', 409);
|
||||
const id = SEAT_IDS.find((s) => !room.seats.some((taken) => taken.id === s))!;
|
||||
const seat: Seat = { id, name, token: bot ? '' : newId(18), bot };
|
||||
this.commit(room, { t: 'seat', id: seat.id, name, token: seat.token, bot });
|
||||
return seat;
|
||||
const token = bot ? '' : newId(18);
|
||||
const seat: Seat = { id, name, tokenHash: bot ? '' : hashToken(token), bot };
|
||||
this.commit(room, { t: 'seat', id: seat.id, name, tokenHash: seat.tokenHash, bot });
|
||||
return { seat, token };
|
||||
}
|
||||
|
||||
/** The host sends a bot away before the duel begins. People leave by not coming back. */
|
||||
@@ -174,17 +184,14 @@ export class Rooms {
|
||||
this.commit(room, { t: 'unseat', id: seat.id });
|
||||
}
|
||||
|
||||
/** Record a seat's move; resolve the turn once nobody else is awaited. */
|
||||
/** Record a seat's move on the ledger, so a restart keeps it; resolve the turn once nobody else is awaited. */
|
||||
submit(room: Room, token: string | undefined, raw: unknown): void {
|
||||
const seat = this.seatOf(room, token);
|
||||
if (!room.state) throw new RoomError('The duel has not begun: seats are still empty.', 409);
|
||||
if (room.state.over) throw new RoomError('The duel is over.', 409);
|
||||
if (!waitingOn(room).includes(seat.id)) throw new RoomError('It is not your move.', 409);
|
||||
room.pending[seat.id] = cleanInput(raw);
|
||||
room.updatedAt = Date.now();
|
||||
room.seq += 1;
|
||||
this.commit(room, { t: 'input', at: Date.now(), id: seat.id, input: cleanInput(raw) });
|
||||
if (waitingOn(room).length === 0) this.resolve(room);
|
||||
this.notify(room);
|
||||
}
|
||||
|
||||
private resolve(room: Room): void {
|
||||
@@ -286,7 +293,10 @@ function apply(room: Room, line: LedgerLine): void {
|
||||
case 'room':
|
||||
break;
|
||||
case 'seat':
|
||||
room.seats.push({ id: line.id, name: line.name, token: line.token, bot: line.bot });
|
||||
room.seats.push({ id: line.id, name: line.name, tokenHash: line.tokenHash ?? (line.token ? hashToken(line.token) : ''), bot: line.bot });
|
||||
break;
|
||||
case 'input':
|
||||
room.pending[line.id] = line.input;
|
||||
break;
|
||||
case 'unseat':
|
||||
room.seats = room.seats.filter((s) => s.id !== line.id);
|
||||
@@ -314,7 +324,7 @@ function replay(id: string, lines: LedgerLine[]): Room | null {
|
||||
const room: Room = { id, size: first.seats, createdAt: first.createdAt, seats: [], state: null, pending: {}, chat: [], seq: lines.length, updatedAt: first.createdAt };
|
||||
for (const line of lines) {
|
||||
apply(room, line);
|
||||
if (line.t === 'turn') room.updatedAt = line.at;
|
||||
if (line.t === 'turn' || line.t === 'input') room.updatedAt = line.at;
|
||||
}
|
||||
return room;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user