unibutton/index.js

55 lines
1.4 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";
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);
let player = new Player();
let playerI = game.entities.length;
game.entities[playerI] = player;
ws.active = true;
2024-09-30 01:59:06 -04:00
// This will only work under NGINX.
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);
data = {};
}
let { vel, dir, you, ticks, isMenu, r, playing } = data;
let data2 = { vel, dir, you, ticks, isMenu, r, playing };
2024-09-30 01:59:06 -04:00
let you2 = game.entities[playerI].you;
if (you != you2) {
console.log(`A player ${you2} now identifies as ${you}`);
}
2024-09-29 03:55:22 -04:00
game.entities[playerI] = Object.assign(game.entities[playerI], data2);
})
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);