2026-05-26 20:02:59 -05:00
|
|
|
import { useState } from 'react'
|
|
|
|
|
import { type Page, type GameState } from './types'
|
|
|
|
|
import TitleScreen from './pages/TitleScreen'
|
|
|
|
|
import PlayerSetup from './pages/PlayerSetup'
|
|
|
|
|
import ScoreSheet from './pages/ScoreSheet'
|
|
|
|
|
import EnterScores from './pages/EnterScores'
|
|
|
|
|
|
|
|
|
|
const STORAGE_KEY = 'currentGame'
|
|
|
|
|
|
|
|
|
|
function loadGame(): GameState | null {
|
|
|
|
|
const raw = localStorage.getItem(STORAGE_KEY)
|
|
|
|
|
return raw ? (JSON.parse(raw) as GameState) : null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function saveGame(game: GameState) {
|
|
|
|
|
localStorage.setItem(STORAGE_KEY, JSON.stringify(game))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function clearGame() {
|
|
|
|
|
localStorage.removeItem(STORAGE_KEY)
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-26 19:30:46 -05:00
|
|
|
export default function App() {
|
2026-05-27 00:23:36 -05:00
|
|
|
const [game, setGame] = useState<GameState | null>(loadGame)
|
|
|
|
|
const [page, setPage] = useState<Page>(() => (game ? 'scoresheet' : 'title'))
|
2026-05-28 21:52:10 -05:00
|
|
|
const [previousPlayers, setPreviousPlayers] = useState<string[] | undefined>(undefined)
|
2026-05-26 20:02:59 -05:00
|
|
|
|
|
|
|
|
function handleNewGame() {
|
2026-05-28 21:52:10 -05:00
|
|
|
setPreviousPlayers(game?.players)
|
2026-05-26 20:02:59 -05:00
|
|
|
clearGame()
|
|
|
|
|
setGame(null)
|
|
|
|
|
setPage('setup')
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-27 00:06:46 -05:00
|
|
|
function handleStart(players: string[]) {
|
|
|
|
|
const newGame: GameState = { players, rounds: [] }
|
2026-05-26 20:02:59 -05:00
|
|
|
saveGame(newGame)
|
|
|
|
|
setGame(newGame)
|
|
|
|
|
setPage('scoresheet')
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-28 21:25:21 -05:00
|
|
|
function handleSubmitScores(scores: number[]) {
|
|
|
|
|
const updated: GameState = { ...game!, rounds: [...game!.rounds, scores] }
|
|
|
|
|
saveGame(updated)
|
|
|
|
|
setGame(updated)
|
2026-05-26 20:02:59 -05:00
|
|
|
setPage('scoresheet')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (page === 'title') return <TitleScreen onNewGame={handleNewGame} />
|
2026-05-28 21:52:10 -05:00
|
|
|
if (page === 'setup') return <PlayerSetup onStart={handleStart} initialNames={previousPlayers} />
|
2026-05-28 00:34:23 -05:00
|
|
|
if (page === 'scoresheet') return <ScoreSheet game={game!} onScoreRound={() => setPage('enterscores')} onNewGame={handleNewGame} />
|
2026-05-28 21:25:21 -05:00
|
|
|
if (page === 'enterscores') return <EnterScores game={game!} onSubmit={handleSubmitScores} />
|
2026-05-26 19:30:46 -05:00
|
|
|
}
|