Files
wikideck/scripts/sim/duel-standin-adapter.ts

77 lines
4.3 KiB
TypeScript

// EngineAdapter + DuelLens over the sim stand-in duel engine (scripts/sim/duel-standin.ts).
import type { DeckSpec, EngineAdapter, Seat } from '../../shared/bots/types.ts';
import { duelEvaluate, type DuelLens, type LensAction } from '../../shared/bots/duel-bots.ts';
import { existsSync } from 'node:fs';
import { applyAction, createDuel, fromDuelCards, isCreature, legalActions, pow, tough, viewFor, deriveSample, STANDIN_RULES, type SAction, type SCard, type SRules, type SState } from './duel-standin.ts';
import { mulberry32 } from '../../shared/bots/index.ts';
import type { DuelSim } from './duel-sim-types.ts';
export const standinLens: DuelLens<SState, SAction> = {
phase: s => s.phase,
life: (s, p) => s.players[p].life,
handSize: (s, p) => (s as any).view && (s as any).view.seat !== p ? (s as any).view.oppHand : s.players[p].hand.length,
creatures: (s, p) => s.players[p].board.filter(isCreature).map(b => ({
uid: String(b.uid), power: pow(b), toughness: Math.max(0, tough(b) - b.damage), tapped: b.tapped,
canAttack: b.card.type === 'Figure' && !b.tapped && !b.sick, canBlock: !b.tapped,
value: pow(b) + tough(b) * 0.8 + (b.card.type === 'Figure' ? 0.5 : 0),
})),
attackers: s => s.attackers.map(String),
blocks: s => s.blocks.map(([b, a]) => [String(b), String(a)]),
decode: (s, a): LensAction => {
switch (a.type) {
case 'play': { const c = s.players[a.seat].hand.find(h => h.id === a.card); return { kind: 'play', cost: c?.cost ?? 0, cardType: c?.type ?? '?' }; }
case 'toggleAttacker': return { kind: 'attack', uid: String(a.uid), on: !s.attackers.includes(a.uid) };
case 'confirmAttack': return { kind: 'confirmAttack' };
case 'block': return { kind: 'block', blocker: String(a.blocker), attacker: String(a.attacker) };
case 'confirmBlocks': return { kind: 'confirmBlocks' };
case 'endPhase': return { kind: 'endMain' };
}
},
};
export function makeStandinSim(poolFile: string, rules: Partial<SRules> = {}): DuelSim<SState, SAction, SCard> {
const fixture = poolFile.replace(/card-sample\.json$/, 'duel-card-sample.json');
const pool = process.env.SIM_STANDIN_POOL !== 'own' && existsSync(fixture) ? fromDuelCards(fixture) : deriveSample(poolFile);
const R = { ...STANDIN_RULES, ...rules };
const engine: EngineAdapter<SState, SAction> = {
name: 'duel-standin',
rulesets: () => ['standin'],
decks: () => [],
createMatch: (_r, decks, seed) => createDuel([decks[0].payload as SCard[], decks[1].payload as SCard[]], seed, R),
toAct: s => (s.phase === 'block' ? (1 - s.active) : s.active) as Seat,
legalActions: (s, seat) => legalActions(s, seat),
applyAction: (s, a) => applyAction(s, a),
isOver: s => s.over,
result: s => ({ winner: s.winner, reason: s.reason }),
turn: s => s.turn,
evaluate: (s, seat) => duelEvaluate(standinLens, s, seat),
cardsOf: (s, a) => (a.type === 'play' ? [a.card] : []),
actionKind: a => a.type,
firstSeat: s => s.first,
observe: (s, seat) => viewFor(s, seat),
assume: (obs, seat, rng) => {
// Refill hidden zones from the unseen part of the pool (a determinization).
const v = structuredClone(obs) as SState & { view: { oppHand: number; oppLib: number; myLib: number } };
const seen = new Set<string>();
for (const p of v.players) for (const z of [p.hand, p.grave, p.board.map(b => b.card)]) for (const c of z) seen.add(c.id);
const unseen = pool.filter(c => !seen.has(c.id));
const r = mulberry32(Math.floor(rng() * 2 ** 31));
for (let i = unseen.length - 1; i > 0; i--) { const j = Math.floor(r() * (i + 1)); [unseen[i], unseen[j]] = [unseen[j], unseen[i]]; }
let k = 0;
const take = (n: number) => { const out: SCard[] = []; for (let i = 0; i < n; i++) out.push(unseen[k++ % Math.max(1, unseen.length)]); return out; };
const o = 1 - seat;
v.players[o].hand = take(v.view.oppHand);
v.players[o].library = take(v.view.oppLib);
v.players[seat].library = take(v.view.myLib);
delete (v as any).view;
return v;
},
};
return {
name: 'standin', engine, lens: standinLens, pool,
deckPayload: cards => cards,
metrics: s => ({ connectedFires: s.stats.connectedFires, plays: s.stats.plays, lifeLeft: s.winner === null ? 0 : s.players[s.winner].life }),
connected: (a, b) => a.connect.some((k: string) => b.connect.includes(k)),
};
}