unibutton/index.js

95 lines
2.5 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
2024-10-08 20:48:38 -04:00
import initDb from "./db.js";
let db = await initDb();
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();
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'));
2024-10-19 16:03:38 -04:00
app.get('/leaderboard', async function (ws, req) {
2024-10-23 18:44:19 -04:00
let scores = game.scores || [];
2024-11-04 20:37:42 -05:00
req.send(scores.map((x,i) => `#${i+1}: ${x[0]} [${x[1]}: ${x[2]}]`).join('\n'));
2024-10-08 20:48:38 -04:00
})
2024-09-29 02:05:42 -04:00
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-10-08 20:48:38 -04:00
ws.ip = req.headers["x-real-ip"];
2024-10-02 04:14:56 -04:00
2024-09-30 01:59:06 -04:00
// This will only work under NGINX.
2024-10-08 20:48:38 -04:00
console.log(`Player ${player.you} joined under IP ${ws.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-19 16:03:38 -04:00
let type = data[0];
if (type == 'AUTH') {
2024-10-21 20:46:47 -04:00
game.authUser(ws,data[1]);
2024-10-19 16:03:38 -04:00
return;
}
data.splice(0, 1);
2024-10-02 04:14:56 -04:00
let newEnt = game.entities[playerI];
2024-09-30 01:59:06 -04:00
2024-10-06 09:47:52 -04:00
let { you } = newEnt;
2024-10-02 04:14:56 -04:00
let props = player.legalProps;
for (let i in props) {
2024-10-06 09:47:52 -04:00
let prop = data[i];
let typed = (typeof prop);
let keys = Object.keys(prop);
let isC = (typed === 'object' && keys[0] == 'x' && keys[1] == 'y' && keys.length == 2 && typeof prop.x == 'number' && typeof prop.y == 'number');
if (typed !== 'string' && typed !== 'number' && typed !== 'boolean' && typed !== 'undefined' && !isC) {
console.warn(`Player ${you} attempted to send an invalid packet ${props[i]}`)
continue;
}
2024-10-02 04:14:56 -04:00
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}`);
}
2024-10-06 09:47:52 -04:00
2024-10-06 07:14:05 -04:00
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);