summaryrefslogtreecommitdiff
path: root/web/src/lib/game/rules.ts
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@spawned.biz>2026-11-16 22:19:39 +0100
committerJoel Klinghed <the_jk@spawned.biz>2026-11-16 22:19:39 +0100
commit94e2850fb73005f3fb30af8489905e5cdfb6fac5 (patch)
tree1aea8e7e6683af770f0c57c0fb654003778c9f16 /web/src/lib/game/rules.ts
parent4f50f2008f4d7c2a635f7f26861fcfc7f7504423 (diff)
Phase 4: cards and decks (M4)
Full card play in the browser: ability-line rendering, monster ability decks, the full modifier-deck (AMD) suite, and the loot deck — all server commands, faithful to the reference flows and layout. Web (Svelte 5): - Line renderer + in-game translations: $lib/game/line/ renders the reference line_builder output (emphasis, range markers, element styling); gameI18n.ts loads the /i18n/ in-game ability-text tables (the client-side TranslationService equivalent) - Card components: stat cards, monster ability cards (OG + FH styles, rendered lines, element/flying tokens), modifier cards (gfx resolver incl. P<n> perk graphics), loot cards (party-size value rules, owner, enhancements) - Monster ability decks: AbilityCardSlot in each monster row (front of the current card while active in playTurns, else the back with the draw-pile count); AbilityCardsMenu (reveal top cards, draw extra, extra shuffle, activate/inactivate, drag-reorder, remove); card zoom - Modifier decks: ModifierDeckWidget (tap to draw, face-up revealed top card, empty-box class icon for the current character's own deck, discard stack); ModifierDeckHeader (bad omen, corrosive spew, ±1/−2/+0, imbue 1/2, reveal with the Cassandra 8-button cap, save/don't-save revealed cards, removed count, Hail perk, CS sanctuary/party cards, faction icons, bless/curse and the empower/enfeeble counters); ModifierDeckMenu (draw-pile drag-reorder + send-to-bottom/shuffle/remove, discard remove/return-top, removed pile with return, faction grid with gray-out, perks menu with the Frosthaven opt-in); card zoom - Loot deck: LootDeckWidget (player-gated draw, current-character icon, discard stack, hidden when both piles empty); LootCardsMenu (set owner, per-character loot totals, add card, enhance with the reference material/herb/coin splits, special cards 1418/1419, return top/bottom) - Bottom DeckArea: loot deck left; the Character Decks toggle with the reference open-state cycling (oneOpen/allOpen/noOpen per round state; one-deck view for the current character during playTurns); ally deck behind shouldShowAlliesDeck; global deck - Pure helper modules ported from the reference: rules.ts (FH card styling, inactive-for-rule, ally-deck gate, deck resolution), modifierDeck.ts (pile derivations, faction cards), perks.ts (active perk set, card-text tokens, descriptions, add/remove gating), cards/lootCard.ts (values + per-character totals) - App wiring: campaign data loaded on currentCampaign change (guarded against out-of-order fetches); ability/AMD/loot menus and zooms driven by live-derived state so open menus track pile changes; Escape closes every dialog Server: - /i18n/ static mount: the in-game ability-text translation files the card renderer resolves (the reference TranslationService data) - Loot deck JSON gains pools (pool card ids per pool — the client derives card gfx from pool key + index and reads the enhancements map; ids only, since pool cards are the same live instances that mutate in the piles) - Modifier deck JSON gains removables (the counter totals the header reads) plus load-time round-trip for saves written before the field existed Docs: - PLAN.md: Phase 3 and Phase 4 marked complete (corrected the 'character ability decks' mislabel — ability decks are monster-only), progress log entries for M3 and M4 svelte-check 0 errors; web tests 177/177; build OK; dart analyze 0 errors; dart test 592/592; scripts/ci.sh green.
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);
+}