From 038e079ce81e023a3d05c4a82261b8ef499c2eeb Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 21:39:48 -0500 Subject: [PATCH] [sim-balance] Duel bots (heuristic, MC) and duel simulation harness Engine-agnostic duel bots over a DuelLens, a 40-card deck builder from the 236 sample cards with type/curve-skewed archetypes, a <=4-worker sim runner and DUEL_BALANCE metrics. Runs on a small stand-in duel engine until duel-core's shared/duel alpha lands; then shared/bots/duel-adapter.ts binds it. Co-Authored-By: Claude Opus 5.5 --- scripts/sim/duel-decks.ts | 49 +++++++ scripts/sim/duel-run.ts | 137 ++++++++++++++++++ scripts/sim/duel-sim-types.ts | 18 +++ scripts/sim/duel-standin-adapter.ts | 74 ++++++++++ scripts/sim/duel-standin.ts | 216 ++++++++++++++++++++++++++++ scripts/sim/duel-stats.ts | 156 ++++++++++++++++++++ shared/bots/duel-bots.ts | 194 +++++++++++++++++++++++++ tests/sim-balance-duel.test.ts | 70 +++++++++ 8 files changed, 914 insertions(+) create mode 100644 scripts/sim/duel-decks.ts create mode 100644 scripts/sim/duel-run.ts create mode 100644 scripts/sim/duel-sim-types.ts create mode 100644 scripts/sim/duel-standin-adapter.ts create mode 100644 scripts/sim/duel-standin.ts create mode 100644 scripts/sim/duel-stats.ts create mode 100644 shared/bots/duel-bots.ts create mode 100644 tests/sim-balance-duel.test.ts diff --git a/scripts/sim/duel-decks.ts b/scripts/sim/duel-decks.ts new file mode 100644 index 0000000..388bd49 --- /dev/null +++ b/scripts/sim/duel-decks.ts @@ -0,0 +1,49 @@ +// 40-card sim decks drawn from a card pool (the 236 real sample cards). Every card is unique (one NFT each), +// so decks sample without replacement. Archetypes skew the type or cost mix. Pure, seeded. +import { mulberry32 } from '../../shared/bots/index.ts'; + +export interface PoolCard { id: string; type: string; portal: string; cost: number; keywords: string[]; fame?: number } +export interface SimDeck { id: string; tags: string[]; cards: C[] } + +export const ARCHETYPES = ['random', 'figure-heavy', 'doctrine-heavy', 'event-heavy', 'location-heavy', 'institution-heavy', 'low-curve', 'high-curve'] as const; +export type Archetype = typeof ARCHETYPES[number]; + +function weight(c: PoolCard, arch: Archetype): number { + switch (arch) { + case 'figure-heavy': return c.type === 'Figure' ? 8 : 1; + case 'doctrine-heavy': return c.type === 'Doctrine' ? 8 : 1; + case 'event-heavy': return c.type === 'Event' ? 12 : 1; + case 'location-heavy': return c.type === 'Location' ? 8 : 1; + case 'institution-heavy': return c.type === 'Institution' ? 10 : 1; + case 'low-curve': return c.cost <= 2 ? 8 : c.cost === 3 ? 3 : 0.3; + case 'high-curve': return c.cost >= 5 ? 8 : c.cost === 4 ? 3 : 0.5; + default: return 1; + } +} + +/** Weighted sampling without replacement (Efraimidis–Spirakis keys). */ +export function buildDeck(pool: C[], arch: Archetype, seed: number, size = 40): SimDeck { + const rng = mulberry32(seed); + const keyed = pool.map(c => ({ c, k: Math.pow(rng(), 1 / Math.max(1e-6, weight(c, arch))) })); + keyed.sort((a, b) => b.k - a.k); + const cards = keyed.slice(0, Math.min(size, pool.length)).map(x => x.c); + return { id: `${arch}#${seed}`, tags: deckTags(cards, arch), cards }; +} + +export function deckTags(cards: PoolCard[], arch: string): string[] { + const count = (f: (c: PoolCard) => string) => { const m: Record = {}; for (const c of cards) m[f(c)] = (m[f(c)] ?? 0) + 1; return m; }; + const top = (m: Record) => Object.entries(m).sort((a, b) => b[1] - a[1])[0]?.[0] ?? '?'; + const avg = cards.reduce((t, c) => t + c.cost, 0) / (cards.length || 1); + const curve = avg < 2.6 ? 'low' : avg < 3.4 ? 'mid' : 'high'; + return [`arch:${arch}`, `portal:${top(count(c => c.portal))}`, `curve:${curve}`, `mainType:${top(count(c => c.type))}`]; +} + +/** n decks: half random, the rest cycling through the skewed archetypes. */ +export function deckSet(pool: C[], n: number, seed: number, size = 40): SimDeck[] { + const out: SimDeck[] = []; + for (let i = 0; i < n; i++) { + const arch: Archetype = i % 2 === 0 ? 'random' : ARCHETYPES[1 + ((i >> 1) % (ARCHETYPES.length - 1))]; + out.push(buildDeck(pool, arch, (seed * 7919 + i * 104729) >>> 0, size)); + } + return out; +} diff --git a/scripts/sim/duel-run.ts b/scripts/sim/duel-run.ts new file mode 100644 index 0000000..74cfc33 --- /dev/null +++ b/scripts/sim/duel-run.ts @@ -0,0 +1,137 @@ +// Duel balance simulation. +// npx tsx scripts/sim/duel-run.ts --engine duel --matches 2000 --workers 4 --out sim-out/duel [--rules '{"startingLife":18}'] +// Writes /