files-tcg/sim/src/cards.ts
2026-10-01 17:20:33 -04:00

76 lines
2.5 KiB
TypeScript

import { parseCsvObjects } from './csv.ts';
export type CardKind = 'anomaly' | 'server' | 'program' | 'hardware' | 'execution';
export interface CardDef {
name: string;
kind: CardKind;
/** Printed Bandwidth: play cost and salvage value. Anomalies and Servers print none. */
bandwidth: number;
cores: number;
/** Anomaly Health: the damage that destroys it (health rule). 0 when not printed. */
health: number;
/** Expansion Slots (how much Hardware an Anomaly holds). */
slots: number;
/** V / B / G / P, when printed. */
type: string;
/** Card text; flavor text for Anomalies. */
text: string;
ability?: { name: string; cost: number; text: string };
/** Card set: 1 is the base game, 2 the expansion. Rows without a Set column are set 1. */
set: number;
}
export interface CardPool {
all: CardDef[];
byName: Map<string, CardDef>;
}
const KIND_BY_TEMPLATE: Record<string, CardKind> = {
'Anomaly Front': 'anomaly',
'Server Front': 'server',
'Server Front Alt': 'server',
'Program Front': 'program',
'Hardware Front': 'hardware',
'Execution Front': 'execution',
};
const num = (s: string | undefined): number => (s ? Number(s) : 0);
/**
* Reads the Cider deck CSV's text. Reference cards (Turn Sequence) are skipped. By default only set
* 1 (the base game) is loaded; `sets` picks others, or 'all'. Set 2 cards have no scripts yet.
* `source` names the file in errors. Node code can use `loadCards` in load.ts to read the file.
*/
export function parseCards(csv: string, sets: number[] | 'all' = [1], source = 'cards.csv'): CardPool {
const all: CardDef[] = [];
const byName = new Map<string, CardDef>();
for (const row of parseCsvObjects(csv)) {
const kind = KIND_BY_TEMPLATE[row['Front Template']];
if (!kind) continue;
const set = row['Set'] ? Number(row['Set']) : 1;
if (sets !== 'all' && !sets.includes(set)) continue;
const card: CardDef = {
name: row['Name'],
kind,
bandwidth: num(row['Bandwidth']),
cores: num(row['Cores']),
health: num(row['Health']),
slots: num(row['Expansion Slots']),
type: row['Type'],
text: row['Description'],
set,
};
if (row['Ability Name']) {
card.ability = {
name: row['Ability Name'],
cost: num(row['Ability Cost']),
text: row['Ability Description'],
};
}
if (byName.has(card.name)) throw new Error(`Duplicate card name in ${source}: ${card.name}`);
all.push(card);
byName.set(card.name, card);
}
return { all, byName };
}