unibutton/index.js
2024-10-03 05:16:40 -04:00

63 lines
No EOL
1.5 KiB
JavaScript

import express from "express";
import expressW from "express-ws";
import Game from "./game.js";
import NPC from "./common/npc.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(false, true, game);
let playerI = game.entities.length;
game.entities[playerI] = player;
ws.active = true;
ws.ent = player;
// 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 newEnt = game.entities[playerI];
let {you} = newEnt;
let props = player.legalProps;
for (let i in props) {
newEnt[props[i]] = data[i];
}
let you2 = newEnt.you;
if (you != you2) {
console.log(`A player ${you} now identifies as ${you2}`);
}
})
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);