Added variable gameID's
This commit is contained in:
parent
0096d47138
commit
c31d9de3af
14
src/index.js
14
src/index.js
@ -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,9 +63,10 @@ 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 (roomId[1] == data.gamemode) {
|
||||||
if (Object.keys(io.sockets.adapter.rooms[room].sockets).length <= 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
|
// Start game if the max number of players is reached
|
||||||
if (Object.keys(io.sockets.adapter.rooms[room].sockets).length == data.gamemode) {
|
if (Object.keys(io.sockets.adapter.rooms[room].sockets).length == data.gamemode) {
|
||||||
io.in(room).emit("gameStart");
|
io.in(room).emit("gameStart");
|
||||||
@ -76,19 +77,20 @@ 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);
|
||||||
|
Loading…
Reference in New Issue
Block a user