'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, }; } // ---- 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, 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 };