Randomize answers
This commit is contained in:
parent
770cad27b0
commit
c31b7e35b6
@ -1,6 +1,15 @@
|
|||||||
const roomController = require('./room');
|
const roomController = require('./room');
|
||||||
const youtubeService = require('../services/youtubeService');
|
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 gameStates = {};
|
||||||
|
|
||||||
const initializeGameState = (roomId) => {
|
const initializeGameState = (roomId) => {
|
||||||
@ -67,6 +76,7 @@ const determineNextComposer = (roomId, gameState, users) => {
|
|||||||
return users[(currentIndex + 1) % users.length].id;
|
return users[(currentIndex + 1) % users.length].id;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Then modify the selectSongAndOptions function to use the shuffle:
|
||||||
const selectSongAndOptions = async (gameState) => {
|
const selectSongAndOptions = async (gameState) => {
|
||||||
try {
|
try {
|
||||||
const availableIds = await youtubeService.getAvailableSongIds();
|
const availableIds = await youtubeService.getAvailableSongIds();
|
||||||
@ -89,7 +99,8 @@ const selectSongAndOptions = async (gameState) => {
|
|||||||
count++;
|
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;
|
return true;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user