import express from "express"; import expressW from "express-ws"; import Game from "./game.js"; import Player from "./common/player.js"; var app = express(); expressW(app); var game = new Game(); game.init(); app.use('/crypto.js', express.static('./common/crypto.js')); app.use('/js', express.static('./common')); app.use(express.static('./static')); app.ws('/', function (ws, req) { game.ws.push(ws); let player = new Player(); let playerI = game.entities.length; game.entities[playerI] = player; ws.active = true; console.log(`A player ${player.you} joined under IP ${req.headers["x-real-ip"]}`) // This will only work under NGINX. ws.on('message', function message(msg) { let data = {}; try { data = JSON.parse(msg); } catch (err) { console.log(err); data = {}; } let { vel, dir, you, ticks } = data; let data2 = { vel, dir, you, ticks }; let you2 = game.entities[playerI].you; if (you != you2) { console.log(`A player ${you2} now identifies as ${you}`); } game.entities[playerI] = Object.assign(game.entities[playerI], data2); }) ws.on('close', function () { console.log(`A player ${game.entities[playerI].you} left`); ws.active = false; player.health = -1; }) }); app.listen(process.env.PORT || 3069);