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