unibutton/index.js

55 lines
No EOL
1.4 KiB
JavaScript

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;
// This will only work under NGINX.
console.log(`A player ${player.you} joined under IP ${req.headers["x-real-ip"]}`)
ws.on('message', function message(msg) {
let data = {};
try {
data = JSON.parse(msg);
} catch (err) {
console.log(err);
data = {};
}
let { vel, dir, you, ticks, isMenu, r, playing } = data;
let data2 = { vel, dir, you, ticks, isMenu, r, playing };
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);