unibutton/index.js

68 lines
1.6 KiB
JavaScript
Raw Normal View History

2024-09-29 02:05:42 -04:00
import express from "express";
import expressW from "express-ws";
2024-09-29 03:55:22 -04:00
import Game from "./game.js";
2024-10-02 04:14:56 -04:00
import NPC from "./common/npc.js";
2024-09-29 03:55:22 -04:00
import Player from "./common/player.js";
2024-09-29 02:05:42 -04:00
var app = express();
expressW(app);
2024-09-29 03:55:22 -04:00
var game = new Game();
2024-09-29 03:55:22 -04:00
game.init();
2024-09-29 04:09:50 -04:00
app.use('/crypto.js', express.static('./common/crypto.js'));
2024-09-29 03:55:22 -04:00
app.use('/js', express.static('./common'));
2024-09-29 02:05:42 -04:00
app.use(express.static('./static'));
app.ws('/', function (ws, req) {
2024-09-29 03:55:22 -04:00
game.ws.push(ws);
2024-10-03 05:16:40 -04:00
let player = new Player(false, true, game);
2024-09-29 03:55:22 -04:00
let playerI = game.entities.length;
game.entities[playerI] = player;
ws.active = true;
2024-10-02 04:14:56 -04:00
ws.ent = player;
2024-09-30 01:59:06 -04:00
// This will only work under NGINX.
2024-10-06 07:14:05 -04:00
console.log(`Player ${player.you} joined under IP ${req.headers["x-real-ip"]}`)
2024-09-30 01:59:06 -04:00
2024-09-29 03:55:22 -04:00
ws.on('message', function message(msg) {
let data = {};
try {
data = JSON.parse(msg);
} catch (err) {
console.log(err);
2024-10-02 04:14:56 -04:00
data = [];
2024-09-29 03:55:22 -04:00
}
2024-09-30 01:59:06 -04:00
2024-10-02 04:14:56 -04:00
let newEnt = game.entities[playerI];
2024-09-30 01:59:06 -04:00
2024-10-02 04:14:56 -04:00
let {you} = newEnt;
let props = player.legalProps;
for (let i in props) {
newEnt[props[i]] = data[i];
2024-09-30 01:59:06 -04:00
}
2024-10-02 04:14:56 -04:00
let you2 = newEnt.you;
if (you != you2) {
2024-10-06 07:14:05 -04:00
console.log(`Player ${you} now identifies as ${you2}`);
}
if (newEnt.ref) {
console.log(`Player ${you2} discovered this game from ${newEnt.ref}`);
newEnt.ref = undefined;
2024-10-02 04:14:56 -04:00
}
2024-09-29 03:55:22 -04:00
})
ws.on('close', function () {
2024-10-06 07:14:05 -04:00
console.log(`Player ${game.entities[playerI].you} left`);
2024-09-29 03:55:22 -04:00
ws.active = false;
player.health = -1;
})
2024-09-29 02:05:42 -04:00
});
app.listen(process.env.PORT || 3069);