Fix playlist bug
This commit is contained in:
parent
a1193ea87f
commit
195980032c
@ -14,6 +14,9 @@ const gameStates = {};
|
|||||||
|
|
||||||
const initializeGameState = (roomId) => {
|
const initializeGameState = (roomId) => {
|
||||||
if (!gameStates[roomId]) {
|
if (!gameStates[roomId]) {
|
||||||
|
const selectedPlaylist = roomController.getWinningPlaylist(roomId);
|
||||||
|
console.log(`Initializing game with winning playlist: ${selectedPlaylist}`);
|
||||||
|
|
||||||
gameStates[roomId] = {
|
gameStates[roomId] = {
|
||||||
round: 0,
|
round: 0,
|
||||||
phase: 'waiting',
|
phase: 'waiting',
|
||||||
@ -29,6 +32,7 @@ const initializeGameState = (roomId) => {
|
|||||||
guessing: 10
|
guessing: 10
|
||||||
},
|
},
|
||||||
lastFrequency: 440,
|
lastFrequency: 440,
|
||||||
|
selectedPlaylist: selectedPlaylist,
|
||||||
};
|
};
|
||||||
|
|
||||||
const users = roomController.getRoomUsers(roomId);
|
const users = roomController.getRoomUsers(roomId);
|
||||||
@ -82,7 +86,8 @@ const determineNextComposer = (roomId, gameState, users) => {
|
|||||||
// Then modify the selectSongAndOptions function to use the shuffle:
|
// Then modify the selectSongAndOptions function to use the shuffle:
|
||||||
const selectSongAndOptions = async (gameState) => {
|
const selectSongAndOptions = async (gameState) => {
|
||||||
try {
|
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) {
|
if (!availableIds || availableIds.length === 0) {
|
||||||
console.error("No song IDs available for selection");
|
console.error("No song IDs available for selection");
|
||||||
|
@ -167,3 +167,33 @@ module.exports.voteForPlaylist = (roomId, userId, playlistId) => {
|
|||||||
module.exports.getPlaylistVotes = (roomId) => {
|
module.exports.getPlaylistVotes = (roomId) => {
|
||||||
return rooms[roomId]?.playlistVotes || {};
|
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;
|
||||||
|
};
|
@ -301,11 +301,20 @@ module.exports = (io) => (socket) => {
|
|||||||
|
|
||||||
socket.on("get-playlist-songs", async () => {
|
socket.on("get-playlist-songs", async () => {
|
||||||
try {
|
try {
|
||||||
const songs = await youtubeService.fetchPlaylistSongs();
|
const roomId = roomController.getUserRoom(socket.id);
|
||||||
|
if (!roomId) {
|
||||||
|
throw new Error("User not in a room");
|
||||||
|
}
|
||||||
|
|
||||||
|
const gameState = gameController.getGameState(roomId);
|
||||||
|
const playlistId = gameState?.selectedPlaylist || null;
|
||||||
|
|
||||||
|
console.log(`Fetching songs for playlist ${playlistId}`);
|
||||||
|
const songs = await youtubeService.fetchPlaylistSongs(playlistId);
|
||||||
socket.emit("playlist-songs", { songs });
|
socket.emit("playlist-songs", { songs });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error sending playlist songs:", error);
|
console.error("Error sending playlist songs:", error);
|
||||||
socket.emit("playlist-songs", { songs: youtubeService.getDefaultSongs() });
|
socket.emit("playlist-songs", { songs: [] });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -21,8 +21,14 @@ const roomController = require('./controller/room');
|
|||||||
const gameController = require('./controller/game');
|
const gameController = require('./controller/game');
|
||||||
roomController.setCleanupGameState(gameController.cleanupGameState);
|
roomController.setCleanupGameState(gameController.cleanupGameState);
|
||||||
|
|
||||||
// Handle socket connections
|
io.on("connection", (socket) => {
|
||||||
io.on("connection", require("./handler/connection")(io));
|
try {
|
||||||
|
require("./handler/connection")(io)(socket);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error handling socket connection:', error);
|
||||||
|
socket.emit('error', { message: 'Internal server error' });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
server.on('error', (error) => {
|
server.on('error', (error) => {
|
||||||
console.error('Server error:', error);
|
console.error('Server error:', error);
|
||||||
|
@ -12,23 +12,30 @@ const PLAYLISTS = {
|
|||||||
|
|
||||||
const API_KEY = process.env.YOUTUBE_API_KEY;
|
const API_KEY = process.env.YOUTUBE_API_KEY;
|
||||||
|
|
||||||
let cachedSongs = null;
|
const cachedSongsByPlaylist = {};
|
||||||
let lastFetchTime = 0;
|
|
||||||
const CACHE_TTL = 3600000; // 1 hour
|
const CACHE_TTL = 3600000; // 1 hour
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetches songs from YouTube playlist and returns them
|
* Fetches songs from YouTube playlist and returns them
|
||||||
*/
|
*/
|
||||||
const fetchPlaylistSongs = async () => {
|
const fetchPlaylistSongs = async (playlistId = null) => {
|
||||||
|
if (!playlistId) {
|
||||||
|
console.warn("No playlist ID provided, using default");
|
||||||
|
playlistId = PLAYLISTS.seventies; // default fallback
|
||||||
|
}
|
||||||
|
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
if (cachedSongs && cachedSongs.length > 0 && (now - lastFetchTime) < CACHE_TTL) {
|
if (cachedSongsByPlaylist[playlistId] &&
|
||||||
console.log("Returning cached YouTube songs:", cachedSongs.length);
|
cachedSongsByPlaylist[playlistId].songs.length > 0 &&
|
||||||
return cachedSongs;
|
(now - cachedSongsByPlaylist[playlistId].timestamp) < CACHE_TTL) {
|
||||||
|
console.log(`Using cached songs for playlist ${playlistId}`);
|
||||||
|
return cachedSongsByPlaylist[playlistId].songs;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
console.log("Fetching songs from YouTube API...");
|
console.log(`Fetching fresh songs from YouTube API for playlist ${playlistId}...`);
|
||||||
const playlistUrl = `https://www.googleapis.com/youtube/v3/playlistItems?part=snippet,contentDetails&maxResults=50&playlistId=${PLAYLIST_ID}&key=${YOUTUBE_API_KEY}`;
|
const playlistUrl = `https://www.googleapis.com/youtube/v3/playlistItems?part=snippet,contentDetails&maxResults=50&playlistId=${playlistId}&key=${YOUTUBE_API_KEY}`;
|
||||||
const response = await fetch(playlistUrl);
|
const response = await fetch(playlistUrl);
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
|
|
||||||
@ -38,7 +45,6 @@ const fetchPlaylistSongs = async () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!data.items || !data.items.length) {
|
if (!data.items || !data.items.length) {
|
||||||
console.error("No items found in YouTube playlist");
|
|
||||||
throw new Error("No songs found in the playlist");
|
throw new Error("No songs found in the playlist");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,35 +53,24 @@ const fetchPlaylistSongs = async () => {
|
|||||||
youtubeId: item.contentDetails.videoId,
|
youtubeId: item.contentDetails.videoId,
|
||||||
title: item.snippet.title,
|
title: item.snippet.title,
|
||||||
artist: item.snippet.videoOwnerChannelTitle || "Unknown Artist",
|
artist: item.snippet.videoOwnerChannelTitle || "Unknown Artist",
|
||||||
coverUrl: item.snippet.thumbnails.high?.url || item.snippet.thumbnails.default?.url ||
|
coverUrl: item.snippet.thumbnails.high?.url || item.snippet.thumbnails.default?.url,
|
||||||
"https://place-hold.it/500x500/333/fff?text=No%20Thumbnail",
|
playlistId: playlistId
|
||||||
}));
|
}));
|
||||||
|
|
||||||
cachedSongs = songs;
|
cachedSongsByPlaylist[playlistId] = {
|
||||||
lastFetchTime = now;
|
songs,
|
||||||
console.log("Fetched and cached YouTube songs:", songs.length);
|
timestamp: now
|
||||||
|
};
|
||||||
|
|
||||||
return songs;
|
return songs;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error fetching YouTube playlist:", error);
|
console.error(`Error fetching YouTube playlist ${playlistId}:`, error);
|
||||||
|
return cachedSongsByPlaylist[playlistId]?.songs || getDefaultSongs();
|
||||||
if (cachedSongs && cachedSongs.length > 0) {
|
|
||||||
console.log("Using last cached songs due to API error");
|
|
||||||
return cachedSongs;
|
|
||||||
}
|
|
||||||
|
|
||||||
return [{
|
|
||||||
id: 1,
|
|
||||||
title: "Could not load songs",
|
|
||||||
artist: "API Error",
|
|
||||||
coverUrl: "https://place-hold.it/500x500/f00/fff?text=Error",
|
|
||||||
youtubeId: "dQw4w9WgXcQ"
|
|
||||||
}];
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const getAvailableSongIds = async () => {
|
const getAvailableSongIds = async (playlistId) => {
|
||||||
const songs = await fetchPlaylistSongs();
|
const songs = await fetchPlaylistSongs(playlistId);
|
||||||
return songs.map(song => song.id);
|
return songs.map(song => song.id);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user