summaryrefslogtreecommitdiff
path: root/web/src/lib/game/rules.ts
diff options
context:
space:
mode:
Diffstat (limited to 'web/src/lib/game/rules.ts')
-rw-r--r--web/src/lib/game/rules.ts178
1 files changed, 178 insertions, 0 deletions
diff --git a/web/src/lib/game/rules.ts b/web/src/lib/game/rules.ts
new file mode 100644
index 0000000..dad59d6
--- /dev/null
+++ b/web/src/lib/game/rules.ts
@@ -0,0 +1,178 @@
+/**
+ * Game-rule helpers the deck UI needs (Phase 4 §6): the port of the reference
+ * `GameMethods` predicates that gate ability-card display and styling. The
+ * server owns the rules; these are pure read-only views of the state the
+ * client already receives.
+ */
+
+import type {
+ GameState,
+ ListItem,
+ ModifierDeck
+} from '$lib/protocol/types';
+import { isCharacter, isMonster } from '$lib/protocol/types';
+
+const MAX_OG_SOLO_SCENARIO = 100;
+
+/** The editions whose cards always render in the Frosthaven (icon) style. */
+export function isFrosthavenStyledEdition(
+ edition: string,
+ scenario: string
+): boolean {
+ if (edition === 'Solo') {
+ if (scenario.includes('#19 ')) return false; // Forgotten Circles
+ // #100+ are the OG solo scenarios.
+ for (let i = 1; i <= MAX_OG_SOLO_SCENARIO; i++) {
+ if (scenario.includes(`#${i} `)) return true;
+ }
+ return false;
+ }
+ return (
+ edition === 'Frosthaven' ||
+ edition === 'Buttons and Bugs' ||
+ edition === 'Gloomhaven 2nd Edition' ||
+ edition === 'Skulls in the Snow' ||
+ edition === 'Mercenary Packs'
+ );
+}
+
+/**
+ * Whether a monster's ability cards render in the Frosthaven style (the
+ * reference `GameMethods.isFrosthavenStyle`). `styleTheme` is the client's
+ * 'frosthaven' | 'gloomhaven' | 'original' setting.
+ */
+export function isFrosthavenStyleForMonster(
+ monsterEdition: string,
+ state: GameState,
+ styleTheme: 'frosthaven' | 'gloomhaven' | 'original'
+): boolean {
+ const monsterFh = isFrosthavenStyledEdition(
+ monsterEdition,
+ state.scenario
+ );
+ if (monsterFh) return true;
+ if (styleTheme !== 'frosthaven' && !monsterFh) return false;
+ return (
+ styleTheme === 'frosthaven' ||
+ (styleTheme === 'original' &&
+ isFrosthavenStyledEdition(state.currentCampaign, state.scenario))
+ );
+}
+
+/**
+ * Whether a monster is inactive for the current round under the scenario's
+ * "inactive monster" special rule (the reference `isInactiveForRule`).
+ */
+export function isInactiveForRule(
+ state: GameState,
+ monsterId: string
+): boolean {
+ const rule = state.scenarioSpecialRules.find(
+ (r) => r.type === 'InactiveMonster' && r.name === monsterId
+ );
+ if (rule != null && rule.list.includes(state.round)) return true;
+ return false;
+}
+
+/** The campaign is one of the custom (Crimson Scales / Trail of Ashes / CCUG). */
+export function isCustomCampaign(campaign: string): boolean {
+ return (
+ campaign === 'Crimson Scales' ||
+ campaign === 'Trail of Ashes' ||
+ campaign === 'CCUG'
+ );
+}
+
+/**
+ * The campaign is Crimson Scales or Trail of Ashes (the reference
+ * `isCSCampaign` — CCUG has no sanctuary/party-card handling).
+ */
+export function isCSCampaign(campaign: string): boolean {
+ return campaign === 'Crimson Scales' || campaign === 'Trail of Ashes';
+}
+
+/** The character items in the current list. */
+export function charactersIn(list: ListItem[]): ListItem[] {
+ return list.filter((item) => isCharacter(item));
+}
+
+/** The party size (character count) — loot values depend on it. */
+export function characterCount(state: GameState): number {
+ return charactersIn(state.currentList).length;
+}
+
+/** A character id → its class name, for deck `name` resolution. */
+export function characterClassOf(
+ state: GameState,
+ id: string
+): string | null {
+ const item = state.currentList.find(
+ (i) => isCharacter(i) && i.id === id
+ );
+ return item && isCharacter(item) ? item.characterClass : null;
+}
+
+/** The ally-deck monster id, when the ally deck is in play. */
+export function allyDeckOwnerId(state: GameState): string | null {
+ const item = state.currentList.find((i) => isMonster(i) && i.isAlly);
+ return item && isMonster(item) ? item.id : null;
+}
+
+/** The id of the character whose turn it is (turnState === current). */
+export function currentCharacterId(state: GameState): string | null {
+ const item = state.currentList.find(
+ (i) => isCharacter(i) && i.turnState === 1
+ );
+ return item && isCharacter(item) ? item.id : null;
+}
+
+/** A figure (character or monster) by id — the reference `getFigure`. */
+export function figureById(
+ state: GameState,
+ id: string
+): ListItem | null {
+ return state.currentList.find((i) => i.id === id) ?? null;
+}
+
+/**
+ * The modifier deck for a deck [name] — "" is the party (monster) deck,
+ * "allies" the ally deck, a character id its personal deck (the reference
+ * `GameMethods.getModifierDeck`).
+ */
+export function modifierDeckByName(
+ state: GameState,
+ name: string
+): ModifierDeck {
+ if (name === 'allies') return state.modifierDeckAllies;
+ if (name === '') return state.modifierDeck;
+ const item = state.currentList.find((i) => isCharacter(i) && i.id === name);
+ return item && isCharacter(item)
+ ? item.characterState.modifierDeck
+ : state.modifierDeck;
+}
+
+/** Whether a character of [className] is in the game (reference `getFigure`). */
+export function hasCharacterClass(
+ state: GameState,
+ className: string
+): boolean {
+ return state.currentList.some(
+ (i) => isCharacter(i) && i.characterClass === className
+ );
+}
+
+/** The campaign is an OG-Gloomhaven (non-Frosthaven-styled) edition. */
+export function isOgGloomEdition(state: GameState): boolean {
+ return !isFrosthavenStyledEdition(state.currentCampaign, state.scenario);
+}
+
+/**
+ * Whether the ally deck should be shown (the reference
+ * `GameMethods.shouldShowAlliesDeck`; the web settings always show the AMD
+ * deck, so the `showAmdDeck` gate is dropped).
+ */
+export function shouldShowAlliesDeck(state: GameState): boolean {
+ if (state.showAllyDeck) return true;
+ if (!state.allyDeckInOGGloom && isOgGloomEdition(state)) return false;
+ return state.currentList.some((i) => isMonster(i) && i.isAlly);
+}