Built: - Economy: raid loot capped to the room the raiders have at home (rest stays with the victim, "lootLeft" in the report); trade escrow reserves its room so refunds always fit; trade accept refuses with no_room_for_goods when the goods would not fit. Loot destroyed per 30-day run 31-61k -> ~1k, refunds clipped ~27k -> 0 (docs/BALANCE.md, 3 seeds). - End game: members of the holding alliance garrison landing sites (migration 005_site_garrison.sql), garrison counts in site defence, 72 h capture shield, withdraw route, POP view lists garrisoned units. Shipping capacity scales with world speed (3x world: winner 76-98 -> ~30-36 days after sites open). Bot runs show real contests (84-194 attacks on held sites at 1x). - Ops: deploy/update.sh (backup, stop, ff-merge, npm ci --ignore-scripts, health check, rollback), deploy/upstream-offsite (rsync only), admin end-world snapshots results, EPIPE fix, docs/workflows/ filled in. - e2e 62 -> 163 checks: units, raids/reports, alliance+messages, trade, end game, nb, PWA, reconnect, slow polling fallback (opt-in). Two app bugs found: message push ignored, "New" button stayed English. Tested: npm test 274/274, openapi check, npm run e2e 163/163 (137 s), bot runs 50x30d seeds 1-3 and 140-day/60-day speed-3 end-game runs INVARIANTS OK, adversarial review pass (findings fixed or documented). NOT tested: update.sh on a real box (systemd, real npm, runuser; stubs only, shellcheck not installed), garrison card/loot text on a phone, Node 22.13, humans. Known leaks left open and documented (BALANCE S6): loot/shipments can still overflow when stock arrives in flight, bounced backbone shipments lose overflow (reported to the sender). Garrison numbers (heldDefenceMult 2, shield 72 h) are first guesses from 2 seeds. Not claimed: balanced. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Q2MJaQ5LXAHzGnTDgceT14
149 lines
7.9 KiB
JavaScript
149 lines
7.9 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
|
|
* (booked from `trade_refund_clipped` notifications: with escrow reserving its room in the Rack hall this is only a hall that was
|
|
* shot down below stock + escrow in the meantime, so it is 0 in normal runs)
|
|
*
|
|
* 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 = ?';
|
|
|
|
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 that did not fit (trade_refund_clipped notifications)
|
|
this.clipEvents = 0;
|
|
this.sourceStart = zero(); // operators' start stock
|
|
this.stack = [];
|
|
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);
|
|
}
|
|
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 r of db.all('SELECT unit, SUM(count) AS n FROM site_stationed GROUP BY unit')) add(r.unit, r.n); // garrisons at landing sites (E6)
|
|
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 };
|