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.
2026-11-19 01:52:30 +01:00
|
|
|
/* Test helpers: start the real server on a random port with an in-memory database; WebSocket test clients. */
|
2026-11-19 01:40:31 +01:00
|
|
|
'use strict';
|
|
|
|
|
process.env.NODE_ENV = 'test';
|
|
|
|
|
process.env.DB_FILE = ':memory:';
|
|
|
|
|
delete process.env.JWT_SECRET;
|
|
|
|
|
|
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.
2026-11-19 01:52:30 +01:00
|
|
|
const WebSocket = require('ws');
|
|
|
|
|
const srvMod = require('../app');
|
|
|
|
|
const RC = globalThis.RC;
|
2026-11-19 01:40:31 +01:00
|
|
|
|
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.
2026-11-19 01:52:30 +01:00
|
|
|
let n = 0;
|
2026-11-19 01:40:31 +01:00
|
|
|
async function startServer() {
|
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.
2026-11-19 01:52:30 +01:00
|
|
|
const srv = await srvMod.listen(0);
|
|
|
|
|
const port = srv.address().port;
|
|
|
|
|
const base = 'http://127.0.0.1:' + port;
|
2026-11-19 01:40:31 +01:00
|
|
|
const json = async (method, path, body, token) => {
|
|
|
|
|
const res = await fetch(base + path, {
|
|
|
|
|
method,
|
|
|
|
|
headers: Object.assign({ 'Content-Type': 'application/json' }, token ? { Authorization: 'Bearer ' + token } : {}),
|
|
|
|
|
body: body === undefined ? undefined : JSON.stringify(body),
|
|
|
|
|
});
|
|
|
|
|
return { status: res.status, body: await res.json().catch(() => null) };
|
|
|
|
|
};
|
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.
2026-11-19 01:52:30 +01:00
|
|
|
// a fresh registered account: { uid, idToken, ... }
|
|
|
|
|
const account = async (name) => {
|
|
|
|
|
const r = await json('POST', '/api/auth/register', { name: name || 'tester' + (++n), pass: 'secret123' });
|
|
|
|
|
if (r.status !== 200) throw new Error('register failed ' + JSON.stringify(r.body));
|
|
|
|
|
return r.body;
|
|
|
|
|
};
|
|
|
|
|
// connected, greeted WebSocket client with a message inbox
|
|
|
|
|
const client = (token, opt = {}) => new Promise((resolve, reject) => {
|
|
|
|
|
const ws = new WebSocket(`ws://127.0.0.1:${port}/ws`);
|
|
|
|
|
const inbox = [], waiters = [];
|
|
|
|
|
const c = {
|
|
|
|
|
ws, inbox,
|
|
|
|
|
send: m => ws.send(JSON.stringify(m)),
|
|
|
|
|
// resolves with the first (already received or future) message matching type + predicate
|
|
|
|
|
next(t, pred = () => true, ms = 3000) {
|
|
|
|
|
const i = inbox.findIndex(m => m.t === t && pred(m));
|
|
|
|
|
if (i >= 0) return Promise.resolve(inbox.splice(i, 1)[0]);
|
|
|
|
|
return new Promise((res, rej) => {
|
|
|
|
|
const w = { t, pred, res, timer: setTimeout(() => rej(new Error('timeout waiting for ' + t)), ms) };
|
|
|
|
|
waiters.push(w);
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
closed: new Promise(r => ws.on('close', (code, reason) => r({ code, reason: String(reason) }))),
|
|
|
|
|
close: () => ws.close(),
|
|
|
|
|
};
|
|
|
|
|
ws.on('message', d => {
|
|
|
|
|
const m = JSON.parse(d.toString());
|
|
|
|
|
const wi = waiters.findIndex(w => w.t === m.t && w.pred(m));
|
|
|
|
|
if (wi >= 0) { const w = waiters.splice(wi, 1)[0]; clearTimeout(w.timer); w.res(m); } else inbox.push(m);
|
|
|
|
|
});
|
|
|
|
|
ws.on('open', () => {
|
|
|
|
|
if (opt.noHello) return resolve(c);
|
|
|
|
|
c.send({ t: 'hello', v: opt.v !== undefined ? opt.v : RC.Proto.VERSION, token });
|
|
|
|
|
if (opt.expectFail) return resolve(c);
|
|
|
|
|
c.next('welcome').then(() => resolve(c), reject);
|
|
|
|
|
});
|
|
|
|
|
ws.on('error', reject);
|
|
|
|
|
});
|
|
|
|
|
const close = () => new Promise(r => {
|
|
|
|
|
srvMod.runner.stop();
|
|
|
|
|
for (const ws of srvMod.wss.clients) ws.terminate();
|
|
|
|
|
srv.closeAllConnections(); srv.close(() => r());
|
|
|
|
|
});
|
|
|
|
|
return { base, port, json, account, client, close, runner: srvMod.runner, lobby: srvMod.lobby };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// run every live room to its end without waiting for real time
|
|
|
|
|
function fastForward(runner, maxTicks = 60 * 300) {
|
|
|
|
|
runner.stop();
|
|
|
|
|
for (let i = 0; i < maxTicks && runner.rooms.size; i++) for (const r of [...runner.rooms.values()]) r.tick();
|
2026-11-19 01:40:31 +01:00
|
|
|
}
|
|
|
|
|
|
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.
2026-11-19 01:52:30 +01:00
|
|
|
module.exports = { startServer, fastForward };
|