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

View File

@ -54,23 +54,19 @@ 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)} 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 // Define room to be joined
var join = ""; 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) { 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.gameID) { if (roomId[0] == data.gameID) {
if (roomId[1] == data.gamemode) { if (roomId[1] == data.playersMax) {
if (Object.keys(io.sockets.adapter.rooms[room].sockets).length <= data.gamemode) { if (Object.keys(io.sockets.adapter.rooms[room].sockets).length < data.playersMax) {
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");
}
} else { } else {
console.log(traffic.int + colours.warn(`Room ${room} is full, skipping.`)); 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 no available room is found, make one
if (join == "") { if (join == "") {
var count = 0; 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 => { Object.keys(io.sockets.adapter.rooms).forEach(room => {
if (regex.test(room)) { if (regex.test(room)) {
count++; count++;
} }
}); });
join = data.gameID + "_" + data.gamemode + "_" + count; join = data.gameID + "_" + data.playersMax + "_" + count;
console.log(traffic.int + colours.warn(`There is no room available for the requested game or gamemode, making ${join}`)); console.log(traffic.int + colours.warn(`There is no room available for the requested game, making ${join}`));
} }
socket.join(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] = { players[socket.id] = {
username: data.name, username: data.name,