unibutton/index.js

95 lines
2.5 KiB
JavaScript
Raw Normal View History

2024-11-25 14:14:20 -05:00
import express from "express";
import expressW from "express-ws";
2024-11-25 14:14:20 -05:00
import Game from "./game.js";
2024-11-25 14:14:20 -05:00
import NPC from "./common/npc.js";
2024-11-25 14:14:20 -05:00
import Player from "./common/player.js";
2024-11-25 14:14:20 -05:00
2024-11-25 14:14:21 -05:00
import initDb from "./db.js";
let db = await initDb();
2024-11-25 14:14:20 -05:00
var app = express();
expressW(app);
2024-11-25 14:14:20 -05:00
var game = new Game();
game.init();
2024-11-25 14:14:20 -05:00
app.use('/crypto.js', express.static('./common/crypto.js'));
2024-11-25 14:14:20 -05:00
app.use('/js', express.static('./common'));
2024-11-25 14:14:20 -05:00
app.use(express.static('./static'));
2024-11-25 14:14:21 -05:00
app.get('/leaderboard', async function (ws, req) {
2024-11-25 14:14:21 -05:00
let scores = game.scores || [];
2024-11-25 14:14:21 -05:00
req.send(scores.map((x,i) => `#${i+1}: ${x[0]} [${x[1]}: ${x[2]}]`).join('\n'));
2024-11-25 14:14:21 -05:00
})
2024-11-25 14:14:20 -05:00
app.ws('/', function (ws, req) {
2024-11-25 14:14:20 -05:00
game.ws.push(ws);
2024-11-25 14:14:20 -05:00
let player = new Player(false, true, game);
2024-11-25 14:14:20 -05:00
let playerI = game.entities.length;
game.entities[playerI] = player;
ws.active = true;
2024-11-25 14:14:20 -05:00
ws.ent = player;
2024-11-25 14:14:21 -05:00
ws.ip = req.headers["x-real-ip"];
2024-11-25 14:14:20 -05:00
2024-11-25 14:14:20 -05:00
// This will only work under NGINX.
2024-11-25 14:14:21 -05:00
console.log(`Player ${player.you} joined under IP ${ws.ip}`)
2024-11-25 14:14:20 -05:00
2024-11-25 14:14:20 -05:00
ws.on('message', function message(msg) {
let data = {};
try {
data = JSON.parse(msg);
} catch (err) {
console.log(err);
2024-11-25 14:14:20 -05:00
data = [];
2024-11-25 14:14:20 -05:00
}
2024-11-25 14:14:20 -05:00
2024-11-25 14:14:21 -05:00
let type = data[0];
if (type == 'AUTH') {
2024-11-25 14:14:21 -05:00
game.authUser(ws,data[1]);
2024-11-25 14:14:21 -05:00
return;
}
data.splice(0, 1);
2024-11-25 14:14:20 -05:00
let newEnt = game.entities[playerI];
2024-11-25 14:14:20 -05:00
2024-11-25 14:14:20 -05:00
let { you } = newEnt;
2024-11-25 14:14:20 -05:00
let props = player.legalProps;
for (let i in props) {
2024-11-25 14:14:20 -05: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-11-25 14:14:20 -05:00
newEnt[props[i]] = data[i];
2024-11-25 14:14:20 -05:00
}
2024-11-25 14:14:20 -05:00
let you2 = newEnt.you;
if (you != you2) {
2024-11-25 14:14:20 -05:00
console.log(`Player ${you} now identifies as ${you2}`);
}
2024-11-25 14:14:20 -05:00
2024-11-25 14:14:20 -05:00
if (newEnt.ref) {
console.log(`Player ${you2} discovered this game from ${newEnt.ref}`);
newEnt.ref = undefined;
2024-11-25 14:14:20 -05:00
}
2024-11-25 14:14:20 -05:00
})
ws.on('close', function () {
2024-11-25 14:14:20 -05:00
console.log(`Player ${game.entities[playerI].you} left`);
2024-11-25 14:14:20 -05:00
ws.active = false;
player.health = -1;
})
2024-11-25 14:14:20 -05:00
});
app.listen(process.env.PORT || 3069);