Fix guessing phase
This commit is contained in:
@ -107,19 +107,31 @@ const getTimeRemaining = (roomId) => {
|
||||
|
||||
const advancePhase = (roomId) => {
|
||||
const gameState = gameStates[roomId];
|
||||
if (!gameState) return false;
|
||||
|
||||
if (gameState.phase === 'composing') {
|
||||
if (!gameState) {
|
||||
console.error(`Cannot advance phase: no game state for room ${roomId}`);
|
||||
return false;
|
||||
}
|
||||
|
||||
const currentPhase = gameState.phase;
|
||||
console.log(`Advancing phase for room ${roomId} from ${currentPhase}`);
|
||||
|
||||
if (currentPhase === 'composing') {
|
||||
gameState.phase = 'guessing';
|
||||
gameState.roundStartTime = Date.now();
|
||||
return true;
|
||||
} else if (gameState.phase === 'guessing') {
|
||||
console.log(`Room ${roomId} advanced to guessing phase`);
|
||||
return { phase: 'guessing' };
|
||||
}
|
||||
else if (currentPhase === 'guessing') {
|
||||
gameState.phase = 'results';
|
||||
return true;
|
||||
} else if (gameState.phase === 'results') {
|
||||
console.log(`Room ${roomId} advanced to results phase`);
|
||||
return { phase: 'results' };
|
||||
}
|
||||
else if (currentPhase === 'results') {
|
||||
console.log(`Room ${roomId} starting new round from results phase`);
|
||||
return startNewRound(roomId);
|
||||
}
|
||||
|
||||
|
||||
console.warn(`Cannot advance from unknown phase "${currentPhase}" in room ${roomId}`);
|
||||
return false;
|
||||
};
|
||||
|
||||
@ -172,6 +184,12 @@ const cleanupGameState = (roomId) => {
|
||||
delete gameStates[roomId];
|
||||
};
|
||||
|
||||
const getCurrentComposer = (roomId) => gameStates[roomId]?.currentComposer || null;
|
||||
|
||||
const getGameState = (roomId) => {
|
||||
return gameStates[roomId] || null;
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
initializeGameState,
|
||||
startNewRound,
|
||||
@ -185,5 +203,7 @@ module.exports = {
|
||||
getUserRole,
|
||||
getSongOptions,
|
||||
getSelectedSong,
|
||||
cleanupGameState
|
||||
cleanupGameState,
|
||||
getCurrentComposer,
|
||||
getGameState
|
||||
};
|
||||
|
@ -121,4 +121,15 @@ module.exports.validateRoomMembers = (io, roomId) => {
|
||||
return validMembers;
|
||||
}
|
||||
|
||||
module.exports.setCleanupGameState = setCleanupGameState;
|
||||
module.exports.setCleanupGameState = setCleanupGameState;
|
||||
|
||||
module.exports.getUserName = (userId) => {
|
||||
for (const roomId in rooms) {
|
||||
const room = rooms[roomId];
|
||||
const member = room.members.find(m => m.id === userId);
|
||||
if (member) {
|
||||
return member.name;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
};
|
@ -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", () => {
|
||||
|
Reference in New Issue
Block a user