"use client"; import { useState, useEffect, useCallback, useRef } from "react"; import { loadChampions, getRandomChampion, getTodaySeed, } from "@/lib/champion-data"; import { createGame, submitGuess, getGuessedNames, getOrCreateUnlimitedSeed, clearUnlimitedState, saveUnlimitedStats, loadUnlimitedStats, clearExpiredCache, } from "@/lib/game-engine"; import { compareChampions } from "@/lib/classic-mode"; import ChampionSearch from "./champion-search"; import GuessGrid from "./guess-grid"; import GameOver from "./game-over"; import StatsDisplay from "./stats-display"; export default function GameBoard() { const [mode, setMode] = useState("daily"); const [game, setGame] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [stats, setStats] = useState(null); const gameRef = useRef(null); const initGame = useCallback((gameMode) => { const seed = gameMode === "daily" ? getTodaySeed() : getOrCreateUnlimitedSeed(); const target = getRandomChampion(seed); const maxGuesses = gameMode === "daily" ? 6 : 0; const newGame = createGame({ target, compareFn: compareChampions, maxGuesses, mode: gameMode, seed, }); gameRef.current = newGame; setGame({ ...newGame }); setStats(gameMode === "unlimited" ? loadUnlimitedStats() : null); }, []); useEffect(() => { loadChampions() .then(() => { clearExpiredCache(getTodaySeed()); initGame("daily"); setLoading(false); }) .catch((err) => { setError(err.message); setLoading(false); }); }, [initGame]); function switchMode(newMode) { setMode(newMode); initGame(newMode); } const handleGuess = useCallback((champion) => { const currentGame = gameRef.current; if (!currentGame) return; const updated = submitGuess(currentGame, champion); if (!updated) return; gameRef.current = updated; setGame(updated); if (updated.isOver && updated.mode === "unlimited") { saveUnlimitedStats(updated); setStats(loadUnlimitedStats()); } }, []); function handleNewGame() { clearUnlimitedState(); initGame("unlimited"); } if (loading) { return
Loading champions...
; } if (error) { returnFailed to load champion data. Please refresh.
; } if (!game) return null; const excludeNames = game.guesses.map((g) => g.name); return ( <> {/* Mode toggle */}{game.maxGuesses > 0 ? `${game.guesses.length} / ${game.maxGuesses} guesses` : `${game.guesses.length} guess${game.guesses.length !== 1 ? "es" : ""}`}