Files
wikideck/src/game/cards.ts
T

47 lines
3.0 KiB
TypeScript

import { rarity } from '../../shared/protocol.ts';
import type { TestnetBlock } from '../../shared/testnet.ts';
import {
DEFAULT_RULESET, STARTER_DECKS, deriveCard, starterDeckById, type Faction, type GameCard, type PercentileTable, type Ruleset,
} from '../../shared/game/index.ts';
import { percentiles as samplePercentiles } from '../../docs/game/card-sample.json';
import { keywordInfo } from './rulesText.ts';
import type { UICard } from './types.ts';
export const CATEGORIES: Faction[] = ['Space', 'Nature', 'People', 'Science', 'History', 'Culture'];
/** council-data's reference cut points (§1.1), from docs/game/card-sample.json. */
const PERCENTILES = samplePercentiles as unknown as PercentileTable;
export function toUICard(card: GameCard, rules: Ruleset = DEFAULT_RULESET, extra: Partial<UICard> = {}): UICard {
return { id: card.cardId, pageId: card.pageId, title: card.title, category: card.faction, cost: card.cost,
power: card.power, basePower: card.power, home: card.home, trend: card.trend, keyword: keywordInfo(card.keyword, rules),
source: card.source === 'owned' ? 'nft' : 'starter', rarity: card.rarity, game: card, ...extra };
}
/** The four DESIGN §2.1 loaner decks. New players start with the first. */
export const STARTERS = STARTER_DECKS.map(d => ({ id: d.id, name: d.name }));
export const DEFAULT_STARTER = STARTERS[0].id;
export const starterDeck = (rules: Ruleset = DEFAULT_RULESET, id = DEFAULT_STARTER) => starterDeckById(id, rules).map(c => toUICard(c, rules));
/** The bot plays one of the starter decks, picked from the match seed. */
export function opponentDeck(seed: string, rules: Ruleset = DEFAULT_RULESET): UICard[] {
let h = 0; for (const ch of seed) h = (h * 31 + ch.charCodeAt(0)) >>> 0;
return starterDeck(rules, STARTERS[h % STARTERS.length].id);
}
/** Every loaner card once (all four starter decks), for the deck builder. */
export function loanerPool(rules: Ruleset = DEFAULT_RULESET): UICard[] {
const seen = new Set<string>();
return STARTERS.flatMap(d => starterDeck(rules, d.id)).filter(c => !seen.has(c.id) && !!seen.add(c.id));
}
/**
* Owned NFT → GameCard with the engine's §1 derivation. A practice match in the browser can't verify the
* snapshot (§1.10) and doesn't fetch live trends, so the card is 'unchecked' (cost capped) with no trend.
*/
export function cardFromBlock(block: TestnetBlock, rules: Ruleset = DEFAULT_RULESET): UICard {
const st = block.statsSnapshot?.stats;
const category = (CATEGORIES.includes(block.article.category as Faction) ? block.article.category : 'Culture') as Faction;
const card = deriveCard({ cardId: block.tokenId, pageId: block.article.pageId, title: block.article.title, category,
rarity: rarity(block.powHash), snapshot: { stats: { attention: st?.attention ?? null, depth: st?.depth ?? 0, reach: st?.reach ?? 0 } },
live: { trendPercent: null, dailyAverage: null }, check: 'unchecked' }, PERCENTILES, rules);
return toUICard(card, rules, { image: block.article.image, tokenId: block.tokenId });
}