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];
|
||||
|
@ -1,6 +1,15 @@
|
||||
const {connectUserToRoom, roomExists, disconnectUser, getUserRoom, getRoomUsers} = require("../controller/room");
|
||||
const {
|
||||
connectUserToRoom,
|
||||
roomExists,
|
||||
disconnectUser,
|
||||
getUserRoom,
|
||||
getRoomUsers,
|
||||
isRoomOpen,
|
||||
startGame,
|
||||
isUserHost
|
||||
} = require("../controller/room");
|
||||
|
||||
module.exports = (socket) => {
|
||||
module.exports = (io) => (socket) => {
|
||||
let currentRoomId = null;
|
||||
let currentUser = null;
|
||||
|
||||
@ -13,22 +22,33 @@ module.exports = (socket) => {
|
||||
|
||||
socket.on("join-room", ({roomId, name}) => {
|
||||
if (currentRoomId) return socket.emit("already-in-room", currentRoomId);
|
||||
|
||||
roomId = roomId.toString().toUpperCase();
|
||||
|
||||
if (roomExists(roomId.toString())) {
|
||||
if (roomExists(roomId)) {
|
||||
if (!isRoomOpen(roomId)) {
|
||||
return socket.emit("room-closed", roomId);
|
||||
}
|
||||
|
||||
currentUser = {id: socket.id, name: name.toString()};
|
||||
connectUserToRoom(roomId, currentUser);
|
||||
socket.join(roomId);
|
||||
|
||||
const users = getRoomUsers(roomId);
|
||||
io.to(roomId).emit("room-users-update", users);
|
||||
|
||||
socket.to(roomId).emit("user-connected", currentUser);
|
||||
|
||||
socket.emit("room-joined", roomId);
|
||||
currentRoomId = roomId;
|
||||
} else {
|
||||
socket.emit("room-not-found", roomId.toString());
|
||||
socket.emit("room-not-found", roomId);
|
||||
}
|
||||
});
|
||||
|
||||
socket.on("create-room", ({name}) => {
|
||||
if (!name) return socket.emit("room-name-required");
|
||||
const roomId = Math.random().toString(36).substring(7);
|
||||
const roomId = Math.random().toString(36).substring(7).toUpperCase();
|
||||
currentUser = {id: socket.id, name: name?.toString()};
|
||||
connectUserToRoom(roomId, currentUser);
|
||||
socket.join(roomId);
|
||||
@ -36,6 +56,17 @@ module.exports = (socket) => {
|
||||
currentRoomId = roomId;
|
||||
});
|
||||
|
||||
socket.on("start-game", () => {
|
||||
const roomId = getUserRoom(socket.id);
|
||||
if (roomId && isUserHost(socket.id)) {
|
||||
if (startGame(roomId)) {
|
||||
io.to(roomId).emit("game-started");
|
||||
}
|
||||
} else {
|
||||
socket.emit("not-authorized");
|
||||
}
|
||||
});
|
||||
|
||||
socket.on("send-message", (messageData) => {
|
||||
const roomId = getUserRoom(socket.id);
|
||||
if (roomId) {
|
||||
@ -56,4 +87,15 @@ module.exports = (socket) => {
|
||||
socket.emit("room-users", users);
|
||||
}
|
||||
});
|
||||
|
||||
socket.on("check-host-status", () => {
|
||||
const isHost = isUserHost(socket.id);
|
||||
socket.emit("host-status", { isHost });
|
||||
});
|
||||
|
||||
socket.on("get-room-code", () => {
|
||||
if (currentRoomId) {
|
||||
socket.emit("room-code", currentRoomId);
|
||||
}
|
||||
});
|
||||
}
|
@ -13,7 +13,8 @@ app.disable("x-powered-by");
|
||||
const server = http.createServer(app);
|
||||
const io = new Server(server, {cors: {origin: "*"}});
|
||||
|
||||
io.on("connection", require("./handler/connection"));
|
||||
// Pass io to the connection handler
|
||||
io.on("connection", require("./handler/connection")(io));
|
||||
|
||||
server.listen(5287, () => {
|
||||
console.log("Server running on port 5287");
|
||||
|
Reference in New Issue
Block a user