This commit is contained in:
Forrest 2026-10-01 17:20:33 -04:00
parent 5368464042
commit 937f44defe
100 changed files with 10185 additions and 2042 deletions

View file

@ -1,22 +1,20 @@
import { readFileSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
import { parseCsvObjects } from './csv.ts';
export type CardKind = 'avatar' | 'server' | 'program' | 'hardware' | 'execution';
export type CardKind = 'anomaly' | '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;
/** Printed Bandwidth: play cost and salvage value. Anomalies and Servers print none. */
bandwidth: number;
cores: number;
/** Avatar Health: the damage that destroys it (health rule). 0 when not printed. */
/** Anomaly Health: the damage that destroys it (health rule). 0 when not printed. */
health: number;
/** Expansion Slots (Hardware ports). */
/** Expansion Slots (how much Hardware an Anomaly holds). */
slots: number;
/** V / B / G / P, when printed. */
type: string;
/** Card text; flavor text for Avatars. */
/** 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. */
@ -29,7 +27,7 @@ export interface CardPool {
}
const KIND_BY_TEMPLATE: Record<string, CardKind> = {
'Avatar Front': 'avatar',
'Anomaly Front': 'anomaly',
'Server Front': 'server',
'Server Front Alt': 'server',
'Program Front': 'program',
@ -37,20 +35,17 @@ const KIND_BY_TEMPLATE: Record<string, CardKind> = {
'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. By default only set 1 (the
* base game) is loaded; `sets` picks others, or 'all'. Set 2 cards have no scripts yet.
* 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 loadCards(path = DEFAULT_CARDS_CSV, sets: number[] | 'all' = [1]): CardPool {
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(readFileSync(path, 'utf8'))) {
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;
@ -58,7 +53,7 @@ export function loadCards(path = DEFAULT_CARDS_CSV, sets: number[] | 'all' = [1]
const card: CardDef = {
name: row['Name'],
kind,
ram: num(row['RAM']),
bandwidth: num(row['Bandwidth']),
cores: num(row['Cores']),
health: num(row['Health']),
slots: num(row['Expansion Slots']),
@ -73,7 +68,7 @@ export function loadCards(path = DEFAULT_CARDS_CSV, sets: number[] | 'all' = [1]
text: row['Ability Description'],
};
}
if (byName.has(card.name)) throw new Error(`Duplicate card name in ${path}: ${card.name}`);
if (byName.has(card.name)) throw new Error(`Duplicate card name in ${source}: ${card.name}`);
all.push(card);
byName.set(card.name, card);
}