Built: - Client: trade screen, world/end-game screen (landing sites, shipping, node build, final ranking), report sharing + alliance reports tab, alert describers for all notification types, error-code strings (en+nb) for every server CODE. - tools/bot.js (+ tools/bot/): 50 headless bots against the real server in-process with a fake clock; resource/unit conservation ledger, exit 1 on broken invariants. docs/BALANCE.md has method and every run behind a number. - Balance changes (evidence in docs/BALANCE.md): secondQueueCoreLevel 10 -> 3, TRANSPORT.capacityPerLink 500 -> 1000 (end game was shipping-limited in bot runs). - Server fixes: POST /reports/:id/share 500 without a pending reportCleanup; bounced link shipments were silent about cargo overflow. Tested: npm test 207/207 (new: bot determinism + invariants, client alerts/world/i18n over CODES, two regression tests that failed before their fix); openapi check up to date. Real-browser scratch runs (not in repo): WebSocket push shows an incoming-attack banner ~214 ms after launch, polling fallback ~52 s, 15 scripted flows, 71 screenshots. NOT tested: Playwright suite (e2e/ still empty), real devices/Safari, nginx proxy websockets, throttled networks, HTTP-level bot load. NOT claimed: balance. Bots are not humans; open balance items (loot waste, escrow refund clipping, no garrisons at landing sites, end game not scaling with world speed) are listed in docs/BALANCE.md. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Q2MJaQ5LXAHzGnTDgceT14
164 lines
8.4 KiB
JavaScript
164 lines
8.4 KiB
JavaScript
'use strict';
|
|
/*
|
|
* Conservation ledger for the bot harness.
|
|
*
|
|
* RESOURCES. Pools that hold Fiber/Hardware/Capital: POP stock (stored columns, settled values), open trade escrow, cargo of
|
|
* movements that are `out`, and landing-site stockpiles. The identity checked at every checkpoint:
|
|
*
|
|
* pools_now = pools_at_start + sources + production - sinks (to within rounding of reported losses)
|
|
*
|
|
* sources the start stock of every operator and of every founded POP, the incumbent Hardware bonus
|
|
* production EXACT: every `UPDATE towns SET fiber..., res_at ...` (state.settle) writes the lazily grown stock back; the tap
|
|
* on db.run reads the previous stored value and books the difference. A transaction that rolls back discards its
|
|
* bookings (db.tx is tapped too), so only committed settles count. Caps are already inside the stored numbers.
|
|
* sinks build/train/research costs (from the command results), Backbone node levels (from landing_sites), cargo that
|
|
* did not fit the Rack hall (`cargoLost` in notifications), trade escrow refunds clipped by the Rack hall cap
|
|
* (tap on the refund statement)
|
|
*
|
|
* Anything else is DRIFT: a leak or a duplication in the server (or a missing sink here). Drift is printed and fails the run.
|
|
*
|
|
* UNITS. units_alive = trained - lost, where alive = home + stationed + in `out` movements; trained = `trainDone` events that
|
|
* ran; lost = battle/scan/incumbent/landing reports, decommission notifications, 3 crews per founded POP, 1 M&A team per loyalty drop.
|
|
*/
|
|
const D = require('../../public/js/data.js');
|
|
const R = require('../../public/js/rules.js');
|
|
|
|
const RES = D.RESOURCES;
|
|
const zero = () => ({ fiber: 0, hardware: 0, capital: 0 });
|
|
const addRes = (a, b) => { for (const r of RES) a[r] += (b && b[r]) || 0; return a; };
|
|
|
|
const SETTLE_SQL = 'UPDATE towns SET fiber = ?, hardware = ?, capital = ?, battery = ?, footprint = ?, res_at = ?, loyalty = ?, loyalty_at = ? WHERE id = ?';
|
|
const REFUND_RE = /^UPDATE towns SET (fiber|hardware|capital) = \? WHERE id = \?$/;
|
|
const CLOSE_PREFIX = 'UPDATE trade_offers SET status = ?, closed_at = ? WHERE id = ?';
|
|
|
|
class Ledger {
|
|
constructor(db) {
|
|
this.db = db;
|
|
this.production = zero(); // exact, committed settles only
|
|
this.sinkCosts = zero(); // build / train / research (command results)
|
|
this.sinkLost = zero(); // cargoLost notifications
|
|
this.lostEvents = 0;
|
|
this.sinkClip = zero(); // trade refund clipped by the Rack hall cap
|
|
this.clipEvents = 0;
|
|
this.sourceStart = zero(); // operators' start stock
|
|
this.stack = [];
|
|
this.closing = null;
|
|
this.t = { settles: 0 };
|
|
this.installed = false;
|
|
}
|
|
|
|
frame() { return this.stack.length ? this.stack[this.stack.length - 1] : null; }
|
|
book(prodDelta, clip, clipN) {
|
|
const f = this.frame();
|
|
if (f) { addRes(f.prod, prodDelta); addRes(f.clip, clip); f.clipN += clipN; f.settles += 1; }
|
|
else { addRes(this.production, prodDelta); addRes(this.sinkClip, clip); this.clipEvents += clipN; this.t.settles += 1; }
|
|
}
|
|
|
|
install() {
|
|
if (this.installed) return;
|
|
this.installed = true;
|
|
const db = this.db, self = this;
|
|
const run = db.run.bind(db), tx = db.tx.bind(db);
|
|
db.run = function tappedRun(sql, ...p) {
|
|
if (sql === SETTLE_SQL) {
|
|
const prev = db.get('SELECT fiber, hardware, capital FROM towns WHERE id = ?', p[8]);
|
|
if (prev) self.book({ fiber: p[0] - prev.fiber, hardware: p[1] - prev.hardware, capital: p[2] - prev.capital }, zero(), 0);
|
|
} else if (sql === CLOSE_PREFIX) {
|
|
self.closing = p[2];
|
|
} else {
|
|
const m = REFUND_RE.exec(sql);
|
|
if (m && self.closing !== null) {
|
|
const prev = db.get('SELECT ' + m[1] + ' AS v FROM towns WHERE id = ?', p[1]);
|
|
const off = db.get('SELECT give_amount, give_res FROM trade_offers WHERE id = ?', self.closing);
|
|
if (prev && off && off.give_res === m[1]) {
|
|
const credit = p[0] - prev.v;
|
|
const clip = Math.max(0, off.give_amount - credit);
|
|
if (clip > 1e-9) { const c = zero(); c[m[1]] = clip; self.book(zero(), c, 1); }
|
|
}
|
|
self.closing = null;
|
|
}
|
|
}
|
|
return run(sql, ...p);
|
|
};
|
|
db.tx = function tappedTx(fn) {
|
|
const f = { prod: zero(), clip: zero(), clipN: 0, settles: 0 };
|
|
self.stack.push(f);
|
|
let out;
|
|
try { out = tx(fn); } catch (e) { self.stack.pop(); throw e; }
|
|
self.stack.pop();
|
|
const parent = self.frame();
|
|
if (parent) { addRes(parent.prod, f.prod); addRes(parent.clip, f.clip); parent.clipN += f.clipN; parent.settles += f.settles; }
|
|
else { addRes(self.production, f.prod); addRes(self.sinkClip, f.clip); self.clipEvents += f.clipN; self.t.settles += f.settles; }
|
|
return out;
|
|
};
|
|
}
|
|
|
|
sinkCost(cost) { addRes(this.sinkCosts, cost); }
|
|
|
|
// ---- pools (committed DB state) ----
|
|
pools() {
|
|
const db = this.db;
|
|
const stock = zero(), escrow = zero(), cargo = zero(), sites = zero();
|
|
const s = db.get('SELECT COALESCE(SUM(fiber),0) AS fiber, COALESCE(SUM(hardware),0) AS hardware, COALESCE(SUM(capital),0) AS capital FROM towns');
|
|
addRes(stock, s);
|
|
for (const r of RES) escrow[r] = db.get("SELECT COALESCE(SUM(give_amount),0) AS n FROM trade_offers WHERE status = 'open' AND give_res = ?", r).n;
|
|
for (const m of db.all("SELECT cargo FROM movements WHERE status = 'out' AND cargo != '{}'")) addRes(cargo, JSON.parse(m.cargo));
|
|
const ls = db.get('SELECT COALESCE(SUM(fiber),0) AS fiber, COALESCE(SUM(hardware),0) AS hardware, COALESCE(SUM(capital),0) AS capital FROM landing_sites');
|
|
addRes(sites, ls);
|
|
const total = zero();
|
|
for (const p of [stock, escrow, cargo, sites]) addRes(total, p);
|
|
return { stock, escrow, cargo, sites, total };
|
|
}
|
|
|
|
// Cost of everything ever built on landing sites (levels done + the one running).
|
|
backboneSunk() {
|
|
const out = zero();
|
|
for (const s of this.db.all('SELECT level, build_cost FROM landing_sites')) {
|
|
for (let l = 1; l <= s.level; l++) addRes(out, R.backboneCost(l));
|
|
if (s.build_cost) addRes(out, JSON.parse(s.build_cost));
|
|
}
|
|
return out;
|
|
}
|
|
|
|
// The resource identity. `col` = collector (sources that come from reports), `start` = pools at the first call.
|
|
checkResources(col) {
|
|
const db = this.db;
|
|
const p = this.pools();
|
|
const nOps = db.get('SELECT COUNT(*) AS n FROM operators').n;
|
|
const sources = zero();
|
|
for (const r of RES) sources[r] += nOps * D.START.resources[r] + col.foundedTowns * D.EXPANSION.startResources[r];
|
|
sources.hardware += db.get('SELECT COUNT(*) AS n FROM incumbent_takes').n * D.INCUMBENT.hardwareBonus;
|
|
const bb = this.backboneSunk();
|
|
const res = {};
|
|
let worst = 0;
|
|
for (const r of RES) {
|
|
const expected = sources[r] + this.production[r] - this.sinkCosts[r] - this.sinkLost[r] - this.sinkClip[r] - bb[r];
|
|
const drift = p.total[r] - expected;
|
|
res[r] = { pools: p.total[r], sources: sources[r], production: this.production[r], costs: this.sinkCosts[r], lost: this.sinkLost[r], clipped: this.sinkClip[r], backbone: bb[r], drift };
|
|
worst = Math.max(worst, Math.abs(drift));
|
|
}
|
|
const tol = 0.5 * (this.lostEvents + this.clipEvents) + 1;
|
|
return { ok: worst <= tol, worstDrift: worst, tolerance: tol, perResource: res, pools: p };
|
|
}
|
|
|
|
// Negative stock anywhere.
|
|
negatives() {
|
|
const db = this.db;
|
|
const bad = [];
|
|
for (const t of db.all('SELECT id, fiber, hardware, capital, battery FROM towns WHERE fiber < -1e-6 OR hardware < -1e-6 OR capital < -1e-6 OR battery < -1e-6')) bad.push({ town: t.id, fiber: t.fiber, hardware: t.hardware, capital: t.capital, battery: t.battery });
|
|
for (const s of db.all('SELECT id, fiber, hardware, capital FROM landing_sites WHERE fiber < 0 OR hardware < 0 OR capital < 0')) bad.push({ site: s.id });
|
|
return bad;
|
|
}
|
|
}
|
|
|
|
// ---- units ----
|
|
function aliveUnits(db) {
|
|
const out = {};
|
|
const add = (k, n) => { out[k] = (out[k] || 0) + n; };
|
|
for (const r of db.all('SELECT unit, SUM(count) AS n FROM town_units GROUP BY unit')) add(r.unit, r.n);
|
|
for (const r of db.all('SELECT unit, SUM(count) AS n FROM stationed GROUP BY unit')) add(r.unit, r.n);
|
|
for (const m of db.all("SELECT units FROM movements WHERE status = 'out' AND units != '{}'")) { const u = JSON.parse(m.units); for (const k of Object.keys(u)) add(k, u[k]); }
|
|
return out;
|
|
}
|
|
|
|
module.exports = { Ledger, aliveUnits, zero, addRes };
|