Upstream/tools/bot/report.js
jvanvik bf7af0679c Early-level build discount (B4) and start stock 1000 (B5)
Playtest on the live world: first hour feels a bit slow. B4: levels 1-3 of any building take x0.5/x0.67/x0.83 of the time (FORMULAS.earlyLevels/earlyTimeFactor, off at earlyLevels 0). Builds at 1 h (4 sittings/day) 8.9 -> 11.4, at 24 h 16.5 -> 20.4; mid game moves ~1 day head start (default mix), more for 2 sittings/day. B5: start stock 750 -> 1000 (measured separately: mid game unchanged, 60-min first sitting 12.1 -> 15.4 builds at 1 h). Numbers in docs/BALANCE.md.

Tested: npm test 208/208, openapi check, npm run e2e 61/61 (before B5), 50x30-day bot run INVARIANTS OK after B5. NOT tested: humans (bots only, 2 seeds), B4+B5 combined beyond one run, e2e does not exercise the discount (runs at 4000x speed). Not claimed: balanced.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q2MJaQ5LXAHzGnTDgceT14
2026-10-11 22:38:37 +02:00

110 lines
12 KiB
JavaScript

'use strict';
/* Plain-text report of a bot run (the JSON file has everything; this is what a person reads in the terminal). */
const { fmt, table, round } = require('./util');
const pc = (p, k) => (p && p[k] !== null && p[k] !== undefined ? fmt(p[k]) : '-');
function render(s, opts) {
opts = opts || {};
const out = [];
const h = (t) => out.push('', '== ' + t + ' ==');
const m = s.meta;
out.push('UPSTREAM bot run: ' + m.players + ' players, ' + m.days + ' days, world speed ' + m.speed + 'x, seed ' + m.seed + ', radius ' + m.radius +
', sessions/day ' + m.sessionsPerDay + (m.alwaysOn ? ', ALWAYS-ON' : '') + (m.set.length ? ', overrides: ' + m.set.join(' ') : ''));
out.push('mix: ' + Object.entries(m.mix).map(([k, v]) => k + ':' + v).join(' '));
h('invariants');
const inv = s.invariants;
out.push((inv.ok ? 'OK' : 'BROKEN') + ': resources ' + (inv.resources.ok ? 'conserved' : 'DRIFT') + ' (worst drift ' + fmt(inv.resources.worstDrift, 2) + ', tolerance ' + fmt(inv.resources.tolerance, 1) + '), units ' + (inv.units.ok ? 'conserved' : 'DRIFT ' + JSON.stringify(inv.units.drift)) +
', negative stock ' + (inv.negativeStock.ok ? 'none' : 'FOUND'), ' failed events ' + inv.failedEvents.n + ', unexpected errors ' + inv.unexpectedErrors.n);
for (const r of Object.keys(inv.resources.final)) {
const f = inv.resources.final[r];
out.push(' ' + r.padEnd(9) + 'pools ' + fmt(f.pools, 0).padStart(9) + ' = sources ' + fmt(f.sources, 0) + ' + production ' + fmt(f.production, 0) + ' - spent ' + fmt(f.costs, 0) + ' - overflow lost ' + fmt(f.lost, 0) + ' - refund clipped ' + fmt(f.clipped, 0) + ' - backbone ' + fmt(f.backbone, 0) + ' drift ' + fmt(f.drift, 3));
}
for (const e of inv.failedEvents.sample) out.push(' failed event ' + e.id + ' ' + e.type + ': ' + e.error);
for (const e of inv.unexpectedErrors.sample.slice(0, 5)) out.push(' unexpected: ' + e.command + ' ' + e.code + ' ' + (e.message || '') + (e.stack ? ' ' + e.stack : ''));
for (const l of inv.serverLog.slice(0, 5)) out.push(' server log: ' + l);
h('economy per day (mean per POP; stock/production per resource F/H/C)');
const rows = [];
const step = s.days.length > 16 ? Math.ceil(s.days.length / 16) : 1;
for (const d of s.days) {
if (d.day % step !== 0 && d.day !== 1 && d.day !== s.days.length) continue;
rows.push([d.day, d.operators, d.towns, fmt(d.stock.fiber) + '/' + fmt(d.stock.hardware) + '/' + fmt(d.stock.capital), fmt(d.prodPerHour.fiber) + '/' + fmt(d.prodPerHour.hardware) + '/' + fmt(d.prodPerHour.capital),
fmt(d.fillPct, 0) + '%', fmt(d.fullPct, 0) + '%', fmt(d.queueUtilPct, 0) + '%', fmt(d.powerNet), fmt(d.powerNegPct, 0) + '%', fmt(d.unitPowerBoundPct, 0) + '%', fmt(d.unitOfflinePct, 0) + '%']);
}
out.push(table(rows, ['day', 'ops', 'POPs', 'stock F/H/C', 'prod/h F/H/C', 'fill', 'full', 'queue', 'pwrNet', 'unit POPs net<0', 'unit POPs pwr-bound', 'unit POPs offline']));
out.push('production lost to full storage over the run (share of what the rates would give): fiber ' + s.storageWastePct.fiber + '%, hardware ' + s.storageWastePct.hardware + '%, capital ' + s.storageWastePct.capital + '%');
out.push('time at a full store (share of POP-samples >= 95% of the Rack hall) last day: ' + JSON.stringify(s.days[s.days.length - 1].fullBy));
h('progression per day');
const prow = [];
for (const d of s.days) {
if (d.day % step !== 0 && d.day !== 1 && d.day !== s.days.length) continue;
const u = d.units;
prow.push([d.day, fmt(d.fieldAvg, 1), pc(d.core, 'p10') + '/' + pc(d.core, 'p50') + '/' + pc(d.core, 'p90'), pc(d.footprint, 'p10') + '/' + pc(d.footprint, 'p50') + '/' + pc(d.footprint, 'p90'), d.multiPop + ' (max ' + d.popsMax + ')', fmt(d.gateReadyPct, 0) + '%',
Object.keys(u).sort().map((k) => k.replace(/_.*/, '') + ':' + fmt(u[k], 0)).join(' ')]);
}
out.push(table(prow, ['day', 'field avg', 'core p10/50/90', 'footprint p10/50/90', 'operators >=2 POPs', 'over gate', 'units alive']));
if (s.early) out.push('first hours after signing up, capital, mean (p10/50/90) over operators at least that old: ' + Object.keys(s.early).map((k) => { const e = s.early[k]; return k + ': ' + e.builds + ' builds (' + e.buildsP.p10 + '/' + e.buildsP.p50 + '/' + e.buildsP.p90 + '), Core router ' + e.core + ' (n=' + e.n + ')'; }).join('; '));
const f = s.final;
out.push('time to Core router 10 (days after joining): ' + pc(f.core10Days, 'p10') + '/' + pc(f.core10Days, 'p50') + '/' + pc(f.core10Days, 'p90') + ' (reached by ' + f.core10Reached + ' of ' + m.players + ')');
out.push('time to first expansion (days after joining): ' + pc(f.firstExpansionDays, 'p10') + '/' + pc(f.firstExpansionDays, 'p50') + '/' + pc(f.firstExpansionDays, 'p90') + ' (reached by ' + f.firstExpansionReached + ' of ' + m.players + ')');
out.push('by archetype, days after joining to Core router 10 / first expansion (p10/50/90): ' + Object.keys(f.milestones || {}).sort().map((a) => a + ' ' + pc(f.milestones[a].core10Days, 'p10') + '/' + pc(f.milestones[a].core10Days, 'p50') + '/' + pc(f.milestones[a].core10Days, 'p90') + ' | ' + pc(f.milestones[a].firstExpansionDays, 'p10') + '/' + pc(f.milestones[a].firstExpansionDays, 'p50') + '/' + pc(f.milestones[a].firstExpansionDays, 'p90')).join('; '));
out.push('POPs per operator: ' + Object.keys(f.pops).sort((a, b) => a - b).map((k) => k + ':' + f.pops[k]).join(' '));
out.push('infrastructure levels at the capital (mean; the histogram 0..20 is in --json): ' + Object.keys(f.levels).map((k) => k + ' ' + f.levels[k].mean).join(', '));
h('by archetype (means)');
const arows = Object.keys(f.byArchetype).sort().map((a) => {
const r = f.byArchetype[a];
return [a, r.n, r.footprintMean, r.fieldAvg, r.core, r.pops, fmt(r.spentBuild), fmt(r.spentTrain), fmt(r.stock), fmt(r.netWorth), r.raids + '/' + r.raidWins, fmt(r.lootTaken), fmt(r.raidLostValue), r.timesHit, fmt(r.lootLostToRaids), r.incumbentWins];
});
out.push(table(arows, ['archetype', 'n', 'footprint', 'field avg', 'core', 'POPs', 'spent build', 'spent units', 'stock', 'net worth', 'raids/won', 'loot taken', 'raid loss value', 'times hit', 'loot lost', 'incumbent wins']));
h('combat');
const c = s.combat;
out.push('reports: ' + Object.entries(c.reports).map(([k, v]) => k + ':' + v).join(' '));
const r = c.raids;
out.push('raids ' + r.n + ', won ' + r.wins + (r.winRate !== null ? ' (' + fmt(r.winRate * 100, 0) + '%)' : '') + ', loot ' + fmt(r.lootTotal) + ' (' + Object.entries(r.lootRes).map(([k, v]) => k[0] + ':' + fmt(v)).join(' ') + '), loot/raid p10/50/90 ' + pc(r.lootPerRaid, 'p10') + '/' + pc(r.lootPerRaid, 'p50') + '/' + pc(r.lootPerRaid, 'p90'));
out.push(' attacker lost value ' + fmt(r.attackerLostValue) + ', defender lost value ' + fmt(r.defenderLostValue) + ', loot per lost value ' + fmt(r.lootPerLostValue) + ', share against undefended POPs ' + (r.undefendedShare === null ? '-' : fmt(r.undefendedShare * 100, 0) + '%') + ', attack/defence p10/50/90 ' + pc(r.attackOverDefence, 'p10') + '/' + pc(r.attackOverDefence, 'p50') + '/' + pc(r.attackOverDefence, 'p90'));
out.push(' target age at raid (days since the target joined): ' + Object.entries(r.targetAge).map(([k, v]) => k + ':' + v).join(' '));
out.push('full attacks ' + c.attacks.n + ' (won ' + c.attacks.att + '), takeover attacks ' + c.takeoverBattles.n + ' (won ' + c.takeoverBattles.att + '), takeovers: attempted ' + c.takeovers.attempted + ', loyalty hits ' + c.takeovers.dropped + ', captured ' + c.takeovers.captured + ', blocked ' + JSON.stringify(c.takeovers.blocked) + ', siege ' + c.siege.n);
out.push('scans ' + c.scans.n + ' (ok ' + c.scans.ok + ', failed ' + c.scans.failed + '), detected-by-target reports ' + c.scans.detected + ' (identity revealed ' + c.scans.revealed + ')');
out.push('incumbent sites: attacks ' + c.incumbent.n + ', won ' + c.incumbent.wins + ', hardware bonus ' + fmt(c.incumbent.hardware) + ', attacker lost value ' + fmt(c.incumbent.attLostValue));
out.push('bounced movements: ' + JSON.stringify(c.movementFailed));
out.push('units lost: ' + JSON.stringify(c.unitsLost) + ' consumed: ' + JSON.stringify(c.unitsConsumed) + ' decommissioned: ' + JSON.stringify(c.decommissioned));
h('trade and alliances');
const t = s.trade;
out.push('offers by status: ' + Object.entries(t.byStatus).map(([k, v]) => k + ':' + v.n).join(' ') + ', completed volume ' + fmt(t.volumeCompleted) + ', completed give/want ratio p10/50/90 ' + pc(t.ratio, 'p10') + '/' + pc(t.ratio, 'p50') + '/' + pc(t.ratio, 'p90'));
out.push('transport overflow lost (' + t.cargoLostEvents + ' deliveries): ' + JSON.stringify(t.cargoLost) + ', trade refunds clipped by the Rack hall cap (' + t.clipEvents + '): ' + JSON.stringify(t.refundClipped));
const a = s.alliances;
out.push('alliances ' + a.count + ', members p10/50/90 ' + pc(a.members, 'p10') + '/' + pc(a.members, 'p50') + '/' + pc(a.members, 'p90') + ' (max ' + a.membersMax + '), operators in an alliance ' + a.inAlliance + '/' + a.operators + ', relations ' + JSON.stringify(a.relations) + ', proposals ' + JSON.stringify(a.proposals));
if (s.endgame) {
h('end game');
const e = s.endgame;
if (!e.open) out.push('sites not open yet (world age ' + fmt(e.ageDays, 1) + ' days, opens at ' + e.openDay + ')');
else {
out.push('opened at simulated day ' + fmt(e.openedAtSimDay, 1) + ' (world age now ' + fmt(e.ageDays, 0) + '), alliance plans ' + e.plans + ', strikes sent ' + e.strikes + ' (landing reports ' + e.landingReports.n + ', taken ' + e.landingReports.taken + ', units lost value ' + fmt(e.landingReports.attLostValue) + '), strike blockers ' + JSON.stringify(e.strikeSkips));
out.push('shipments ' + e.shipments + ' carrying ' + fmt(e.shippedTotal) + ' (F ' + fmt(e.shipped.fiber) + ' H ' + fmt(e.shipped.hardware) + ' C ' + fmt(e.shipped.capital) + '), ' + fmt(e.shippedPerSimDay) + ' per simulated day; node levels started ' + e.buildsStarted + ' ' + JSON.stringify(e.buildErrors));
out.push(table(e.sites.map((x) => [x.idx, x.holder || '-', x.level, x.takenAt === null ? '-' : fmt(x.takenAt, 1), x.contributors, fmt(x.shippedIn.fiber + x.shippedIn.hardware + x.shippedIn.capital), fmt(x.shippedPerDay), x.daysToLevel100 === null ? '-' : x.daysToLevel100, fmt(x.stock.fiber + x.stock.hardware + x.stock.capital)]), ['site', 'holder', 'node level', 'taken (day)', 'contributors', 'shipped in', 'shipped/day', 'days to lvl 100 at that rate', 'stockpile now']));
out.push('largest army (attack, before research) any single POP holds: ' + fmt(e.maxPopAttackNoResearch) + ' against the defence of the best built-up site now: ' + fmt(e.bestHeldSiteDefenceNow) + ' (one attack is one movement from one POP, so attacks cannot be added up)');
out.push('best node level ' + e.bestLevel + '; still to ship for level 100 from there (per resource) ' + fmt(e.remainingToLevel100.fiber) + '; world ' + e.worldStatus + (e.winner ? ', WINNER ' + e.winner.tag + ' on day ' + fmt(e.winner.wonAtSimDay, 1) : ', no winner'));
}
}
h('performance and errors');
const p = s.perf;
out.push('wall ' + p.wallSeconds + ' s for ' + m.days + ' simulated days (' + p.simulatedPerWallSecond + ' simulated hours per wall second), commands ' + p.commands + ' (' + p.commandsOk + ' ok, ' + p.commandsPerSecond + '/s), p50/p95/p99 latency ' + fmt(p.latencyMs.p50, 2) + '/' + fmt(p.latencyMs.p95, 2) + '/' + fmt(p.latencyMs.p99, 2) + ' ms');
out.push('events processed in the loop ' + p.eventsProcessed + ' (' + fmt(p.runDueSeconds, 2) + ' s in runDue), done by type: ' + Object.entries(p.eventsDone).map(([k, v]) => k + ':' + v).join(' '));
const slow = Object.entries(p.latencyByCommand).sort((x, y) => (y[1].p95 || 0) - (x[1].p95 || 0)).slice(0, 5);
out.push('slowest commands (p95 ms): ' + slow.map(([k, v]) => k + ' ' + fmt(v.p95, 2) + ' (n ' + v.n + ')').join(', '));
out.push('bot actions: ' + JSON.stringify(s.botLogs));
const er = s.errors.rows.slice(0, 14);
out.push('command errors (' + s.errors.expectedTotal + ' expected, ' + s.errors.unexpectedTotal + ' unexpected): ' + er.map((x) => x.key + ' x' + x.n + (x.expected ? '' : ' (!)')).join(', '));
return out.join('\n');
}
module.exports = { render };