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,
|
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,20 +363,24 @@ const Game = forwardRef(({
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="game">
|
<div className="game">
|
||||||
<PlayersModal
|
{!isOnlineMode && (
|
||||||
isOpen={isPlayersModalOpen}
|
<PlayersModal
|
||||||
onClose={() => setIsPlayersModalOpen(false)}
|
isOpen={isPlayersModalOpen}
|
||||||
players={players}
|
onClose={() => setIsPlayersModalOpen(false)}
|
||||||
onAddPlayer={handleAddPlayer}
|
players={players}
|
||||||
onRemovePlayer={handleRemovePlayer}
|
onAddPlayer={handleAddPlayer}
|
||||||
/>
|
onRemovePlayer={handleRemovePlayer}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
<QuestionsModal
|
{!isOnlineMode && (
|
||||||
isOpen={isQuestionsModalOpen}
|
<QuestionsModal
|
||||||
onClose={() => setIsQuestionsModalOpen(false)}
|
isOpen={isQuestionsModalOpen}
|
||||||
questions={questions}
|
onClose={() => setIsQuestionsModalOpen(false)}
|
||||||
onUpdateQuestions={onQuestionsChange}
|
questions={questions}
|
||||||
/>
|
onUpdateQuestions={onQuestionsChange}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
<div className="game-header">
|
<div className="game-header">
|
||||||
{players.length > 0 && (
|
{players.length > 0 && (
|
||||||
|
|
|
||||||
|
|
@ -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>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue