Fix vote bug
This commit is contained in:
parent
9901c1a49e
commit
2ea81493ba
@ -76,14 +76,34 @@ export const WaitingRoom = () => {
|
||||
};
|
||||
|
||||
const handlePlaylistOptions = (options) => {
|
||||
setPlaylists(options);
|
||||
const playlistsWithVotes = { ...options };
|
||||
Object.keys(playlistsWithVotes).forEach(genre => {
|
||||
const playlistId = playlistsWithVotes[genre].id;
|
||||
playlistsWithVotes[genre].votes = votes[playlistId]?.length || 0;
|
||||
});
|
||||
setPlaylists(playlistsWithVotes);
|
||||
};
|
||||
|
||||
const handleVotesUpdated = (newVotes) => {
|
||||
console.log('Received updated votes:', newVotes);
|
||||
setVotes(newVotes);
|
||||
|
||||
setPlaylists(current => {
|
||||
const updated = { ...current };
|
||||
Object.keys(updated).forEach(genre => {
|
||||
const playlistId = updated[genre].id;
|
||||
updated[genre].votes = newVotes[playlistId]?.length || 0;
|
||||
});
|
||||
return updated;
|
||||
});
|
||||
|
||||
Object.entries(newVotes).forEach(([playlistId, voters]) => {
|
||||
if (voters.includes(socket.id)) {
|
||||
setSelectedPlaylist(playlistId);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// Register event listeners
|
||||
const cleanupHostStatus = on("host-status", handleHostStatus);
|
||||
const cleanupUserInfo = on("user-info", handleUserInfo);
|
||||
const cleanupRoomUsers = on("room-users", handleRoomUsers);
|
||||
|
@ -163,19 +163,20 @@ module.exports.voteForPlaylist = (roomId, userId, playlistId) => {
|
||||
const room = rooms[roomId];
|
||||
if (room.state !== 'waiting') return false;
|
||||
|
||||
const previousVote = Object.entries(room.playlistVotes)
|
||||
.find(([_, voters]) => voters.includes(userId));
|
||||
|
||||
if (previousVote) {
|
||||
room.playlistVotes[previousVote[0]] =
|
||||
room.playlistVotes[previousVote[0]].filter(id => id !== userId);
|
||||
if (!room.playlistVotes) {
|
||||
room.playlistVotes = {};
|
||||
}
|
||||
|
||||
Object.entries(room.playlistVotes).forEach(([pid, voters]) => {
|
||||
room.playlistVotes[pid] = voters.filter(id => id !== userId);
|
||||
});
|
||||
|
||||
if (!room.playlistVotes[playlistId]) {
|
||||
room.playlistVotes[playlistId] = [];
|
||||
}
|
||||
room.playlistVotes[playlistId].push(userId);
|
||||
|
||||
console.log(`Updated votes for room ${roomId}:`, room.playlistVotes);
|
||||
return true;
|
||||
};
|
||||
|
||||
|
@ -117,7 +117,7 @@ module.exports = (io) => (socket) => {
|
||||
}
|
||||
});
|
||||
|
||||
socket.on("join-room", ({roomId, name}) => {
|
||||
socket.on("join-room", async ({roomId, name}) => {
|
||||
if (currentRoomId) return socket.emit("already-in-room", currentRoomId);
|
||||
|
||||
roomId = roomId.toString().toUpperCase();
|
||||
@ -128,14 +128,35 @@ module.exports = (io) => (socket) => {
|
||||
}
|
||||
|
||||
currentUser = {id: socket.id, name: name.toString()};
|
||||
roomController.connectUserToRoom(roomId, currentUser);
|
||||
await roomController.connectUserToRoom(roomId, currentUser);
|
||||
socket.join(roomId);
|
||||
|
||||
const users = roomController.getRoomUsers(roomId);
|
||||
io.to(roomId).emit("room-users-update", users);
|
||||
socket.to(roomId).emit("user-connected", currentUser);
|
||||
|
||||
const votes = roomController.getPlaylistVotes(roomId);
|
||||
const availablePlaylists = roomController.getAvailablePlaylists(roomId);
|
||||
|
||||
socket.emit("room-joined", roomId);
|
||||
socket.emit("room-users", users);
|
||||
socket.emit("playlist-votes-updated", votes);
|
||||
|
||||
try {
|
||||
const details = await youtubeService.getPlaylistDetails(availablePlaylists);
|
||||
Object.keys(details).forEach(genre => {
|
||||
const playlistId = details[genre].id;
|
||||
details[genre].votes = votes[playlistId]?.length || 0;
|
||||
});
|
||||
socket.emit("playlist-options", details);
|
||||
} catch (error) {
|
||||
console.error("Error sending playlist details to new user:", error);
|
||||
socket.emit("error", {
|
||||
message: "Failed to load playlists",
|
||||
details: error.message
|
||||
});
|
||||
}
|
||||
|
||||
socket.to(roomId).emit("user-connected", currentUser);
|
||||
io.to(roomId).emit("room-users-update", users);
|
||||
|
||||
currentRoomId = roomId;
|
||||
} else {
|
||||
socket.emit("room-not-found", roomId);
|
||||
|
Loading…
x
Reference in New Issue
Block a user