forked from WikiDeck/wikideck
69 lines
3.6 KiB
TypeScript
69 lines
3.6 KiB
TypeScript
// DuelSim binding for duel-core's shared/duel engine, with the 236 sample cards derived by duel-cards.
|
|
import { readFileSync, existsSync } from 'node:fs';
|
|
import { fileURLToPath } from 'node:url';
|
|
import type { DuelCard, DuelEngineApi, DuelRuleset } from '../../shared/duel/index.ts';
|
|
import { connectedTierOf, duelMetrics, makeDuelAdapter } from '../../shared/bots/duel-adapter.ts';
|
|
import type { DuelSim } from './duel-sim-types.ts';
|
|
import type { PoolCard } from './duel-decks.ts';
|
|
|
|
export interface DuelPoolCard extends PoolCard { title: string; card: DuelCard }
|
|
|
|
const FIXTURE = fileURLToPath(new URL('../../docs/game/duel-card-sample.json', import.meta.url));
|
|
|
|
/** The duel engine's functions, whether engine.ts exports them one by one or as an object. */
|
|
async function loadApi(): Promise<DuelEngineApi> {
|
|
const mod = '../../shared/duel/engine.ts';
|
|
const m = await import(mod) as Record<string, any>;
|
|
const obj = Object.values(m).find(v => v && typeof v === 'object' && typeof v.createDuel === 'function' && typeof v.statsOf === 'function');
|
|
return (obj ?? m) as DuelEngineApi;
|
|
}
|
|
|
|
async function loadCards(sampleFile: string): Promise<DuelCard[]> {
|
|
try {
|
|
const mod = '../../shared/duel/sample.ts';
|
|
const { deriveSample } = await import(mod) as { deriveSample: (c: unknown[], r?: unknown) => DuelCard[] };
|
|
const raw = JSON.parse(readFileSync(sampleFile, 'utf8')).cards;
|
|
// SIM_DERIVE: JSON patch over duel-cards' DERIVE_RULES (data-only derivation tuning).
|
|
if (!process.env.SIM_DERIVE) return deriveSample(raw);
|
|
const d = await import('../../shared/duel/derive.ts' as string) as { DERIVE_RULES: Record<string, unknown> };
|
|
return deriveSample(raw, { ...d.DERIVE_RULES, ...JSON.parse(process.env.SIM_DERIVE) });
|
|
} catch (e) {
|
|
if (process.env.SIM_DERIVE || !existsSync(FIXTURE)) throw e;
|
|
return JSON.parse(readFileSync(FIXTURE, 'utf8')).cards as DuelCard[];
|
|
}
|
|
}
|
|
|
|
export async function makeDuelSim(sampleFile: string, overrides: Record<string, unknown> = {}): Promise<DuelSim<any, any, DuelPoolCard>> {
|
|
const api = await loadApi();
|
|
const rs = await import('../../shared/duel/ruleset.ts') as { mergeDuelRuleset: (o: unknown) => DuelRuleset };
|
|
const rules = rs.mergeDuelRuleset(overrides);
|
|
let cards = await loadCards(sampleFile);
|
|
// SIM_LOCATION_DRAW=n: experiment for duel-design's T2 rider ("When this enters, draw n cards") on every Location.
|
|
const locDraw = Number(process.env.SIM_LOCATION_DRAW ?? 0);
|
|
if (locDraw > 0) cards = cards.map(c => (c.type === 'Location' ? { ...c, effects: [...c.effects, { trigger: 'onEnter', op: 'draw', amount: locDraw, target: 'controller' } as DuelCard['effects'][number]] } : c));
|
|
const pool: DuelPoolCard[] = cards.map(c => ({
|
|
id: c.cardId, title: c.title, type: c.type, portal: c.portal, cost: c.cost, keywords: [...c.keywords],
|
|
fame: (c as any).basis?.fame, card: c,
|
|
}));
|
|
const engine = makeDuelAdapter(api, { rules, pool: cards });
|
|
// SIM_SECOND_EXTRA=n: experiment for a proposed ruleset key. The second seat starts with n extra cards
|
|
// (moved from its library top before mulligans). Sim-only; the engine has no such key yet.
|
|
const extra = Number(process.env.SIM_SECOND_EXTRA ?? 0);
|
|
if (extra > 0) {
|
|
const create = engine.createMatch;
|
|
engine.createMatch = (r, d, seed) => {
|
|
const s = create(r, d, seed);
|
|
const second = (1 - s.firstSeat) as 0 | 1;
|
|
const p = s.players[second];
|
|
p.hand.push(...p.library.splice(0, extra));
|
|
return s;
|
|
};
|
|
}
|
|
return {
|
|
name: 'duel', engine, lens: engine.lens, pool,
|
|
deckPayload: cs => cs.map(c => c.card),
|
|
metrics: duelMetrics,
|
|
connected: (a, b) => connectedTierOf(a.card, b.card, rules.connectedTiers),
|
|
};
|
|
}
|