Files
wikideck/scripts/sim/card-pool.ts
T

49 lines
2.9 KiB
TypeScript

// 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<string, string> = {
Culture: 'viral', Nature: 'rising', People: 'local', Science: 'encyclopedic', History: 'nostalgic', Space: 'translated',
};
interface RawCard { pageId: number; title: string; category?: string; legacyCategory?: string; stats: Record<Stat, number | null>; 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<Stat, number[]>;
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<Stat, number | null>;
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;
});
}