summaryrefslogtreecommitdiff
path: root/web/src/lib/ui/decks/DeckArea.svelte
blob: 6e32deaea860932ca4f573c49143217b14b3e9bf (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<!--
	The bottom deck area (the reference `main_scaffold` bottom row): the loot
	deck on the left, and on the right the character AMD decks (behind the
	"Character Decks" toggle, the reference `CharacterAmdsWidget` open-state
	cycling), the ally deck (when `shouldShowAlliesDeck`), and the global
	deck. Tapping a deck opens its menu; double-tapping zooms its top
	discard card.
-->
<script lang="ts">
	import type { GameSession } from '$lib/state/session';
	import type {
		CharacterItem,
		ModifierCard as Card
	} from '$lib/protocol/types';
	import { isCharacter } from '$lib/protocol/types';
	import type { CampaignData, ClassData } from '$lib/data/gameData';
	import { t } from '$lib/i18n';
	import {
		currentCharacterId,
		modifierDeckByName,
		shouldShowAlliesDeck
	} from '$lib/game/rules';
	import LootDeckWidget from './LootDeckWidget.svelte';
	import ModifierDeckWidget from './ModifierDeckWidget.svelte';

	type OpenState = 'noOpen' | 'oneOpen' | 'allOpen';

	let {
		session,
		campaignData,
		onOpenAmd,
		onAmdZoom,
		onOpenLoot
	}: {
		session: GameSession;
		campaignData: CampaignData | null;
		onOpenAmd: (name: string) => void;
		onAmdZoom: (card: Card, name: string, perks: string[][] | null) => void;
		onOpenLoot: () => void;
	} = $props();

	const gameState = session.state;

	// The class data for a character item (null until the campaign loads).
	function classFor(char: CharacterItem): ClassData | null {
		return (
			campaignData?.classes.find(
				(c) => c.name === char.characterClass || c.id === char.characterClass
			) ?? null
		);
	}

	// Characters whose class has perks (the reference `charsWithPerks`).
	const charsWithPerks = $derived.by((): CharacterItem[] => {
		const gs = $gameState;
		if (!gs || campaignData == null) return [];
		return gs.currentList
			.filter((i): i is CharacterItem => isCharacter(i))
			.filter((c) => (classFor(c)?.perkDetails.length ?? 0) > 0);
	});

	const currentCharacter = $derived.by((): CharacterItem | null => {
		const gs = $gameState;
		if (!gs) return null;
		const id = currentCharacterId(gs);
		if (id == null) return null;
		return (
			gs.currentList.find((i): i is CharacterItem => isCharacter(i) && i.id === id) ??
			null
		);
	});

	const canShowOneDeck = $derived(
		($gameState?.roundState ?? 0) === 1 &&
			currentCharacter != null &&
			charsWithPerks.some((c) => c.id === currentCharacter.id)
	);

	// The reference keeps the user intent per round state; the toggle cycles
	// between them (chooseInitiative: noOpen <-> allOpen; playTurns:
	// oneOpen <-> allOpen when the current character can show one deck).
	let playTurnsIntent = $state<OpenState>('oneOpen');
	let chooseInitIntent = $state<OpenState>('allOpen');

	const visible = $derived.by((): OpenState => {
		const roundState = $gameState?.roundState ?? 0;
		if (roundState !== 1) return chooseInitIntent;
		if (playTurnsIntent === 'oneOpen' && canShowOneDeck) return 'oneOpen';
		return playTurnsIntent;
	});

	function toggle() {
		const roundState = $gameState?.roundState ?? 0;
		if (roundState !== 1) {
			chooseInitIntent = chooseInitIntent === 'noOpen' ? 'allOpen' : 'noOpen';
			return;
		}
		if (canShowOneDeck) {
			playTurnsIntent =
				playTurnsIntent === 'oneOpen'
					? 'allOpen'
					: playTurnsIntent === 'allOpen'
						? 'oneOpen'
						: 'oneOpen';
		} else {
			playTurnsIntent =
				playTurnsIntent === 'noOpen'
					? 'allOpen'
					: playTurnsIntent === 'allOpen'
						? 'oneOpen'
						: 'allOpen';
		}
	}

	function perksFor(char: CharacterItem): string[][] | null {
		return classFor(char)?.perkDetails.map((p) => p.adds) ?? null;
	}

	function onZoom(name: string, char: CharacterItem | null) {
		const gs = $gameState;
		if (!gs) return;
		const deck = modifierDeckByName(gs, name);
		if (!deck || deck.discardPile.length === 0) return;
		const card = deck.discardPile[deck.discardPile.length - 1];
		onAmdZoom(card, name, char ? perksFor(char) : null);
	}

	const showAmds = $derived(campaignData != null && charsWithPerks.length > 0);
</script>

<div class="deckarea">
	<div class="deckarea__loot">
		{#if $gameState}
			<LootDeckWidget {session} deck={$gameState.lootDeck} onOpenMenu={onOpenLoot} />
		{/if}
	</div>

	<div class="deckarea__decks">
		{#if showAmds}
			<button class="toggle" onclick={toggle}>{t('characterDecks')}</button>
			{#if visible !== 'noOpen'}
				{#if visible === 'oneOpen' && canShowOneDeck && currentCharacter}
					<ModifierDeckWidget
						{session}
						deck={currentCharacter.characterState.modifierDeck}
						name={currentCharacter.id}
						className={currentCharacter.characterClass}
						perks={perksFor(currentCharacter)}
						onOpenMenu={() => onOpenAmd(currentCharacter.id)}
						onZoom={() => onZoom(currentCharacter.id, currentCharacter)}
					/>
				{:else}
					{#each charsWithPerks as char (char.id)}
						<ModifierDeckWidget
							{session}
							deck={char.characterState.modifierDeck}
							name={char.id}
							className={char.characterClass}
							perks={perksFor(char)}
							onOpenMenu={() => onOpenAmd(char.id)}
							onZoom={() => onZoom(char.id, char)}
						/>
					{/each}
				{/if}
			{/if}
		{/if}

		{#if $gameState && shouldShowAlliesDeck($gameState)}
			<ModifierDeckWidget
				{session}
				deck={$gameState.modifierDeckAllies}
				name="allies"
				className={null}
				onOpenMenu={() => onOpenAmd('allies')}
				onZoom={() => onZoom('allies', null)}
			/>
		{/if}

		{#if $gameState}
			<ModifierDeckWidget
				{session}
				deck={$gameState.modifierDeck}
				name=""
				className={null}
				onOpenMenu={() => onOpenAmd('')}
				onZoom={() => onZoom('', null)}
			/>
		{/if}
	</div>
</div>

<style>
	.deckarea {
		display: flex;
		justify-content: center;
		align-items: center;
		gap: 24px;
		padding: 4px 8px;
	}
	.deckarea__loot {
		display: flex;
		align-items: center;
	}
	.deckarea__decks {
		display: flex;
		flex-direction: column;
		gap: 4px;
		align-items: flex-end;
	}
	.toggle {
		padding: 2px 10px;
		font-size: 12px;
		border: none;
		border-radius: 4px;
		background: rgba(255, 255, 255, 0.15);
		color: inherit;
		cursor: pointer;
	}
	.toggle:hover {
		background: rgba(255, 255, 255, 0.3);
	}
</style>