/** The page shell: start screen, game, replay. */ import { render } from 'preact'; import { useEffect, useState } from 'preact/hooks'; import { pool } from './cards.ts'; import { Match, type GameRecord, type MatchOptions, type MatchSetup } from './match.ts'; import { Replay } from './replay.ts'; import { loadGame, loadSettings, loadSetup, saveGame, saveSettings, saveSetup, type SavedGame, type Settings } from './storage.ts'; import { GameScreen } from './ui/game.tsx'; import { ReplayScreen } from './ui/replay.tsx'; import { StartScreen } from './ui/start.tsx'; import './styles.css'; type Screen = | { kind: 'start'; resumeError?: string } | { kind: 'game'; match: Match; playtest: boolean; id: number } | { kind: 'replay'; replay: Replay; from: Screen }; let games = 0; const optionsFrom = (s: Settings, playtest: boolean): Partial => ({ autoSkip: s.autoSkip, stops: s.stops, checkInvariants: s.checkInvariants, undo: playtest, }); function App() { const [settings, setSettings] = useState(loadSettings); const [screen, setScreen] = useState({ kind: 'start' }); const changeSettings = (s: Settings) => { setSettings(s); saveSettings(s); if (screen.kind === 'game') screen.match.setOptions({ autoSkip: s.autoSkip, stops: s.stops, checkInvariants: s.checkInvariants }); }; // Save after every move; a finished game isn't offered for resuming. useEffect(() => { if (screen.kind !== 'game') return; const { match, playtest } = screen; const save = () => { const g: SavedGame | null = match.over ? null : { record: match.record(), playtest, savedAt: Date.now() }; saveGame(g); }; save(); return match.subscribe(save); }, [screen]); const play = (setup: MatchSetup, playtest: boolean) => setScreen({ kind: 'game', match: new Match(pool, setup, optionsFrom(settings, playtest)), playtest, id: ++games }); const playRecord = (record: GameRecord, playtest: boolean): string | null => { try { setScreen({ kind: 'game', match: Match.fromRecord(pool, record, optionsFrom(settings, playtest)), playtest, id: ++games }); return null; } catch (e) { return (e as Error).message; } }; current = screen.kind === 'game' ? screen.match : null; if (screen.kind === 'game') { const { match, playtest } = screen; return ( setScreen({ kind: 'start' })} onRematch={() => play(match.setup, playtest)} onReplay={() => setScreen({ kind: 'replay', replay: new Replay(pool, match.record()), from: screen })} /> ); } if (screen.kind === 'replay') { return ( setScreen(screen.from.kind === 'game' && !screen.from.match.over ? screen.from : { kind: 'start' })} onPlayFrom={(record) => { const err = playRecord(record, true); if (err) alert(err); }} /> ); } const saved = loadGame(); return ( { saveSetup(prefs); play(setup, playtest); }} onResume={() => { if (!saved) return; const err = playRecord(saved.record, saved.playtest); if (err) setScreen({ kind: 'start', resumeError: `Couldn’t resume: ${err}` }); }} onDiscardSave={() => { saveGame(null); setScreen({ kind: 'start' }); }} onOpenRecord={(record) => setScreen({ kind: 'replay', replay: new Replay(pool, record), from: { kind: 'start' } })} /> ); } // Dev builds expose the current game for scripted browser tests. let current: Match | null = null; if (import.meta.env.DEV) { (globalThis as Record).__files = { get match() { return current; }, greedyBot: () => import('../../sim/src/bots/greedy.ts').then((m) => m.greedyBot()), }; } render(, document.getElementById('app')!);