summaryrefslogtreecommitdiff
path: root/web/src/lib/ui/decks/AbilityCardsMenu.svelte
diff options
context:
space:
mode:
Diffstat (limited to 'web/src/lib/ui/decks/AbilityCardsMenu.svelte')
-rw-r--r--web/src/lib/ui/decks/AbilityCardsMenu.svelte307
1 files changed, 307 insertions, 0 deletions
diff --git a/web/src/lib/ui/decks/AbilityCardsMenu.svelte b/web/src/lib/ui/decks/AbilityCardsMenu.svelte
new file mode 100644
index 0000000..0cf416c
--- /dev/null
+++ b/web/src/lib/ui/decks/AbilityCardsMenu.svelte
@@ -0,0 +1,307 @@
+<!--
+ The monster ability-card deck menu (the reference `AbilityCardsMenu`):
+ "Reveal cards:" buttons (local flip of the top draw-pile cards, 1-8 /
+ All), Draw extra card, Extra Shuffle, Activate/Inactivate Monster, then
+ the two card columns — the draw pile (face down unless revealed,
+ drag-reorderable, tap to remove) and the discard pile (face up, tap to
+ remove).
+-->
+<script lang="ts">
+ import type { GameSession } from '$lib/state/session';
+ import type {
+ MonsterAbilityCard as AbilityCard,
+ MonsterAbilityCardRef,
+ MonsterAbilityState,
+ MonsterItem
+ } from '$lib/protocol/types';
+ import type { CampaignData } from '$lib/data/gameData';
+ import { t } from '$lib/i18n';
+ import MonsterAbilityCard from '../cards/MonsterAbilityCard.svelte';
+ import AbilityCardBack from '../cards/AbilityCardBack.svelte';
+ import Menu from '../Menu.svelte';
+
+ const LIST_CARD_SCALE = 0.55;
+ const MAX_REVEAL_BUTTONS = 8;
+
+ let {
+ session,
+ item,
+ deck,
+ campaignData,
+ fh,
+ open = $bindable(true)
+ }: {
+ session: GameSession;
+ item: MonsterItem;
+ deck: MonsterAbilityState;
+ campaignData: CampaignData | null;
+ fh: boolean;
+ open?: boolean;
+ } = $props();
+
+ const gameState = session.state;
+ const player = session.player;
+
+ const model = $derived(
+ campaignData?.monsters.find(
+ (m) => m.name === item.type || m.display === item.type
+ ) ?? null
+ );
+
+ // Top-first lists (the reference reverses the bottom-first draw pile).
+ const drawTopFirst = $derived([...deck.drawPile].reverse());
+ const discardList = $derived(deck.discardPile);
+
+ // The locally-revealed top cards (menu-only, not a deck command).
+ let revealedCount = $state(0);
+ // Reset the local reveal whenever the draw pile changes.
+ const drawSignature = $derived(
+ deck.drawPile.map((c) => `${c.nr}`).join(',')
+ );
+ $effect(() => {
+ void drawSignature;
+ revealedCount = 0;
+ });
+
+ const revealButtons = $derived(
+ Math.min(drawTopFirst.length, MAX_REVEAL_BUTTONS)
+ );
+
+ function fullCard(ref: MonsterAbilityCardRef): AbilityCard | null {
+ const list = campaignData?.abilityDecks[ref.deck] ?? [];
+ return list.find((c) => c.nr === ref.nr) ?? null;
+ }
+
+ function cmd(name: string, args: Record<string, unknown> = {}) {
+ if ($player) session.command(name, args);
+ }
+
+ // --- remove-card confirmation -------------------------------------------
+
+ let removeTarget = $state<MonsterAbilityCardRef | null>(null);
+
+ // --- 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;
+ const oldIndex = n - 1 - dragIndex;
+ const newIndex = n - 1 - dropIndex;
+ cmd('reorderAbilityList', {
+ deck: deck.name,
+ oldIndex,
+ newIndex
+ });
+ }
+ dragIndex = null;
+ dropIndex = null;
+ }
+</script>
+
+<Menu {open} title={item.id}>
+ {#if $gameState && $player}
+ <div class="bar">
+ <span class="bar__label">{t('revealCards')}</span>
+ {#each Array(revealButtons) as _, i}
+ <button
+ class="pill"
+ class:pill--wide={i + 1 >= drawTopFirst.length}
+ onclick={() => (revealedCount = i + 1 >= drawTopFirst.length ? drawTopFirst.length : i + 1)}
+ >
+ {i + 1 >= drawTopFirst.length ? t('revealAll') : i + 1}
+ </button>
+ {/each}
+ <button class="pill pill--text" onclick={() => cmd('drawAbilityCard', { ownerId: item.type })}>
+ {t('drawExtraCard')}
+ </button>
+ <button
+ class="pill pill--text"
+ onclick={() => {
+ revealedCount = 0;
+ cmd('shuffleAbilityCard', { ownerId: item.type });
+ }}
+ >
+ {t('extraShuffle')}
+ </button>
+ <button
+ class="pill pill--text"
+ onclick={() =>
+ cmd('activateMonsterType', {
+ name: item.type,
+ activate: !item.isActive
+ })}
+ >
+ {item.isActive ? t('inactivateMonster') : t('activateMonster')}
+ </button>
+ </div>
+
+ <div class="columns">
+ <div class="col" ondragend={() => (dragIndex = null)} ondrop={onDrop}>
+ {#each drawTopFirst as ref, i}
+ {@const c = fullCard(ref)}
+ <div
+ class="cardwrap"
+ draggable
+ ondragstart={() => onDragStart(i)}
+ ondragover={(e) => onDragOver(e, i)}
+ onclick={() => (removeTarget = ref)}
+ role="button"
+ tabindex="0"
+ onkeydown={(e) => {
+ if (e.key === 'Enter' || e.key === ' ')
+ removeTarget = ref;
+ }}
+ >
+ {#if c != null && i < revealedCount}
+ <MonsterAbilityCard
+ card={c}
+ stat={null}
+ {fh}
+ scale={LIST_CARD_SCALE}
+ applyAll
+ />
+ {:else}
+ <AbilityCardBack {fh} scale={LIST_CARD_SCALE} />
+ {/if}
+ </div>
+ {/each}
+ </div>
+
+ <div class="col">
+ {#each discardList as ref}
+ {@const c = fullCard(ref)}
+ {#if c != null}
+ <div
+ class="cardwrap"
+ onclick={() => (removeTarget = ref)}
+ role="button"
+ tabindex="0"
+ onkeydown={(e) => {
+ if (e.key === 'Enter' || e.key === ' ')
+ removeTarget = ref;
+ }}
+ >
+ <MonsterAbilityCard
+ card={c}
+ stat={null}
+ {fh}
+ scale={LIST_CARD_SCALE}
+ applyAll
+ />
+ </div>
+ {/if}
+ {/each}
+ </div>
+ </div>
+ {:else if $gameState}
+ <p class="spectate">{t('web:spectator')}</p>
+ {/if}
+
+ {#if removeTarget}
+ {@const rt = removeTarget}
+ {@const card = fullCard(rt)}
+ <div class="confirm" role="dialog" aria-modal="true">
+ {#if card != null}
+ <MonsterAbilityCard
+ card={card}
+ stat={null}
+ {fh}
+ scale={0.8}
+ applyAll
+ />
+ {/if}
+ <div class="confirm__btns">
+ <button
+ class="pill pill--text"
+ onclick={() => {
+ cmd('removeCard', { deck: rt.deck, nr: rt.nr });
+ removeTarget = null;
+ }}
+ >
+ {t('removeCardQuestion')}
+ </button>
+ </div>
+ </div>
+ {/if}
+</Menu>
+
+<style>
+ .bar {
+ display: flex;
+ flex-wrap: wrap;
+ align-items: center;
+ gap: 4px;
+ margin-bottom: 10px;
+ }
+ .bar__label {
+ font-size: 13px;
+ font-weight: 600;
+ white-space: pre;
+ }
+ .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;
+ }
+ .columns {
+ display: flex;
+ gap: 12px;
+ align-items: flex-start;
+ }
+ .col {
+ flex: 1;
+ display: flex;
+ flex-direction: column;
+ gap: 6px;
+ max-height: 60vh;
+ overflow-y: auto;
+ }
+ .cardwrap {
+ cursor: pointer;
+ border-radius: 6px;
+ }
+ .cardwrap:focus-visible {
+ outline: 2px solid #7ec8ff;
+ }
+ .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;
+ gap: 8px;
+ }
+ .spectate {
+ font-size: 13px;
+ opacity: 0.7;
+ }
+</style>