Updated TanksJS-Server service
This commit is contained in:
parent
7a794a3995
commit
8f4545ed27
@ -15,6 +15,7 @@ const traffic = {
|
||||
};
|
||||
|
||||
var players = {};
|
||||
var rooms = {};
|
||||
|
||||
function handle(io, socket) {
|
||||
// Handle disconnection
|
||||
@ -23,18 +24,19 @@ function handle(io, socket) {
|
||||
const room = players[socket.id].room;
|
||||
|
||||
var playerUpdate = {};
|
||||
if (io.adapter.rooms[room]) {
|
||||
for (const id in io.adapter.rooms[room].sockets) {
|
||||
if (io.sockets.adapter.rooms.get(room)) {
|
||||
io.sockets.adapter.rooms.get(room).forEach((id) => {
|
||||
playerUpdate[id] = players[id];
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
io.in(room).emit("roomUpdate", playerUpdate);
|
||||
|
||||
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`));
|
||||
delete rooms[room];
|
||||
}
|
||||
}
|
||||
|
||||
@ -55,13 +57,17 @@ function handle(io, socket) {
|
||||
var join = "";
|
||||
|
||||
// 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)) {
|
||||
var roomId = room.split("_");
|
||||
if (roomId[0] == data.gameID) {
|
||||
if (roomId[1] == data.playersMax) {
|
||||
if (io.adapter.rooms.get(room).size < data.playersMax) {
|
||||
join += room;
|
||||
if (io.sockets.adapter.rooms.get(room).size < data.playersMax) {
|
||||
if (!rooms[room].started) {
|
||||
join += room;
|
||||
} else {
|
||||
console.log(traffic.int + colours.room(`Room ${room} has already started, skipping.`));
|
||||
}
|
||||
} else {
|
||||
console.log(traffic.int + colours.room(`Room ${room} is full, skipping.`));
|
||||
}
|
||||
@ -74,22 +80,28 @@ function handle(io, socket) {
|
||||
if (join == "") {
|
||||
var count = 0;
|
||||
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)) {
|
||||
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}`));
|
||||
}
|
||||
|
||||
socket.join(join);
|
||||
|
||||
// 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`));
|
||||
io.in(join).emit("gameStart");
|
||||
rooms[join].started = true;
|
||||
}
|
||||
|
||||
players[socket.id] = {
|
||||
@ -98,7 +110,7 @@ function handle(io, socket) {
|
||||
};
|
||||
|
||||
var playerUpdate = {};
|
||||
io.adapter.rooms.get(join).forEach((id) => {
|
||||
io.sockets.adapter.rooms.get(join).forEach((id) => {
|
||||
playerUpdate[id] = players[id];
|
||||
});
|
||||
|
||||
@ -117,7 +129,7 @@ function handle(io, socket) {
|
||||
});
|
||||
|
||||
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}`));
|
||||
socket.join(data);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user