updates
This commit is contained in:
parent
e590baf28d
commit
6382de19c9
15 changed files with 239 additions and 28 deletions
|
|
@ -15,6 +15,23 @@ export interface RulesConfig {
|
|||
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 = {
|
||||
|
|
@ -22,8 +39,44 @@ export const DEFAULT_RULES: RulesConfig = {
|
|||
openingHand: 7,
|
||||
winLocations: 10,
|
||||
maxTurns: 200,
|
||||
levelRam: { 1: 4, 2: 4, 3: 6, 4: 8, 5: 10 },
|
||||
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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue