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
165 lines
9.9 KiB
JavaScript
165 lines
9.9 KiB
JavaScript
'use strict';
|
|
/*
|
|
* Incremental ingestion of what the server wrote down: reports (battles, scans, settles), and the notifications that carry
|
|
* numbers the ledger needs (cargo lost at delivery, decommissioned units). Reports are cleaned after 14 days, so the harness
|
|
* ingests at least once per simulated day and keeps its own totals. Everything here only READS the database.
|
|
*/
|
|
const R = require('../../public/js/rules.js');
|
|
const D = require('../../public/js/data.js');
|
|
const { bump, addInto, DAY } = require('./util');
|
|
|
|
const RES = D.RESOURCES;
|
|
const lootValue = (l) => RES.reduce((s, r) => s + ((l && l[r]) || 0), 0);
|
|
const unitsValue = (u) => Object.keys(u || {}).reduce((s, k) => s + (R.unitDef(k) ? R.unitValue(k) * u[k] : 0), 0);
|
|
|
|
function newOpStats() { return { raids: 0, raidWins: 0, raidLoot: 0, raidLostValue: 0, attacks: 0, attackWins: 0, hit: 0, hitLoot: 0, defLostValue: 0, scans: 0, scanOk: 0, incumbent: 0, incumbentWins: 0, incumbentHw: 0 }; }
|
|
|
|
class Collector {
|
|
constructor(db) {
|
|
this.db = db;
|
|
// Cursors are (created_at, ids seen at that instant), not plain ids: the report cleanup deletes every expired report and SQLite then
|
|
// reuses low ids (rowid without AUTOINCREMENT), so "id > last id" would silently skip new reports in a long run.
|
|
this.rep = { at: 0, seen: new Set() }; this.noteCursor = 0;
|
|
this.foundedTowns = 0;
|
|
this.unitsLost = {}; // unit -> count, every way of losing units that the ledger knows about
|
|
this.unitsConsumed = {}; // crews/teams used up (not battle losses)
|
|
this.byType = {}; // report type -> count
|
|
this.battles = { raid: { n: 0, att: 0, loot: 0, lootRes: { fiber: 0, hardware: 0, capital: 0 }, lootLeft: { fiber: 0, hardware: 0, capital: 0 }, emptyWins: 0, attLost: 0, defLost: 0 }, attack: { n: 0, att: 0, loot: 0, attLost: 0, defLost: 0 }, takeover: { n: 0, att: 0, loot: 0, attLost: 0, defLost: 0 } };
|
|
this.raidSamples = []; // { loot, attLostValue, defLostValue, A, D, win, targetAgeDays, undefended }
|
|
this.takeovers = { attempted: 0, dropped: 0, blocked: {}, captured: 0, loyaltyDropped: 0 };
|
|
this.scans = { n: 0, ok: 0, failed: 0, detected: 0, revealed: 0 };
|
|
this.incumbent = { n: 0, wins: 0, hardware: 0, attLostValue: 0 };
|
|
this.landing = { n: 0, taken: 0, attLostValue: 0, vsHeld: 0, vsHeldTaken: 0, vsGarrison: 0, garrisonLostValue: 0, garrisonLost: {}, attackPower: 0, defencePower: 0 };
|
|
this.movementFailed = {};
|
|
this.settles = { ok: 0 };
|
|
this.siege = { n: 0, levels: 0 };
|
|
this.ops = new Map(); // operatorId -> opStats
|
|
this.notes = {}; // notification type -> count
|
|
this.cargoLostTotal = { fiber: 0, hardware: 0, capital: 0 };
|
|
this.cargoLostEvents = 0;
|
|
this.cargoLostBy = {}; // where the overflow happened: transport_arrived | raid_return | link_return -> { fiber, hardware, capital, n }
|
|
this.decommissioned = {};
|
|
this.unitsOfflineNotes = 0;
|
|
this.opCreated = new Map();
|
|
this.bounces = 0;
|
|
}
|
|
|
|
op(id) { if (!this.ops.has(id)) this.ops.set(id, newOpStats()); return this.ops.get(id); }
|
|
created(opId) {
|
|
if (!this.opCreated.has(opId)) { const o = this.db.get('SELECT created_at FROM operators WHERE id = ?', opId); this.opCreated.set(opId, o ? o.created_at : 0); }
|
|
return this.opCreated.get(opId);
|
|
}
|
|
|
|
ingest(ledger) {
|
|
const db = this.db;
|
|
for (const rep of db.all('SELECT id, type, created_at, data FROM reports WHERE created_at >= ? ORDER BY created_at, id', this.rep.at)) {
|
|
if (rep.created_at === this.rep.at && this.rep.seen.has(rep.id)) continue;
|
|
if (rep.created_at > this.rep.at) { this.rep.at = rep.created_at; this.rep.seen = new Set(); }
|
|
this.rep.seen.add(rep.id);
|
|
bump(this.byType, rep.type);
|
|
const d = JSON.parse(rep.data);
|
|
switch (rep.type) {
|
|
case 'raid': case 'attack': case 'takeover': this.battle(rep, d); break;
|
|
case 'scan': this.scan(d); break;
|
|
case 'scan_detected': this.scans.detected += 1; if (d.identityRevealed) this.scans.revealed += 1; break;
|
|
case 'incumbent': this.incumbentReport(d); break;
|
|
case 'landing': this.landingReport(d); break;
|
|
case 'settle': if (d.success) { this.foundedTowns += 1; bump(this.unitsConsumed, D.EXPANSION.settleUnit, D.EXPANSION.crews); this.settles.ok += 1; } break;
|
|
case 'movement_failed': bump(this.movementFailed, d.movementType + ':' + d.reason); this.bounces += 1; break;
|
|
default: break;
|
|
}
|
|
}
|
|
const rows = db.all("SELECT id, type, payload FROM notifications WHERE id > ? AND type IN ('transport_arrived','movement_returned','units_decommissioned','units_offline','trade_refund_clipped') ORDER BY id", this.noteCursor);
|
|
for (const n of rows) {
|
|
this.noteCursor = n.id;
|
|
const p = JSON.parse(n.payload);
|
|
bump(this.notes, n.type);
|
|
if (n.type === 'units_decommissioned') { addInto(this.decommissioned, p.units); addInto(this.unitsLost, p.units); }
|
|
else if (n.type === 'units_offline') this.unitsOfflineNotes += 1;
|
|
else if (n.type === 'trade_refund_clipped') { ledger.sinkClip[p.resource] += p.lost; ledger.clipEvents += 1; }
|
|
else if (p.cargoLost) {
|
|
let any = false;
|
|
for (const r of RES) if (p.cargoLost[r]) { this.cargoLostTotal[r] += p.cargoLost[r]; ledger.sinkLost[r] += p.cargoLost[r]; any = true; }
|
|
if (any) {
|
|
ledger.lostEvents += 1; this.cargoLostEvents += 1;
|
|
let where = n.type;
|
|
if (n.type === 'movement_returned') { const m = db.get('SELECT m.extra, pm.type AS ptype, pm.to_town_id AS ptown FROM movements m LEFT JOIN movements pm ON pm.id = m.parent_id WHERE m.id = ?', p.movementId); where = m && JSON.parse(m.extra || '{}').link ? 'link_return' : (m && m.ptype && !m.ptown ? 'site_return' : 'raid_return'); }
|
|
const by = this.cargoLostBy[where] || (this.cargoLostBy[where] = { fiber: 0, hardware: 0, capital: 0, n: 0 });
|
|
for (const r of RES) by[r] += p.cargoLost[r] || 0;
|
|
by.n += 1;
|
|
}
|
|
}
|
|
}
|
|
// notifications are numbered independently of the filter above: move the cursor past rows we skipped
|
|
const top = db.get('SELECT MAX(id) AS m FROM notifications').m || 0;
|
|
if (top > this.noteCursor) this.noteCursor = top;
|
|
}
|
|
|
|
battle(rep, d) {
|
|
const kind = rep.type;
|
|
const b = this.battles[kind];
|
|
b.n += 1;
|
|
const win = d.outcome === 'attacker_won';
|
|
const attLost = d.attacker.units.lost || {};
|
|
const attLostVal = unitsValue(attLost);
|
|
let defLost = {};
|
|
for (const g of d.defender.groups || []) addInto(defLost, g.lost);
|
|
const defLostVal = unitsValue(defLost);
|
|
addInto(this.unitsLost, attLost); addInto(this.unitsLost, defLost);
|
|
const loot = lootValue(d.loot);
|
|
if (win) b.att += 1;
|
|
b.loot += loot; b.attLost += attLostVal; b.defLost += defLostVal;
|
|
if (kind === 'raid') { addInto(b.lootRes, d.loot); addInto(b.lootLeft, d.lootLeft || {}); if (win && !loot) b.emptyWins += 1; }
|
|
const aId = d.attacker.operator.id, dId = d.defender.operator.id;
|
|
const a = this.op(aId), df = this.op(dId);
|
|
if (kind === 'raid') { a.raids += 1; if (win) a.raidWins += 1; a.raidLoot += loot; a.raidLostValue += attLostVal; }
|
|
else { a.attacks += 1; if (win) a.attackWins += 1; a.raidLoot += loot; a.raidLostValue += attLostVal; }
|
|
df.hit += 1; df.hitLoot += loot; df.defLostValue += defLostVal;
|
|
if (d.siege) { this.siege.n += 1; this.siege.levels += d.siege.from - d.siege.to; }
|
|
if (d.takeover) {
|
|
const t = d.takeover;
|
|
this.takeovers.attempted += 1;
|
|
if (t.blocked) bump(this.takeovers.blocked, t.blocked);
|
|
if (t.drop > 0) { this.takeovers.dropped += 1; this.takeovers.loyaltyDropped += t.drop; bump(this.unitsConsumed, D.EXPANSION.conquestUnit, 1); }
|
|
if (t.captured) this.takeovers.captured += 1;
|
|
}
|
|
if (kind === 'raid') {
|
|
const defenderHome = (d.defender.groups || []).reduce((s, g) => s + R.defencePower(g.sent, {}), 0);
|
|
this.raidSamples.push({
|
|
loot, attLost: attLostVal, defLost: defLostVal, A: d.power.attack, D: d.power.defence, win,
|
|
targetAgeDays: (rep.created_at - this.created(dId)) / DAY, undefended: defenderHome <= 0, at: rep.created_at, attacker: aId, defender: dId,
|
|
});
|
|
}
|
|
}
|
|
|
|
scan(d) {
|
|
this.scans.n += 1;
|
|
if (d.success) this.scans.ok += 1; else this.scans.failed += 1;
|
|
addInto(this.unitsLost, (d.scanners && d.scanners.lost) || {});
|
|
}
|
|
incumbentReport(d) {
|
|
this.incumbent.n += 1;
|
|
if (d.outcome === 'attacker_won') this.incumbent.wins += 1;
|
|
if (d.bonus) this.incumbent.hardware += d.bonus.hardware || 0;
|
|
const lost = (d.attacker.units && d.attacker.units.lost) || {};
|
|
this.incumbent.attLostValue += unitsValue(lost);
|
|
addInto(this.unitsLost, lost);
|
|
const a = this.op(d.attacker.operator.id);
|
|
a.incumbent += 1; if (d.outcome === 'attacker_won') a.incumbentWins += 1; if (d.bonus) a.incumbentHw += d.bonus.hardware || 0;
|
|
}
|
|
landingReport(d) {
|
|
this.landing.n += 1;
|
|
if (d.taken) this.landing.taken += 1;
|
|
const lost = (d.attacker.units && d.attacker.units.lost) || {};
|
|
this.landing.attLostValue += unitsValue(lost);
|
|
addInto(this.unitsLost, lost);
|
|
// contests (E6): attacks on a HELD site, those that met a garrison, and what the garrison lost (units stationed at a site die in these reports)
|
|
if (d.site && d.site.holder) { this.landing.vsHeld += 1; if (d.taken) this.landing.vsHeldTaken += 1; this.landing.firstVsHeldAt = this.landing.firstVsHeldAt || d.arrivedAt; this.landing.lastVsHeldAt = d.arrivedAt; }
|
|
const gar = (d.defender && d.defender.garrison) || [];
|
|
if (gar.some((g) => Object.keys(g.sent || {}).length)) this.landing.vsGarrison += 1;
|
|
for (const g of gar) { addInto(this.unitsLost, g.lost || {}); addInto(this.landing.garrisonLost, g.lost || {}); this.landing.garrisonLostValue += unitsValue(g.lost || {}); }
|
|
if (d.power) { this.landing.attackPower += d.power.attack || 0; this.landing.defencePower += d.power.defence || 0; }
|
|
}
|
|
}
|
|
|
|
module.exports = { Collector, lootValue, unitsValue };
|