A game may take drafts: an input it does not call settled can be replaced, and the round waits for settled ones
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Jm2auWk6RP71CjaAb4FMoG
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
6133a2469a
commit
927e49cf79
@@ -43,4 +43,24 @@ describe('rooms', () => {
|
||||
expect(reopened.get(room.id).state).not.toBeNull();
|
||||
errors.mockRestore();
|
||||
});
|
||||
|
||||
it('holds a draft until it is settled, and lets it be replaced until then', () => {
|
||||
type Draft = Input & { final?: boolean };
|
||||
const drafts: GameSpec<State, Input> = {
|
||||
...game,
|
||||
cleanInput: (raw) => ({ ...game.cleanInput(raw), final: (raw as Draft)?.final === true }) as Draft,
|
||||
settled: (input) => (input as Draft).final === true
|
||||
};
|
||||
const { rooms, room, ann, bo } = table(drafts);
|
||||
rooms.submit(room, ann, { pick: 5 }, 0);
|
||||
rooms.submit(room, bo, { pick: 2, final: true }, 0);
|
||||
expect(rooms.waitingOn(room)).toEqual(['A']);
|
||||
rooms.submit(room, ann, { pick: 4 }, 0);
|
||||
expect(room.resolved).toBe(0);
|
||||
rooms.submit(room, ann, { pick: 3, final: true }, 0);
|
||||
expect(room.resolved).toBe(1);
|
||||
expect(room.state!.rounds[0].picks).toEqual({ A: 3, B: 2 });
|
||||
rooms.submit(room, bo, { pick: 1, final: true }, 1);
|
||||
expect(() => rooms.submit(room, bo, { pick: 2 }, 1)).toThrow(/not your move/);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -360,6 +360,7 @@ export class Rooms<State, Input> {
|
||||
|
||||
/**
|
||||
* Record a seat's move on the ledger, so a restart keeps it; resolve the round once nobody else is awaited.
|
||||
* A draft (an input the game does not call settled) may be replaced until it is settled.
|
||||
* `resolved` is how many rounds the page had seen played when the move was made: a move that
|
||||
* arrives after its round was played (a slow connection, a second click, a stale tab) is refused
|
||||
* rather than taken as the move for the next round.
|
||||
@@ -386,6 +387,11 @@ export class Rooms<State, Input> {
|
||||
this.driveBots(room);
|
||||
}
|
||||
|
||||
/** Whether an input is final; a game without drafts makes every input final. */
|
||||
private settled(input: unknown): boolean {
|
||||
return this.game.settled ? this.game.settled(input as Input) : true;
|
||||
}
|
||||
|
||||
/** Table talk, from a seat to everyone at the table and in the gallery. */
|
||||
say(room: Room<State>, token: string | undefined, raw: unknown): void {
|
||||
const seat = this.seatOf(room, token);
|
||||
@@ -402,11 +408,11 @@ export class Rooms<State, Input> {
|
||||
return text;
|
||||
}
|
||||
|
||||
/** Seats whose input the next resolution needs and has not yet received. */
|
||||
/** Seats whose input the next resolution needs and has not yet received settled; a draft still waits. */
|
||||
waitingOn(room: Room<State>): SeatId[] {
|
||||
if (!room.state || this.game.over(room.state)) return [];
|
||||
return room.seats
|
||||
.filter((s) => !s.bot && this.game.needsInput(room.state!, s.id) && !(s.id in room.pending))
|
||||
.filter((s) => !s.bot && this.game.needsInput(room.state!, s.id) && !(s.id in room.pending && this.settled(room.pending[s.id])))
|
||||
.map((s) => s.id);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user