Randomize answers

This commit is contained in:
Mathias Wagner 2025-03-01 14:41:40 +01:00
parent 770cad27b0
commit c31b7e35b6

View File

@ -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) {