import React, { useState, useEffect } from 'react'; import { GameStats, LeaderboardEntry, gameApi } from '@/lib/api'; interface GameOverModalProps { isOpen: boolean; gameStats: GameStats | null; sessionId: number | null; onClose: () => void; onNewGame: () => void; } const GameOverModal: React.FC = ({ isOpen, gameStats, sessionId, onNewGame, }) => { const [playerName, setPlayerName] = useState(''); const [submitted, setSubmitted] = useState(false); const [leaderboard, setLeaderboard] = useState([]); const [leaderboardPosition, setLeaderboardPosition] = useState(null); const [showLeaderboard, setShowLeaderboard] = useState(false); const [loading, setLoading] = useState(false); useEffect(() => { if (isOpen && !submitted) { // Load leaderboard when modal opens loadLeaderboard(); } }, [isOpen, submitted]); const loadLeaderboard = async () => { try { const response = await gameApi.getLeaderboard(); setLeaderboard(response.leaderboard); } catch (error) { console.error('Failed to load leaderboard:', error); } }; const handleSubmit = async (e: React.FormEvent | React.MouseEvent) => { e.preventDefault(); if (!sessionId || submitted) return; setLoading(true); try { const response = await gameApi.savePlayerName( sessionId, playerName.trim() || 'Anonymous' ); setLeaderboardPosition(response.leaderboard_position); setSubmitted(true); await loadLeaderboard(); setShowLeaderboard(true); } catch (error) { console.error('Failed to save score:', error); } finally { setLoading(false); } }; if (!isOpen || !gameStats) return null; return (
{/* Header */}

GAME OVER

{/* Score Display */} {!showLeaderboard && ( <>
Final Score:
{gameStats.score}
Moles Killed:
{gameStats.moles_killed}
Moles Escaped:
{gameStats.moles_escaped}
Commands Used:
{gameStats.commands_used}
Time Played:
{gameStats.time_taken}
{/* Name Input */} {!submitted ? (
setPlayerName(e.target.value)} onKeyDown={(e) => { if (e.key === 'Enter') { e.preventDefault(); handleSubmit(e); } }} placeholder="Anonymous" maxLength={20} className="flex-1 bg-black border border-green-500 text-green-300 px-4 py-2 rounded font-terminal focus:outline-none focus:border-green-400" autoFocus disabled={loading} />
) : (

Score saved! You ranked #{leaderboardPosition}

)} )} {/* Leaderboard */} {showLeaderboard && (

Top Scores

{leaderboard.map((entry) => (
#{entry.rank} {entry.player_name}
{entry.score} pts {entry.moles_killed} moles {entry.commands_used} cmds
))}
)} {/* Action Buttons */}
{!showLeaderboard && submitted && ( )} {showLeaderboard && ( )}
); }; export default GameOverModal;