forked from WikiDeck/wikideck
Pure deterministic off-chain engine in shared/game: createMatch, legalActions, applyAction, isOver, result, viewFor, serialize/deserialize, replay. Placeholder Snap-style lanes rules until DESIGN.md v1; keyword hook registry for engine-b. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
93 lines
4.2 KiB
TypeScript
93 lines
4.2 KiB
TypeScript
/**
|
||
* Turning WikiDeck data into GameCards, and starter decks for players with no NFTs.
|
||
* PLACEHOLDER numbers until docs/game/DESIGN.md v1; engine-b tunes the mapping.
|
||
* Starter cards are offline preview cards: not NFTs, never minted or moved.
|
||
*/
|
||
import { Rng } from './rng.ts';
|
||
import { DEFAULT_RULESET } from './ruleset.ts';
|
||
import type { Category, GameCard, Ruleset } from './types.ts';
|
||
|
||
type StarterSpec = [pageId: number, name: string, category: Category, cost: number, power: number, keyword: string | null];
|
||
|
||
/** 24 curated preview cards, 4 per cost 1–6. pageIds outside shared/catalog.ts are unverified (display only). */
|
||
export const STARTER_POOL: readonly StarterSpec[] = [
|
||
[22780, 'Octopus', 'Nature', 1, 2, null],
|
||
[19331, 'Moon', 'Space', 1, 1, 'anchor'],
|
||
[23053, 'Periodic table', 'Science', 1, 2, null],
|
||
[3383, 'Bronze Age', 'History', 1, 1, 'rally'],
|
||
[5843419, 'Chess', 'Culture', 2, 3, null],
|
||
[9228, 'Earth', 'Space', 2, 2, 'rally'],
|
||
[2008, 'Ada Lovelace', 'People', 2, 4, null],
|
||
[21830, 'Honey bee', 'Nature', 2, 3, 'anchor'],
|
||
[736, 'Albert Einstein', 'People', 3, 5, null],
|
||
[10979, 'Fungus', 'Nature', 3, 4, 'rally'],
|
||
[26903, 'Solar System', 'Space', 3, 6, null],
|
||
[30758, 'Roman Empire', 'History', 3, 4, 'anchor'],
|
||
[9649, 'Evolution', 'Science', 4, 7, null],
|
||
[8424, 'Jazz', 'Culture', 4, 6, 'rally'],
|
||
[12898, 'Marie Curie', 'People', 4, 8, null],
|
||
[14533, 'Silk Road', 'History', 4, 6, 'anchor'],
|
||
[19468, 'Mount Everest', 'Nature', 5, 9, null],
|
||
[22936, 'Photosynthesis', 'Science', 5, 8, 'rally'],
|
||
[18110, 'Leonardo da Vinci', 'People', 5, 10, null],
|
||
[14627, 'Ancient Egypt', 'History', 5, 8, 'anchor'],
|
||
[4177, 'Black hole', 'Space', 6, 12, null],
|
||
[25443, 'Quantum mechanics', 'Science', 6, 11, 'rally'],
|
||
[17867, 'Library of Alexandria', 'History', 6, 11, 'anchor'],
|
||
[20217, 'Music', 'Culture', 6, 12, null],
|
||
];
|
||
|
||
export function starterCard(spec: StarterSpec, copy = 0): GameCard {
|
||
const [pageId, name, category, cost, power, keyword] = spec;
|
||
return { id: `starter:${pageId}:${copy}`, name, pageId, category, cost, power, keyword, source: 'starter' };
|
||
}
|
||
|
||
/**
|
||
* A deterministic starter deck: 2 cards of each cost 1–6 (for a 12-card deck),
|
||
* drawn from STARTER_POOL by `seed`. Cards whose keyword the ruleset doesn't
|
||
* enable still play (as vanilla). Fills to ruleset.deck.min.
|
||
*/
|
||
export function starterDeck(seed: string, rules: Ruleset = DEFAULT_RULESET): GameCard[] {
|
||
const rng = Rng.fromSeed(`starter:${seed}`);
|
||
const byCost = new Map<number, StarterSpec[]>();
|
||
for (const spec of STARTER_POOL) byCost.set(spec[3], [...(byCost.get(spec[3]) ?? []), spec]);
|
||
const picks: StarterSpec[] = [];
|
||
const costs = [...byCost.keys()].sort((a, b) => a - b);
|
||
for (const cost of costs) picks.push(...rng.shuffle(byCost.get(cost)!).slice(0, 2));
|
||
const deck = rng.shuffle(picks).slice(0, rules.deck.max).map(spec => starterCard(spec));
|
||
// Pad for modes with bigger decks, respecting maxCopiesPerPage.
|
||
let copy = 1;
|
||
while (deck.length < rules.deck.min && copy < rules.deck.maxCopiesPerPage) {
|
||
for (const spec of rng.shuffle(STARTER_POOL)) {
|
||
if (deck.length >= rules.deck.min) break;
|
||
deck.push(starterCard(spec, copy));
|
||
}
|
||
copy++;
|
||
}
|
||
return deck;
|
||
}
|
||
|
||
/** Input for an owned card; matches PageEnrichment / MintStatsSnapshot fields (shared/enrichment.ts). */
|
||
export interface CardSource {
|
||
id: string;
|
||
title: string;
|
||
pageId: number;
|
||
category: Category;
|
||
stats: { attention: number | null; depth: number; reach: number };
|
||
}
|
||
|
||
/**
|
||
* PLACEHOLDER stat → card mapping (DESIGN.md will replace it). Famous cards cost
|
||
* more (sidegrade): cost from the mean score, power = 2·cost (+1 if no keyword).
|
||
*/
|
||
export function cardFromStats(source: CardSource): GameCard {
|
||
const { attention, depth, reach } = source.stats;
|
||
const mean = ((attention ?? 0) + depth + reach) / 3;
|
||
const cost = Math.max(1, Math.min(6, Math.ceil(mean / (100 / 6))));
|
||
const keyword = depth >= reach && depth >= (attention ?? 0) ? 'anchor' : reach >= (attention ?? 0) ? 'rally' : null;
|
||
return {
|
||
id: source.id, name: source.title, pageId: source.pageId, category: source.category,
|
||
cost, power: 2 * cost + (keyword ? 0 : 1), keyword, source: 'owned',
|
||
};
|
||
}
|