Preferences: motion, confirming a move, coordinates on the board, and the last table choices remembered

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Jm2auWk6RP71CjaAb4FMoG
This commit is contained in:
Eric Wagoner
2026-09-23 18:54:55 -04:00
co-authored by Claude Fable 5.1
parent 5db89edbc1
commit aa4f86bc56
7 changed files with 233 additions and 8 deletions
+65
View File
@@ -0,0 +1,65 @@
// Preferences: what this browser remembers about how its player likes to
// play. The kit's own settings are motion and whether a move is confirmed
// before it is sent; a game declares more in GameSpec.preferences, and the
// panel renders every declaration. Nothing here leaves the browser.
import { game } from '$lib/game';
export interface Preference {
key: string;
label: string;
/** A toggle holds a boolean; a choice holds one of its choices' values. */
kind: 'toggle' | 'choice';
choices?: { value: string; label: string }[];
default: boolean | string;
note?: string;
}
/** The settings every game in the kit shares. */
export const KIT_PREFERENCES: Preference[] = [
{ key: 'motion', label: 'Motion', kind: 'choice', choices: [{ value: 'full', label: 'pieces move and fade' }, { value: 'reduced', label: 'changes appear at once' }], default: 'full' },
{ key: 'confirmMoves', label: 'Confirm a move before it is sent', kind: 'toggle', default: false }
];
export const PREFERENCES: Preference[] = [...KIT_PREFERENCES, ...(game.preferences ?? [])];
const KEY = 'hnefatafl:prefs';
function defaults(): Record<string, boolean | string> {
return Object.fromEntries(PREFERENCES.map((p) => [p.key, p.default]));
}
function load(): Record<string, boolean | string> {
const base = defaults();
try {
const saved = JSON.parse(localStorage.getItem(KEY) ?? '{}') as Record<string, unknown>;
for (const p of PREFERENCES) {
const v = saved[p.key];
if (p.kind === 'toggle' && typeof v === 'boolean') base[p.key] = v;
if (p.kind === 'choice' && typeof v === 'string' && p.choices?.some((c) => c.value === v)) base[p.key] = v;
}
// The hall's last table options ride along under table.<key>, undeclared.
for (const [k, v] of Object.entries(saved)) if (k.startsWith('table.') && typeof v === 'string') base[k] = v;
} catch {
// Without storage the defaults apply for this visit.
}
return base;
}
/** The live preferences; read them where they matter, set them through setPref. */
export const prefs = $state<Record<string, boolean | string>>(typeof localStorage === 'undefined' ? defaults() : load());
export function setPref(key: string, value: boolean | string): void {
prefs[key] = value;
try {
localStorage.setItem(KEY, JSON.stringify(prefs));
} catch {
// The change still applies for this visit.
}
}
/** The reduced-motion preference, or the system's, whichever asks for calm. */
export function reducedMotion(): boolean {
if (prefs.motion === 'reduced') return true;
return typeof matchMedia !== 'undefined' && matchMedia('(prefers-reduced-motion: reduce)').matches;
}