import React, { useState, useEffect } from 'react'; import { useGame } from '../context/GameContext'; import { GamePhase } from '../types'; import { Trophy, Zap, Target, LogOut, Image as ImageIcon } 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 (
Enter your details to enter the lobby.
Sit tight! You'll be in shortly.
You got the points!
Better luck next time.
| Rank | Name | Score | Acc |
|---|---|---|---|
| #{i + 1} | {p.name} | {p.score} | {p.stats.correctAnswers}/{p.stats.totalBuzzes} |
Current Question
{/* Media Preview on Player Device */} {currentQ.mediaUrl && ({currentQ.text}
Answer the host now.
Waiting for game to start...
} {gameState.phase === GamePhase.COUNTDOWN &&{gameState.countdownValue}
} {gameState.phase === GamePhase.QUESTION_DISPLAY &&Listen carefully...
}