stuff
This commit is contained in:
parent
a09e9ad73c
commit
9eb487035b
2 changed files with 63 additions and 16 deletions
|
|
@ -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 (
|
||||
<div className="game">
|
||||
<PlayersModal
|
||||
isOpen={isPlayersModalOpen}
|
||||
onClose={() => setIsPlayersModalOpen(false)}
|
||||
players={players}
|
||||
onAddPlayer={handleAddPlayer}
|
||||
onRemovePlayer={handleRemovePlayer}
|
||||
/>
|
||||
{!isOnlineMode && (
|
||||
<PlayersModal
|
||||
isOpen={isPlayersModalOpen}
|
||||
onClose={() => setIsPlayersModalOpen(false)}
|
||||
players={players}
|
||||
onAddPlayer={handleAddPlayer}
|
||||
onRemovePlayer={handleRemovePlayer}
|
||||
/>
|
||||
)}
|
||||
|
||||
<QuestionsModal
|
||||
isOpen={isQuestionsModalOpen}
|
||||
onClose={() => setIsQuestionsModalOpen(false)}
|
||||
questions={questions}
|
||||
onUpdateQuestions={onQuestionsChange}
|
||||
/>
|
||||
{!isOnlineMode && (
|
||||
<QuestionsModal
|
||||
isOpen={isQuestionsModalOpen}
|
||||
onClose={() => setIsQuestionsModalOpen(false)}
|
||||
questions={questions}
|
||||
onUpdateQuestions={onQuestionsChange}
|
||||
/>
|
||||
)}
|
||||
|
||||
<div className="game-header">
|
||||
{players.length > 0 && (
|
||||
|
|
|
|||
|
|
@ -210,6 +210,8 @@ const GamePage = () => {
|
|||
currentQuestionIndex={currentQuestionIndex}
|
||||
onQuestionIndexChange={setCurrentQuestionIndex}
|
||||
onQuestionsChange={handleQuestionsChange}
|
||||
roomParticipants={participants}
|
||||
isOnlineMode={true}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
|||
Loading…
Reference in a new issue