diff --git a/scripts/sim/card-pool.ts b/scripts/sim/card-pool.ts new file mode 100644 index 0000000..2d4d779 --- /dev/null +++ b/scripts/sim/card-pool.ts @@ -0,0 +1,48 @@ +// Sim card pool from council-data's docs/game/card-sample.json (or starter-cards.json), +// derived per DESIGN §1 (percentile -> fame -> cost, home stat) and §10.1 (faction keyword). +// Percentile cut points come from the file itself. Power is (re)set from the ruleset by the adapter. +import { readFileSync } from 'node:fs'; +import { KEYWORDS, type GameCard, type Stat } from '../../shared/game/index.ts'; + +const STATS: Stat[] = ['attention', 'depth', 'reach']; +const HOME_TIE: Stat[] = ['depth', 'reach', 'attention']; +/** DESIGN v1 §10.2 (PR #17): one keyword per faction. */ +export const FACTION_KEYWORD: Record = { + Culture: 'viral', Nature: 'rising', People: 'local', Science: 'encyclopedic', History: 'nostalgic', Space: 'translated', +}; + +interface RawCard { pageId: number; title: string; category?: string; legacyCategory?: string; stats: Record; trendPercent?: number; pageviews?: { trendPercent?: number | null; dailyAverage?: number | null } } + +function cuts(values: number[]): number[] { + const s = [...values].sort((a, b) => a - b); + return Array.from({ length: 99 }, (_, i) => s[Math.min(s.length - 1, Math.floor(((i + 1) / 100) * s.length))]); +} +function percentile(score: number, c: number[]): number { + let below = 0, equal = 0; + for (const x of c) { if (x < score) below++; else if (x === score) equal++; } + return Math.floor(below + equal / 2); +} + +export interface PoolOptions { costThresholds?: number[]; keywords?: 'faction' | 'none'; table?: string } + +export function loadPool(file: string, opts: PoolOptions = {}): GameCard[] { + const raw = JSON.parse(readFileSync(file, 'utf8')).cards as RawCard[]; + const tableRaw = opts.table ? (JSON.parse(readFileSync(opts.table, 'utf8')).cards as RawCard[]) : raw; + const table = Object.fromEntries(STATS.map(s => [s, cuts(tableRaw.map(c => c.stats[s]).filter((v): v is number => v !== null))])) as Record; + const thresholds = opts.costThresholds ?? [15, 35, 55, 75, 90]; + return raw.map(c => { + const pct = Object.fromEntries(STATS.map(s => [s, c.stats[s] === null ? null : percentile(c.stats[s]!, table[s])])) as Record; + const vals = STATS.map(s => pct[s]).filter((v): v is number => v !== null); + const fame = Math.floor(vals.reduce((a, b) => a + b, 0) / vals.length + 0.5); + const cost = 1 + thresholds.filter(t => t <= fame).length; + let home: Stat = HOME_TIE[0]; + for (const s of HOME_TIE) { const v = pct[s], cur = pct[home]; if (v !== null && (cur === null || v > cur)) home = s; } + const faction = (c.category ?? c.legacyCategory ?? 'Culture') as GameCard['faction']; + const kw = FACTION_KEYWORD[faction]; + const keyword = cost === 1 || opts.keywords === 'none' || !kw || !Object.hasOwn(KEYWORDS, kw) ? null : kw; + return { + cardId: `loaner:${c.pageId}`, pageId: c.pageId, title: c.title, faction, rarity: 'Common', cost, power: 0, + home, trend: 0, keyword, source: 'loaner', + } satisfies GameCard; + }); +} diff --git a/scripts/sim/run.ts b/scripts/sim/run.ts index 2591a03..f5511fa 100644 --- a/scripts/sim/run.ts +++ b/scripts/sim/run.ts @@ -28,7 +28,12 @@ export async function loadEngine(name: string): Promise> rulesets[id] = merged; } } - return makeGameEngine({ rulesets }); + let pool; + if (process.env.SIM_POOL) { + const { loadPool } = await import('./card-pool.ts'); + pool = loadPool(process.env.SIM_POOL, { keywords: process.env.SIM_KEYWORDS === 'none' ? 'none' : 'faction', costThresholds: process.env.SIM_COST_THRESHOLDS ? JSON.parse(process.env.SIM_COST_THRESHOLDS) : undefined }); + } + return makeGameEngine({ rulesets, pool }); } throw new Error(`unknown engine ${name}`); } diff --git a/scripts/sim/variants/v1.json b/scripts/sim/variants/v1.json new file mode 100644 index 0000000..31413f4 --- /dev/null +++ b/scripts/sim/variants/v1.json @@ -0,0 +1,3 @@ +{ + "v1": { "powerByCost": [0, 1, 2, 4, 6, 8, 10], "laneCapacity": 3, "underdog": false } +}