diff --git a/server/controller/game.js b/server/controller/game.js index 1eb584c..6aeedfa 100644 --- a/server/controller/game.js +++ b/server/controller/game.js @@ -1,6 +1,15 @@ const roomController = require('./room'); const youtubeService = require('../services/youtubeService'); +const shuffleArray = (array) => { + const shuffled = [...array]; + for (let i = shuffled.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)); + [shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]]; + } + return shuffled; +}; + const gameStates = {}; const initializeGameState = (roomId) => { @@ -67,6 +76,7 @@ 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 { const availableIds = await youtubeService.getAvailableSongIds(); @@ -89,7 +99,8 @@ const selectSongAndOptions = async (gameState) => { count++; } - gameState.songOptions = Array.from(optionIds).map(id => ({ id })); + // Shuffle the song options to randomize the position of the correct answer + gameState.songOptions = shuffleArray(Array.from(optionIds).map(id => ({ id }))); return true; } catch (error) {