forked from WikiDeck/wikideck
Not a PR yet: needs #72 (contracts) and the engine alpha to merge first. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
52 lines
3.1 KiB
TypeScript
52 lines
3.1 KiB
TypeScript
import { test } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { existsSync } from 'node:fs';
|
|
import { fileURLToPath } from 'node:url';
|
|
import { mulberry32 } from '../shared/bots/index.ts';
|
|
import { makeDuelBot, DUEL_BOT_NAMES } from '../shared/bots/duel-bots.ts';
|
|
import { deckSet } from '../scripts/sim/duel-decks.ts';
|
|
import { playDuel } from '../scripts/sim/duel-run.ts';
|
|
|
|
const hasEngine = existsSync(new URL('../shared/duel/engine.ts', import.meta.url));
|
|
const SAMPLE = fileURLToPath(new URL('../docs/game/card-sample.json', import.meta.url));
|
|
|
|
test('duel adapter: bots play legal actions on shared/duel and matches end', { skip: !hasEngine && 'shared/duel/engine.ts not merged yet' }, async () => {
|
|
const { makeDuelSim } = await import('../scripts/sim/duel-core-sim.ts');
|
|
const sim = await makeDuelSim(SAMPLE);
|
|
const decks = deckSet(sim.pool, 4, 3);
|
|
assert.equal(decks[0].cards.length, 40);
|
|
for (const name of DUEL_BOT_NAMES) {
|
|
const bot = makeDuelBot(name, sim.engine, sim.lens);
|
|
let s = sim.engine.createMatch('duel', [{ id: 'a', tags: [], payload: sim.deckPayload(decks[0].cards) }, { id: 'b', tags: [], payload: sim.deckPayload(decks[1].cards) }], 9);
|
|
const rng = mulberry32(4);
|
|
for (let i = 0; i < (name.startsWith('mc') ? 150 : 1500) && !sim.engine.isOver(s); i++) {
|
|
const seat = sim.engine.toAct(s);
|
|
const legal = sim.engine.legalActions(s, seat);
|
|
const a = bot(sim.engine.observe!(s, seat), seat, rng);
|
|
assert.ok(legal.some(x => JSON.stringify(x) === JSON.stringify(a)), `${name} chose an illegal action`);
|
|
s = sim.engine.applyAction(s, a);
|
|
}
|
|
}
|
|
const r1 = playDuel(sim, [decks[2], decks[3]], ['heuristic', 'heuristic'], 21);
|
|
const r2 = playDuel(sim, [decks[2], decks[3]], ['heuristic', 'heuristic'], 21);
|
|
assert.equal(r1.truncated, false);
|
|
assert.deepEqual({ ...r1, ms: 0 }, { ...r2, ms: 0 });
|
|
});
|
|
|
|
test('duel adapter: determinization keeps the visible state and the view', { skip: !hasEngine && 'shared/duel/engine.ts not merged yet' }, async () => {
|
|
const { makeDuelSim } = await import('../scripts/sim/duel-core-sim.ts');
|
|
const sim = await makeDuelSim(SAMPLE);
|
|
const decks = deckSet(sim.pool, 2, 8);
|
|
let s = sim.engine.createMatch('duel', [{ id: 'a', tags: [], payload: sim.deckPayload(decks[0].cards) }, { id: 'b', tags: [], payload: sim.deckPayload(decks[1].cards) }], 5);
|
|
const bot = makeDuelBot('heuristic', sim.engine, sim.lens);
|
|
const rng = mulberry32(1);
|
|
for (let i = 0; i < 120 && !sim.engine.isOver(s); i++) { const seat = sim.engine.toAct(s); s = sim.engine.applyAction(s, bot(sim.engine.observe!(s, seat), seat, rng)); }
|
|
const seat = sim.engine.toAct(s);
|
|
const obs = sim.engine.observe!(s, seat) as any;
|
|
const guess = sim.engine.assume!(obs, seat, rng) as any;
|
|
assert.equal(guess.players[seat].hand.length, s.players[seat].hand.length);
|
|
assert.equal(guess.players[1 - seat].hand.length, s.players[1 - seat].hand.length);
|
|
assert.equal(guess.players[seat].library.length, s.players[seat].library.length);
|
|
assert.equal(sim.engine.evaluate(guess, seat).toFixed(6), sim.engine.evaluate(s, seat).toFixed(6), 'material eval sees only public info');
|
|
});
|