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:
41
server/match/runner.js
Normal file
41
server/match/runner.js
Normal 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 };
|
||||
Reference in New Issue
Block a user