Fixed disconnect bug and added room game status

Players will not be able to join a game that has already started
This commit is contained in:
Arne van Iterson 2020-11-27 19:08:20 +01:00
parent 9b4424c830
commit e4ae27ee11

View File

@ -21,6 +21,7 @@ const traffic = {
}; };
var players = {}; var players = {};
var rooms = {};
// Send players json object // Send players json object
app.get("/", (req, res) => { app.get("/", (req, res) => {
@ -35,10 +36,10 @@ io.on("connection", (socket) => {
const room = players[socket.id].room; const room = players[socket.id].room;
var playerUpdate = {}; var playerUpdate = {};
if (io.sockets.adapter.rooms[room]) { if (io.sockets.adapter.rooms.get(room)) {
for (const id in io.sockets.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);
@ -47,6 +48,7 @@ io.on("connection", (socket) => {
if (io.sockets.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];
} }
} }
@ -73,7 +75,11 @@ io.on("connection", (socket) => {
if (roomId[0] == data.gameID) { if (roomId[0] == data.gameID) {
if (roomId[1] == data.playersMax) { if (roomId[1] == data.playersMax) {
if (io.sockets.adapter.rooms.get(room).size < data.playersMax) { if (io.sockets.adapter.rooms.get(room).size < data.playersMax) {
if (!rooms[room].started) {
join += room; 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.`));
} }
@ -93,6 +99,11 @@ io.on("connection", (socket) => {
}); });
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}`));
} }
@ -102,6 +113,7 @@ io.on("connection", (socket) => {
if (io.sockets.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] = {
@ -109,8 +121,6 @@ io.on("connection", (socket) => {
room: join room: join
}; };
console.log(players);
var playerUpdate = {}; var playerUpdate = {};
io.sockets.adapter.rooms.get(join).forEach((id) => { io.sockets.adapter.rooms.get(join).forEach((id) => {
playerUpdate[id] = players[id]; playerUpdate[id] = players[id];