Implement proper youtube support
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
const roomController = require("../controller/room");
|
||||
const gameController = require("../controller/game");
|
||||
const youtubeService = require('../services/youtubeService');
|
||||
|
||||
module.exports = (io) => (socket) => {
|
||||
let currentRoomId = null;
|
||||
@ -147,7 +148,7 @@ module.exports = (io) => (socket) => {
|
||||
currentRoomId = roomId;
|
||||
});
|
||||
|
||||
socket.on("start-game", () => {
|
||||
socket.on("start-game", async () => {
|
||||
const roomId = roomController.getUserRoom(socket.id);
|
||||
if (!roomId || !roomController.isUserHost(socket.id)) {
|
||||
return socket.emit("not-authorized");
|
||||
@ -162,10 +163,19 @@ module.exports = (io) => (socket) => {
|
||||
|
||||
if (!roomController.startGame(roomId)) return;
|
||||
gameController.initializeGameState(roomId);
|
||||
if (!gameController.startNewRound(roomId)) return;
|
||||
|
||||
io.to(roomId).emit("game-started");
|
||||
handleRoundStart(roomId);
|
||||
try {
|
||||
const success = await gameController.startNewRound(roomId);
|
||||
if (!success) {
|
||||
return socket.emit("error", { message: "Failed to start game - could not load songs" });
|
||||
}
|
||||
|
||||
io.to(roomId).emit("game-started");
|
||||
handleRoundStart(roomId);
|
||||
} catch (error) {
|
||||
console.error("Error starting game:", error);
|
||||
socket.emit("error", { message: "Failed to start game due to an error" });
|
||||
}
|
||||
});
|
||||
|
||||
socket.on("send-message", (messageData) => {
|
||||
@ -233,7 +243,7 @@ module.exports = (io) => (socket) => {
|
||||
}
|
||||
});
|
||||
|
||||
socket.on("next-round", () => {
|
||||
socket.on("next-round", async () => {
|
||||
const roomId = roomController.getUserRoom(socket.id);
|
||||
if (!roomId || !roomController.isUserHost(socket.id)) return;
|
||||
|
||||
@ -244,8 +254,16 @@ module.exports = (io) => (socket) => {
|
||||
return socket.emit("error", { message: "At least 2 players are required" });
|
||||
}
|
||||
|
||||
if (gameController.startNewRound(roomId)) {
|
||||
handleRoundStart(roomId);
|
||||
try {
|
||||
const success = await gameController.startNewRound(roomId);
|
||||
if (success) {
|
||||
handleRoundStart(roomId);
|
||||
} else {
|
||||
socket.emit("error", { message: "Failed to start next round" });
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Error starting next round:", error);
|
||||
socket.emit("error", { message: "Failed to start next round due to an error" });
|
||||
}
|
||||
});
|
||||
|
||||
@ -257,4 +275,14 @@ module.exports = (io) => (socket) => {
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
socket.on("get-playlist-songs", async () => {
|
||||
try {
|
||||
const songs = await youtubeService.fetchPlaylistSongs();
|
||||
socket.emit("playlist-songs", { songs });
|
||||
} catch (error) {
|
||||
console.error("Error sending playlist songs:", error);
|
||||
socket.emit("playlist-songs", { songs: youtubeService.getDefaultSongs() });
|
||||
}
|
||||
});
|
||||
};
|
Reference in New Issue
Block a user