Changed gamemode to playersMax

Gamemodes will have to be defined on the clients side
This is to help make the server support different games
This commit is contained in:
Arne van Iterson 2020-04-28 13:24:40 +02:00
parent c31d9de3af
commit b351b281bd
1 changed files with 13 additions and 11 deletions

View File

@ -54,23 +54,19 @@ io.on("connection", (socket) => {
// Wait for identification
socket.on("identification", (data) => {
console.log(traffic.in + colours.info(`User ${String(socket.id)} playing ${data.gameID} trying to join with gamemode ${data.gamemode}`));
console.log(traffic.in + colours.info(`User ${String(socket.id)} playing ${data.gameID} is trying to join a room with ${data.playersMax} players max`));
// Define room to be joined
var join = "";
// Check if there are any available rooms for the requested gamemode
// Check if there are any available rooms for the requested game and check if they aren't full
for (const room in io.sockets.adapter.rooms) {
if (/[0-9]_[0-9].*/i.test(room)) {
var roomId = room.split("_");
if (roomId[0] == data.gameID) {
if (roomId[1] == data.gamemode) {
if (Object.keys(io.sockets.adapter.rooms[room].sockets).length <= data.gamemode) {
if (roomId[1] == data.playersMax) {
if (Object.keys(io.sockets.adapter.rooms[room].sockets).length < data.playersMax) {
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.`));
}
@ -82,18 +78,24 @@ io.on("connection", (socket) => {
// If no available room is found, make one
if (join == "") {
var count = 0;
var regex = new RegExp(data.gameID + "_" + data.gamemode + "_[0-9].*");
var regex = new RegExp(data.gameID + "_" + data.playersMax + "_[0-9].*");
Object.keys(io.sockets.adapter.rooms).forEach(room => {
if (regex.test(room)) {
count++;
}
});
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}`));
join = data.gameID + "_" + data.playersMax + "_" + count;
console.log(traffic.int + colours.warn(`There is no room available for the requested game, making ${join}`));
}
socket.join(join);
// Start game if the max number of players is reached
if (Object.keys(io.sockets.adapter.rooms[join].sockets).length == data.playersMax) {
console.log(traffic.int + colours.warn(`Room ${join} has reached the maximum amount of players, starting game`));
io.in(join).emit("gameStart");
}
players[socket.id] = {
username: data.name,