Fix playlist bug
This commit is contained in:
@ -14,6 +14,9 @@ const gameStates = {};
|
||||
|
||||
const initializeGameState = (roomId) => {
|
||||
if (!gameStates[roomId]) {
|
||||
const selectedPlaylist = roomController.getWinningPlaylist(roomId);
|
||||
console.log(`Initializing game with winning playlist: ${selectedPlaylist}`);
|
||||
|
||||
gameStates[roomId] = {
|
||||
round: 0,
|
||||
phase: 'waiting',
|
||||
@ -29,6 +32,7 @@ const initializeGameState = (roomId) => {
|
||||
guessing: 10
|
||||
},
|
||||
lastFrequency: 440,
|
||||
selectedPlaylist: selectedPlaylist,
|
||||
};
|
||||
|
||||
const users = roomController.getRoomUsers(roomId);
|
||||
@ -82,7 +86,8 @@ const determineNextComposer = (roomId, gameState, users) => {
|
||||
// Then modify the selectSongAndOptions function to use the shuffle:
|
||||
const selectSongAndOptions = async (gameState) => {
|
||||
try {
|
||||
const availableIds = await youtubeService.getAvailableSongIds();
|
||||
console.log(`Fetching songs from playlist: ${gameState.selectedPlaylist}`);
|
||||
const availableIds = await youtubeService.getAvailableSongIds(gameState.selectedPlaylist);
|
||||
|
||||
if (!availableIds || availableIds.length === 0) {
|
||||
console.error("No song IDs available for selection");
|
||||
|
@ -166,4 +166,34 @@ module.exports.voteForPlaylist = (roomId, userId, playlistId) => {
|
||||
|
||||
module.exports.getPlaylistVotes = (roomId) => {
|
||||
return rooms[roomId]?.playlistVotes || {};
|
||||
};
|
||||
|
||||
module.exports.getWinningPlaylist = (roomId) => {
|
||||
const room = rooms[roomId];
|
||||
if (!room || !room.playlistVotes) {
|
||||
console.log(`No votes found for room ${roomId}, using default playlist`);
|
||||
return Object.values(require('../services/youtubeService').PLAYLISTS)[0];
|
||||
}
|
||||
|
||||
let maxVotes = 0;
|
||||
let winningPlaylist = null;
|
||||
|
||||
console.log(`Calculating winning playlist for room ${roomId}`);
|
||||
console.log('Current votes:', room.playlistVotes);
|
||||
|
||||
Object.entries(room.playlistVotes).forEach(([playlistId, voters]) => {
|
||||
console.log(`Playlist ${playlistId} has ${voters.length} votes`);
|
||||
if (voters.length > maxVotes) {
|
||||
maxVotes = voters.length;
|
||||
winningPlaylist = playlistId;
|
||||
}
|
||||
});
|
||||
|
||||
if (!winningPlaylist) {
|
||||
console.log('No winning playlist found, using default');
|
||||
winningPlaylist = Object.values(require('../services/youtubeService').PLAYLISTS)[0];
|
||||
}
|
||||
|
||||
console.log(`Selected winning playlist: ${winningPlaylist}`);
|
||||
return winningPlaylist;
|
||||
};
|
Reference in New Issue
Block a user