Implement waiting room
This commit is contained in:
@ -2,11 +2,18 @@ let rooms = {};
|
||||
|
||||
module.exports.roomExists = (roomId) => rooms[roomId] !== undefined;
|
||||
|
||||
module.exports.isRoomOpen = (roomId) => rooms[roomId] && rooms[roomId].state === 'waiting';
|
||||
|
||||
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: {}};
|
||||
rooms[roomId] = {
|
||||
members: [{...user, creator: true}],
|
||||
settings: {},
|
||||
state: 'waiting'
|
||||
};
|
||||
}
|
||||
console.log(`User ${user.name} connected to room ${roomId}`);
|
||||
}
|
||||
@ -35,6 +42,33 @@ module.exports.getRoomCreator = (roomId) => {
|
||||
return null;
|
||||
}
|
||||
|
||||
module.exports.isUserHost = (userId) => {
|
||||
for (const roomId in rooms) {
|
||||
const room = rooms[roomId];
|
||||
const member = room.members.find(m => m.id === userId);
|
||||
if (member && member.creator) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
module.exports.startGame = (roomId) => {
|
||||
if (rooms[roomId]) {
|
||||
rooms[roomId].state = 'playing';
|
||||
console.log(`Game started in room ${roomId}`);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
module.exports.getRoomState = (roomId) => {
|
||||
if (rooms[roomId]) {
|
||||
return rooms[roomId].state;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
module.exports.disconnectUser = (userId) => {
|
||||
for (const roomId in rooms) {
|
||||
const room = rooms[roomId];
|
||||
|
Reference in New Issue
Block a user