From 560f016b0843c8ab43a140dd3547a100c8f6bdb8 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Fri, 9 Jan 2026 02:05:03 +0300 Subject: [PATCH] users fix --- backend/src/game/game.gateway.ts | 46 +++++++++++++++++++++++++------- src/components/Game.jsx | 4 +-- src/pages/GamePage.jsx | 17 ++++++++++-- 3 files changed, 53 insertions(+), 14 deletions(-) diff --git a/backend/src/game/game.gateway.ts b/backend/src/game/game.gateway.ts index c920ee9..5045f3e 100644 --- a/backend/src/game/game.gateway.ts +++ b/backend/src/game/game.gateway.ts @@ -322,7 +322,26 @@ export class GameGateway implements OnGatewayConnection, OnGatewayDisconnect, On if (!room) return; - const questions = ((room.roomPack as unknown as { questions?: Question[] } | null)?.questions || []) as Question[]; + // Извлекаем вопросы из roomPack.questions (JSON поле) + const roomPackQuestions = (room.roomPack as unknown as { questions?: any } | null)?.questions; + let questions: Question[] = []; + + if (roomPackQuestions) { + // Если это уже массив, используем как есть + if (Array.isArray(roomPackQuestions)) { + questions = roomPackQuestions as Question[]; + } else if (typeof roomPackQuestions === 'string') { + // Если это строка, парсим JSON + try { + questions = JSON.parse(roomPackQuestions) as Question[]; + } catch (e) { + console.error('Error parsing roomPack.questions:', e); + questions = []; + } + } + } + + console.log(`📋 Room ${roomCode}: Found ${questions.length} questions`); // Инициализация currentQuestionId если не установлен или невалиден let currentQuestionId = (room.currentQuestionId as string | null) || null; @@ -383,15 +402,22 @@ export class GameGateway implements OnGatewayConnection, OnGatewayDisconnect, On role: p.role, score: p.score })), - questions: questions.map((q) => ({ - id: q.id, - text: q.text || q.question || '', - answers: (q.answers || []).map((a) => ({ - id: a.id, - text: a.text, - points: a.points - })) - })) + questions: questions.map((q: any) => { + // Убеждаемся, что у вопроса есть id + const questionId = q.id || (typeof q === 'object' && 'id' in q ? q.id : null); + if (!questionId) { + console.warn('⚠️ Question without ID:', q); + } + return { + id: questionId || `temp-${Math.random()}`, + text: q.text || q.question || '', + answers: (q.answers || []).map((a: any) => ({ + id: a.id || `answer-${Math.random()}`, + text: a.text || '', + points: a.points || 0 + })) + }; + }) }; this.server.to(roomCode).emit('gameStateUpdated', fullState); diff --git a/src/components/Game.jsx b/src/components/Game.jsx index 3c5d0c1..02306b0 100644 --- a/src/components/Game.jsx +++ b/src/components/Game.jsx @@ -24,7 +24,7 @@ const Game = forwardRef(({ const handleAnswerClick = (answerId, points) => { if (!currentQuestion) return; if (revealedAnswers.includes(answerId)) return; // Проверка по UUID - if (!currentPlayerId) return; + // Убираем проверку currentPlayerId - хост может открывать ответы вручную if (!onAnswerClick) return; onAnswerClick(answerId, points); // Передаем UUID @@ -48,7 +48,7 @@ const Game = forwardRef(({ )} - {players.length > 0 && currentPlayerId ? ( + {players.length > 0 ? (
{currentQuestion ? ( { const myParticipant = gameState.participants.find(p => p.userId === user.id); if (!myParticipant) return; - // Проверка очереди - if (gameState.currentPlayerId !== myParticipant.id) { + // Проверка очереди (только для не-хостов) + if (!isHost && gameState.currentPlayerId !== myParticipant.id) { alert('Сейчас не ваша очередь!'); return; } @@ -271,6 +271,19 @@ const GamePage = () => { q => q.id === gameState.currentQuestionId ); + // Отладочная информация + useEffect(() => { + if (gameState.questions.length > 0) { + console.log('📋 Questions loaded:', gameState.questions.length); + console.log('🔍 Current question ID:', gameState.currentQuestionId); + console.log('✅ Current question found:', !!currentQuestion); + if (!currentQuestion && gameState.currentQuestionId) { + console.warn('⚠️ Question ID not found in questions list:', gameState.currentQuestionId); + console.log('📝 Available question IDs:', gameState.questions.map(q => q.id)); + } + } + }, [gameState.questions, gameState.currentQuestionId, currentQuestion]); + const revealedForCurrentQ = gameState.revealedAnswers[gameState.currentQuestionId] || []; const playerScores = gameState.participants.reduce(