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 02:27:44 -04:00
|
|
|
|
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);
|
|
|
|
let player = new Player();
|
|
|
|
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-01 04:03:55 -04:00
|
|
|
console.log(`A 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) {
|
|
|
|
console.log(`A player ${you} now identifies as ${you2}`);
|
|
|
|
}
|
2024-09-29 03:55:22 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
ws.on('close', function () {
|
2024-09-30 02:00:28 -04:00
|
|
|
console.log(`A 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);
|