Added variable gameID's

This commit is contained in:
Arne van Iterson 2020-04-28 12:19:29 +02:00
parent 0096d47138
commit c31d9de3af

View File

@ -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,9 +63,10 @@ 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 (roomId[0] == data.gameID) {
if (roomId[1] == data.gamemode) {
if (Object.keys(io.sockets.adapter.rooms[room].sockets).length <= data.gamemode) {
join = room;
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");
@ -76,19 +77,20 @@ 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);