Seats are banked in a cookie the server sets, and a lost seat comes back by name to the address it was last used from

A seat lived in the browser's storage alone, and Safari clears that after a week away, or at a word from the person on another device. Now every seat taken is banked as well in an HttpOnly cookie the server writes, which such clearing leaves alone, and the hall takes its seats back from the bank on arrival. For a browser that lost even the cookie, the ledger notes the address each seat was last used from, as a salted hash, at most once in ten minutes, and a name given in the hall or at the duel gets its seats back from the same address, with a new token; where the address differs, two people at the duel share it, or the host gave the seat to a bot, the duel is only counted, and the host's phrase or the keeper's link is the way back. The keeper's script finds a name across duels. The seat endpoints answer only this site's own pages, under a limit of their own.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CCLa2PaEeCj8njQZSWGqHx
This commit is contained in:
Eric WagonerandClaude Fable 5.1 committed 2026-11-10 21:35:43 -05:00
1 parent b8f65a74d3
commit 29a68f4d9d
8 files changed
+443 -13

No files matched your search

+79
View File
@@ -29,8 +29,15 @@ export interface Seat {
bot: boolean;
/** The host gave this wizard's seat to a bot; a phrase from the host, or the keeper, gives it back. */
standIn?: true;
/** The address this person's seat was last used from, as a salted hash; a seat their browser lost goes back to the same address. */
addr?: string;
}
/** A table whose ledger has not been written in this long is not looked through for a lost seat. */
const LOST_SEAT_LOOKBACK_MS = 90 * 24 * 60 * 60 * 1000;
/** A seat's address is noted at most this often: one seat open on two networks at once must not write a line per request. */
const SEEN_EVERY_MS = 10 * 60 * 1000;
export function hashToken(token: string): string {
return createHash('sha256').update(token).digest('hex');
}
@@ -276,6 +283,73 @@ export class Rooms {
return { phrase, expiresAt };
}
/** When each seat's address was last noted, by room and seat. */
private seenAt = new Map<string, number>();
/** The address a seat is used from, noted when it changes, and not more often than SEEN_EVERY_MS. Not a change the table need hear of: nothing wakes for it. */
seen(room: Room, token: string | undefined, addr: string): void {
let seat: Seat;
try {
seat = this.seatOf(room, token);
} catch {
return;
}
if (seat.addr === addr) return;
// Keyed by the token too: a seat given a new token is noted afresh at once, whoever held it before.
const key = `${room.id}/${seat.id}/${seat.tokenHash}`;
const now = Date.now();
if ((this.seenAt.get(key) ?? 0) > now - SEEN_EVERY_MS) return;
this.seenAt.set(key, now);
const line: LedgerLine = { t: 'seen', at: now, id: seat.id, addr };
this.store.append(room.id, line);
apply(room, line);
}
/** The tables not yet over, written to lately, where a person of this name sits; one table alone when it is named. */
private tablesOf(name: string, only?: string): { room: Room; seat: Seat }[] {
const wanted = cleanName(name).toLowerCase();
const since = Date.now() - LOST_SEAT_LOOKBACK_MS;
const ids = only ? [normalizeCode(String(only))] : this.store.roomIds().filter((id) => this.store.updatedAt(id) > since);
const found: { room: Room; seat: Seat }[] = [];
for (const id of ids) {
let room: Room;
try {
room = this.get(id);
} catch {
continue;
}
if (room.over) continue;
const seat = room.seats.find((s) => (!s.bot || s.standIn) && s.name.toLowerCase() === wanted);
if (seat) found.push({ room, seat });
}
return found;
}
/**
* The seats a name holds at tables not yet over, for a browser that lost them. A seat last used from the
* asker's own address, at a table where no other person shares that address, is theirs again with a new
* token, the old one dead. The rest are only counted: a name proves nothing, so neither the tables nor the
* company at them are told to the asker, who can get a phrase from the host, or a link from the keeper. A
* seat the host gave to a bot is the host's to give back, by a phrase, never taken back by name.
*/
findSeats(rawName: string, addr: string, only?: string): { seated: { roomId: string; seat: WizardId; token: string }[]; elsewhere: number } {
const seated: { roomId: string; seat: WizardId; token: string }[] = [];
let elsewhere = 0;
for (const { room, seat } of this.tablesOf(rawName, only)) {
const shared = room.seats.some((s) => s !== seat && (!s.bot || s.standIn) && s.addr === addr);
if (seat.addr === addr && !shared && !seat.standIn) {
const { token } = this.reseat(room, seat.id);
seated.push({ roomId: room.id, seat: seat.id, token });
} else elsewhere += 1;
}
return { seated, elsewhere };
}
/** For the keeper: every table not yet over where a person of this name sits, with the company there, to re-seat them by. */
tablesForKeeper(name: string): { roomId: string; seat: WizardId; standIn: boolean; players: string[]; updatedAt: number }[] {
return this.tablesOf(name).map(({ room, seat }) => ({ roomId: room.id, seat: seat.id, standIn: !!seat.standIn, players: room.seats.filter((s) => s !== seat).map((s) => s.name), updatedAt: room.updatedAt }));
}
/** Claim a phrase: the seat and its token, once. Too many misses void every pending phrase. */
claim(raw: unknown): { roomId: string; seat: WizardId; token: string } {
const now = Date.now();
@@ -514,6 +588,11 @@ function apply(room: Room, line: LedgerLine): void {
case 'seat':
room.seats.push({ id: line.id, name: line.name, tokenHash: line.tokenHash ?? (line.token ? hashToken(line.token) : ''), bot: line.bot });
break;
case 'seen': {
const seat = room.seats.find((s) => s.id === line.id);
if (seat) seat.addr = line.addr;
break;
}
case 'reseat': {
const seat = room.seats.find((s) => s.id === line.id);
if (!seat) break;