diff --git a/src/index.js b/src/index.js index efa85fb..2540ba9 100644 --- a/src/index.js +++ b/src/index.js @@ -54,7 +54,7 @@ io.on("connection", (socket) => { // Wait for identification socket.on("identification", (data) => { - console.log(traffic.in + colours.info(`User ${String(socket.id)} trying to join with gamemode ${data.gamemode}`)); + console.log(traffic.in + colours.info(`User ${String(socket.id)} playing ${data.gameID} trying to join with gamemode ${data.gamemode}`)); // Define room to be joined var join = ""; @@ -63,15 +63,17 @@ io.on("connection", (socket) => { for (const room in io.sockets.adapter.rooms) { if (/[0-9]_[0-9].*/i.test(room)) { var roomId = room.split("_"); - if (roomId[0] == data.gamemode) { - if (Object.keys(io.sockets.adapter.rooms[room].sockets).length <= data.gamemode) { - join = room; - // Start game if the max number of players is reached - if (Object.keys(io.sockets.adapter.rooms[room].sockets).length == data.gamemode) { - io.in(room).emit("gameStart"); + if (roomId[0] == data.gameID) { + if (roomId[1] == data.gamemode) { + if (Object.keys(io.sockets.adapter.rooms[room].sockets).length <= data.gamemode) { + join += room; + // Start game if the max number of players is reached + if (Object.keys(io.sockets.adapter.rooms[room].sockets).length == data.gamemode) { + io.in(room).emit("gameStart"); + } + } else { + console.log(traffic.int + colours.warn(`Room ${room} is full, skipping.`)); } - } else { - console.log(traffic.int + colours.warn(`Room ${room} is full, skipping.`)); } } } @@ -80,15 +82,15 @@ io.on("connection", (socket) => { // If no available room is found, make one if (join == "") { var count = 0; - var regex = new RegExp(data.gamemode + "_[0-9].*"); + var regex = new RegExp(data.gameID + "_" + data.gamemode + "_[0-9].*"); Object.keys(io.sockets.adapter.rooms).forEach(room => { if (regex.test(room)) { count++; } }); - join = data.gamemode + "_" + count; - console.log(traffic.int + colours.warn(`There is no room available for the requested gamemode, making ${join}`)); + join = data.gameID + "_" + data.gamemode + "_" + count; + console.log(traffic.int + colours.warn(`There is no room available for the requested game or gamemode, making ${join}`)); } socket.join(join);