Upstream/tools/bot/util.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

61 lines
2.5 KiB
JavaScript

'use strict';
/* Small helpers for the bot harness: seeded randomness, statistics, formatting. No game logic here. */
const R = require('../../public/js/rules.js');
const HOUR = 3600 * 1000;
const DAY = 24 * HOUR;
// A seeded generator with helpers. Everything a bot decides comes from one of these (never Math.random).
function makeRng(seed, a, b) {
const next = R.rng(R.hash(seed | 0, a | 0, b | 0));
const r = () => next();
r.int = (lo, hi) => lo + Math.floor(next() * (hi - lo + 1)); // inclusive
r.pick = (list) => list[Math.floor(next() * list.length)];
r.chance = (p) => next() < p;
r.range = (lo, hi) => lo + next() * (hi - lo);
return r;
}
function pct(sorted, p) {
if (!sorted.length) return 0;
const i = Math.min(sorted.length - 1, Math.max(0, Math.round((p / 100) * (sorted.length - 1))));
return sorted[i];
}
function percentiles(values, ps) {
const s = values.filter((v) => Number.isFinite(v)).sort((a, b) => a - b);
const out = {};
for (const p of ps || [10, 50, 90]) out['p' + p] = s.length ? pct(s, p) : null;
out.n = s.length;
return out;
}
const mean = (a) => (a.length ? a.reduce((s, v) => s + v, 0) / a.length : 0);
const sum = (a) => a.reduce((s, v) => s + v, 0);
const round = (v, d) => { const k = Math.pow(10, d === undefined ? 1 : d); return Math.round(v * k) / k; };
// Seeded string key for a stable iteration order.
const byId = (a, b) => a.id - b.id;
function fmt(v, d) {
if (v === null || v === undefined || Number.isNaN(v)) return '-';
if (typeof v !== 'number') return String(v);
const a = Math.abs(v);
if (d !== undefined) return v.toFixed(d);
if (a >= 1e6) return (v / 1e6).toFixed(2) + 'M';
if (a >= 1e4) return (v / 1e3).toFixed(1) + 'k';
if (a >= 100) return String(Math.round(v));
if (a >= 10) return v.toFixed(1);
return v.toFixed(2);
}
function table(rows, header) {
const all = header ? [header].concat(rows) : rows;
const w = [];
for (const r of all) r.forEach((c, i) => { w[i] = Math.max(w[i] || 0, String(c).length); });
return all.map((r) => r.map((c, i) => (i === 0 ? String(c).padEnd(w[i]) : String(c).padStart(w[i]))).join(' ')).join('\n');
}
// Add `n` to obj[key] (counter maps).
const bump = (obj, key, n) => { obj[key] = (obj[key] || 0) + (n === undefined ? 1 : n); };
const addInto = (a, b) => { for (const k of Object.keys(b || {})) a[k] = (a[k] || 0) + b[k]; return a; };
module.exports = { HOUR, DAY, makeRng, pct, percentiles, mean, sum, round, byId, fmt, table, bump, addInto };