diff --git a/package-lock.json b/package-lock.json index 8892d76..7bfa0c7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -498,6 +498,11 @@ "integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=", "dev": true }, + "colors": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/colors/-/colors-1.4.0.tgz", + "integrity": "sha512-a+UqTh4kgZg/SlGvfbzDHpgRu7AAQOmmqRHJnxhRZICKFUT91brVhNNt58CMWU9PsBbv3PDCZUHbVxuDiH2mtA==" + }, "component-bind": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/component-bind/-/component-bind-1.0.0.tgz", diff --git a/package.json b/package.json index 03fb8f0..89ab55f 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ }, "license": "ISC", "dependencies": { + "colors": "^1.4.0", "express": "^4.17.1", "socket.io": "^2.3.0" }, diff --git a/src/index.js b/src/index.js index cd536f2..caadcc2 100644 --- a/src/index.js +++ b/src/index.js @@ -2,6 +2,21 @@ var app = require("express")(); var http = require("http").createServer(app); var io = require("socket.io")(http); +var colours = require("colors"); +colours.enable(); +colours.setTheme({ + silly: "rainbow", + input: "grey", + verbose: "cyan", + prompt: "grey", + info: "green", + data: "grey", + help: "cyan", + warn: "yellow", + debug: "blue", + error: "red" +}); + var path = require("path"); // Send html & css files @@ -13,15 +28,25 @@ app.get("/index.css", (req, res) => { res.sendFile(path.join(__dirname + "/../css/index.css")); }); -var players = []; +var players = {}; // Handle Connection io.on("connection", (socket) => { // Handle disconnection socket.on("disconnect", () => { - delete players[String(socket.id)]; - console.warn(players); - console.log(`User ${socket.id} disconnected!`); + const room = players[socket.id].room; + delete players[socket.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); + + console.log(colours.warn(`User ${String(socket.id)} disconnected from room ${room}`)); }); // Request identification @@ -41,21 +66,26 @@ io.on("connection", (socket) => { socket.join(room); - players[String(socket.id)] = { + players[socket.id] = { username: data.name, + controls: data.controls, room: room }; - console.warn(players); - io.in(room).emit("roomUpdate", players); + var playerUpdate = {}; + for (const id in io.sockets.adapter.rooms[room].sockets) { + playerUpdate[id] = players[id]; + } - console.log(`User ${socket.id} connected to room ${room}`); + io.in(room).emit("roomUpdate", playerUpdate); + + console.log(colours.help(`User ${String(socket.id)} connected to room ${room}`)); }); socket.on("update", (data) => { - if (players[String(socket.id)]) { - socket.to(players[String(socket.id)].room).emit("update", { - uuid: String(socket.id), + if (players[socket.id]) { + socket.to(players[socket.id].room).emit("update", { + uuid: socket.id, controls: data }); } @@ -63,5 +93,5 @@ io.on("connection", (socket) => { }); http.listen(3000, () => { - console.log("listening on *:3000"); + console.log(colours.data("\nTanksJS-Server ready, listening on port 3000\n")); }); \ No newline at end of file