users fix
This commit is contained in:
parent
0fb6e53d7a
commit
5cabdba72f
4 changed files with 72 additions and 5 deletions
|
|
@ -121,7 +121,10 @@ export class GameGateway implements OnGatewayConnection, OnGatewayDisconnect, On
|
||||||
if (room) {
|
if (room) {
|
||||||
const questions = ((room.roomPack as unknown as { questions?: Question[] } | null)?.questions || []) as Question[];
|
const questions = ((room.roomPack as unknown as { questions?: Question[] } | null)?.questions || []) as Question[];
|
||||||
const firstQuestion = questions[0];
|
const firstQuestion = questions[0];
|
||||||
const firstParticipant = room.participants[0];
|
|
||||||
|
// Админ (хост) должен быть первым игроком
|
||||||
|
const hostParticipant = room.participants.find(p => p.userId === room.hostId);
|
||||||
|
const firstParticipant = hostParticipant || room.participants[0];
|
||||||
|
|
||||||
// Убеждаемся что firstQuestion.id - строка (UUID)
|
// Убеждаемся что firstQuestion.id - строка (UUID)
|
||||||
const firstQuestionId = firstQuestion?.id && typeof firstQuestion.id === 'string'
|
const firstQuestionId = firstQuestion?.id && typeof firstQuestion.id === 'string'
|
||||||
|
|
@ -348,12 +351,28 @@ export class GameGateway implements OnGatewayConnection, OnGatewayDisconnect, On
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Инициализация currentPlayerId если не установлен
|
||||||
|
let currentPlayerId = room.currentPlayerId;
|
||||||
|
if (!currentPlayerId && room.participants.length > 0) {
|
||||||
|
// Админ (хост) должен быть первым игроком
|
||||||
|
const hostParticipant = room.participants.find(p => p.userId === room.hostId);
|
||||||
|
const firstParticipant = hostParticipant || room.participants[0];
|
||||||
|
|
||||||
|
if (firstParticipant) {
|
||||||
|
currentPlayerId = firstParticipant.id;
|
||||||
|
await this.prisma.room.update({
|
||||||
|
where: { id: room.id },
|
||||||
|
data: { currentPlayerId: currentPlayerId }
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const fullState = {
|
const fullState = {
|
||||||
roomId: room.id,
|
roomId: room.id,
|
||||||
roomCode: room.code,
|
roomCode: room.code,
|
||||||
status: room.status,
|
status: room.status,
|
||||||
currentQuestionId: currentQuestionId,
|
currentQuestionId: currentQuestionId,
|
||||||
currentPlayerId: room.currentPlayerId,
|
currentPlayerId: currentPlayerId,
|
||||||
revealedAnswers: room.revealedAnswers as RevealedAnswers,
|
revealedAnswers: room.revealedAnswers as RevealedAnswers,
|
||||||
isGameOver: room.isGameOver,
|
isGameOver: room.isGameOver,
|
||||||
hostId: room.hostId,
|
hostId: room.hostId,
|
||||||
|
|
@ -414,7 +433,10 @@ export class GameGateway implements OnGatewayConnection, OnGatewayDisconnect, On
|
||||||
if (room) {
|
if (room) {
|
||||||
const questions = ((room.roomPack as unknown as { questions?: Question[] } | null)?.questions || []) as Question[];
|
const questions = ((room.roomPack as unknown as { questions?: Question[] } | null)?.questions || []) as Question[];
|
||||||
const firstQuestion = questions[0];
|
const firstQuestion = questions[0];
|
||||||
const firstParticipant = room.participants[0];
|
|
||||||
|
// Админ (хост) должен быть первым игроком
|
||||||
|
const hostParticipant = room.participants.find(p => p.userId === room.hostId);
|
||||||
|
const firstParticipant = hostParticipant || room.participants[0];
|
||||||
|
|
||||||
// Убеждаемся что firstQuestion.id - строка (UUID)
|
// Убеждаемся что firstQuestion.id - строка (UUID)
|
||||||
const firstQuestionId = firstQuestion?.id && typeof firstQuestion.id === 'string'
|
const firstQuestionId = firstQuestion?.id && typeof firstQuestion.id === 'string'
|
||||||
|
|
@ -443,6 +465,36 @@ export class GameGateway implements OnGatewayConnection, OnGatewayDisconnect, On
|
||||||
await this.broadcastFullState(payload.roomCode);
|
await this.broadcastFullState(payload.roomCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SubscribeMessage('setCurrentPlayer')
|
||||||
|
async handleSetCurrentPlayer(client: Socket, payload: { roomId: string; roomCode: string; userId: string; participantId: string }) {
|
||||||
|
const isHost = await this.isHost(payload.roomId, payload.userId);
|
||||||
|
if (!isHost) {
|
||||||
|
client.emit('error', { message: 'Only the host can set current player' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Проверяем, что участник существует и активен
|
||||||
|
const participant = await this.prisma.participant.findFirst({
|
||||||
|
where: {
|
||||||
|
id: payload.participantId,
|
||||||
|
roomId: payload.roomId,
|
||||||
|
isActive: true
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!participant) {
|
||||||
|
client.emit('error', { message: 'Participant not found' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.prisma.room.update({
|
||||||
|
where: { id: payload.roomId },
|
||||||
|
data: { currentPlayerId: payload.participantId }
|
||||||
|
});
|
||||||
|
|
||||||
|
await this.broadcastFullState(payload.roomCode);
|
||||||
|
}
|
||||||
|
|
||||||
@SubscribeMessage('updateRoomPack')
|
@SubscribeMessage('updateRoomPack')
|
||||||
async handleUpdateRoomPack(client: Socket, payload: { roomId: string; roomCode: string; userId: string; questions: any[] }) {
|
async handleUpdateRoomPack(client: Socket, payload: { roomId: string; roomCode: string; userId: string; questions: any[] }) {
|
||||||
const isHost = await this.isHost(payload.roomId, payload.userId);
|
const isHost = await this.isHost(payload.roomId, payload.userId);
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,7 @@ const Game = forwardRef(({
|
||||||
onAnswerClick = null,
|
onAnswerClick = null,
|
||||||
onPreviousQuestion = null,
|
onPreviousQuestion = null,
|
||||||
onNextQuestion = null,
|
onNextQuestion = null,
|
||||||
|
onSelectPlayer = null,
|
||||||
}, ref) => {
|
}, ref) => {
|
||||||
// Нет локального state - всё из props!
|
// Нет локального state - всё из props!
|
||||||
// Нет useEffect - нет синхронизации!
|
// Нет useEffect - нет синхронизации!
|
||||||
|
|
@ -42,6 +43,7 @@ const Game = forwardRef(({
|
||||||
players={players}
|
players={players}
|
||||||
currentPlayerId={currentPlayerId}
|
currentPlayerId={currentPlayerId}
|
||||||
playerScores={playerScores}
|
playerScores={playerScores}
|
||||||
|
onSelectPlayer={onSelectPlayer}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -13,8 +13,8 @@ const Players = ({ players, currentPlayerId, playerScores, onSelectPlayer }) =>
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
key={player.id}
|
key={player.id}
|
||||||
className={`player-item ${isActive ? 'player-active' : ''}`}
|
className={`player-item ${isActive ? 'player-active' : ''} ${onSelectPlayer ? 'player-clickable' : ''}`}
|
||||||
onClick={() => onSelectPlayer(player.id)}
|
onClick={() => onSelectPlayer && onSelectPlayer(player.id)}
|
||||||
>
|
>
|
||||||
<span className="player-name">{player.name}</span>
|
<span className="player-name">{player.name}</span>
|
||||||
<span className="player-score">{score}</span>
|
<span className="player-score">{score}</span>
|
||||||
|
|
|
||||||
|
|
@ -249,6 +249,18 @@ const GamePage = () => {
|
||||||
console.warn('Manual point award not implemented yet');
|
console.warn('Manual point award not implemented yet');
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleSelectPlayer = (participantId) => {
|
||||||
|
if (!gameState.roomId || !user) return;
|
||||||
|
if (!isHost) return; // Только хост может выбирать игрока
|
||||||
|
|
||||||
|
socketService.emit('setCurrentPlayer', {
|
||||||
|
roomId: gameState.roomId,
|
||||||
|
roomCode: gameState.roomCode,
|
||||||
|
userId: user.id,
|
||||||
|
participantId: participantId
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
// === Вычисляемые значения ===
|
// === Вычисляемые значения ===
|
||||||
|
|
||||||
const currentQuestion = gameState.questions.find(
|
const currentQuestion = gameState.questions.find(
|
||||||
|
|
@ -341,6 +353,7 @@ const GamePage = () => {
|
||||||
onAnswerClick={handleAnswerClick}
|
onAnswerClick={handleAnswerClick}
|
||||||
onPreviousQuestion={canGoPrev ? handlePrevQuestion : null}
|
onPreviousQuestion={canGoPrev ? handlePrevQuestion : null}
|
||||||
onNextQuestion={canGoNext ? handleNextQuestion : null}
|
onNextQuestion={canGoNext ? handleNextQuestion : null}
|
||||||
|
onSelectPlayer={isHost ? handleSelectPlayer : null}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue