sim
This commit is contained in:
parent
746d218eda
commit
e590baf28d
44 changed files with 8308 additions and 0 deletions
185
sim/src/stats.ts
Normal file
185
sim/src/stats.ts
Normal file
|
|
@ -0,0 +1,185 @@
|
|||
import type { CardPool } from './cards.ts';
|
||||
import { gridCount } from './engine.ts';
|
||||
import { STAT_KEYS, type CardUse, type GameState, type StatKey } from './types.ts';
|
||||
|
||||
export interface GameRecord {
|
||||
seed: number;
|
||||
winner: 0 | 1 | null;
|
||||
firstPlayer: 0 | 1;
|
||||
turns: number;
|
||||
locations: [number, number];
|
||||
stats: Record<StatKey, [number, number]>;
|
||||
cardUse: Record<string, CardUse>;
|
||||
/** Card names in each player's decks (Avatars, Servers and main deck). */
|
||||
decks: [string[], string[]];
|
||||
}
|
||||
|
||||
export interface BatchSummary {
|
||||
games: number;
|
||||
wins: [number, number];
|
||||
draws: number;
|
||||
firstPlayerWins: number;
|
||||
rounds: { mean: number; median: number; p10: number; p90: number };
|
||||
/** Average per game, per player. */
|
||||
perGame: Record<StatKey, [number, number]>;
|
||||
locations: [number, number];
|
||||
}
|
||||
|
||||
export function recordOf(seed: number, s: GameState): GameRecord {
|
||||
return {
|
||||
seed,
|
||||
winner: s.winner,
|
||||
firstPlayer: s.firstPlayer,
|
||||
turns: s.turn,
|
||||
locations: [gridCount(s, 0), gridCount(s, 1)],
|
||||
stats: s.stats,
|
||||
cardUse: s.cardUse,
|
||||
decks: [0, 1].map((p) => [...new Set(s.cards.filter((c) => c.owner === p).map((c) => c.name))]) as [string[], string[]],
|
||||
};
|
||||
}
|
||||
|
||||
/** Rounds: both players taking one turn each. */
|
||||
const roundsOf = (r: GameRecord) => Math.ceil(r.turns / 2);
|
||||
|
||||
export function summarize(records: GameRecord[]): BatchSummary {
|
||||
const n = records.length;
|
||||
const avg = (f: (r: GameRecord) => number) => records.reduce((t, r) => t + f(r), 0) / n;
|
||||
const rounds = records.map(roundsOf).sort((a, b) => a - b);
|
||||
const at = (q: number) => rounds[Math.min(n - 1, Math.floor(q * n))];
|
||||
return {
|
||||
games: n,
|
||||
wins: [records.filter((r) => r.winner === 0).length, records.filter((r) => r.winner === 1).length],
|
||||
draws: records.filter((r) => r.winner === null).length,
|
||||
firstPlayerWins: records.filter((r) => r.winner !== null && r.winner === r.firstPlayer).length,
|
||||
rounds: { mean: avg(roundsOf), median: at(0.5), p10: at(0.1), p90: at(0.9) },
|
||||
perGame: Object.fromEntries(
|
||||
STAT_KEYS.map((k) => [k, [avg((r) => r.stats[k][0]), avg((r) => r.stats[k][1])]]),
|
||||
) as Record<StatKey, [number, number]>,
|
||||
locations: [avg((r) => r.locations[0]), avg((r) => r.locations[1])],
|
||||
};
|
||||
}
|
||||
|
||||
const pct = (x: number, of: number) => (of === 0 ? '—' : `${((100 * x) / of).toFixed(1)}%`);
|
||||
const fmt = (x: number) => x.toFixed(1);
|
||||
|
||||
export function formatSummary(sum: BatchSummary, labels: [string, string]): string {
|
||||
const g = sum.perGame;
|
||||
const decided = sum.wins[0] + sum.wins[1];
|
||||
const width = Math.max(9, ...labels.map((l) => l.length + 2));
|
||||
const row = (label: string, south: string, north: string) =>
|
||||
` ${label.padEnd(34)}${south.padStart(width)}${north.padStart(width)}`;
|
||||
const both = (label: string, k: StatKey) => row(label, fmt(g[k][0]), fmt(g[k][1]));
|
||||
const combats = g.attacks[0] + g.attacks[1];
|
||||
const enemyCaptures =
|
||||
g.capturesUndefended[0] + g.capturesUndefended[1] + g.capturesInCombat[0] + g.capturesInCombat[1];
|
||||
return [
|
||||
'Results',
|
||||
` ${labels[0]} wins`.padEnd(36) + pct(sum.wins[0], sum.games),
|
||||
` ${labels[1]} wins`.padEnd(36) + pct(sum.wins[1], sum.games),
|
||||
' Draws (turn limit)'.padEnd(36) + pct(sum.draws, sum.games),
|
||||
' First player wins'.padEnd(36) + `${pct(sum.firstPlayerWins, decided)} of decided games`,
|
||||
'',
|
||||
'Game length, in rounds (both players take a turn)',
|
||||
` mean ${fmt(sum.rounds.mean)} · median ${sum.rounds.median} · 10% of games ≤ ${sum.rounds.p10} · 90% ≤ ${sum.rounds.p90}`,
|
||||
'',
|
||||
row('Average per player, per game', labels[0], labels[1]),
|
||||
both('Captures: unowned', 'capturesUnowned'),
|
||||
both('Captures: undefended enemy', 'capturesUndefended'),
|
||||
both('Captures: won in combat', 'capturesInCombat'),
|
||||
both('Attacks', 'attacks'),
|
||||
row('Attack win rate', pct(g.attacksWon[0], g.attacks[0]), pct(g.attacksWon[1], g.attacks[1])),
|
||||
both('Firewalls placed', 'firewallsPlaced'),
|
||||
both('Firewalls upgraded', 'firewallsUpgraded'),
|
||||
both('Firewalls cleared', 'firewallsCleared'),
|
||||
both('RAM from captures', 'ramFromCaptures'),
|
||||
both('RAM from salvage', 'ramFromSalvage'),
|
||||
both('RAM spent on Firewalls', 'ramOnFirewalls'),
|
||||
both('RAM spent on cards', 'ramOnCards'),
|
||||
both('Cards salvaged', 'cardsSalvaged'),
|
||||
both('Hardware installed', 'hardwareInstalled'),
|
||||
both('Hardware lost in combat', 'hardwareLost'),
|
||||
both('Programs installed', 'programsInstalled'),
|
||||
both('Programs lost', 'programsLost'),
|
||||
both('Executions played', 'executionsPlayed'),
|
||||
both('Abilities used', 'abilitiesUsed'),
|
||||
both('Combats ended early', 'combatsEnded'),
|
||||
both('RAM from card effects', 'ramFromEffects'),
|
||||
both('RAM spent on Abilities', 'ramOnAbilities'),
|
||||
both('Cards drawn', 'cardsDrawn'),
|
||||
row('Locations at game end', fmt(sum.locations[0]), fmt(sum.locations[1])),
|
||||
'',
|
||||
`Fights: ${fmt(combats)} combats per game; ${pct(g.capturesInCombat[0] + g.capturesInCombat[1], enemyCaptures)} of enemy locations taken were won in combat.`,
|
||||
].join('\n');
|
||||
}
|
||||
|
||||
export interface CardReport {
|
||||
name: string;
|
||||
kind: string;
|
||||
/** Player-games with the card in the deck, and that player's win rate. */
|
||||
inDeck: number;
|
||||
deckWinRate: number;
|
||||
/** Share of those player-games where it was played, used, or (Servers) placed. */
|
||||
playedShare: number;
|
||||
/** Plays and fires per player-game where it was played. */
|
||||
plays: number;
|
||||
fired: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* How each card did. Random decks make "win rate in deck" a fair measure of card strength: the
|
||||
* card's presence is the only thing that differs on average. "Played" numbers show how the bots
|
||||
* used it.
|
||||
*/
|
||||
export function cardReport(pool: CardPool, records: GameRecord[]): CardReport[] {
|
||||
return pool.all.map((card) => {
|
||||
let inDeck = 0;
|
||||
let wins = 0;
|
||||
let playedGames = 0;
|
||||
let plays = 0;
|
||||
let fired = 0;
|
||||
for (const r of records) {
|
||||
const use = r.cardUse[card.name];
|
||||
for (const p of [0, 1] as const) {
|
||||
if (!r.decks[p].includes(card.name)) continue;
|
||||
inDeck++;
|
||||
if (r.winner === p) wins++;
|
||||
if (!use || (use.played[p] === 0 && use.fired[p] === 0)) continue;
|
||||
playedGames++;
|
||||
plays += use.played[p];
|
||||
fired += use.fired[p];
|
||||
}
|
||||
}
|
||||
return {
|
||||
name: card.name,
|
||||
kind: card.kind,
|
||||
inDeck,
|
||||
deckWinRate: inDeck ? wins / inDeck : 0,
|
||||
playedShare: inDeck ? playedGames / inDeck : 0,
|
||||
plays: playedGames ? plays / playedGames : 0,
|
||||
fired: playedGames ? fired / playedGames : 0,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
export function formatCardReport(rows: CardReport[]): string {
|
||||
const pctOf = (x: number) => `${(100 * x).toFixed(1)}%`;
|
||||
const out = [
|
||||
'Cards. Win rate is for the player whose deck held the card (± is one standard error).',
|
||||
'Played is how often that player got it into play; plays and fired are per game where it was played.',
|
||||
'',
|
||||
` ${'Card'.padEnd(28)}${'decks'.padStart(7)}${'win rate'.padStart(10)}${'±'.padStart(6)}${'played'.padStart(9)}${'plays'.padStart(7)}${'fired'.padStart(7)}`,
|
||||
];
|
||||
let kind = '';
|
||||
for (const r of rows) {
|
||||
if (r.kind !== kind) {
|
||||
kind = r.kind;
|
||||
out.push(` ${{ avatar: 'Avatars', server: 'Servers', program: 'Programs', hardware: 'Hardware', execution: 'Executions' }[kind] ?? kind}`);
|
||||
}
|
||||
const se = r.inDeck ? Math.sqrt((r.deckWinRate * (1 - r.deckWinRate)) / r.inDeck) : 0;
|
||||
out.push(
|
||||
` ${r.name.padEnd(26)}${String(r.inDeck).padStart(7)}${(r.inDeck ? pctOf(r.deckWinRate) : '—').padStart(10)}` +
|
||||
`${(r.inDeck ? (100 * se).toFixed(1) : '').padStart(6)}${pctOf(r.playedShare).padStart(9)}${r.plays.toFixed(2).padStart(7)}${r.fired.toFixed(2).padStart(7)}`,
|
||||
);
|
||||
}
|
||||
return out.join('\n');
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue