Files
wikideck/shared/game/derive.ts

113 lines
4.2 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* Card derivation (DESIGN.md §1): a frozen CardInput becomes a GameCard.
* Pure and deterministic. The engine never reads raw 0–100 stats as power.
*/
export type Stat = 'attention' | 'depth' | 'reach';
export type Faction = 'Space' | 'Nature' | 'People' | 'Science' | 'History' | 'Culture';
export type Rarity = 'Legendary' | 'Epic' | 'Rare' | 'Common';
export const STATS: readonly Stat[] = ['attention', 'depth', 'reach'];
export const FACTIONS: readonly Faction[] = ['Space', 'Nature', 'People', 'Science', 'History', 'Culture'];
/** §1.4 tie-break order: depth and reach are the hardest to inflate. */
export const HOME_TIE_ORDER: readonly Stat[] = ['depth', 'reach', 'attention'];
export interface CardInput {
cardId: string;
pageId: number;
title: string;
category: Faction;
rarity: Rarity;
snapshot: { stats: { attention: number | null; depth: number; reach: number } };
/** Frozen by the host at match start. */
live: { trendPercent: number | null };
}
export interface GameCard {
cardId: string;
pageId: number;
title: string;
faction: Faction;
rarity: Rarity;
cost: number;
power: number;
home: Stat;
trend: -1 | 0 | 1;
keyword: string | null;
}
/** 99 ascending cut points per stat (§1.1). */
export type PercentileTable = Record<Stat, readonly number[]>;
/** The subset of the rules config that card derivation reads. */
export interface DerivationRules {
costThresholds: readonly number[];
powerByCost: readonly number[];
trendUp: number;
trendDown: number;
}
export type KeywordPicker = (card: Omit<GameCard, 'keyword'>) => string | null;
/** §1.1: cut points strictly below, plus half of those equal, rounded down. */
export function percentile(score: number, cuts: readonly number[]): number {
let below = 0, equal = 0;
for (const cut of cuts) {
if (cut < score) below++;
else if (cut === score) equal++;
}
return Math.floor(below + equal / 2);
}
export function percentiles(stats: CardInput['snapshot']['stats'], table: PercentileTable): Record<Stat, number | null> {
return {
attention: stats.attention === null ? null : percentile(stats.attention, table.attention),
depth: percentile(stats.depth, table.depth),
reach: percentile(stats.reach, table.reach),
};
}
/** §1.2: integer mean of the non-null percentiles, rounded half up. */
export function fame(pct: Record<Stat, number | null>): number {
const values = STATS.map(stat => pct[stat]).filter((value): value is number => value !== null);
const sum = values.reduce((total, value) => total + value, 0);
return Math.floor(sum / values.length + 0.5);
}
/** §1.2: 1 + the number of thresholds ≤ fame. */
export function costFor(fameValue: number, thresholds: readonly number[]): number {
return 1 + thresholds.filter(threshold => threshold <= fameValue).length;
}
/** §1.4: highest percentile, ties by depth > reach > attention; null never wins. */
export function homeStat(pct: Record<Stat, number | null>): Stat {
let best: Stat = HOME_TIE_ORDER[0];
for (const stat of HOME_TIE_ORDER) {
const value = pct[stat];
const current = pct[best];
if (value !== null && (current === null || value > current)) best = stat;
}
return best;
}
/** §1.6: ±1 past the thresholds, null → 0. */
export function trendFor(trendPercent: number | null, rules: Pick<DerivationRules, 'trendUp' | 'trendDown'>): -1 | 0 | 1 {
if (trendPercent === null || !Number.isFinite(trendPercent)) return 0;
if (trendPercent >= rules.trendUp) return 1;
if (trendPercent <= rules.trendDown) return -1;
return 0;
}
export function deriveCard(input: CardInput, rules: DerivationRules, table: PercentileTable,
pickKeyword: KeywordPicker = () => null): GameCard {
const pct = percentiles(input.snapshot.stats, table);
const cost = costFor(fame(pct), rules.costThresholds);
const power = rules.powerByCost[cost];
if (power === undefined) throw new RangeError(`powerByCost has no entry for cost ${cost}`);
const base = {
cardId: input.cardId, pageId: input.pageId, title: input.title,
faction: input.category, rarity: input.rarity,
cost, power, home: homeStat(pct), trend: trendFor(input.live.trendPercent, rules),
};
return { ...base, keyword: pickKeyword(base) };
}