summaryrefslogtreecommitdiff
path: root/web/src/lib/ui/decks/LootCardsMenu.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/LootCardsMenu.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/LootCardsMenu.svelte')
-rw-r--r--web/src/lib/ui/decks/LootCardsMenu.svelte492
1 files changed, 492 insertions, 0 deletions
diff --git a/web/src/lib/ui/decks/LootCardsMenu.svelte b/web/src/lib/ui/decks/LootCardsMenu.svelte
new file mode 100644
index 0000000..638fd0c
--- /dev/null
+++ b/web/src/lib/ui/decks/LootCardsMenu.svelte
@@ -0,0 +1,492 @@
+<!--
+ The loot-cards menu (the reference `LootCardsMenu`): the button bar —
+ Character loot, add/remove special cards 1418/1419, Enhance cards,
+ Add Card, Return to Top/Bottom — above the discard grid (tap a card →
+ Set Loot Owner). The other buttons open nested submenus (the
+ reference's CharacterLootMenu / AddLootCardMenu /
+ LootCardEnhancementMenu dialogs).
+-->
+<script lang="ts">
+ import type { GameSession } from '$lib/state/session';
+ import type { LootCard as Card, LootPoolKey } from '$lib/protocol/types';
+ import { isCharacter } from '$lib/protocol/types';
+ import { t } from '$lib/i18n';
+ import {
+ characterLootTotal,
+ poolCardGfx
+ } from '$lib/game/cards/lootCard';
+ import LootCard from '../cards/LootCard.svelte';
+ import LootEnhancementCounter from './LootEnhancementCounter.svelte';
+ import Menu from '../Menu.svelte';
+
+ const CARD_1418 = 1418;
+ const CARD_1419 = 1419;
+ const GRID_COLUMNS = 4;
+
+ // Enhancement sections, in the reference order: materials (8 cards:
+ // 2× "1", 3× "2 for 2", 3× "2 for 2-3"), herbs (2 cards), coin
+ // (12× "1", 6× "2", 2× "3" in rows of 3).
+ const MATERIAL_KEYS: LootPoolKey[] = ['hide', 'lumber', 'metal'];
+ // Per material: 2× value-1, 3× "2 for 2", 3× "2 for 2-3".
+ const MATERIAL_SPLIT = [2, 3, 3];
+ const HERB_KEYS: LootPoolKey[] = [
+ 'arrowvine',
+ 'axenut',
+ 'corpsecap',
+ 'flamefruit',
+ 'rockroot',
+ 'snowthistle'
+ ];
+ // Coin pool: 12× "1", 6× "2", 2× "3", shown in rows of 3.
+ const COIN_SPLIT = [12, 6, 2];
+ const COIN_ROW_SIZE = 3;
+
+ // AddLootCardMenu names (in `addedCards` order).
+ const ADD_LOOT_NAMES = [
+ 'hide',
+ 'lumber',
+ 'metal',
+ 'arrowvine',
+ 'axenut',
+ 'corpsecap',
+ 'flamefruit',
+ 'rockroot',
+ 'snowthistle'
+ ];
+
+ // CharacterLootMenu rows (coin first).
+ const LOOT_ROWS = [
+ 'coin',
+ 'hide',
+ 'lumber',
+ 'metal',
+ 'arrowvine',
+ 'axenut',
+ 'corpsecap',
+ 'flamefruit',
+ 'rockroot',
+ 'snowthistle'
+ ];
+
+ let {
+ session,
+ deck,
+ open = $bindable(true)
+ }: {
+ session: GameSession;
+ deck: import('$lib/protocol/types').LootDeck;
+ open?: boolean;
+ } = $props();
+
+ const gameState = session.state;
+ const player = session.player;
+
+ // The discard pile displays most-recently-drawn first (the reference
+ // reverses the pile list).
+ const discardDisplay = $derived([...deck.discardPile].reverse());
+
+ const characters = $derived(
+ ($gameState?.currentList ?? []).filter(isCharacter)
+ );
+
+ function cmd(command: string, args: Record<string, unknown> = {}) {
+ if ($player) session.command(command, args);
+ }
+
+ function lootName(label: string): string {
+ return t(`lootName${label.charAt(0).toUpperCase()}${label.slice(1)}`);
+ }
+
+ /** A character's display name by id ("" until found). */
+ function characterDisplay(charId: string): string {
+ for (const c of $gameState?.currentList ?? []) {
+ if (isCharacter(c) && c.id === charId) return c.characterState.display;
+ }
+ return '';
+ }
+
+ // --- submenus (nested overlays, the reference's dialog stack) ------------
+
+ type Sub =
+ | { kind: 'owner'; card: Card }
+ | { kind: 'charLoot' }
+ | { kind: 'addLoot' }
+ | { kind: 'enhance' };
+
+ let sub = $state<Sub | null>(null);
+
+ // One enhancement counter per pool card.
+ function counter(key: LootPoolKey, index: number) {
+ const pool = deck.pools[key] ?? [];
+ const id = pool[index] ?? -1;
+ return {
+ id,
+ gfx: poolCardGfx(key, index),
+ value: id >= 0 ? deck.enhancements[String(id)] ?? 0 : 0
+ };
+ }
+
+ // The enhancement sections, in the reference order (the
+ // `LootCardEnhancementMenu` column): materials, herbs, coin.
+ type EnhSection = { key: LootPoolKey; label: string; rows: number[][] };
+ const enhSections = $derived.by<EnhSection[]>(() => {
+ const sections: EnhSection[] = [];
+ MATERIAL_KEYS.forEach((key) => {
+ let start = 0;
+ [
+ '1',
+ t('lootAmount2For2'),
+ t('lootAmount2For23')
+ ].forEach((label, i) => {
+ const count = MATERIAL_SPLIT[i];
+ sections.push({
+ key,
+ label: `${lootName(key)} ${label}`,
+ rows: rowsFor(start, count, count)
+ });
+ start += count;
+ });
+ });
+ HERB_KEYS.forEach((key) => {
+ sections.push({
+ key,
+ label: lootName(key),
+ rows: rowsFor(0, 2, 2)
+ });
+ });
+ let coinStart = 0;
+ ['1', '2', '3'].forEach((label, i) => {
+ const count = COIN_SPLIT[i];
+ sections.push({
+ key: 'coin',
+ label: `${lootName('coin')} ${label}`,
+ rows: rowsFor(coinStart, count, COIN_ROW_SIZE)
+ });
+ coinStart += count;
+ });
+ return sections;
+ });
+
+ // `count` cards, `rowSize` per row, starting at pool [start].
+ function rowsFor(start: number, count: number, rowSize: number): number[][] {
+ const rows: number[][] = [];
+ for (let s = start; s < start + count; s += rowSize) {
+ rows.push(Array.from({ length: Math.min(rowSize, start + count - s) }, (_, j) => s + j));
+ }
+ return rows;
+ }
+
+ // Character loot totals per row (the reference `_getLootAmount`).
+ function charLootRows(charId: string) {
+ const characterCount = characters.length;
+ return LOOT_ROWS.map((name) => ({
+ name,
+ amount: characterLootTotal(
+ deck.discardPile,
+ charId,
+ name,
+ characterCount
+ )
+ }));
+ }
+</script>
+
+<Menu {open} title="Loot Cards">
+ {#if $gameState}
+ <div class="bar">
+ <button class="pill pill--text" onclick={() => (sub = { kind: 'charLoot' })}>
+ {t('characterLoot')}
+ </button>
+ <button
+ class="pill pill--text"
+ disabled={!player}
+ onclick={() =>
+ cmd(deck['1418'] ? 'removeSpecialLootCard' : 'addSpecialLootCard', {
+ nr: CARD_1418
+ })}
+ >
+ {deck['1418']
+ ? t('removeSpecialCard', { nr: CARD_1418 })
+ : t('addSpecialCard', { nr: CARD_1418 })}
+ </button>
+ <button
+ class="pill pill--text"
+ disabled={!player}
+ onclick={() =>
+ cmd(deck['1419'] ? 'removeSpecialLootCard' : 'addSpecialLootCard', {
+ nr: CARD_1419
+ })}
+ >
+ {deck['1419']
+ ? t('removeSpecialCard', { nr: CARD_1419 })
+ : t('addSpecialCard', { nr: CARD_1419 })}
+ </button>
+ <button class="pill pill--text" onclick={() => (sub = { kind: 'enhance' })}>
+ {t('enhanceCards')}
+ </button>
+ <button class="pill pill--text" onclick={() => (sub = { kind: 'addLoot' })}>
+ {t('addLootCard')}
+ </button>
+ {#if deck.discardPile.length > 0}
+ <button class="pill pill--text" disabled={!player} onclick={() => cmd('returnLootCard', { top: true })}>
+ {t('returnToTop')}
+ </button>
+ <button class="pill pill--text" disabled={!player} onclick={() => cmd('returnLootCard', { top: false })}>
+ {t('returnToBottom')}
+ </button>
+ {/if}
+ </div>
+
+ <div class="grid" style="grid-template-columns: repeat({GRID_COLUMNS}, 1fr)">
+ {#each discardDisplay as card}
+ <button
+ class="grid__cell"
+ onclick={() => (sub = { kind: 'owner', card })}
+ role="button"
+ tabindex="0"
+ aria-label="loot card {card.id}"
+ >
+ <LootCard
+ {card}
+ revealed
+ ownerName={characterDisplay(card.owner)}
+ characterCount={characters.length}
+ scale={0.6}
+ />
+ </button>
+ {/each}
+ </div>
+ {/if}
+
+ <!-- set loot owner -->
+ {#if sub && sub.kind === 'owner'}
+ {@const s = sub}
+ <div class="sub" role="dialog" aria-modal="true">
+ <div class="sub__title">{t('setLootOwner')}</div>
+ {#each characters as char}
+ <button
+ class="ownerrow"
+ disabled={!player}
+ onclick={() => {
+ cmd('setLootOwner', { ownerId: char.id, cardId: s.card.id });
+ sub = null;
+ }}
+ >
+ <img src="/images/class-icons/{char.id}.png" alt={char.id} />
+ <span>{char.characterState.display}</span>
+ </button>
+ {/each}
+ </div>
+ {/if}
+
+ <!-- character loot totals -->
+ {#if sub?.kind === 'charLoot'}
+ <div class="sub sub--scroll">
+ {#each characters as char}
+ <div class="divider"></div>
+ <div class="charrow">
+ <img src="/images/class-icons/{char.id}.png" alt={char.id} />
+ <span>{t('characterLootTitle', { name: char.characterState.display })}</span>
+ </div>
+ {#each charLootRows(char.id) as row}
+ {#if row.amount > 0}
+ <div class="lootrow">
+ <img src="/images/loot/{row.name}_icon.png" alt={row.name} />
+ <span>{lootName(row.name)}</span>
+ <span class="lootrow__amount">{row.amount}</span>
+ </div>
+ {/if}
+ {/each}
+ {/each}
+ </div>
+ {/if}
+
+ <!-- add an extra loot card -->
+ {#if sub?.kind === 'addLoot'}
+ <div class="sub sub--scroll">
+ <div class="sub__title">{t('addExtraLootCard')}</div>
+ {#each ADD_LOOT_NAMES as name, i}
+ <button
+ class="lootrow"
+ disabled={!player}
+ onclick={() => cmd('addLootCard', { resourceType: name })}
+ >
+ <img src="/images/loot/{name}_icon.png" alt={name} />
+ <span>{lootName(name)}</span>
+ <span class="lootrow__amount">
+ {t('addedCount', { count: deck.addedCards[i] ?? 0 })}
+ </span>
+ </button>
+ {/each}
+ </div>
+ {/if}
+
+ <!-- enhancements -->
+ {#if sub?.kind === 'enhance'}
+ <div class="sub sub--scroll">
+ <div class="sub__title">{t('lootCardEnhancements')}</div>
+ {#each enhSections as section}
+ <div class="secheader">
+ <img src="/images/loot/{section.key}_icon.png" alt={section.key} />
+ <span>{section.label}</span>
+ </div>
+ {#each section.rows as row}
+ <div class="coinrow">
+ {#each row as idx}
+ {@const c = counter(section.key, idx)}
+ <LootEnhancementCounter {session} {...c} />
+ {/each}
+ </div>
+ {/each}
+ <div class="divider"></div>
+ {/each}
+ </div>
+ {/if}
+</Menu>
+
+<style>
+ .bar {
+ display: flex;
+ flex-wrap: wrap;
+ align-items: center;
+ gap: 4px;
+ margin-bottom: 10px;
+ }
+ .pill {
+ 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:disabled {
+ opacity: 0.35;
+ cursor: default;
+ }
+ .pill--text {
+ min-width: 0;
+ }
+ .grid {
+ display: grid;
+ gap: 2px;
+ max-height: 55vh;
+ overflow-y: auto;
+ }
+ .grid__cell {
+ padding: 0;
+ background: none;
+ border: none;
+ cursor: pointer;
+ aspect-ratio: 0.72;
+ }
+ .grid__cell:focus-visible {
+ outline: 2px solid #7ec8ff;
+ }
+ .sub {
+ display: flex;
+ flex-direction: column;
+ gap: 6px;
+ margin-top: 10px;
+ padding: 10px;
+ border-radius: 8px;
+ background: rgba(0, 0, 0, 0.35);
+ }
+ .sub--scroll {
+ max-height: 60vh;
+ overflow-y: auto;
+ }
+ .sub__title {
+ font-size: 14px;
+ font-weight: 600;
+ }
+ .ownerrow {
+ display: flex;
+ align-items: center;
+ gap: 10px;
+ padding: 6px 8px;
+ font-size: 14px;
+ border: none;
+ border-radius: 4px;
+ background: rgba(255, 255, 255, 0.12);
+ color: inherit;
+ cursor: pointer;
+ }
+ .ownerrow:hover:not(:disabled) {
+ background: rgba(255, 255, 255, 0.25);
+ }
+ .ownerrow:disabled {
+ opacity: 0.35;
+ cursor: default;
+ }
+ .ownerrow img {
+ width: 24px;
+ height: 24px;
+ object-fit: contain;
+ }
+ .charrow {
+ display: flex;
+ align-items: flex-end;
+ justify-content: center;
+ gap: 10px;
+ font-size: 14px;
+ font-weight: 600;
+ }
+ .charrow img {
+ width: 24px;
+ height: 24px;
+ object-fit: contain;
+ }
+ .lootrow {
+ display: flex;
+ align-items: center;
+ gap: 8px;
+ padding: 4px 8px;
+ font-size: 13px;
+ border: none;
+ border-radius: 4px;
+ background: rgba(255, 255, 255, 0.08);
+ color: inherit;
+ }
+ .lootrow:not(:disabled) {
+ cursor: pointer;
+ }
+ .lootrow:hover:not(:disabled) {
+ background: rgba(255, 255, 255, 0.2);
+ }
+ .lootrow img {
+ width: 24px;
+ height: 24px;
+ object-fit: contain;
+ }
+ .lootrow__amount {
+ margin-left: auto;
+ font-weight: 700;
+ }
+ .divider {
+ height: 1px;
+ background: rgba(255, 255, 255, 0.2);
+ }
+ .secheader {
+ display: flex;
+ align-items: center;
+ gap: 6px;
+ font-size: 13px;
+ font-weight: 600;
+ }
+ .secheader img {
+ width: 24px;
+ height: 24px;
+ object-fit: contain;
+ }
+ .coinrow {
+ display: flex;
+ justify-content: space-evenly;
+ gap: 4px;
+ }
+</style>