game: previewPool from frozen starter-cards.json, derived via §1 against the card-sample percentile table

scripts/game-card-data.mjs embeds docs/game/{card-sample,starter-cards}.json
as shared/game/card-data.ts (the engine does no I/O).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-16 23:20:55 -05:00
co-authored by Claude Opus 5.5
parent ab561cf4a3
commit b46049bc72
5 changed files with 96 additions and 2 deletions
+46
View File
@@ -0,0 +1,46 @@
#!/usr/bin/env node
// Generates shared/game/card-data.ts from council-data's frozen files (docs/game/*.json).
// The engine does no I/O, so the data is embedded as constants. Rerun after the JSON changes:
// node scripts/game-card-data.mjs
import { readFileSync, writeFileSync } from 'node:fs';
const root = new URL('../', import.meta.url);
const sample = JSON.parse(readFileSync(new URL('docs/game/card-sample.json', root), 'utf8'));
const starter = JSON.parse(readFileSync(new URL('docs/game/starter-cards.json', root), 'utf8'));
// DESIGN §1.1: 99 ascending cut points per stat. Cut k (1..99) is the value at rank floor(k·n/100)
// of the sample's sorted scores; null attention is excluded from its table.
function cuts(values) {
const sorted = values.filter(v => typeof v === 'number').sort((a, b) => a - b);
if (sorted.length < 2) throw new Error('sample too small');
return Array.from({ length: 99 }, (_, i) => sorted[Math.min(sorted.length - 1, Math.floor((i + 1) * sorted.length / 100))]);
}
const table = Object.fromEntries(['attention', 'depth', 'reach'].map(stat => [stat, cuts(sample.cards.map(c => c.stats?.[stat]))]));
const daysIn = month => { const [y, m] = month.split('-').map(Number); return new Date(Date.UTC(y, m, 0)).getUTCDate(); };
const pool = starter.cards.map(c => ({
pageId: c.pageId, title: c.title, category: c.category,
stats: { attention: c.stats.attention ?? null, depth: c.stats.depth, reach: c.stats.reach },
trendPercent: c.trendPercent ?? null,
dailyAverage: typeof c.monthlyViews === 'number' && c.latestMonth ? Math.round(c.monthlyViews / daysIn(c.latestMonth) * 100) / 100 : null,
}));
const out = `// GENERATED by scripts/game-card-data.mjs from docs/game/card-sample.json (${sample.cards.length} cards,
// generated ${sample.generatedAt}) and docs/game/starter-cards.json (frozen ${starter.frozenAt}). Do not edit by hand.
import type { PercentileTable } from './derive.ts';
import type { Faction } from './types.ts';
/** DESIGN §1.1 reference table "card-sample-2026-11-16": 99 cut points per stat. */
export const CARD_SAMPLE_PERCENTILES: PercentileTable = ${JSON.stringify(table)};
export interface LoanerSnapshot {
pageId: number; title: string; category: Faction;
stats: { attention: number | null; depth: number; reach: number };
trendPercent: number | null; dailyAverage: number | null;
}
/** Frozen loaner pool (§2): real snapshots of random articles, for filling deck slots and draft modes. */
export const LOANER_SNAPSHOTS: readonly LoanerSnapshot[] = ${JSON.stringify(pool, null, 1).replace(/\n\s*/g, ' ')};
`;
writeFileSync(new URL('shared/game/card-data.ts', root), out);
console.log(`card-data.ts: ${pool.length} loaners, percentile tables from ${sample.cards.length} sample cards`);
+16
View File
@@ -0,0 +1,16 @@
// GENERATED by scripts/game-card-data.mjs from docs/game/card-sample.json (100 cards,
// generated 2026-11-17T04:05:04.081324+00:00) and docs/game/starter-cards.json (frozen 2026-11-17). Do not edit by hand.
import type { PercentileTable } from './derive.ts';
import type { Faction } from './types.ts';
/** DESIGN §1.1 reference table "card-sample-2026-11-16": 99 cut points per stat. */
export const CARD_SAMPLE_PERCENTILES: PercentileTable = {"attention":[17,18,18,20,21,21,21,22,22,23,23,23,23,23,24,24,24,24,25,25,25,26,26,27,27,28,28,28,28,28,29,29,29,30,30,30,30,30,30,31,31,31,31,31,32,32,32,33,33,33,33,34,34,34,35,35,35,35,35,36,36,36,36,36,37,37,38,38,38,38,39,39,40,41,41,41,43,44,45,45,47,48,49,49,50,50,50,51,53,53,55,56,56,57,59,61,61,63,69],"depth":[51,51,52,53,53,53,54,54,54,54,54,54,55,55,55,55,56,57,57,57,58,58,58,59,59,59,59,59,59,60,60,60,60,60,60,61,61,61,61,61,61,61,62,62,62,62,63,63,63,63,63,64,64,64,64,64,64,65,65,65,65,65,66,66,66,66,67,67,67,67,67,68,68,68,68,69,69,69,69,69,69,69,70,70,71,71,71,71,71,72,72,73,73,75,75,76,79,80,81],"reach":[0,0,0,0,0,0,0,0,0,10,10,10,10,10,10,10,10,10,10,10,10,10,16,16,16,16,16,16,16,16,16,16,16,16,20,20,20,20,20,20,20,20,20,23,23,23,23,23,23,26,26,26,28,28,28,28,28,30,30,30,30,30,32,32,32,33,33,35,36,36,36,36,36,36,37,37,38,38,39,39,40,43,43,43,45,45,46,48,48,49,50,50,51,51,51,52,53,55,59]};
export interface LoanerSnapshot {
pageId: number; title: string; category: Faction;
stats: { attention: number | null; depth: number; reach: number };
trendPercent: number | null; dailyAverage: number | null;
}
/** Frozen loaner pool (§2): real snapshots of random articles, for filling deck slots and draft modes. */
export const LOANER_SNAPSHOTS: readonly LoanerSnapshot[] = [ { "pageId": 49830722, "title": "Dicroidium odontopteroides", "category": "Nature", "stats": { "attention": 30, "depth": 55, "reach": 0 }, "trendPercent": 5.4, "dailyAverage": 1.9 }, { "pageId": 25856905, "title": "Dichromorpha viridis", "category": "Nature", "stats": { "attention": 35, "depth": 52, "reach": 23 }, "trendPercent": -43.6, "dailyAverage": 4.29 }, { "pageId": 66508497, "title": "Amanita mairei", "category": "Nature", "stats": { "attention": 30, "depth": 50, "reach": 32 }, "trendPercent": -21.8, "dailyAverage": 1.97 }, { "pageId": 4289899, "title": "Anthodon (reptile)", "category": "Nature", "stats": { "attention": 39, "depth": 66, "reach": 37 }, "trendPercent": -8.3, "dailyAverage": 7.52 }, { "pageId": 47519878, "title": "Jeanne Madden", "category": "People", "stats": { "attention": 24, "depth": 54, "reach": 23 }, "trendPercent": -40.9, "dailyAverage": 0.84 }, { "pageId": 46263191, "title": "Max Touloute", "category": "People", "stats": { "attention": 28, "depth": 67, "reach": 16 }, "trendPercent": 95.8, "dailyAverage": 1.52 }, { "pageId": 3372934, "title": "Hermes Ramírez", "category": "People", "stats": { "attention": 30, "depth": 65, "reach": 38 }, "trendPercent": -9.2, "dailyAverage": 1.9 }, { "pageId": 1374812, "title": "Tom Ewell", "category": "People", "stats": { "attention": 61, "depth": 71, "reach": 50 }, "trendPercent": -19.8, "dailyAverage": 146.71 }, { "pageId": 13799810, "title": "Levophacetoperane", "category": "Science", "stats": { "attention": 37, "depth": 61, "reach": 16 }, "trendPercent": -1.8, "dailyAverage": 5.32 }, { "pageId": 36740675, "title": "The Best of Men", "category": "Science", "stats": { "attention": 45, "depth": 63, "reach": 23 }, "trendPercent": -3.4, "dailyAverage": 15.35 }, { "pageId": 4426773, "title": "Agda (programming language)", "category": "Science", "stats": { "attention": 56, "depth": 69, "reach": 36 }, "trendPercent": 7.8, "dailyAverage": 78.68 }, { "pageId": 42770753, "title": "Château de la Faye (Villexavier)", "category": "History", "stats": { "attention": 20, "depth": 54, "reach": 10 }, "trendPercent": 7.7, "dailyAverage": 0.45 }, { "pageId": 15362336, "title": "1954 Gold Coast general election", "category": "History", "stats": { "attention": 38, "depth": 60, "reach": 16 }, "trendPercent": -10.4, "dailyAverage": 5.84 }, { "pageId": 9026000, "title": "Names of Poland", "category": "History", "stats": { "attention": 53, "depth": 71, "reach": 23 }, "trendPercent": -0.9, "dailyAverage": 46.16 }, { "pageId": 25275009, "title": "Amur Annexation", "category": "History", "stats": { "attention": 59, "depth": 70, "reach": 33 }, "trendPercent": -6.7, "dailyAverage": 104.45 }, { "pageId": 7674700, "title": "Shipley Plateau", "category": "Culture", "stats": { "attention": 23, "depth": 54, "reach": 10 }, "trendPercent": -55.1, "dailyAverage": 0.71 }, { "pageId": 15810707, "title": "Charmont-en-Beauce", "category": "Culture", "stats": { "attention": 23, "depth": 54, "reach": 51 }, "trendPercent": 27.8, "dailyAverage": 0.74 }, { "pageId": 48653101, "title": "Tornadoes of 1978", "category": "Culture", "stats": { "attention": 45, "depth": 70, "reach": 0 }, "trendPercent": 3.2, "dailyAverage": 16.48 }, { "pageId": 981340, "title": "Tatsunoko Production", "category": "Culture", "stats": { "attention": 61, "depth": 81, "reach": 43 }, "trendPercent": -9.4, "dailyAverage": 152.84 } ];
+1
View File
@@ -17,3 +17,4 @@ export {
export {
STARTER_POOL, STARTER_DECKS, loanerCard, loanerInput, previewPool, starterDeck, starterDeckById, type StarterDeckDef,
} from './starter.ts';
export { CARD_SAMPLE_PERCENTILES, LOANER_SNAPSHOTS, type LoanerSnapshot } from './card-data.ts';
+12 -2
View File
@@ -4,6 +4,7 @@
* Loaners are off-chain preview cards: not NFTs, never minted or moved.
* engine-b / council-data replace STARTER_POOL with derived cards (cards.ts).
*/
import { CARD_SAMPLE_PERCENTILES, LOANER_SNAPSHOTS } from './card-data.ts';
import { DEFAULT_DERIVATION, deriveCard, type CardInput, type DerivationRules } from './derive.ts';
import { Rng } from './rng.ts';
import type { Faction, GameCard, Stat } from './types.ts';
@@ -53,8 +54,17 @@ export function loanerCard(spec: Spec, copy = 0): GameCard {
};
}
/** Every placeholder loaner card, once each (24 cards). Draft modes can sample from this. */
export function previewPool(): GameCard[] { return STARTER_POOL.map(spec => loanerCard(spec)); }
/**
* The frozen loaner pool (DESIGN §2): council-data's docs/game/starter-cards.json snapshots, derived by §1
* against the card-sample percentile table. For filling loaner slots and draft modes; not the starter decks.
* Ids are `loaner:<pageId>`. `rules` lets a mode's curve apply to loaners exactly as to owned cards.
*/
export function previewPool(rules: DerivationRules = DEFAULT_DERIVATION): GameCard[] {
return LOANER_SNAPSHOTS.map(l => deriveCard({
cardId: `loaner:${l.pageId}`, pageId: l.pageId, title: l.title, category: l.category, rarity: 'Common',
snapshot: { stats: l.stats }, live: { trendPercent: l.trendPercent, dailyAverage: l.dailyAverage }, check: 'loaner',
}, CARD_SAMPLE_PERCENTILES, rules));
}
/** A deterministic 12-card loaner deck: 2 cards of each cost 1–6, chosen by `seed`. */
export function starterDeck(seed: number | string): GameCard[] {
+21
View File
@@ -50,3 +50,24 @@ test('modes changing the curve change loaners too; unknown ids are rejected', ()
assert.ok(flat.every(c => c.power === 3));
assert.throws(() => starterDeckById('nope'), RangeError);
});
test('previewPool derives the frozen starter-cards.json loaners through §1', async () => {
const { previewPool, LOANER_SNAPSHOTS, CARD_SAMPLE_PERCENTILES, DEFAULT_RULESET, validateDeck } = await import('../shared/game/index.ts');
const pool = previewPool();
assert.equal(pool.length, LOANER_SNAPSHOTS.length);
assert.deepEqual(previewPool(), pool, 'deterministic');
for (const stat of ['attention', 'depth', 'reach'] as const) {
const cuts = CARD_SAMPLE_PERCENTILES[stat];
assert.equal(cuts.length, 99);
assert.ok(cuts.every((v, i) => i === 0 || v >= cuts[i - 1]), `${stat} cuts ascend`);
}
assert.equal(new Set(pool.map(c => c.cardId)).size, pool.length);
for (const c of pool) {
assert.equal(c.source, 'loaner');
assert.match(c.cardId, /^loaner:\d+$/);
assert.ok(c.cost >= 1 && c.cost <= 6);
assert.equal(c.power, DEFAULT_DERIVATION.powerByCost[c.cost] + (c.keyword === null && c.cost === 1 ? DEFAULT_DERIVATION.vanillaBonus : 0));
}
// 12 of them make a legal loaner deck.
assert.deepEqual(validateDeck(DEFAULT_RULESET, pool.slice(0, 12)), []);
});