Files
wikideck/scripts/sim/duel-core-sim.ts
T
claudeanderic dbb89ce9c5 Night 2: 40-card Magic-style duel, PvP with owned decks, polite Wikimedia client, Android mining (#23)
Night 2 (2026-11-17), from the `claude/wikideck` fork's `night/2026-11-17` branch. It follows Night 1 (#22).

## What's in it
- **Duel engine** (fork #72, #80): 40-card decks of cards you own, Notability/Citations, keywords, effects T1–T12, Connected, a hidden-information-safe `viewFor`/`logFor`, and replay. Balance v1 tuning (#84) gives the first player about 51%.
- **Card profiles** (#70, #82): Figure, Location, Event, Institution or Doctrine, plus stats, keywords and rules text, all derived from real article data. Templates only.
- **Design and story** (#66, #69, #83): DUEL_DESIGN.md, plus the match log and the encyclopedic "Battle of X (2026)" recap.
- **PvP** (#74, #77, #81, #76, #85):
  - The server checks ownership, and 40-card eligibility is enforced.
  - Modes: Classic (ranked), Draft and Trending.
  - Invites, quick match, Elo, a ladder and recap pages. Online matches render on the duel board.
- **Duel board UI** (#79): MTG-style card faces and combat declaration, built for 360 px first.
- **Polite Wikimedia client** (#73, #75): throttled, cached and identified requests, the mint-path fix, and a prefetch pool.
- **Android mining** (#78): native RandomX, as a sideload build. The emulator mined a testnet block.
- **Testnet ops** (#67, #68).

PR numbers refer to `claude/wikideck`.

## Status
- **Tests:** 423, of which 422 pass, 0 fail and 1 is skipped. The build is OK (`node:24-alpine`, `--network none`).
- **Testnet:** running this commit (036489c) on r350.
- **Untouched:** no changes to `server/testnet/consensus.ts`, `randomx.ts`, `native/` or the mainnet compose.
- **Review:** night-review reviewed fork #66–#79. #80–#85 came from the agents' unfinished branches and were not reviewed.
- **Known gaps:**
  - The PvP end-to-end test hasn't been run (it needs two eom.dev accounts).
  - Bot matches aren't reachable from Play.
  - Card identity pre-bake hasn't been done.
  - The RandomX verifier latch (`server/testnet/randomx.ts:140`) is still open.

This PR is for Eric to merge.

🤖 Generated with [Claude Code](https://claude.com/claude-code)Reviewed-on: #23

Co-authored-by: Claude <claude@eom.dev>
2026-11-18 18:12:24 +00:00

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),
};
}