From 9eb487035b42ceaa7105611b09e8959535b7bfdf Mon Sep 17 00:00:00 2001 From: Dmitry Date: Wed, 7 Jan 2026 17:43:03 +0300 Subject: [PATCH] stuff --- src/components/Game.jsx | 77 ++++++++++++++++++++++++++++++++--------- src/pages/GamePage.jsx | 2 ++ 2 files changed, 63 insertions(+), 16 deletions(-) diff --git a/src/components/Game.jsx b/src/components/Game.jsx index 1c24473..8675ad4 100644 --- a/src/components/Game.jsx +++ b/src/components/Game.jsx @@ -12,13 +12,27 @@ const Game = forwardRef(({ currentQuestionIndex = 0, onQuestionIndexChange, onQuestionsChange, + roomParticipants = null, // Участники для онлайн игры + isOnlineMode = false, // Флаг онлайн режима }, ref) => { const { playEffect } = useVoice(); + + // Для локальной игры используем cookies, для онлайн - props const [players, setPlayers] = useState(() => { + if (isOnlineMode && roomParticipants) { + return roomParticipants.map(p => ({ + id: p.id, + name: p.name, + })) + } const savedPlayers = getCookie('gamePlayers') return savedPlayers || [] }) + const [currentPlayerId, setCurrentPlayerId] = useState(() => { + if (isOnlineMode && roomParticipants && roomParticipants.length > 0) { + return roomParticipants[0].id + } const savedId = getCookie('gameCurrentPlayerId') return savedId !== null ? savedId : null }) @@ -87,12 +101,39 @@ const Game = forwardRef(({ setCookie('gameOver', gameOver) }, [gameOver]) - // Устанавливаем первого игрока текущим, если есть игроки, но нет текущего игрока + // Обновляем игроков при изменении roomParticipants (для онлайн режима) 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) } - }, [players, currentPlayerId]) + }, [isOnlineMode, players, currentPlayerId]) const currentQuestion = questions[currentQuestionIndex] const isLastQuestion = currentQuestionIndex === questions.length - 1 @@ -322,20 +363,24 @@ const Game = forwardRef(({ return (
- setIsPlayersModalOpen(false)} - players={players} - onAddPlayer={handleAddPlayer} - onRemovePlayer={handleRemovePlayer} - /> + {!isOnlineMode && ( + setIsPlayersModalOpen(false)} + players={players} + onAddPlayer={handleAddPlayer} + onRemovePlayer={handleRemovePlayer} + /> + )} - setIsQuestionsModalOpen(false)} - questions={questions} - onUpdateQuestions={onQuestionsChange} - /> + {!isOnlineMode && ( + setIsQuestionsModalOpen(false)} + questions={questions} + onUpdateQuestions={onQuestionsChange} + /> + )}
{players.length > 0 && ( diff --git a/src/pages/GamePage.jsx b/src/pages/GamePage.jsx index 40099a7..1348375 100644 --- a/src/pages/GamePage.jsx +++ b/src/pages/GamePage.jsx @@ -210,6 +210,8 @@ const GamePage = () => { currentQuestionIndex={currentQuestionIndex} onQuestionIndexChange={setCurrentQuestionIndex} onQuestionsChange={handleQuestionsChange} + roomParticipants={participants} + isOnlineMode={true} />