Upstream/tools/bot/sim.js
jvanvik 931f59e11e Phase 3: trade/end-game client, bot simulator, first balance pass
Built:
- Client: trade screen, world/end-game screen (landing sites, shipping, node build, final ranking), report sharing + alliance reports tab, alert describers for all notification types, error-code strings (en+nb) for every server CODE.
- tools/bot.js (+ tools/bot/): 50 headless bots against the real server in-process with a fake clock; resource/unit conservation ledger, exit 1 on broken invariants. docs/BALANCE.md has method and every run behind a number.
- Balance changes (evidence in docs/BALANCE.md): secondQueueCoreLevel 10 -> 3, TRANSPORT.capacityPerLink 500 -> 1000 (end game was shipping-limited in bot runs).
- Server fixes: POST /reports/:id/share 500 without a pending reportCleanup; bounced link shipments were silent about cargo overflow.

Tested: npm test 207/207 (new: bot determinism + invariants, client alerts/world/i18n over CODES, two regression tests that failed before their fix); openapi check up to date. Real-browser scratch runs (not in repo): WebSocket push shows an incoming-attack banner ~214 ms after launch, polling fallback ~52 s, 15 scripted flows, 71 screenshots.

NOT tested: Playwright suite (e2e/ still empty), real devices/Safari, nginx proxy websockets, throttled networks, HTTP-level bot load. NOT claimed: balance. Bots are not humans; open balance items (loot waste, escrow refund clipping, no garrisons at landing sites, end game not scaling with world speed) are listed in docs/BALANCE.md.

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

289 lines
16 KiB
JavaScript

