Updated TanksJS-Server service

This commit is contained in:
Arne van Iterson 2020-11-27 19:58:23 +01:00
parent 7a794a3995
commit 8f4545ed27

View File

@ -15,6 +15,7 @@ const traffic = {
}; };
var players = {}; var players = {};
var rooms = {};
function handle(io, socket) { function handle(io, socket) {
// Handle disconnection // Handle disconnection
@ -23,18 +24,19 @@ function handle(io, socket) {
const room = players[socket.id].room; const room = players[socket.id].room;
var playerUpdate = {}; var playerUpdate = {};
if (io.adapter.rooms[room]) { if (io.sockets.adapter.rooms.get(room)) {
for (const id in io.adapter.rooms[room].sockets) { io.sockets.adapter.rooms.get(room).forEach((id) => {
playerUpdate[id] = players[id]; playerUpdate[id] = players[id];
} });
} }
io.in(room).emit("roomUpdate", playerUpdate); io.in(room).emit("roomUpdate", playerUpdate);
console.log(traffic.out + colours.disconnect(`User ${String(socket.id)} disconnected from room ${room}`)); console.log(traffic.out + colours.disconnect(`User ${String(socket.id)} disconnected from room ${room}`));
if (io.adapter.rooms.get(room) == undefined) { if (io.sockets.adapter.rooms.get(room) == undefined) {
console.log(traffic.int + colours.room(`Room ${room} has been deleted due to a lack of players`)); console.log(traffic.int + colours.room(`Room ${room} has been deleted due to a lack of players`));
delete rooms[room];
} }
} }
@ -55,13 +57,17 @@ function handle(io, socket) {
var join = ""; var join = "";
// Check if there are any available rooms for the requested game and check if they aren't full // Check if there are any available rooms for the requested game and check if they aren't full
io.adapter.rooms.forEach((set, room) => { io.sockets.adapter.rooms.forEach((set, room) => {
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.playersMax) { if (roomId[1] == data.playersMax) {
if (io.adapter.rooms.get(room).size < data.playersMax) { if (io.sockets.adapter.rooms.get(room).size < data.playersMax) {
join += room; if (!rooms[room].started) {
join += room;
} else {
console.log(traffic.int + colours.room(`Room ${room} has already started, skipping.`));
}
} else { } else {
console.log(traffic.int + colours.room(`Room ${room} is full, skipping.`)); console.log(traffic.int + colours.room(`Room ${room} is full, skipping.`));
} }
@ -74,22 +80,28 @@ function handle(io, socket) {
if (join == "") { if (join == "") {
var count = 0; var count = 0;
var regex = new RegExp(data.gameID + "_" + data.playersMax + "_[0-9].*"); var regex = new RegExp(data.gameID + "_" + data.playersMax + "_[0-9].*");
io.adapter.rooms.forEach((set, room) => { io.sockets.adapter.rooms.forEach((set, room) => {
if (regex.test(room)) { if (regex.test(room)) {
count++; count++;
} }
}); });
join = data.gameID + "_" + data.playersMax + "_" + count; join = data.gameID + "_" + data.playersMax + "_" + count;
rooms[join] = {
started: false
};
console.log(traffic.int + colours.room(`There is no room available for the requested game, making room ${join}`)); console.log(traffic.int + colours.room(`There is no room available for the requested game, making room ${join}`));
} }
socket.join(join); socket.join(join);
// Start game if the max number of players is reached // Start game if the max number of players is reached
if (io.adapter.rooms.get(join).size == data.playersMax) { if (io.sockets.adapter.rooms.get(join).size == data.playersMax) {
console.log(traffic.int + colours.room(`Room ${join} has reached the maximum amount of players, starting game`)); console.log(traffic.int + colours.room(`Room ${join} has reached the maximum amount of players, starting game`));
io.in(join).emit("gameStart"); io.in(join).emit("gameStart");
rooms[join].started = true;
} }
players[socket.id] = { players[socket.id] = {
@ -98,7 +110,7 @@ function handle(io, socket) {
}; };
var playerUpdate = {}; var playerUpdate = {};
io.adapter.rooms.get(join).forEach((id) => { io.sockets.adapter.rooms.get(join).forEach((id) => {
playerUpdate[id] = players[id]; playerUpdate[id] = players[id];
}); });
@ -117,7 +129,7 @@ function handle(io, socket) {
}); });
socket.on("spectate", (data) => { socket.on("spectate", (data) => {
if (io.adapter.rooms.has(data)) { if (io.sockets.adapter.rooms.has(data)) {
console.log(traffic.in + colours.connect(`User ${String(socket.id)} is spectating room ${data}`)); console.log(traffic.in + colours.connect(`User ${String(socket.id)} is spectating room ${data}`));
socket.join(data); socket.join(data);
} }