This commit is contained in:
Dmitry 2026-01-07 17:43:03 +03:00
parent a09e9ad73c
commit 9eb487035b
2 changed files with 63 additions and 16 deletions

View file

@ -12,13 +12,27 @@ const Game = forwardRef(({
currentQuestionIndex = 0, currentQuestionIndex = 0,
onQuestionIndexChange, onQuestionIndexChange,
onQuestionsChange, onQuestionsChange,
roomParticipants = null, // Участники для онлайн игры
isOnlineMode = false, // Флаг онлайн режима
}, ref) => { }, ref) => {
const { playEffect } = useVoice(); const { playEffect } = useVoice();
// Для локальной игры используем cookies, для онлайн - props
const [players, setPlayers] = useState(() => { const [players, setPlayers] = useState(() => {
if (isOnlineMode && roomParticipants) {
return roomParticipants.map(p => ({
id: p.id,
name: p.name,
}))
}
const savedPlayers = getCookie('gamePlayers') const savedPlayers = getCookie('gamePlayers')
return savedPlayers || [] return savedPlayers || []
}) })
const [currentPlayerId, setCurrentPlayerId] = useState(() => { const [currentPlayerId, setCurrentPlayerId] = useState(() => {
if (isOnlineMode && roomParticipants && roomParticipants.length > 0) {
return roomParticipants[0].id
}
const savedId = getCookie('gameCurrentPlayerId') const savedId = getCookie('gameCurrentPlayerId')
return savedId !== null ? savedId : null return savedId !== null ? savedId : null
}) })
@ -87,12 +101,39 @@ const Game = forwardRef(({
setCookie('gameOver', gameOver) setCookie('gameOver', gameOver)
}, [gameOver]) }, [gameOver])
// Устанавливаем первого игрока текущим, если есть игроки, но нет текущего игрока // Обновляем игроков при изменении roomParticipants (для онлайн режима)
useEffect(() => { useEffect(() => {
if (players.length > 0 && !currentPlayerId) { if (isOnlineMode && roomParticipants) {
const updatedPlayers = roomParticipants.map(p => ({
id: p.id,
name: p.name,
}))
setPlayers(updatedPlayers)
// Устанавливаем текущего игрока, если его нет
if (!currentPlayerId && updatedPlayers.length > 0) {
setCurrentPlayerId(updatedPlayers[0].id)
}
// Обновляем scores для новых игроков
setPlayerScores(prev => {
const newScores = { ...prev }
updatedPlayers.forEach(player => {
if (!(player.id in newScores)) {
newScores[player.id] = 0
}
})
return newScores
})
}
}, [isOnlineMode, roomParticipants, currentPlayerId])
// Устанавливаем первого игрока текущим, если есть игроки, но нет текущего игрока (для локальной игры)
useEffect(() => {
if (!isOnlineMode && players.length > 0 && !currentPlayerId) {
setCurrentPlayerId(players[0].id) setCurrentPlayerId(players[0].id)
} }
}, [players, currentPlayerId]) }, [isOnlineMode, players, currentPlayerId])
const currentQuestion = questions[currentQuestionIndex] const currentQuestion = questions[currentQuestionIndex]
const isLastQuestion = currentQuestionIndex === questions.length - 1 const isLastQuestion = currentQuestionIndex === questions.length - 1
@ -322,6 +363,7 @@ const Game = forwardRef(({
return ( return (
<div className="game"> <div className="game">
{!isOnlineMode && (
<PlayersModal <PlayersModal
isOpen={isPlayersModalOpen} isOpen={isPlayersModalOpen}
onClose={() => setIsPlayersModalOpen(false)} onClose={() => setIsPlayersModalOpen(false)}
@ -329,13 +371,16 @@ const Game = forwardRef(({
onAddPlayer={handleAddPlayer} onAddPlayer={handleAddPlayer}
onRemovePlayer={handleRemovePlayer} onRemovePlayer={handleRemovePlayer}
/> />
)}
{!isOnlineMode && (
<QuestionsModal <QuestionsModal
isOpen={isQuestionsModalOpen} isOpen={isQuestionsModalOpen}
onClose={() => setIsQuestionsModalOpen(false)} onClose={() => setIsQuestionsModalOpen(false)}
questions={questions} questions={questions}
onUpdateQuestions={onQuestionsChange} onUpdateQuestions={onQuestionsChange}
/> />
)}
<div className="game-header"> <div className="game-header">
{players.length > 0 && ( {players.length > 0 && (

View file

@ -210,6 +210,8 @@ const GamePage = () => {
currentQuestionIndex={currentQuestionIndex} currentQuestionIndex={currentQuestionIndex}
onQuestionIndexChange={setCurrentQuestionIndex} onQuestionIndexChange={setCurrentQuestionIndex}
onQuestionsChange={handleQuestionsChange} onQuestionsChange={handleQuestionsChange}
roomParticipants={participants}
isOnlineMode={true}
/> />
</div> </div>
</div> </div>