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 // Wait for identification
socket.on("identification", (data) => { 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 // Define room to be joined
var join = ""; var join = "";
@ -63,15 +63,17 @@ io.on("connection", (socket) => {
for (const room in io.sockets.adapter.rooms) { for (const room in io.sockets.adapter.rooms) {
if (/[0-9]_[0-9].*/i.test(room)) { if (/[0-9]_[0-9].*/i.test(room)) {
var roomId = room.split("_"); var roomId = room.split("_");
if (roomId[0] == data.gamemode) { if (roomId[0] == data.gameID) {
if (Object.keys(io.sockets.adapter.rooms[room].sockets).length <= data.gamemode) { if (roomId[1] == data.gamemode) {
join = room; if (Object.keys(io.sockets.adapter.rooms[room].sockets).length <= data.gamemode) {
// Start game if the max number of players is reached join += room;
if (Object.keys(io.sockets.adapter.rooms[room].sockets).length == data.gamemode) { // Start game if the max number of players is reached
io.in(room).emit("gameStart"); 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 no available room is found, make one
if (join == "") { if (join == "") {
var count = 0; 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 => { Object.keys(io.sockets.adapter.rooms).forEach(room => {
if (regex.test(room)) { if (regex.test(room)) {
count++; count++;
} }
}); });
join = data.gamemode + "_" + count; join = data.gameID + "_" + data.gamemode + "_" + count;
console.log(traffic.int + colours.warn(`There is no room available for the requested gamemode, making ${join}`)); console.log(traffic.int + colours.warn(`There is no room available for the requested game or gamemode, making ${join}`));
} }
socket.join(join); socket.join(join);