files-tcg/sim/src/rules.ts
2026-09-28 02:54:54 -04:00

82 lines
3.3 KiB
TypeScript

/**
* Every number from rules.md that a rule variant might change. Pass a modified copy to
* createGame to A/B test a rule.
*/
export interface RulesConfig {
startingRam: number;
openingHand: number;
/** Grid locations needed to win (starting Servers don't count). */
winLocations: number;
/** Player turns before the game is called a draw. */
maxTurns: number;
/** RAM paid on capture, by level. */
levelRam: Record<number, number>;
/** Cost to place, upgrade to, or clear a Firewall of each level; index 0 = Lv 1. */
firewallCost: readonly number[];
maxProgramsPerLocation: number;
deck: { avatars: number; servers: number; main: number; maxCopies: number };
/** Avatars face up at the start (rules: Avatar 1 only). */
startingAvatars: number;
/** What the first player may do on turn 1 (the rules allow everything). */
firstTurn: {
advance: boolean;
/** Captures pay capture RAM. A Server card's own RAM bonus is still paid. */
captureRam: boolean;
/** Captures unlock Avatars. They unlock on a later capture instead. */
unlock: boolean;
/** Playing cards from hand: Hardware, Programs, Executions. */
cards: boolean;
};
/**
* Catch-up for whoever goes second: extra starting RAM, extra Avatars face up, extra cards in the
* opening hand, and (0 or 1) owning the location next to their starting Server at the start.
*/
secondPlayer: { ram: number; avatars: number; cards: number; locations: number };
}
export const DEFAULT_RULES: RulesConfig = {
startingRam: 4,
openingHand: 7,
winLocations: 10,
maxTurns: 200,
levelRam: { 1: 4, 2: 4, 3: 4, 4: 4, 5: 4 },
firewallCost: [2, 4, 8],
maxProgramsPerLocation: 2,
deck: { avatars: 4, servers: 20, main: 30, maxCopies: 2 },
startingAvatars: 1,
firstTurn: { advance: true, captureRam: true, unlock: true, cards: true },
secondPlayer: { ram: 0, avatars: 0, cards: 0, locations: 0 },
};
/**
* A copy of `rules` with one setting changed, from text like "winLocations=9",
* "secondPlayer.ram=4", "firstTurn.advance=false" or "levelRam=4,4,4,4,4" (Lv 1 to 5).
*/
export function withRule(rules: RulesConfig, spec: string): RulesConfig {
const m = spec.match(/^([\w.]+)=(.+)$/);
if (!m) throw new Error(`Rule "${spec}" should look like name=value`);
const [, key, raw] = m;
const next = structuredClone(rules) as unknown as Record<string, unknown>;
const path = key.split('.');
let obj = next;
for (const part of path.slice(0, -1)) {
if (typeof obj[part] !== 'object' || obj[part] === null) throw new Error(`Unknown rule "${key}"`);
obj = obj[part] as Record<string, unknown>;
}
const last = path[path.length - 1];
const old = obj[last];
const num = (x: string) => {
const n = Number(x);
if (!Number.isFinite(n)) throw new Error(`Rule "${key}" needs numbers, got "${x}"`);
return n;
};
if (typeof old === 'number') obj[last] = num(raw);
else if (typeof old === 'boolean') {
if (raw !== 'true' && raw !== 'false') throw new Error(`Rule "${key}" needs true or false`);
obj[last] = raw === 'true';
} else if (key === 'levelRam') {
obj[last] = Object.fromEntries(raw.split(',').map((x, i) => [i + 1, num(x)]));
} else if (Array.isArray(old)) obj[last] = raw.split(',').map(num);
else throw new Error(`Unknown rule "${key}"`);
return next as unknown as RulesConfig;
}