'use strict';
/*
* The simulation: the real server in-process (createServer: in-memory SQLite, FakeClock, autoArm false), N bots playing through
* srv.commands.run (the function every REST route calls, so the same validation and transactions, without HTTP/JSON/scrypt
* overhead), the clock advanced stop by stop (next bot session / next sample), scheduler.runDue() settling every event that falls due.
* Called from tools/bot.js; returns a plain stats object (see report.js for how it is printed).
*/
const { performance } = require('perf_hooks');
const crypto = require('crypto');
const D = require('../../public/js/data.js');
const R = require('../../public/js/rules.js');
const { FakeClock } = require('../../server/clock');
const { HOUR, DAY, makeRng, percentiles, mean, round, bump, addInto } = require('./util');
const { Ledger, aliveUnits, zero } = require('./ledger');
const { Collector } = require('./collector');
const RES = D.RESOURCES;
const DEFAULT_MIX = { builder: 0.22, raider: 0.14, conqueror: 0.06, turtle: 0.12, expander: 0.16, trader: 0.12, alliancer: 0.18 };
// Error codes a sensible bot may run into now and then (rules it pre-checks can still change between check and send).
const EXPECTED = new Set(['not_enough_resources', 'queue_full', 'requirements_not_met', 'already_queued', 'target_protected', 'protection_active',
'footprint_too_low', 'no_transport_link', 'units_offline', 'invalid_target', 'trade_out_of_range', 'alliance_full', 'peering_office_required',
'max_level', 'offer_closed', 'own_offer', 'queue_full', 'out_of_range', 'cannot_takeover', 'diplomacy_invalid', 'diplomacy_pending', 'proposal_exists',
'proposal_expired', 'invite_expired', 'world_ended', 'already_in_alliance', 'capacity_exceeded', 'conflict', 'backbone_closed', 'not_enough_units', 'alliance_name_taken', 'alliance_tag_taken', 'forbidden']);
function parseMix(str, n) {
const weights = {};
if (str) {
for (const part of String(str).split(',')) { const [k, v] = part.split(':'); if (k) weights[k.trim()] = Number(v === undefined ? 1 : v); }
} else Object.assign(weights, DEFAULT_MIX);
const { ARCHETYPES } = require('./bots');
for (const k of Object.keys(weights)) if (!Object.prototype.hasOwnProperty.call(ARCHETYPES, k) || !(weights[k] >= 0)) throw new Error('--strategy-mix: unknown archetype or bad weight "' + k + '" (known: ' + Object.keys(ARCHETYPES).join(', ') + ')');
const names = Object.keys(weights).filter((k) => weights[k] > 0);
if (!names.length) throw new Error('--strategy-mix needs at least one positive weight');
const total = names.reduce((s, k) => s + weights[k], 0);
// largest remainder, so the counts add up to n
const exact = names.map((k) => ({ k, v: weights[k] / total * n }));
const counts = {};
let used = 0;
for (const e of exact) { counts[e.k] = Math.floor(e.v); used += counts[e.k]; }
exact.sort((a, b) => (b.v - Math.floor(b.v)) - (a.v - Math.floor(a.v)) || (a.k < b.k ? -1 : 1));
for (let i = 0; used < n; i++, used++) counts[exact[i % exact.length].k] += 1;
const list = [];
for (const k of names) for (let i = 0; i < counts[k]; i++) list.push(k);
return list;
}
async function runSim(o) {
const { createServer } = require('../../server');
const { Bot, ARCHETYPES } = require('./bots');
const makeView = require('./view');
const { createEndgame } = require('./endgame');
const logs = [];
const clock = new FakeClock(Date.UTC(2026, 0, 1));
const t0 = clock.now();
const srv = await createServer({
port: 0, host: '127.0.0.1', dbPath: ':memory:', clock, autoArm: false,
log: (m) => { if (/fail|error|exception/i.test(m)) { if (logs.length < 50) logs.push(m); } },
world: { seed: o.seed, speed: o.speed, mapRadius: o.radius, name: 'bot world' },
});
const db = srv.db;
const world = require('../../server/state').loadWorld(db, db.get('SELECT id FROM worlds LIMIT 1').id);
const ledger = new Ledger(db);
ledger.install();
const col = new Collector(db);
const view = makeView(db, world);
const wall0 = performance.now();
const sim = { potential: zero(), opts: o, srv, db, world, clock, t0, view, ledger, col, bots: [], alliancePlans: new Map(), call: null, endgame: null };
// ---------------------------------------------------------------- command wrapper (accounting, latency, errors)
const lat = {};
const counts = {};
const errors = {};
const unexpected = [];
let commandsRun = 0, commandsOk = 0;
let runDueMs = 0, eventsProcessed = 0;
sim.call = (bot, name, args) => {
const a = Object.assign({ userId: bot.userId }, args);
const s = performance.now();
commandsRun += 1;
try {
const out = srv.commands.run(name, a);
const ms = performance.now() - s;
(lat[name] || (lat[name] = [])).push(ms);
bump(counts, name); commandsOk += 1;
if (out && out.cost && (name === 'build' || name === 'train' || name === 'research')) {
ledger.sinkCost(out.cost);
const v = RES.reduce((sum, r) => sum + (out.cost[r] || 0), 0);
bot.spent[name] = (bot.spent[name] || 0) + v;
}
return { ok: true, out, args: a };
} catch (e) {
const ms = performance.now() - s;
(lat[name] || (lat[name] = [])).push(ms);
const code = e && e.code ? e.code : 'EXC:' + (e && e.message || e);
bump(errors, name + ':' + code);
if (process.env.BOT_DEBUG && (errors[name + ':' + code] <= 3)) console.error('DEBUG error', name, code, JSON.stringify(a).slice(0, 300), JSON.stringify(e.details || {}));
if (!(e && e.code && EXPECTED.has(e.code)) && unexpected.length < 30) unexpected.push({ command: name, code, message: String(e && e.message || e).slice(0, 200), details: e && e.details, stack: e && !e.code ? String(e.stack).split('\n').slice(0, 4).join(' | ') : undefined });
return { ok: false, code, error: e, args: a };
}
};
sim.endgame = createEndgame(sim);
// ---------------------------------------------------------------- the bots
const mix = parseMix(o.mix, o.players);
const order = mix.map((k, i) => ({ k, r: makeRng(o.seed, i, 0xa11)() })).sort((a, b) => a.r - b.r).map((x) => x.k); // seeded shuffle
const joinSpan = Math.max(0, o.joinDays) * DAY;
const pending = []; // bots waiting to join
for (let i = 0; i < o.players; i++) {
const jr = makeRng(o.seed, i, 0x70a);
pending.push({ idx: i, archetype: order[i], at: t0 + Math.floor(i * joinSpan / Math.max(1, o.players) + jr() * (joinSpan / Math.max(1, o.players)) * 0.9) });
}
const join = (p) => {
const name = 'bot' + p.idx;
const r = srv.commands.run('signup', { username: name, passHash: 'x', tokenHash: crypto.createHash('sha256').update('bot-token-' + o.seed + '-' + p.idx).digest('hex'), device: 'bot' });
const bot = new Bot(sim, p.idx, p.archetype, { userId: r.userId, operatorId: r.operatorId, name }, { sessionsPerDay: o.sessionsPerDay });
bot.joinedAt = clock.now();
bot.dayIdx = Math.floor((clock.now() - t0) / DAY);
bot.times = bot.sessionTimes(bot.dayIdx, t0).filter((t) => t >= clock.now());
bot.times.unshift(clock.now() + 1000); // a first look right after signing up
sim.bots.push(bot);
bot.nextAt = bot.times.shift();
if (o.alwaysOn) bot.nextAt = Infinity;
};
const advanceBot = (bot) => {
if (o.alwaysOn) { bot.nextAt = Infinity; return; }
while (!bot.times.length) {
bot.dayIdx += 1;
bot.times = bot.sessionTimes(bot.dayIdx, t0);
}
bot.nextAt = bot.times.shift();
};
// ---------------------------------------------------------------- sampling
const sampleEvery = Math.max(10, o.sampleMin) * 60 * 1000;
const days = [];
const dayAcc = new Map(); // day index -> accumulators of the periodic samples
const S = require('../../server/state');
const sample = (now) => {
const di = Math.floor((now - t0) / DAY);
let acc = dayAcc.get(di);
if (!acc) { acc = { fullBy: {}, n: 0, towns: 0, stock: zero(), prod: zero(), fill: 0, full: 0, qUtil: 0, qTowns: 0, pNet: 0, pNeg: 0, pEmpty: 0, unitTowns: 0, unitPowerBound: 0, unitOffline: 0, pDraw: 0, pOut: 0, capSum: 0, batFill: 0 }; dayAcc.set(di, acc); }
acc.n += 1;
const qs = new Map(db.all("SELECT town_id AS id, COUNT(*) AS n FROM events WHERE type = 'buildDone' AND status = 'pending' GROUP BY town_id").map((r) => [r.id, r.n]));
for (const row of db.all('SELECT * FROM towns ORDER BY id')) {
const t = S.townFromRow(db, row, world);
const cur = R.resourcesAt(t, now);
const cap = cur.caps.fiber;
acc.towns += 1;
for (const r of RES) { acc.stock[r] += cur.amounts[r]; acc.prod[r] += cur.rates[r]; }
const fills = RES.map((r) => cur.amounts[r] / cap);
acc.fill += mean(fills);
if (fills.some((f) => f >= 0.95)) acc.full += 1;
RES.forEach((r, i) => { if (fills[i] >= 0.95) acc.fullBy[r] = (acc.fullBy[r] || 0) + 1; sim.potential[r] += cur.rates[r] * sampleEvery / HOUR; });
const slots = R.queueSlots(t);
acc.qUtil += Math.min(1, (qs.get(row.id) || 0) / slots); acc.qTowns += 1;
acc.pNet += cur.rates.powerNet; acc.pDraw += cur.rates.powerDraw; acc.pOut += cur.rates.powerOut;
const hasUnits = t.unitDraw > 0;
if (cur.rates.powerNet < 0 && hasUnits) acc.pNeg += 1;
if (hasUnits) { acc.unitTowns += 1; if (cur.rates.powerNet < 3) acc.unitPowerBound += 1; if (cur.amounts.battery <= 1e-6 && cur.rates.powerNet < 0) acc.unitOffline += 1; }
}
};
const snapshot = (now, di) => {
const acc = dayAcc.get(di) || { n: 1, towns: 1, stock: zero(), prod: zero(), fill: 0, full: 0, qUtil: 0, qTowns: 1, pNet: 0, pNeg: 0, pEmpty: 0, unitTowns: 0, unitPowerBound: 0, unitOffline: 0, pDraw: 0, pOut: 0 };
const tn = Math.max(1, acc.towns);
const ops = db.all('SELECT id, alliance_id FROM operators ORDER BY id');
const fps = [], cores = [], fields = [], pops = [];
const gate = { ready: 0, n: 0 };
for (const op of ops) {
const towns = view.towns(op.id);
pops.push(towns.length);
if (!towns.length) continue;
let fp = 0;
for (const t of towns) fp += R.footprintAt(t, now);
fps.push(fp);
const cap = towns[0];
cores.push(view.level(cap, 'core_router'));
const fl = view.fieldLevels(cap).map((f) => f.level);
fields.push(mean(fl));
gate.n += 1;
if (R.footprintGate(towns.length, fp).ok) gate.ready += 1;
}
const units = aliveUnits(db);
const stockAvg = {}, prodAvg = {};
for (const r of RES) { stockAvg[r] = acc.stock[r] / tn; prodAvg[r] = acc.prod[r] / tn; }
days.push({
day: di + 1, operators: ops.length, towns: db.get('SELECT COUNT(*) AS n FROM towns').n,
stock: stockAvg, prodPerHour: prodAvg, fullBy: Object.fromEntries(RES.map((r) => [r, round((acc.fullBy[r] || 0) / tn * 100, 1)])), fillPct: acc.fill / tn * 100, fullPct: acc.full / tn * 100,
queueUtilPct: acc.qUtil / Math.max(1, acc.qTowns) * 100,
powerNet: acc.pNet / tn, powerDraw: acc.pDraw / tn, powerOut: acc.pOut / tn, powerNegPct: acc.unitTowns ? acc.pNeg / acc.unitTowns * 100 : 0,
unitTownsPct: acc.unitTowns / tn * 100, unitPowerBoundPct: acc.unitTowns ? acc.unitPowerBound / acc.unitTowns * 100 : 0, unitOfflinePct: acc.unitTowns ? acc.unitOffline / acc.unitTowns * 100 : 0,
fieldAvg: mean(fields), core: percentiles(cores, [10, 50, 90]), footprint: percentiles(fps, [10, 50, 90]),
popsMax: Math.max(0, ...pops), popsMean: mean(pops), multiPop: pops.filter((p) => p >= 2).length, gateReadyPct: gate.n ? gate.ready / gate.n * 100 : 0,
units, alliances: db.get('SELECT COUNT(*) AS n FROM alliances').n, allied: db.get('SELECT COUNT(*) AS n FROM operators WHERE alliance_id IS NOT NULL').n,
openOffers: db.get("SELECT COUNT(*) AS n FROM trade_offers WHERE status = 'open'").n, eventsPending: db.get("SELECT COUNT(*) AS n FROM events WHERE status = 'pending'").n,
});
};
// ---------------------------------------------------------------- invariants, checked at every day boundary
const invariantLog = [];
const checkpoint = (now, final) => {
col.ingest(ledger);
const res = ledger.checkResources(col);
const neg = ledger.negatives();
const failed = db.get("SELECT COUNT(*) AS n FROM events WHERE status = 'failed'").n;
const trained = {};
for (const r of db.all("SELECT json_extract(payload, '$.unit') AS u, SUM(json_extract(payload, '$.count')) AS n FROM events WHERE type = 'trainDone' AND status = 'done' GROUP BY u")) trained[r.u] = r.n;
const alive = aliveUnits(db);
const lost = addInto(addInto({}, col.unitsLost), col.unitsConsumed);
const unitDrift = {};
for (const k of new Set([...Object.keys(trained), ...Object.keys(alive), ...Object.keys(lost)])) {
const d = (alive[k] || 0) - ((trained[k] || 0) - (lost[k] || 0));
if (d !== 0) unitDrift[k] = d;
}
const rec = { day: (now - t0) / DAY, resourcesOk: res.ok, worstDrift: res.worstDrift, tolerance: res.tolerance, negatives: neg.length, failedEvents: failed, unitDrift };
invariantLog.push(rec);
if (final) rec.detail = res.perResource;
return { res, neg, failed, unitDrift, trained, alive, lost };
};
// ---------------------------------------------------------------- main loop
const end = t0 + o.days * DAY;
let nextSample = t0 + sampleEvery;
let nextDay = t0 + DAY;
let guard = 0;
while (clock.now() < end) {
// next stop
let next = end;
let kind = 'end', who = null;
if (pending.length && pending[0].at < next) { next = pending[0].at; kind = 'join'; }
for (const b of sim.bots) if (b.nextAt < next) { next = b.nextAt; kind = 'bot'; who = b; }
if (o.alwaysOn) {
const tick = Math.ceil((clock.now() + 1) / (o.stepMin * 60000)) * (o.stepMin * 60000);
if (tick < next) { next = tick; kind = 'tick'; }
}
if (o.endgameStepMin > 0 && sim.endgame.isOpen(clock.now())) {
const tick = Math.ceil((clock.now() + 1) / (o.endgameStepMin * 60000)) * (o.endgameStepMin * 60000);
if (tick < next) { next = tick; kind = 'egtick'; }
}
if (nextSample < next) { next = nextSample; kind = 'sample'; }
if (nextDay <= next) { next = nextDay; kind = 'day'; }
if (next < clock.now()) next = clock.now();
clock.set(next);
const r0 = performance.now();
eventsProcessed += srv.scheduler.runDue();
runDueMs += performance.now() - r0;
if (++guard > 5e6) throw new Error('simulation loop did not terminate');
if (kind === 'join') { join(pending.shift()); }
else if (kind === 'bot') {
if (db.get('SELECT status FROM worlds WHERE id = ?', world.id).status === 'ended') { advanceBot(who); continue; } // the world is over: nothing to play
who.session(next);
const cont = who.continuation(next);
if (cont !== null) who.nextAt = cont; else advanceBot(who);
}
else if (kind === 'egtick') { for (const b of sim.bots) { if (sim.alliancePlans.has(b.op && b.op.alliance_id) || (b.op && b.op.alliance_id)) { b.session(next); } } }
else if (kind === 'tick') { for (const b of sim.bots) { b.session(next); } }
else if (kind === 'sample') {
sample(next); nextSample += sampleEvery;
if (process.env.BOT_DEBUG_DRIFT) {
col.ingest(ledger);
const c = ledger.checkResources(col);
const d = RES.map((r) => c.perResource[r].drift.toFixed(2)).join('/');
if (d !== sim.lastDrift) { console.error('drift at day ' + ((next - t0) / DAY).toFixed(3) + ' = ' + d); sim.lastDrift = d; }
}
}
else if (kind === 'day') {
const di = Math.floor((next - t0) / DAY) - 1;
if (!dayAcc.has(di)) sample(next - 1);
checkpoint(next, false);
snapshot(next, di);
nextDay += DAY;
if (o.progress) o.progress(di + 1, o.days);
if (o.onDay) o.onDay(sim, di + 1); // test hook: lets a test tamper with the world to prove the invariants notice
}
}
srv.scheduler.runDue();
const fin = checkpoint(clock.now(), true);
const wallMs = performance.now() - wall0;
const stats = require('./stats').build({ sim, o, days, ledger, col, fin, invariantLog, lat, counts, errors, unexpected, commandsRun, commandsOk, runDueMs, eventsProcessed, wallMs, logs, EXPECTED, mix: order });
await srv.close();
return stats;
}
module.exports = { runSim, parseMix, DEFAULT_MIX, EXPECTED };