103 lines
4.6 KiB
TypeScript
103 lines
4.6 KiB
TypeScript
/** What the app needs from the sim: action labels, private log lines, Cores breakdowns. */
|
|
import assert from 'node:assert/strict';
|
|
import { test } from 'node:test';
|
|
import type { Bot } from '../src/bots/bot.ts';
|
|
import { greedyBot } from '../src/bots/greedy.ts';
|
|
import { randomBot } from '../src/bots/random.ts';
|
|
import { randomDeck } from '../src/deck.ts';
|
|
import { describeAction } from '../src/describe.ts';
|
|
import { actionKey, applyAction, createGame, decider, legalActions, viewFor } from '../src/engine.ts';
|
|
import { coresOf, fighterOf, type CorePart } from '../src/query.ts';
|
|
import { loadCards } from '../src/load.ts';
|
|
import { makeRng } from '../src/rng.ts';
|
|
import { DEFAULT_RULES, withRule, type RulesConfig } from '../src/rules.ts';
|
|
import { fightersOf, logFor, nameOf, programLocation, wearerOf } from '../src/state.ts';
|
|
import type { Action, GameState } from '../src/types.ts';
|
|
|
|
const pool = loadCards();
|
|
|
|
/** Plays a game, calling `each` at every decision with the legal actions. */
|
|
function play(seed: number, bots: [Bot, Bot], each: (s: GameState, legal: Action[]) => void, rules: RulesConfig = DEFAULT_RULES): GameState {
|
|
const rng = makeRng(seed ^ 0x9e3779b9);
|
|
const s = createGame({ pool, decks: [randomDeck(pool, rng, rules), randomDeck(pool, rng, rules)], seed, rules, log: true });
|
|
for (let n = 0; !s.over && n < 50_000; n++) {
|
|
const p = decider(s)!;
|
|
const legal = legalActions(s);
|
|
each(s, legal);
|
|
applyAction(s, bots[p].choose(viewFor(s, p), legal, p), legal);
|
|
}
|
|
return s;
|
|
}
|
|
|
|
/**
|
|
* Two actions are the same move when they differ only in which of two same-named cards in the same
|
|
* place they use: salvaging either copy in hand, swapping out either of two identical Hardware.
|
|
*/
|
|
function sameMove(s: GameState, a: Action, b: Action): boolean {
|
|
const where = (id: number) => {
|
|
const wearer = wearerOf(s, id);
|
|
return `${nameOf(s, id)}@${programLocation(s, id) ?? (wearer ? `${wearer.player}:${wearer.slot}` : zoneOf(s, id))}`;
|
|
};
|
|
const canon = (x: Action) =>
|
|
JSON.stringify(x, (k, v) => (['card', 'cards'].includes(k) ? (Array.isArray(v) ? v.map(where) : typeof v === 'number' ? where(v) : v) : v));
|
|
return canon(a) === canon(b);
|
|
}
|
|
|
|
const zoneOf = (s: GameState, id: number): string => {
|
|
const pl = s.players[s.cards[id].owner];
|
|
return pl.hand.includes(id) ? 'hand' : pl.discard.includes(id) ? 'discard' : 'elsewhere';
|
|
};
|
|
|
|
function checkLabels(s: GameState, legal: Action[]): void {
|
|
const seen = new Map<string, Action>();
|
|
for (const a of legal) {
|
|
const label = describeAction(s, a);
|
|
assert.ok(label.length > 0, `empty label for ${actionKey(a)}`);
|
|
assert.ok(!label.includes('undefined') && !label.includes('NaN'), `bad label "${label}" for ${actionKey(a)}`);
|
|
const prev = seen.get(label);
|
|
if (prev && !sameMove(s, prev, a)) assert.fail(`"${label}" labels both ${actionKey(prev)} and ${actionKey(a)} (turn ${s.turn})`);
|
|
seen.set(label, a);
|
|
}
|
|
}
|
|
|
|
test('every legal action gets a label, and no two moves in a decision share one', () => {
|
|
for (let seed = 1; seed <= 60; seed++) play(seed, [randomBot(seed * 2), randomBot(seed * 2 + 1)], checkLabels);
|
|
for (let seed = 1; seed <= 30; seed++) play(seed, [greedyBot(), greedyBot()], checkLabels);
|
|
for (const spec of ['recruit.on=true', 'buyAnomalies.on=true', 'commands.on=false']) {
|
|
const rules = withRule(DEFAULT_RULES, spec);
|
|
for (let seed = 1; seed <= 10; seed++) play(seed, [randomBot(seed * 2), randomBot(seed * 2 + 1)], checkLabels, rules);
|
|
}
|
|
});
|
|
|
|
test("a player's log never names the other player's secret choices", () => {
|
|
let secrets = 0;
|
|
for (let seed = 1; seed <= 80; seed++) {
|
|
const s = play(seed, [greedyBot(), greedyBot()], () => {});
|
|
for (const l of s.log!) if (l.to !== undefined) secrets++;
|
|
for (const p of [0, 1] as const) {
|
|
const mine = logFor(s, p);
|
|
s.log!.forEach((l, i) => {
|
|
if (l.to === undefined) assert.equal(mine[i], l.text);
|
|
else assert.equal(mine[i], l.to === p ? l.text : l.public);
|
|
});
|
|
}
|
|
}
|
|
assert.ok(secrets > 0, 'no secret lines came up; the check tested nothing');
|
|
});
|
|
|
|
test('a Cores breakdown adds up to the total', () => {
|
|
let combats = 0;
|
|
for (let seed = 1; seed <= 20; seed++) {
|
|
play(seed, [greedyBot(), greedyBot()], (s) => {
|
|
if (!s.combat || s.combat.ended) return;
|
|
combats++;
|
|
for (const r of fightersOf(s.combat)) {
|
|
const parts: CorePart[] = [];
|
|
const total = coresOf(s, fighterOf(s, r), undefined, parts);
|
|
assert.equal(parts[0].kind, 'anomaly');
|
|
assert.equal(Math.max(1, parts.reduce((t, x) => t + x.cores, 0)), total);
|
|
}
|
|
});
|
|
}
|
|
assert.ok(combats > 50);
|
|
});
|