import { LOCATIONS } from './map.ts'; import { capacity, portsTotal } from './query.ts'; import { scriptOf } from './scripts.ts'; import { controls } from './state.ts'; import type { GameState } from './types.ts'; /** Rule violations in a state; empty when it's consistent. Used by tests and `--check`. */ export function checkInvariants(s: GameState): string[] { const errors: string[] = []; const zone = new Map(); const put = (id: number, where: string) => { const prev = zone.get(id); if (prev) errors.push(`card ${id} (${s.cards[id]?.name}) is in both ${prev} and ${where}`); zone.set(id, where); }; s.players.forEach((pl, p) => { const own = (ids: number[], where: string) => { for (const id of ids) { put(id, `p${p}.${where}`); if (s.cards[id].owner !== p) errors.push(`p${p}.${where} holds the opponent's card ${id}`); } }; own(pl.hand, 'hand'); own(pl.deck, 'deck'); own(pl.discard, 'discard'); own(pl.serverPile, 'serverPile'); if (pl.ram < 0) errors.push(`p${p} has ${pl.ram} RAM`); const count = new Map(); pl.avatars.forEach((av, i) => { const slot = i + 1; const copy = slot > 4; if (!copy) { if (av.card >= 0 || !s.rules.recruit.on) own([av.card], `avatar${slot}`); else if (av.at !== null || av.hardware.length) errors.push(`p${p} empty slot ${slot} is in use`); own(av.hardware, `avatar${slot}.hardware`); const ported = av.hardware.filter((h) => !scriptOf(s.cards[h].name)?.noPort && s.cards[h].pushedUntil === undefined); if (av.card >= 0 && ported.length > portsTotal(s, { player: p as 0 | 1, slot })) errors.push(`p${p} Avatar ${slot} is over its slots`); } else { if (av.hardware.length) errors.push(`p${p} copy ${slot - 4} has Hardware of its own`); if (av.at !== null && pl.avatars[i - 4].at === null) errors.push(`p${p} copy ${slot - 4} is in play without its original`); if (av.card !== pl.avatars[i - 4].card) errors.push(`p${p} copy ${slot - 4} doesn't match its original`); } if (av.at === null) return; if (!av.faceUp) errors.push(`p${p} Avatar ${slot} is in play face down`); if (!controls(s, p as 0 | 1, av.at)) errors.push(`p${p} Avatar ${slot} is on ${av.at}, which it doesn't control`); count.set(av.at, (count.get(av.at) ?? 0) + 1); }); for (const [loc, n] of count) if (n > capacity(s, loc)) errors.push(`p${p} has ${n} Avatars on ${loc}`); const total = pl.avatars.filter((av, i) => (i < 4 ? av.card >= 0 : av.at !== null)).length; if (s.rules.recruit.on && total > 4) errors.push(`p${p} has ${total} Avatars, counting copies`); }); for (const loc of LOCATIONS) { const L = s.locations[loc]; if (L.server !== null) put(L.server, `${loc}.server`); for (const id of L.programs) put(id, `${loc}.programs`); if (L.owner === null && (L.server !== null || L.programs.length > 0)) errors.push(`${loc} is unowned but has cards`); const layers = L.programs.filter((id) => s.cards[id].name === 'Compatibility Layer').length; if (L.programs.length > s.rules.maxProgramsPerLocation + layers) errors.push(`${loc} has too many Programs`); } for (const item of s.stack) if (item.kind === 'execution') put(item.card, 'stack'); if (zone.size !== s.cards.length) errors.push(`${s.cards.length - zone.size} cards are missing from every zone`); if (s.choice && s.choice.options.length === 0) errors.push(`choice ${s.choice.kind} has no options`); return errors; }