Create Ending
All checks were successful
Publish Docker image / Push Docker image to Docker Hub (push) Successful in 1m36s
All checks were successful
Publish Docker image / Push Docker image to Docker Hub (push) Successful in 1m36s
This commit is contained in:
@@ -44,6 +44,8 @@ const initializeGameState = (roomId) => {
|
||||
return gameStates[roomId];
|
||||
};
|
||||
|
||||
const MAX_ROUNDS = 5;
|
||||
|
||||
const startNewRound = async (roomId) => {
|
||||
const gameState = gameStates[roomId];
|
||||
if (!gameState) return false;
|
||||
@@ -52,6 +54,11 @@ const startNewRound = async (roomId) => {
|
||||
if (users.length < 2) return false;
|
||||
|
||||
gameState.round += 1;
|
||||
|
||||
if (gameState.round > MAX_ROUNDS) {
|
||||
return { gameEnd: true, finalScores: gameState.scores };
|
||||
}
|
||||
|
||||
gameState.phase = 'composing';
|
||||
gameState.guessResults = {};
|
||||
gameState.roundStartTime = Date.now();
|
||||
@@ -83,7 +90,6 @@ const determineNextComposer = (roomId, gameState, users) => {
|
||||
return users[(currentIndex + 1) % users.length].id;
|
||||
};
|
||||
|
||||
// Then modify the selectSongAndOptions function to use the shuffle:
|
||||
const selectSongAndOptions = async (gameState) => {
|
||||
try {
|
||||
console.log(`Fetching songs from playlist: ${gameState.selectedPlaylist}`);
|
||||
@@ -107,7 +113,6 @@ const selectSongAndOptions = async (gameState) => {
|
||||
count++;
|
||||
}
|
||||
|
||||
// Shuffle the song options to randomize the position of the correct answer
|
||||
gameState.songOptions = shuffleArray(Array.from(optionIds).map(id => ({ id })));
|
||||
|
||||
return true;
|
||||
@@ -219,6 +224,27 @@ const getGameState = (roomId) => {
|
||||
return gameStates[roomId] || null;
|
||||
};
|
||||
|
||||
const getFinalScores = (roomId) => {
|
||||
const gameState = gameStates[roomId];
|
||||
if (!gameState) return null;
|
||||
|
||||
const users = roomController.getRoomUsers(roomId);
|
||||
const finalScores = {};
|
||||
|
||||
Object.entries(gameState.scores).forEach(([userId, score]) => {
|
||||
const user = users.find(u => u.id === userId);
|
||||
finalScores[userId] = {
|
||||
score: score,
|
||||
name: user?.name || "Player"
|
||||
};
|
||||
});
|
||||
|
||||
return {
|
||||
scores: finalScores,
|
||||
lastRound: gameState.round
|
||||
};
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
initializeGameState,
|
||||
startNewRound,
|
||||
@@ -234,5 +260,7 @@ module.exports = {
|
||||
getSelectedSong,
|
||||
cleanupGameState,
|
||||
getCurrentComposer,
|
||||
getGameState
|
||||
getGameState,
|
||||
getFinalScores,
|
||||
MAX_ROUNDS
|
||||
};
|
||||
|
@@ -109,6 +109,23 @@ module.exports = (io) => (socket) => {
|
||||
io.to(roomId).emit('round-results', results);
|
||||
};
|
||||
|
||||
const handleNextRound = async (roomId) => {
|
||||
try {
|
||||
const result = await gameController.startNewRound(roomId);
|
||||
|
||||
if (result.gameEnd) {
|
||||
const finalData = gameController.getFinalScores(roomId);
|
||||
io.to(roomId).emit('game-ended', finalData);
|
||||
return;
|
||||
}
|
||||
|
||||
handleRoundStart(roomId);
|
||||
} catch (error) {
|
||||
console.error("Error starting next round:", error);
|
||||
socket.emit("error", { message: "Failed to start next round due to an error" });
|
||||
}
|
||||
};
|
||||
|
||||
socket.on("disconnect", () => {
|
||||
const roomId = roomController.getUserRoom(socket.id);
|
||||
if (roomId) {
|
||||
@@ -298,17 +315,7 @@ module.exports = (io) => (socket) => {
|
||||
return socket.emit("error", { message: "At least 2 players are required" });
|
||||
}
|
||||
|
||||
try {
|
||||
const success = await gameController.startNewRound(roomId);
|
||||
if (success) {
|
||||
handleRoundStart(roomId);
|
||||
} else {
|
||||
socket.emit("error", { message: "Failed to start next round" });
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Error starting next round:", error);
|
||||
socket.emit("error", { message: "Failed to start next round due to an error" });
|
||||
}
|
||||
await handleNextRound(roomId);
|
||||
});
|
||||
|
||||
socket.on("get-current-frequency", () => {
|
||||
|
Reference in New Issue
Block a user