summaryrefslogtreecommitdiff
path: root/web/src/lib/ui/decks/ModifierDeckMenu.svelte
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/ui/decks/ModifierDeckMenu.svelte
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/ui/decks/ModifierDeckMenu.svelte')
-rw-r--r--web/src/lib/ui/decks/ModifierDeckMenu.svelte570
1 files changed, 570 insertions, 0 deletions
diff --git a/web/src/lib/ui/decks/ModifierDeckMenu.svelte b/web/src/lib/ui/decks/ModifierDeckMenu.svelte
new file mode 100644
index 0000000..2cfac21
--- /dev/null
+++ b/web/src/lib/ui/decks/ModifierDeckMenu.svelte
@@ -0,0 +1,570 @@
+<!--
+ The modifier-deck menu (the reference `ModifierDeckMenu`): the header
+ command bar above two card columns — the draw pile (top-first,
+ drag-reorderable, tap → "send to bottom / shuffle / remove") and the
+ discard pile (face up, tap → "remove / return top card"). The header's
+ "Removed", faction, and Perks buttons open nested submenus (the
+ reference's RemovedModifierCardMenu / GH2eFactionAMDCardMenu /
+ PerksMenu dialogs).
+-->
+<script lang="ts">
+ import type { GameSession } from '$lib/state/session';
+ import type { ModifierDeck as Deck } from '$lib/protocol/types';
+ import { isCharacter } from '$lib/protocol/types';
+ import type { ClassData } from '$lib/data/gameData';
+ import { t } from '$lib/i18n';
+ import {
+ drawPileTopFirst,
+ factionCards,
+ factionCardInAnyCharacterDeck
+ } from '$lib/game/modifierDeck';
+ import {
+ activePerks,
+ canAddPerk,
+ canRemovePerk,
+ perkDescription
+ } from '$lib/game/perks';
+ import ModifierCard from '../cards/ModifierCard.svelte';
+ import ModifierDeckHeader from './ModifierDeckHeader.svelte';
+ import Menu from '../Menu.svelte';
+
+ const LIST_CARD_SCALE = 0.8;
+ // Submenu card preview (the reference zooms at 6x, clamped to width).
+ const PREVIEW_SCALE = 3.0;
+
+ let {
+ session,
+ deck,
+ name,
+ characterClassData = null,
+ open = $bindable(true)
+ }: {
+ session: GameSession;
+ deck: Deck;
+ /** "" party deck, "allies", or a character id. */
+ name: string;
+ /** The owning character's class data (character decks only). */
+ characterClassData?: ClassData | null;
+ open?: boolean;
+ } = $props();
+
+ const gameState = session.state;
+ const player = session.player;
+
+ const isCharacterDeck = $derived(name !== '' && name !== 'allies');
+
+ const drawTopFirst = $derived(drawPileTopFirst(deck));
+ // The discard pile displays most-recently-drawn first (the reference
+ // reverses the pile list); taps map back to pile indices.
+ const discardDisplay = $derived([...deck.discardPile].reverse());
+ const removedDisplay = $derived([...deck.removedPile].reverse());
+
+ // The top `deck.revealed` draw cards are face up (Cassandra's special).
+ function isDrawRevealed(pileIndex: number): boolean {
+ const topFirst = drawPileTopFirst(deck);
+ return topFirst.length - 1 - pileIndex < deck.revealed;
+ }
+
+ function cmd(command: string, args: Record<string, unknown> = {}) {
+ if ($player) session.command(command, args);
+ }
+
+ // --- draw-pile reordering (HTML5 drag & drop) ----------------------------
+
+ let dragIndex = $state<number | null>(null);
+ let dropIndex = $state<number | null>(null);
+
+ function onDragStart(i: number) {
+ dragIndex = i;
+ }
+ function onDragOver(e: DragEvent, i: number) {
+ e.preventDefault();
+ dropIndex = i;
+ }
+ function onDrop() {
+ if (dragIndex != null && dropIndex != null && dragIndex !== dropIndex) {
+ // The list is top-first; the wire pile is bottom-first.
+ const n = deck.drawPile.length;
+ cmd('reorderModifierList', {
+ newIndex: n - 1 - dropIndex,
+ oldIndex: n - 1 - dragIndex,
+ name
+ });
+ }
+ dragIndex = null;
+ dropIndex = null;
+ }
+
+ // --- submenus (nested overlays, the reference's dialog stack) ------------
+
+ type Sub =
+ | { kind: 'sendToBottom'; displayIndex: number }
+ | { kind: 'removeAmd'; pileIndex: number }
+ | { kind: 'removed' }
+ | { kind: 'returnAmd'; pileIndex: number }
+ | { kind: 'faction'; faction: string }
+ | { kind: 'perks' };
+
+ let sub = $state<Sub | null>(null);
+
+ // Close a stale discard-pile submenu when the pile shrinks (the reference
+ // pops when `index >= discardPileSize`).
+ $effect(() => {
+ if (
+ sub?.kind === 'removeAmd' &&
+ sub.pileIndex >= deck.discardPile.length
+ ) {
+ sub = null;
+ }
+ });
+
+ // The character (for the perks submenu).
+ const character = $derived(
+ ($gameState?.currentList ?? []).find(
+ (i): i is import('$lib/protocol/types').CharacterItem =>
+ isCharacter(i) && i.id === name
+ ) ?? null
+ );
+
+ // The faction-cards submenu state (which card this deck has added).
+ const factionGfxs = $derived(
+ sub?.kind === 'faction' ? factionCards(sub.faction) : []
+ );
+ const factionAdded = $derived(
+ sub?.kind === 'faction'
+ ? factionGfxs.find((g) =>
+ deck.drawPile.some((c) => c.gfx === g) ||
+ deck.discardPile.some((c) => c.gfx === g)
+ ) ?? null
+ : null
+ );
+
+ /** Every character's personal deck (the faction gray-out check). */
+ const characterDecks = $derived(
+ ($gameState?.currentList ?? []).filter(isCharacter).map((c) => ({
+ name: c.id,
+ deck: c.characterState.modifierDeck
+ }))
+ );
+
+ function factionGrayed(gfx: string): boolean {
+ return factionCardInAnyCharacterDeck(characterDecks, gfx);
+ }
+
+ // Perks submenu state.
+ const perks = $derived(
+ isCharacterDeck && character && characterClassData
+ ? activePerks(characterClassData, character.characterState)
+ : []
+ );
+</script>
+
+<Menu {open} title={name === '' ? 'Global' : name}>
+ {#if $gameState}
+ <ModifierDeckHeader
+ {session}
+ state={$gameState}
+ {deck}
+ {name}
+ {characterClassData}
+ onOpenRemoved={() => (sub = { kind: 'removed' })}
+ onOpenPerks={() => (sub = { kind: 'perks' })}
+ onOpenFaction={(faction) => (sub = { kind: 'faction', faction })}
+ />
+
+ <div class="columns">
+ <div class="col" ondragend={() => (dragIndex = null)} ondrop={onDrop}>
+ {#each drawTopFirst as card, i}
+ {@const pileIndex = deck.drawPile.length - 1 - i}
+ <div
+ class="cardwrap"
+ draggable
+ ondragstart={() => onDragStart(i)}
+ ondragover={(e) => onDragOver(e, i)}
+ onclick={() =>
+ (sub = { kind: 'sendToBottom', displayIndex: i })}
+ role="button"
+ tabindex="0"
+ onkeydown={(e) => {
+ if (e.key === 'Enter' || e.key === ' ')
+ sub = { kind: 'sendToBottom', displayIndex: i };
+ }}
+ >
+ <ModifierCard
+ {card}
+ {name}
+ revealed={isDrawRevealed(pileIndex)}
+ scale={LIST_CARD_SCALE}
+ />
+ </div>
+ {/each}
+ </div>
+
+ <div class="col">
+ {#each discardDisplay as card, i}
+ {@const pileIndex = deck.discardPile.length - 1 - i}
+ <div
+ class="cardwrap"
+ onclick={() => (sub = { kind: 'removeAmd', pileIndex })}
+ role="button"
+ tabindex="0"
+ onkeydown={(e) => {
+ if (e.key === 'Enter' || e.key === ' ')
+ sub = { kind: 'removeAmd', pileIndex };
+ }}
+ >
+ <ModifierCard {card} {name} revealed scale={LIST_CARD_SCALE} />
+ </div>
+ {/each}
+ </div>
+ </div>
+ {/if}
+
+ <!-- send-to-bottom (draw-pile card) -->
+ {#if sub?.kind === 'sendToBottom'}
+ {@const pileIndex = deck.drawPile.length - 1 - sub.displayIndex}
+ {@const card = deck.drawPile[pileIndex]}
+ <div class="confirm" role="dialog" aria-modal="true">
+ {#if card}
+ <ModifierCard
+ {card}
+ {name}
+ revealed={isDrawRevealed(pileIndex)}
+ scale={PREVIEW_SCALE}
+ />
+ {/if}
+ <div class="confirm__btns">
+ <button
+ class="pill pill--text"
+ onclick={() => {
+ cmd('reorderModifierList', {
+ newIndex: 0,
+ oldIndex: pileIndex,
+ name
+ });
+ sub = null;
+ }}
+ >
+ {t('sendToBottom')}
+ </button>
+ <button
+ class="pill pill--text"
+ onclick={() => {
+ cmd('shuffleAMDCard', { name });
+ sub = null;
+ }}
+ >
+ {t('shuffleUndrawnCards')}
+ </button>
+ <button
+ class="pill pill--text"
+ onclick={() => {
+ cmd('removeAMDCard', { index: pileIndex, name, fromDrawPile: true });
+ sub = null;
+ }}
+ >
+ {t('removeCardQuestion')}
+ </button>
+ </div>
+ </div>
+ {/if}
+
+ <!-- remove / return (discard-pile card) -->
+ {#if sub && sub.kind === 'removeAmd'}
+ {@const s = sub}
+ {@const card = deck.discardPile[s.pileIndex]}
+ <div class="confirm" role="dialog" aria-modal="true">
+ {#if card}
+ <ModifierCard {card} {name} revealed scale={PREVIEW_SCALE} />
+ {/if}
+ <div class="confirm__btns">
+ <button
+ class="pill pill--text"
+ onclick={() => {
+ cmd('removeAMDCard', { index: s.pileIndex, name });
+ sub = null;
+ }}
+ >
+ {t('removeCardQuestion')}
+ </button>
+ <button
+ class="pill pill--text"
+ onclick={() => cmd('returnModifierCard', { name })}
+ >
+ {t('returnTopCard')}
+ </button>
+ </div>
+ </div>
+ {/if}
+
+ <!-- removed cards -->
+ {#if sub?.kind === 'removed'}
+ <div class="sub">
+ <div class="sub__title">Removed cards</div>
+ <div class="col">
+ {#each removedDisplay as card, i}
+ {@const pileIndex = deck.removedPile.length - 1 - i}
+ <div
+ class="cardwrap"
+ onclick={() => (sub = { kind: 'returnAmd', pileIndex })}
+ role="button"
+ tabindex="0"
+ onkeydown={(e) => {
+ if (e.key === 'Enter' || e.key === ' ')
+ sub = { kind: 'returnAmd', pileIndex };
+ }}
+ >
+ <ModifierCard {card} {name} revealed scale={LIST_CARD_SCALE} />
+ </div>
+ {/each}
+ </div>
+ </div>
+ {/if}
+
+ <!-- return a removed card -->
+ {#if sub && sub.kind === 'returnAmd'}
+ {@const s = sub}
+ {@const card = deck.removedPile[s.pileIndex]}
+ <div class="confirm" role="dialog" aria-modal="true">
+ {#if card}
+ <ModifierCard {card} {name} revealed scale={PREVIEW_SCALE} />
+ {/if}
+ <div class="confirm__btns">
+ <button
+ class="pill pill--text"
+ onclick={() => {
+ cmd('returnRemovedAMDCard', {
+ index: s.pileIndex,
+ name,
+ toDrawPile: false
+ });
+ sub = null;
+ }}
+ >
+ {t('returnToDiscardPile')}
+ </button>
+ <button
+ class="pill pill--text"
+ onclick={() => {
+ cmd('returnRemovedAMDCard', {
+ index: s.pileIndex,
+ name,
+ toDrawPile: true
+ });
+ sub = null;
+ }}
+ >
+ {t('returnToDrawPile')}
+ </button>
+ </div>
+ </div>
+ {/if}
+
+ <!-- faction cards -->
+ {#if sub?.kind === 'faction'}
+ <div class="sub">
+ <div class="grid">
+ {#each factionGfxs as gfx}
+ <button
+ class="grid__cell"
+ class:grid__cell--grayed={factionGrayed(gfx)}
+ disabled={!player}
+ aria-label={gfx}
+ onclick={() => {
+ if (factionAdded == null && !factionGrayed(gfx)) {
+ cmd('addFactionCard', {
+ characterId: name,
+ cardId: gfx,
+ add: true
+ });
+ }
+ }}
+ >
+ <ModifierCard
+ card={{ gfx }}
+ name=""
+ revealed
+ scale={PREVIEW_SCALE}
+ />
+ </button>
+ {/each}
+ </div>
+ <div class="confirm__btns">
+ {#if factionAdded == null}
+ <span class="label">{t('tapCardToAddToDeck')}</span>
+ {:else}
+ <button
+ class="pill pill--text"
+ onclick={() => {
+ cmd('addFactionCard', {
+ characterId: name,
+ cardId: factionAdded,
+ add: false
+ });
+ }}
+ >
+ {t('removeCardFromDeckQuestion')}
+ </button>
+ {/if}
+ </div>
+ </div>
+ {/if}
+
+ <!-- perks -->
+ {#if sub?.kind === 'perks' && character && characterClassData}
+ <div class="sub sub--perks">
+ <div class="sub__title">{t('addPerks')}</div>
+ {#if characterClassData.perkDetailsFH.length > 0}
+ <label class="perkrow">
+ <input
+ type="checkbox"
+ checked={character.characterState.useFHPerks}
+ disabled={!player}
+ onchange={() =>
+ cmd('useFHPerks', { characterId: character.id })}
+ />
+ <span>{t('useFrosthavenPerks')}</span>
+ </label>
+ {/if}
+ {#each perks as perk, i}
+ {@const added = character.characterState.perkList[i] ?? false}
+ {@const enabled = added
+ ? canRemovePerk(
+ character.characterState.modifierDeck,
+ perks,
+ i
+ )
+ : canAddPerk(
+ character.characterState.modifierDeck,
+ perks,
+ character.characterState.perkList,
+ i
+ )}
+ <div class="divider"></div>
+ <label class="perkrow" class:perkrow--disabled={!enabled}>
+ <input
+ type="checkbox"
+ checked={added}
+ disabled={!player || !enabled}
+ onchange={() =>
+ cmd('addPerk', { characterId: character.id, index: i })}
+ />
+ <span class="perkrow__desc">{perkDescription(perk)}</span>
+ </label>
+ {/each}
+ </div>
+ {/if}
+</Menu>
+
+<style>
+ .columns {
+ display: flex;
+ gap: 12px;
+ align-items: flex-start;
+ }
+ .col {
+ flex: 1;
+ display: flex;
+ flex-direction: column;
+ gap: 8px;
+ max-height: 50vh;
+ overflow-y: auto;
+ }
+ .cardwrap {
+ cursor: pointer;
+ border-radius: 6px;
+ }
+ .cardwrap:focus-visible {
+ outline: 2px solid #7ec8ff;
+ }
+ .pill {
+ min-width: 32px;
+ padding: 4px 8px;
+ font-size: 13px;
+ border: none;
+ border-radius: 4px;
+ background: rgba(255, 255, 255, 0.12);
+ color: inherit;
+ cursor: pointer;
+ white-space: pre;
+ }
+ .pill:hover {
+ background: rgba(255, 255, 255, 0.25);
+ }
+ .pill--text {
+ min-width: 0;
+ }
+ .confirm {
+ display: flex;
+ flex-direction: column;
+ align-items: center;
+ gap: 10px;
+ margin-top: 10px;
+ padding: 10px;
+ border-radius: 8px;
+ background: rgba(0, 0, 0, 0.35);
+ }
+ .confirm__btns {
+ display: flex;
+ flex-wrap: wrap;
+ gap: 8px;
+ justify-content: center;
+ }
+ .sub {
+ display: flex;
+ flex-direction: column;
+ align-items: center;
+ gap: 10px;
+ margin-top: 10px;
+ padding: 10px;
+ border-radius: 8px;
+ background: rgba(0, 0, 0, 0.35);
+ }
+ .sub__title {
+ font-size: 14px;
+ font-weight: 600;
+ }
+ .sub--perks {
+ align-items: stretch;
+ max-height: 60vh;
+ overflow-y: auto;
+ }
+ .grid {
+ display: grid;
+ grid-template-columns: repeat(auto-fit, 60px);
+ gap: 8px;
+ justify-content: center;
+ }
+ .grid__cell {
+ padding: 0;
+ background: none;
+ border: none;
+ cursor: pointer;
+ }
+ .grid__cell--grayed {
+ filter: grayscale(1);
+ opacity: 0.5;
+ }
+ .label {
+ font-size: 13px;
+ }
+ .perkrow {
+ display: flex;
+ align-items: center;
+ gap: 8px;
+ padding: 4px 0;
+ font-size: 13px;
+ cursor: pointer;
+ }
+ .perkrow--disabled {
+ opacity: 0.35;
+ cursor: default;
+ }
+ .perkrow__desc {
+ flex: 1;
+ }
+ .divider {
+ height: 1px;
+ background: rgba(255, 255, 255, 0.2);
+ }
+</style>