diff --git a/.eslintrc.js b/.eslintrc.js index 58af6b7..86d29a2 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -29,6 +29,7 @@ module.exports = { "semi": [ "error", "always" - ] + ], + "no-mixed-spaces-and-tabs": "off" } -}; \ No newline at end of file +}; diff --git a/src/index.js b/src/index.js index 14cdc50..6af3933 100644 --- a/src/index.js +++ b/src/index.js @@ -31,23 +31,27 @@ app.get("/", (req, res) => { io.on("connection", (socket) => { // Handle disconnection socket.on("disconnect", () => { - const room = players[socket.id].room; - delete players[socket.id]; + + if (players[socket.id]) { + const room = players[socket.id].room; - var playerUpdate = {}; - if (io.sockets.adapter.rooms[room]) { - for (const id in io.sockets.adapter.rooms[room].sockets) { - playerUpdate[id] = players[id]; - } - } + var playerUpdate = {}; + if (io.sockets.adapter.rooms[room]) { + for (const id in io.sockets.adapter.rooms[room].sockets) { + 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.sockets.adapter.rooms[room] == undefined) { - console.log(traffic.int + colours.room(`Room ${room} has been deleted due to a lack of players`)); + if (io.sockets.adapter.rooms[room] == undefined) { + console.log(traffic.int + colours.room(`Room ${room} has been deleted due to a lack of players`)); + } } + + delete players[socket.id]; }); // Request identification @@ -125,4 +129,4 @@ io.on("connection", (socket) => { http.listen(port, () => { console.log(colours.data(`\nServer ready, listening on port ${port}\n`)); -}); \ No newline at end of file +});