Add custom playlists
This commit is contained in:
@ -10,16 +10,22 @@ module.exports.roomExists = (roomId) => rooms[roomId] !== undefined;
|
||||
|
||||
module.exports.isRoomOpen = (roomId) => rooms[roomId] && rooms[roomId].state === 'waiting';
|
||||
|
||||
const initializeRoom = (roomId, user) => {
|
||||
rooms[roomId] = {
|
||||
members: [{...user, creator: true}],
|
||||
settings: {},
|
||||
state: 'waiting',
|
||||
playlistVotes: {},
|
||||
selectedPlaylist: null
|
||||
};
|
||||
};
|
||||
|
||||
module.exports.connectUserToRoom = (roomId, user) => {
|
||||
roomId = roomId.toUpperCase();
|
||||
if (rooms[roomId]) {
|
||||
rooms[roomId].members.push({...user, creator: false});
|
||||
} else {
|
||||
rooms[roomId] = {
|
||||
members: [{...user, creator: true}],
|
||||
settings: {},
|
||||
state: 'waiting'
|
||||
};
|
||||
initializeRoom(roomId, user);
|
||||
}
|
||||
console.log(`User ${user.name} connected to room ${roomId}`);
|
||||
}
|
||||
@ -132,4 +138,32 @@ module.exports.getUserName = (userId) => {
|
||||
}
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
module.exports.voteForPlaylist = (roomId, userId, playlistId) => {
|
||||
if (!rooms[roomId]) return false;
|
||||
|
||||
const room = rooms[roomId];
|
||||
if (room.state !== 'waiting') return false;
|
||||
|
||||
// Remove previous vote if exists
|
||||
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);
|
||||
}
|
||||
|
||||
// Add new vote
|
||||
if (!room.playlistVotes[playlistId]) {
|
||||
room.playlistVotes[playlistId] = [];
|
||||
}
|
||||
room.playlistVotes[playlistId].push(userId);
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
module.exports.getPlaylistVotes = (roomId) => {
|
||||
return rooms[roomId]?.playlistVotes || {};
|
||||
};
|
Reference in New Issue
Block a user