Fix guessing phase

This commit is contained in:
2025-03-01 00:40:01 +01:00
parent 87fc9a2f39
commit a3daab2f84
6 changed files with 285 additions and 44 deletions

View File

@ -8,6 +8,7 @@ module.exports = (io) => (socket) => {
const clearRoomTimers = (roomId) => {
if (phaseTimers[roomId]) {
console.log(`Clearing timer for room ${roomId}`);
clearTimeout(phaseTimers[roomId]);
delete phaseTimers[roomId];
}
@ -16,21 +17,33 @@ module.exports = (io) => (socket) => {
const startPhaseTimer = (roomId) => {
clearRoomTimers(roomId);
const gameState = gameController.getGameState(roomId);
if (!gameState) {
console.error(`Cannot start timer: no game state for room ${roomId}`);
return;
}
const timeRemaining = gameController.getTimeRemaining(roomId) * 1000;
console.log(`Starting ${gameState.phase} phase timer for room ${roomId} with ${timeRemaining}ms`);
phaseTimers[roomId] = setTimeout(() => {
console.log(`Timer expired for room ${roomId}, advancing phase from ${gameState.phase}`);
const advanced = gameController.advancePhase(roomId);
if (!advanced) return;
if (!advanced) {
console.log(`Failed to advance phase for room ${roomId}`);
return;
}
if (typeof advanced === 'boolean') {
handleRoundStart(roomId);
} else {
const currentPhase = advanced.phase || 'results';
const newPhase = gameController.getGameState(roomId).phase;
console.log(`Advanced to ${newPhase} phase in room ${roomId}`);
if (currentPhase === 'guessing') {
if (newPhase === 'guessing') {
handleGuessingPhaseStart(roomId);
} else if (currentPhase === 'results') {
} else if (newPhase === 'results') {
handleResultsPhaseStart(roomId);
}
}
@ -59,10 +72,20 @@ module.exports = (io) => (socket) => {
};
const handleGuessingPhaseStart = (roomId) => {
const gameState = gameController.getGameState(roomId);
if (!gameState) return;
const roles = gameController.getRoles(roomId);
const songOptions = gameController.getSongOptions(roomId);
const timeLeft = gameController.getTimeRemaining(roomId);
console.log(`Starting guessing phase for room ${roomId} with ${Object.keys(roles).length} players`);
io.to(roomId).emit('phase-changed', {
phase: 'guessing',
timeRemaining: timeLeft
});
Object.entries(roles).forEach(([userId, role]) => {
if (role === 'guesser') {
io.to(userId).emit('guessing-phase-started', {
@ -71,7 +94,7 @@ module.exports = (io) => (socket) => {
});
}
});
startPhaseTimer(roomId);
};
@ -186,8 +209,28 @@ module.exports = (io) => (socket) => {
const roomId = roomController.getUserRoom(socket.id);
if (!roomId) return;
console.log(`User ${socket.id} submitted guess: Song ID ${songId}`);
const gamePhase = gameController.getGameState(roomId)?.phase;
if (gamePhase !== 'guessing') {
console.log(`Ignoring guess: room ${roomId} is in ${gamePhase} phase, not guessing`);
return;
}
const result = gameController.submitGuess(roomId, socket.id, songId);
if (result) socket.emit("guess-result", result);
if (result) {
console.log(`Guess result for ${socket.id}:`, result);
socket.emit("guess-result", result);
const currentComposer = gameController.getCurrentComposer(roomId);
if (currentComposer) {
const guesserName = roomController.getUserName(socket.id) || "Someone";
io.to(currentComposer).emit("player-guessed", {
guesserName,
isCorrect: result.isCorrect
});
}
}
});
socket.on("next-round", () => {