feat(server)!: websocket lobby, server-run matches, server economy, new schema (P1-2, P1-4, P2-1, P2-1b, P2-5)

Replaces the Firebase-style /db facade (rtdb, rules, nodes tree) with explicit REST endpoints and a
WebSocket hub. Login by name or optional e-mail, seed accounts via npm run db:reset.

BREAKING CHANGE: old clients (lobby/netfight over /db) no longer work against this server; database is new.
This commit is contained in:
Benjamin Oezcan
2026-11-19 01:52:30 +01:00
parent 46499e354b
commit dca1032ac4
20 changed files with 908 additions and 322 deletions

View File

@@ -47,6 +47,14 @@ class Room {
if (raw && Number.isInteger(raw.seq)) this.acks[seat] = raw.seq;
}
disconnect(seat) { if (!this.away[seat]) this.away[seat] = 1; }
// player gives up: his robot is knocked out at once
forfeit(seat) {
const s = this.s, r = s.robots[seat];
if (this.ended || !r || s.result) return;
r.hp = 0; r.dead = true;
s.result = { winner: 1 - seat, reason: 'ko' }; s.endIn = 0;
if (this.tickN <= COUNTDOWN) this.tickN = COUNTDOWN; // end now even during the countdown
}
reconnect(seat) { this.away[seat] = 0; this.ai[seat] = null; }
connected(seat) { return !this.away[seat]; }

41
server/match/runner.js Normal file
View File

@@ -0,0 +1,41 @@
/* Drives all live rooms at a fixed 60 Hz. One timer for the whole server; catches up after a hitch
(max 10 ticks per wake-up), logs when a tick takes too long. */
'use strict';
const { Room, DT } = require('./room');
function createRunner(opt = {}) {
const rooms = new Map();
const STEP = DT * 1000;
let next = 0, timer = null, slowLogged = 0;
function loop() {
const now = performance.now();
if (!next) next = now;
let n = 0;
while (now >= next && n < 10) {
const t0 = performance.now();
for (const room of rooms.values()) room.tick();
const ms = performance.now() - t0;
if (ms > 8 && Date.now() - slowLogged > 10000) { slowLogged = Date.now(); console.warn(`[match] slow tick ${ms.toFixed(1)} ms for ${rooms.size} rooms`); }
next += STEP; n++;
}
if (now - next > 250) next = now; // fell far behind (sleeping laptop, debugger): don't replay it
timer = setTimeout(loop, Math.max(0, next - performance.now()));
}
return {
rooms,
create(o) {
const room = new Room(Object.assign({}, o, {
onEnd: r => { rooms.delete(r.id); if (o.onEnd) o.onEnd(r); },
}));
rooms.set(room.id, room);
if (!timer && opt.autoStart !== false) loop();
return room;
},
get: id => rooms.get(id),
stop() { clearTimeout(timer); timer = null; next = 0; },
};
}
module.exports = { createRunner };