Implement proper youtube support

This commit is contained in:
2025-03-01 12:58:33 +01:00
parent f00ca9ba7c
commit 206988f4b7
8 changed files with 585 additions and 38 deletions

View File

@ -1,18 +1,5 @@
const roomController = require('./room');
const SONGS = [
{
id: 1,
title: "Black Steam",
artist: "Carrot Quest GmbH",
coverUrl: "https://mir-s3-cdn-cf.behance.net/project_modules/1400/fe529a64193929.5aca8500ba9ab.jpg"
},
{id: 2, title: "Sunset Dreams", artist: "Ocean Waves", coverUrl: "https://place-hold.it/500x500/"},
{id: 3, title: "Neon Nights", artist: "Electric Avenue", coverUrl: "https://place-hold.it/500x500/"},
{id: 4, title: "Mountain Echo", artist: "Wild Terrain", coverUrl: "https://place-hold.it/500x500/"},
{id: 5, title: "Urban Jungle", artist: "City Dwellers", coverUrl: "https://place-hold.it/500x500/"},
{id: 6, title: "Cosmic Journey", artist: "Stargazers", coverUrl: "https://place-hold.it/500x500/"}
];
const youtubeService = require('../services/youtubeService');
const gameStates = {};
@ -44,7 +31,7 @@ const initializeGameState = (roomId) => {
return gameStates[roomId];
};
const startNewRound = (roomId) => {
const startNewRound = async (roomId) => {
const gameState = gameStates[roomId];
if (!gameState) return false;
@ -63,9 +50,7 @@ const startNewRound = (roomId) => {
gameState.roles[user.id] = user.id === gameState.currentComposer ? 'composer' : 'guesser';
});
selectSongAndOptions(gameState);
return true;
return await selectSongAndOptions(gameState);
};
const determineNextComposer = (roomId, gameState, users) => {
@ -82,16 +67,35 @@ const determineNextComposer = (roomId, gameState, users) => {
return users[(currentIndex + 1) % users.length].id;
};
const selectSongAndOptions = (gameState) => {
gameState.selectedSong = SONGS[Math.floor(Math.random() * SONGS.length)];
const songOptions = [...SONGS].sort(() => Math.random() - 0.5).slice(0, 5);
if (!songOptions.includes(gameState.selectedSong)) {
songOptions[Math.floor(Math.random() * songOptions.length)] = gameState.selectedSong;
const selectSongAndOptions = async (gameState) => {
try {
const availableIds = await youtubeService.getAvailableSongIds();
if (!availableIds || availableIds.length === 0) {
console.error("No song IDs available for selection");
return false;
}
gameState.songOptions = songOptions;
const selectedId = availableIds[Math.floor(Math.random() * availableIds.length)];
gameState.selectedSong = { id: selectedId };
const optionIds = new Set([selectedId]);
const attempts = Math.min(20, availableIds.length);
let count = 0;
while (optionIds.size < 5 && count < attempts) {
const randomId = availableIds[Math.floor(Math.random() * availableIds.length)];
optionIds.add(randomId);
count++;
}
gameState.songOptions = Array.from(optionIds).map(id => ({ id }));
return true;
} catch (error) {
console.error("Error selecting songs and options:", error);
return false;
}
};
const getTimeRemaining = (roomId) => {