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
192 lines
14 KiB
JavaScript
192 lines
14 KiB
JavaScript
'use strict';
|
|
/* Builds the final stats object (JSON-able) from what the simulation collected. Wall-clock numbers live under `perf` only,
|
|
* so everything else is identical for the same seed (the determinism test compares it without `perf`). */
|
|
const D = require('../../public/js/data.js');
|
|
const R = require('../../public/js/rules.js');
|
|
const { DAY, percentiles, mean, round, sum } = require('./util');
|
|
const { lootValue } = require('./collector');
|
|
|
|
const RES = D.RESOURCES;
|
|
|
|
function histogram(values, max) {
|
|
const h = {};
|
|
for (const v of values) h[v] = (h[v] || 0) + 1;
|
|
const out = [];
|
|
for (let i = 0; i <= max; i++) out.push(h[i] || 0);
|
|
return out;
|
|
}
|
|
|
|
function build(x) {
|
|
const { sim, o, days, ledger, col, fin, invariantLog, lat, counts, errors, unexpected, commandsRun, commandsOk, runDueMs, eventsProcessed, wallMs, logs, EXPECTED } = x;
|
|
const { db, world, view, t0 } = sim;
|
|
const now = sim.clock.now();
|
|
const S = require('../../server/state');
|
|
|
|
// ---- progression at the end
|
|
const ops = db.all('SELECT * FROM operators ORDER BY id');
|
|
const byArch = {};
|
|
const opArch = new Map(sim.bots.map((b) => [b.operatorId, b]));
|
|
const levels = {};
|
|
const fieldMins = [];
|
|
const popsDist = {};
|
|
const footprints = [];
|
|
const core10 = [], expand1 = [];
|
|
const milestones = {};
|
|
const core10Rows = new Map(db.all(`SELECT t.owner_id AS oid, MIN(e.due_at) AS at FROM events e JOIN towns t ON t.id = e.town_id
|
|
WHERE e.type = 'buildDone' AND e.status = 'done' AND json_extract(e.payload, '$.type') = 'core_router' AND json_extract(e.payload, '$.toLevel') >= 10 GROUP BY t.owner_id`).map((r) => [r.oid, r.at]));
|
|
const exp1Rows = new Map(db.all(`SELECT m.owner_id AS oid, MIN(m.arrive_at) AS at FROM movements m JOIN towns t ON t.world_id = m.world_id AND t.x = m.to_x AND t.y = m.to_y AND t.created_at = m.arrive_at
|
|
WHERE m.type = 'settle' AND m.status = 'done' GROUP BY m.owner_id`).map((r) => [r.oid, r.at]));
|
|
const infraTypes = Object.keys(D.BUILDINGS).filter((k) => D.BUILDINGS[k].kind === 'infra' && !D.BUILDINGS[k].special);
|
|
for (const t of infraTypes) levels[t] = [];
|
|
for (const op of ops) {
|
|
const towns = view.towns(op.id);
|
|
const bot = opArch.get(op.id);
|
|
const a = bot ? bot.archetype : '?';
|
|
const row = byArch[a] || (byArch[a] = { n: 0, footprint: [], fieldAvg: [], pops: [], spentBuild: [], spentTrain: [], spentResearch: [], stock: [], core: [] });
|
|
row.n += 1;
|
|
popsDist[towns.length] = (popsDist[towns.length] || 0) + 1;
|
|
if (!towns.length) continue;
|
|
const fp = towns.reduce((s2, t) => s2 + R.footprintAt(t, now), 0);
|
|
footprints.push(fp);
|
|
const cap = towns[0];
|
|
for (const t of infraTypes) levels[t].push(view.level(cap, t));
|
|
const fl = view.fieldLevels(cap).map((f) => f.level);
|
|
fieldMins.push(Math.min(...fl));
|
|
row.footprint.push(fp); row.fieldAvg.push(mean(fl)); row.pops.push(towns.length); row.core.push(view.level(cap, 'core_router'));
|
|
row.spentBuild.push(bot ? (bot.spent.build || 0) : 0); row.spentTrain.push(bot ? (bot.spent.train || 0) : 0); row.spentResearch.push(bot ? (bot.spent.research || 0) : 0);
|
|
row.stock.push(towns.reduce((s2, t) => s2 + RES.reduce((q, r) => q + R.resourcesAt(t, now).amounts[r], 0), 0));
|
|
if (core10Rows.has(op.id)) { const d0 = (core10Rows.get(op.id) - op.created_at) / DAY; core10.push(d0); (milestones[a] || (milestones[a] = { core10: [], exp1: [] })).core10.push(d0); }
|
|
if (exp1Rows.has(op.id)) { const d1 = (exp1Rows.get(op.id) - op.created_at) / DAY; expand1.push(d1); (milestones[a] || (milestones[a] = { core10: [], exp1: [] })).exp1.push(d1); }
|
|
}
|
|
const archTable = {};
|
|
for (const a of Object.keys(byArch)) {
|
|
const r = byArch[a];
|
|
const cs = col.ops;
|
|
let raids = 0, wins = 0, loot = 0, lost = 0, hit = 0, hitLoot = 0, incWins = 0, incHw = 0;
|
|
for (const b of sim.bots) if (b.archetype === a) {
|
|
const s = cs.get(b.operatorId);
|
|
if (!s) continue;
|
|
raids += s.raids; wins += s.raidWins; loot += s.raidLoot; lost += s.raidLostValue; hit += s.hit; hitLoot += s.hitLoot; incWins += s.incumbentWins; incHw += s.incumbentHw;
|
|
}
|
|
archTable[a] = {
|
|
n: r.n, footprintMean: round(mean(r.footprint), 0), footprintP50: round(percentiles(r.footprint, [50]).p50 || 0, 0), fieldAvg: round(mean(r.fieldAvg), 1), core: round(mean(r.core), 1), pops: round(mean(r.pops), 2),
|
|
spentBuild: round(mean(r.spentBuild), 0), spentTrain: round(mean(r.spentTrain), 0), spentResearch: round(mean(r.spentResearch), 0), stock: round(mean(r.stock), 0),
|
|
netWorth: round(mean(r.spentBuild) + mean(r.spentTrain) + mean(r.spentResearch) + mean(r.stock), 0),
|
|
raids: raids, raidWins: wins, lootTaken: round(loot, 0), raidLostValue: round(lost, 0), timesHit: hit, lootLostToRaids: round(hitLoot, 0), incumbentWins: incWins, incumbentHardware: incHw,
|
|
};
|
|
}
|
|
|
|
// ---- first hours: what an operator had built N hours after signing up (only operators that are at least that old at the end).
|
|
// Counted from the buildDone events of the capital, so it needs no extra sampling. Builds = levels gained (one level per build).
|
|
const early = {};
|
|
const capOf = new Map(db.all('SELECT owner_id AS oid, MIN(id) AS tid FROM towns GROUP BY owner_id').map((r) => [r.oid, r.tid]));
|
|
const doneByTown = new Map();
|
|
for (const r of db.all("SELECT town_id AS tid, due_at AS at, json_extract(payload, '$.type') AS type, json_extract(payload, '$.toLevel') AS lvl FROM events WHERE type = 'buildDone' AND status = 'done' ORDER BY due_at, id")) {
|
|
(doneByTown.get(r.tid) || doneByTown.set(r.tid, []).get(r.tid)).push(r);
|
|
}
|
|
for (const h of [1, 6, 24]) {
|
|
const builds = [], cores = [], fieldBuilds = [];
|
|
for (const op of ops) {
|
|
if (op.created_at + h * 3600000 > now || !capOf.has(op.id)) continue;
|
|
const list = (doneByTown.get(capOf.get(op.id)) || []).filter((e) => e.at <= op.created_at + h * 3600000);
|
|
builds.push(list.length);
|
|
fieldBuilds.push(list.filter((e) => D.BUILDINGS[e.type] && D.BUILDINGS[e.type].kind === 'field').length);
|
|
cores.push(Math.max(D.START.infra.core_router || 1, ...list.filter((e) => e.type === 'core_router').map((e) => e.lvl)));
|
|
}
|
|
early[h + 'h'] = { n: builds.length, builds: round(mean(builds), 1), buildsP: percentiles(builds, [10, 50, 90]), fieldBuilds: round(mean(fieldBuilds), 1), core: round(mean(cores), 2), coreP: percentiles(cores, [10, 50, 90]) };
|
|
}
|
|
|
|
// ---- combat summary
|
|
const raid = col.battles.raid;
|
|
const samples = col.raidSamples;
|
|
const ratio = samples.filter((s) => s.attLost > 0).map((s) => s.loot / s.attLost);
|
|
const lootPerRaid = samples.map((s) => s.loot);
|
|
const ageBuckets = { '<3d': 0, '3-4d': 0, '4-7d': 0, '7-14d': 0, '14d+': 0 };
|
|
for (const s of samples) { const a = s.targetAgeDays; ageBuckets[a < 3 ? '<3d' : a < 4 ? '3-4d' : a < 7 ? '4-7d' : a < 14 ? '7-14d' : '14d+'] += 1; }
|
|
const undefended = samples.filter((s) => s.undefended);
|
|
const combat = {
|
|
reports: col.byType,
|
|
raids: { n: raid.n, wins: raid.att, winRate: raid.n ? raid.att / raid.n : null, lootTotal: round(raid.loot, 0), lootRes: raid.lootRes, lootPerRaid: percentiles(lootPerRaid, [10, 50, 90]), attackerLostValue: round(raid.attLost, 0), defenderLostValue: round(raid.defLost, 0),
|
|
lootPerLostValue: raid.attLost > 0 ? round(raid.loot / raid.attLost, 2) : null, undefendedShare: samples.length ? round(undefended.length / samples.length, 3) : null, undefendedLoot: round(sum(undefended.map((s) => s.loot)), 0),
|
|
lootOverLossValue: percentiles(ratio, [10, 50, 90]), targetAge: ageBuckets, attackOverDefence: percentiles(samples.filter((s) => s.D > 0).map((s) => s.A / s.D), [10, 50, 90]) },
|
|
attacks: col.battles.attack, takeoverBattles: col.battles.takeover, takeovers: col.takeovers, siege: col.siege,
|
|
scans: col.scans, incumbent: col.incumbent, landing: col.landing, movementFailed: col.movementFailed,
|
|
unitsLost: col.unitsLost, unitsConsumed: col.unitsConsumed, decommissioned: col.decommissioned, unitsOfflineNotes: col.unitsOfflineNotes,
|
|
};
|
|
|
|
// ---- trade, alliances
|
|
const tradeRows = db.all('SELECT status, COUNT(*) AS n, SUM(give_amount) AS give, SUM(want_amount) AS want FROM trade_offers GROUP BY status');
|
|
const trade = { byStatus: Object.fromEntries(tradeRows.map((r) => [r.status, { n: r.n, give: r.give, want: r.want }])), refundClipped: ledger.sinkClip, clipEvents: ledger.clipEvents,
|
|
cargoLost: col.cargoLostTotal, cargoLostEvents: col.cargoLostEvents };
|
|
const completed = db.all("SELECT give_res, give_amount, want_res, want_amount FROM trade_offers WHERE status = 'completed'");
|
|
trade.volumeCompleted = sum(completed.map((r) => r.give_amount + r.want_amount));
|
|
trade.ratio = percentiles(completed.map((r) => r.give_amount / r.want_amount), [10, 50, 90]);
|
|
const allianceRows = db.all('SELECT a.id, a.tag, (SELECT COUNT(*) FROM alliance_members m WHERE m.alliance_id = a.id) AS members FROM alliances a ORDER BY a.id');
|
|
const relRows = db.all('SELECT state, COUNT(*) AS n FROM alliance_relations GROUP BY state');
|
|
const alliances = {
|
|
count: allianceRows.length, members: percentiles(allianceRows.map((a) => a.members), [10, 50, 90]), membersMax: Math.max(0, ...allianceRows.map((a) => a.members)),
|
|
inAlliance: db.get('SELECT COUNT(*) AS n FROM operators WHERE alliance_id IS NOT NULL').n, operators: ops.length,
|
|
relations: Object.fromEntries(relRows.map((r) => [r.state, r.n])),
|
|
proposals: Object.fromEntries(db.all('SELECT status, COUNT(*) AS n FROM alliance_proposals GROUP BY status').map((r) => [r.status, r.n])),
|
|
};
|
|
|
|
// ---- endgame
|
|
const endgame = sim.endgame.summary(now);
|
|
|
|
// ---- perf and errors
|
|
const latency = {};
|
|
const all = [];
|
|
for (const k of Object.keys(lat)) { latency[k] = Object.assign({ n: lat[k].length }, percentiles(lat[k], [50, 95, 99])); for (const v of lat[k]) all.push(v); }
|
|
const failedRows = db.all("SELECT id, type, error FROM events WHERE status = 'failed' ORDER BY id LIMIT 10");
|
|
const evByStatus = Object.fromEntries(db.all('SELECT status, COUNT(*) AS n FROM events GROUP BY status').map((r) => [r.status, r.n]));
|
|
const evByType = Object.fromEntries(db.all("SELECT type, COUNT(*) AS n FROM events WHERE status = 'done' GROUP BY type").map((r) => [r.type, r.n]));
|
|
const errorRows = Object.keys(errors).sort((a, b) => errors[b] - errors[a]).map((k) => ({ key: k, n: errors[k], expected: EXPECTED.has(k.split(':').slice(1).join(':')) }));
|
|
// production lost to the storage cap: what the rates would have produced (sampled) against what was really stored
|
|
const unsettled = { fiber: 0, hardware: 0, capital: 0 };
|
|
for (const row of db.all('SELECT * FROM towns')) { const t = S.townFromRow(db, row, world); const cur = R.resourcesAt(t, now).amounts; for (const r of RES) unsettled[r] += cur[r] - row[r]; }
|
|
const waste = {};
|
|
for (const r of RES) { const actual = ledger.production[r] + unsettled[r]; waste[r] = sim.potential[r] > 0 ? round(Math.max(0, 1 - actual / sim.potential[r]) * 100, 1) : 0; }
|
|
const perf = {
|
|
wallSeconds: round(wallMs / 1000, 2), commands: commandsRun, commandsOk, commandsPerSecond: round(commandsRun / (wallMs / 1000), 0),
|
|
latencyMs: Object.assign({ n: all.length }, percentiles(all, [50, 95, 99])), latencyByCommand: latency,
|
|
eventsProcessed, eventsDone: evByType, runDueSeconds: round(runDueMs / 1000, 2),
|
|
simulatedPerWallSecond: round(o.days * DAY / 1000 / (wallMs / 1000) / 3600, 1), // simulated hours per wall second
|
|
};
|
|
|
|
// ---- invariants
|
|
const resBad = invariantLog.filter((r) => !r.resourcesOk);
|
|
const negBad = invariantLog.filter((r) => r.negatives > 0);
|
|
const unitBad = invariantLog.filter((r) => Object.keys(r.unitDrift).length);
|
|
const failedEvents = fin.failed;
|
|
const invariants = {
|
|
ok: !resBad.length && !negBad.length && !failedEvents && !unitBad.length,
|
|
resources: { ok: !resBad.length, worstDrift: Math.max(0, ...invariantLog.map((r) => r.worstDrift)), tolerance: fin.res.tolerance, final: fin.res.perResource, firstBad: resBad[0] || null },
|
|
units: { ok: !unitBad.length, trained: fin.trained, alive: fin.alive, lost: fin.lost, drift: fin.unitDrift, firstBad: unitBad[0] || null },
|
|
negativeStock: { ok: !negBad.length, firstBad: negBad[0] || null },
|
|
failedEvents: { n: failedEvents, sample: failedRows },
|
|
unexpectedErrors: { n: unexpected.length, sample: unexpected.slice(0, 10) },
|
|
serverLog: logs.slice(0, 10),
|
|
};
|
|
if (unexpected.length) invariants.ok = false;
|
|
if (logs.length) invariants.ok = false;
|
|
|
|
const meta = {
|
|
players: o.players, days: o.days, speed: o.speed, seed: o.seed, radius: o.radius, sessionsPerDay: o.sessionsPerDay || 'per archetype', alwaysOn: !!o.alwaysOn, joinDays: o.joinDays,
|
|
mix: Object.fromEntries(Object.entries(sim.bots.reduce((m, b) => { m[b.archetype] = (m[b.archetype] || 0) + 1; return m; }, {})).sort()),
|
|
set: o.set || [], worldId: world.id, endAt: now,
|
|
};
|
|
|
|
return {
|
|
meta, invariants, days, early,
|
|
final: { levels: Object.fromEntries(Object.keys(levels).map((k) => [k, { mean: round(mean(levels[k]), 2), hist: histogram(levels[k], 20) }])), fieldMin: percentiles(fieldMins, [10, 50, 90]),
|
|
pops: popsDist, footprint: percentiles(footprints, [10, 50, 90]), core10Days: percentiles(core10, [10, 50, 90]), core10Reached: core10.length, firstExpansionDays: percentiles(expand1, [10, 50, 90]), firstExpansionReached: expand1.length,
|
|
milestones: Object.fromEntries(Object.entries(milestones).map(([k, v]) => [k, { core10Days: percentiles(v.core10, [10, 50, 90]), firstExpansionDays: percentiles(v.exp1, [10, 50, 90]) }])),
|
|
unitsAlive: fin.alive, byArchetype: archTable, eventsFailed: evByStatus.failed || 0 },
|
|
storageWastePct: waste, combat, trade, alliances, endgame,
|
|
errors: { rows: errorRows, expectedTotal: errorRows.filter((r) => r.expected).reduce((s2, r) => s2 + r.n, 0), unexpectedTotal: errorRows.filter((r) => !r.expected).reduce((s2, r) => s2 + r.n, 0), commandCounts: counts },
|
|
botLogs: sim.bots.reduce((m, b) => { for (const k of Object.keys(b.log)) m[k] = (m[k] || 0) + b.log[k]; return m; }, {}),
|
|
perf,
|
|
};
|
|
}
|
|
|
|
module.exports = { build };
|