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

+35 -1
View File
@@ -3,7 +3,7 @@
import TallyBoard from './Tally.svelte';
import { goto } from '$app/navigation';
import { loadFamily, type FamilyGame } from '$lib/family';
import { allSeats, api, doubtSeat, forgetSeat, rememberSeat, ServerError, trustSeat, type Report, type Tally } from '$lib/game/client';
import { restoreSeats, allSeats, api, doubtSeat, forgetSeat, rememberSeat, ServerError, trustSeat, type Report, type Tally } from '$lib/game/client';
import { Duel, duel, NAME_MAX, playerName, rememberName } from '$lib/game/duel.svelte';
import { unreadTalk } from '$lib/game/talk';
import type { RoomView } from '$lib/game/view';
@@ -13,6 +13,14 @@
let busy = $state(false);
let error = $state('');
let seats = $state(allSeats());
// The bank is asked once on arrival: a browser whose storage was cleared has its seats again before it misses them.
$effect(() => {
void restoreSeats()
.then((found) => {
if (found) seats = allSeats();
})
.catch(() => {});
});
let games = $state<Record<string, RoomView | null>>({});
let aboutOpen = $state(false);
$effect(() => {
@@ -62,6 +70,30 @@
let answers = $state<Record<string, string>>({});
let answerError = $state('');
let finding = $state(false);
/** What a search for lost seats turned up, in words. */
let found = $state('');
/** Seats under this name at duels still in play: those last used from here are taken back; the rest are named, to ask the host. */
async function findSeats() {
finding = true;
error = '';
found = '';
try {
rememberName(name);
const r = await api.findSeats(name.trim());
for (const s of r.seated) rememberSeat(s.roomId, { seat: s.seat, token: s.token });
if (r.seated.length) seats = allSeats();
const back = r.seated.length ? `Seated again at ${r.seated.map((s) => s.roomId).join(', ')}.` : '';
const n = r.elsewhere;
const ask = n ? ` ${n === 1 ? 'One more table' : `${n} more duels`} in play ${n === 1 ? 'has' : 'have'} a seat under your name, last used from another address, or given to a bot by the host: ask the host there for a transfer phrase, or write to the keeper.` : '';
found = back + ask || 'No duel still in play has a seat under that name.';
} catch (e) {
error = e instanceof Error ? e.message : 'The search did not go through.';
} finally {
finding = false;
}
}
// Fetch the reports for every held seat; re-runs when the held seats change.
$effect(() => {
const held = seats;
@@ -249,6 +281,8 @@
<input type="text" bind:value={phrase} placeholder="the four-word phrase" autocomplete="off" aria-label="transfer phrase" />
<button type="submit" class="quiet" disabled={claiming || phrase.trim().split(/[\s-]+/).length < 4}>Claim</button>
</form>
<p class="muted small lost">Lost your duels? If this browser forgot them, <button type="button" class="link" disabled={!ready || finding} onclick={findSeats}>find the seats under your name</button>.</p>
{#if found}<p class="muted small">{found}</p>{/if}
{#if error}<p class="warning">{error}</p>{/if}
</div>
+33 -1
View File
@@ -42,8 +42,32 @@ export function forgetSeat(roomId: string): void {
} catch {
// Nothing to forget without storage.
}
void bankSeats([roomId]).catch(() => {});
}
/**
* The seats this browser holds are banked with the server as well, in a cookie it sets and the browser keeps for
* it alone. A browser whose storage is cleared, or that Safari clears for it after a week away, still has the
* cookie, and gets its seats back from it. The answer is every seat the bank holds that still opens.
*/
export async function bankSeats(forget: string[] = []): Promise<{ roomId: string; seat: WizardId; token: string }[]> {
const seats = allSeats().map(({ roomId, token }) => ({ roomId, token }));
const { seats: banked } = await call<{ seats: { roomId: string; seat: WizardId; token: string }[] }>('POST', '/api/seats', { seats, forget });
return banked;
}
/** The seats the bank holds that this browser does not, taken into storage; true when there were any. */
export async function restoreSeats(): Promise<boolean> {
let found = false;
for (const s of await bankSeats()) {
if (seatFor(s.roomId)) continue;
store(s.roomId, { seat: s.seat, token: s.token });
found = true;
}
return found;
}
/**
* A seat the server refused is doubted, not dropped: a restarting server, a
* stale answer or the wrong backend all answer with ignorance, and ignorance
@@ -92,7 +116,7 @@ export function trustSeat(roomId: string): void {
}
}
export function rememberSeat(roomId: string, held: HeldSeat): void {
function store(roomId: string, held: HeldSeat): void {
try {
localStorage.setItem(SEATS_KEY, JSON.stringify({ ...heldSeats(), [roomId]: held }));
} catch {
@@ -100,6 +124,12 @@ export function rememberSeat(roomId: string, held: HeldSeat): void {
}
}
/** A seat taken is kept here and banked with the server, each time, so the two agree. */
export function rememberSeat(roomId: string, held: HeldSeat): void {
store(roomId, held);
void bankSeats().catch(() => {});
}
export class ServerError extends Error {
constructor(
message: string,
@@ -173,6 +203,8 @@ export const api = {
hostPhrase: (roomId: string, token: string, seat: WizardId) => call<{ phrase: string; expiresAt: number }>('POST', `/api/rooms/${roomId}/phrase`, { token, seat }),
standIn: (roomId: string, token: string, seat: WizardId) => call<RoomView>('POST', `/api/rooms/${roomId}/standin`, { token, seat }),
claim: (phrase: string) => call<{ roomId: string; seat: WizardId; token: string }>('POST', '/api/transfer', { phrase }),
/** The seats a name holds at duels not over: those last used from this address come back seated; the rest are only counted. */
findSeats: (name: string, roomId?: string) => call<{ seated: { roomId: string; seat: WizardId; token: string }[]; elsewhere: number }>('POST', '/api/seats/find', { name, ...(roomId ? { roomId } : {}) }),
report: (roomId: string, token: string, happened: string, expected: string) =>
call<{ id: string }>('POST', `/api/rooms/${roomId}/report`, { token, happened, expected }),
reportImage: async (id: string, file: File) => {
+37 -1
View File
@@ -177,12 +177,44 @@
room = new Duel(await Duel.joinRoom(roomId, name));
joining = false;
} catch (err) {
// A seat already under this name may be this person's own, lost with the browser's storage: it is looked for first.
if (err instanceof ServerError && err.status === 409 && (await sitBackDown())) return;
error = err instanceof Error ? err.message : 'The seat could not be taken.';
} finally {
busy = false;
}
}
/** The seat under this name at this table, back again if it was last used from this address; true when it was. */
async function sitBackDown(): Promise<boolean> {
const r = await api.findSeats(name.trim(), roomId).catch(() => null);
const mine = r?.seated.find((s) => s.roomId === roomId);
if (!mine) {
if (r?.elsewhere) error = 'A seat here is under that name, but it was last used from another address, or the host gave it to a bot. Ask the host for a transfer phrase, or write to the keeper.';
return false;
}
rememberSeat(roomId, { seat: mine.seat, token: mine.token });
trustSeat(roomId);
seatLost = '';
room = new Duel(await Duel.resumeRoom(roomId, { seat: mine.seat, token: mine.token }));
joining = false;
return true;
}
/** From the gallery of a duel begun: the seat under this name, if it was last used from here. */
async function findMySeat(e: SubmitEvent) {
e.preventDefault();
busy = true;
error = '';
try {
if (!(await sitBackDown()) && !error) error = 'No seat here is under that name.';
} catch (err) {
error = err instanceof Error ? err.message : 'The seat could not be found.';
} finally {
busy = false;
}
}
async function copyLink() {
try {
await navigator.clipboard.writeText(link);
@@ -262,7 +294,11 @@
<div class="notice muted">
{#if seatLost}<p>{seatLost} Claim it here, or <a href="mailto:eric@ericwagoner.com?subject={encodeURIComponent(`My seat in room ${roomId}`)}">write to the keeper</a>.</p>
{:else}{#if room.remote.expected.length > 0}<p>{room.speaker(room.remote.host)} began the duel before {room.remote.expected.join(' and ')} arrived, and the seat held for {room.remote.expected.length === 1 ? 'them' : 'each'} went with it.</p>{/if}
<p>Had a seat at this duel? Ask the host for a transfer phrase, or make one on the device that still holds the seat, and claim it here. Or <a href="mailto:eric@ericwagoner.com?subject={encodeURIComponent(`My seat in room ${roomId}`)}">write to the keeper</a> for a link that brings it back.</p>{/if}
<p>Had a seat at this duel? Give your name, and if the seat was last used from here it is yours again. Otherwise ask the host for a transfer phrase, or make one on the device that still holds the seat, and claim it here. Or <a href="mailto:eric@ericwagoner.com?subject={encodeURIComponent(`My seat in room ${roomId}`)}">write to the keeper</a> for a link that brings it back.</p>{/if}
<form class="rename" onsubmit={findMySeat}>
<input type="text" bind:value={name} maxlength={NAME_MAX} placeholder="your name at this duel" autocomplete="nickname" aria-label="your name at this duel" />
<button type="submit" class="quiet" disabled={busy || !name.trim()}>Sit back down</button>
</form>
<form class="rename" onsubmit={claim}>
<input type="text" bind:value={claimPhrase} placeholder="the four-word phrase" autocomplete="off" aria-label="transfer phrase" />
<button type="submit" class="quiet" disabled={claiming || claimPhrase.trim().split(/[\s-]+/).length < 4}>Claim the seat</button>