import React, { useState, useEffect } from 'react'; import { useGame } from '../context/GameContext'; import { GamePhase } from '../types'; import type { Player } from '../types'; import { Trophy, Zap, LogOut } from 'lucide-react'; export const PlayerView: React.FC = () => { const { gameState, players, teams, currentPlayerId, addPlayer, removePlayer, buzzQueue, handleBuzz, questions } = useGame(); const [name, setName] = useState(''); const [team, setTeam] = useState(''); const [hasJoined, setHasJoined] = useState(false); // Auto-detect join state if currentPlayerId is set externally useEffect(() => { if (currentPlayerId) { setHasJoined(true); } }, [currentPlayerId]); const currentPlayer = players.find(p => p.id === currentPlayerId); const currentTeam = teams.find(t => t.id === currentPlayer?.teamId); // Handle case where player was removed (kicked or hard reset) useEffect(() => { if (hasJoined && !currentPlayer) { setHasJoined(false); setName(''); setTeam(''); } }, [currentPlayer, hasJoined]); const myBuzz = buzzQueue.find(b => b.playerId === currentPlayerId); const isLockedOut = myBuzz && (myBuzz.status === 'WRONG' || (buzzQueue.length > 0 && buzzQueue[0].playerId !== currentPlayerId && myBuzz.status === 'PENDING')); const isMyTurn = myBuzz && myBuzz.status === 'PENDING' && buzzQueue[0].playerId === currentPlayerId; const isCorrect = myBuzz && myBuzz.status === 'CORRECT'; const isWrong = myBuzz && myBuzz.status === 'WRONG'; // Calculate Rank const sortedTeams = [...teams].sort((a, b) => b.score - a.score); const myRank = currentTeam ? sortedTeams.findIndex(t => t.id === currentTeam.id) + 1 : '-'; const currentQ = questions[gameState.currentQuestionIndex]; // Helper for YouTube ID const getYoutubeId = (url: string) => { const regExp = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|&v=)([^#&?]*).*/; const match = url.match(regExp); return (match && match[2].length === 11) ? match[2] : null; }; const handleJoin = (e: React.FormEvent) => { e.preventDefault(); if (name && team) { addPlayer(name, team); setHasJoined(true); } }; const handleLeave = () => { if (currentPlayerId) { removePlayer(currentPlayerId); setHasJoined(false); setName(''); setTeam(''); } }; if (!hasJoined) { return (

Join Quiz

Enter your details to enter the lobby.

setName(e.target.value)} className="w-full px-4 py-3 border border-slate-300 rounded-lg focus:ring-2 focus:ring-blue-500 outline-none text-lg text-black bg-white" placeholder="e.g. Maverick" required />
setTeam(e.target.value)} className="w-full px-4 py-3 border border-slate-300 rounded-lg focus:ring-2 focus:ring-blue-500 outline-none text-lg text-black bg-white" placeholder="e.g. Top Guns" required />
); } if (currentPlayer && !currentPlayer.isApproved) { return (

Waiting for Host...

Sit tight! You'll be in shortly.

); } // --- EXPLICIT FEEDBACK SCREENS --- if (isCorrect) { return (
🎉

CORRECT!

You got the points!

Wait for next question...
); } if (isWrong) { return (

WRONG!

Better luck next time.

You are locked out for this question.
); } // --- FINAL STATS VIEW --- if (gameState.phase === GamePhase.FINAL_STATS) { const winningTeam = sortedTeams[0]; let fastestPlayer: Player | null = null; let fastestTime = Infinity; players.forEach(p => { if (p.stats.bestReactionTime && p.stats.bestReactionTime < fastestTime) { fastestTime = p.stats.bestReactionTime; fastestPlayer = p; } }); const topPlayers = [...players].sort((a,b) => b.score - a.score).slice(0, 5); return (
{/* Header */}

Game Over

{/* Winning Team Trophy */}
Winning Team
{winningTeam?.name || "No Winner"}
{winningTeam?.score || 0} PTS
{/* Fastest Finger */} {fastestPlayer && (
Fastest Buzzer
{(fastestPlayer as Player).name}
{(fastestTime / 1000).toFixed(2)}s reaction
)} {/* Top Players Table */}

Top 5 Players

{topPlayers.map((p, i) => ( ))}
Rank Name Score Acc
#{i + 1} {p.name} {p.score} {p.stats.correctAnswers}/{p.stats.totalBuzzes}
); } const isGameActive = gameState.phase !== GamePhase.LOBBY && gameState.phase !== GamePhase.LEADERBOARD; // --- REGULAR GAMEPLAY UI --- return (
{/* Header */}
{currentPlayer?.name}
{currentTeam?.name || 'No Team'}
{currentTeam?.score || 0} PTS
Rank #{myRank}
{/* Main Action Area */}
{/* Dynamic Question Text & Media on Mobile */} {isGameActive && currentQ && (

Current Question

{/* Media Preview on Player Device */} {currentQ.mediaUrl && (
{currentQ.mediaType === 'video' ? (