files-tcg/sim/src/cards.ts
2026-09-28 22:34:18 -04:00

70 lines
2 KiB
TypeScript

import { readFileSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
import { parseCsvObjects } from './csv.ts';
export type CardKind = 'avatar' | 'server' | 'program' | 'hardware' | 'execution';
export interface CardDef {
name: string;
kind: CardKind;
/** Printed RAM: play cost and salvage value. Avatars and Servers print none. */
ram: number;
cores: number;
/** Expansion Slots (Hardware ports). */
slots: number;
/** V / B / G / P, when printed. */
type: string;
/** Card text; flavor text for Avatars. */
text: string;
ability?: { name: string; cost: number; text: string };
}
export interface CardPool {
all: CardDef[];
byName: Map<string, CardDef>;
}
const KIND_BY_TEMPLATE: Record<string, CardKind> = {
'Avatar Front': 'avatar',
'Server Front': 'server',
'Server Front Alt': 'server',
'Program Front': 'program',
'Hardware Front': 'hardware',
'Execution Front': 'execution',
};
export const DEFAULT_CARDS_CSV = fileURLToPath(
new URL('../../decks/files-deck-1/cards.csv', import.meta.url),
);
const num = (s: string | undefined): number => (s ? Number(s) : 0);
/** Loads the Cider deck CSV. Reference cards (Turn Sequence) are skipped. */
export function loadCards(path = DEFAULT_CARDS_CSV): CardPool {
const all: CardDef[] = [];
const byName = new Map<string, CardDef>();
for (const row of parseCsvObjects(readFileSync(path, 'utf8'))) {
const kind = KIND_BY_TEMPLATE[row['Front Template']];
if (!kind) continue;
const card: CardDef = {
name: row['Name'],
kind,
ram: num(row['RAM']),
cores: num(row['Cores']),
slots: num(row['Expansion Slots']),
type: row['Type'],
text: row['Description'],
};
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 ${path}: ${card.name}`);
all.push(card);
byName.set(card.name, card);
}
return { all, byName };
}