summaryrefslogtreecommitdiff
path: root/web/src/lib/ui/decks/AbilityCardSlot.svelte
blob: 8b5250cb41a9fcb4d2ee952567ced42115a8668a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<!--
	One monster's ability card in its row (the reference
	`MonsterAbilityCardWidget`): the front of the current (top discard)
	card while the round is playing turns and the monster is active,
	else the card back with the draw-pile size. Tap opens the deck
	menu; double-tap (front only) zooms the card.
-->
<script lang="ts">
	import type {
		GameState,
		MonsterAbilityCard as AbilityCard,
		MonsterAbilityState,
		MonsterItem
	} from '$lib/protocol/types';
	import type { CampaignData } from '$lib/data/gameData';
	import type { StatMonster } from '$lib/game/line/statApplier';
	import { statMonsterFor } from '$lib/game/line/buildAbility';
	import { isInactiveForRule } from '$lib/game/rules';
	import MonsterAbilityCard from '../cards/MonsterAbilityCard.svelte';
	import AbilityCardBack from '../cards/AbilityCardBack.svelte';

	const ROW_CARD_SCALE = 0.72;

	let {
		state,
		item,
		deck,
		campaignData,
		fh,
		onOpen,
		onZoom
	}: {
		state: GameState;
		item: MonsterItem;
		/** This monster's ability deck (matched by model deck name), if any. */
		deck: MonsterAbilityState | null;
		campaignData: CampaignData | null;
		fh: boolean;
		onOpen: () => void;
		onZoom: () => void;
	} = $props();

	const model = $derived(
		campaignData?.monsters.find(
			(m) => m.name === item.type || m.display === item.type
		) ?? null
	);

	const showFront = $derived(
		state.roundState === 1 &&
			item.isActive &&
			!isInactiveForRule(state, item.type) &&
			deck != null &&
			deck.discardPile.length > 0
	);

	const cardRef = $derived(
		showFront && deck != null
			? deck.discardPile[deck.discardPile.length - 1]
			: null
	);
	const card = $derived.by((): AbilityCard | null => {
		if (cardRef == null) return null;
		const list = campaignData?.abilityDecks[cardRef.deck] ?? [];
		return list.find((c) => c.nr === cardRef.nr) ?? null;
	});

	const stat = $derived.by((): StatMonster | null => {
		if (model == null) return null;
		return statMonsterFor(
			model,
			item.level,
			item.monsterInstances.map((s) => s.type),
			item.isActive
		);
	});

	function onKeydown(e: KeyboardEvent) {
		if (e.key === 'Enter' || e.key === ' ') {
			e.preventDefault();
			onOpen();
		}
	}
</script>

<div
	class="slot"
	role="button"
	tabindex="0"
	aria-label="ability cards"
	onclick={onOpen}
	ondblclick={() => {
		if (showFront) onZoom();
	}}
	onkeydown={onKeydown}
>
	{#if showFront && card != null}
		<MonsterAbilityCard
			{card}
			{stat}
			{fh}
			flying={model?.flying ?? false}
			scale={ROW_CARD_SCALE}
			applyStats={false}
		/>
	{:else}
		<AbilityCardBack
			{fh}
			size={deck ? deck.drawPile.length : -1}
			scale={ROW_CARD_SCALE}
		/>
	{/if}
</div>

<style>
	.slot {
		flex-shrink: 0;
		margin-left: 4px;
		cursor: pointer;
	}
</style>