summaryrefslogtreecommitdiff
path: root/web/src/lib/ui/CharacterRow.svelte
blob: 52ebfc38f3414bd06d9aa1df868b60cdfd110d2d (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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<!--
	A character row (the reference `CharacterWidget`): class-coloured
	backdrop, class art, the initiative slot, the name and health line with
	condition chips, and the XP / level on the right.

	Initiative (the server-gated secret, PLAN.md section 3.4):
	  - during chooseInitiative the owning player sees an editable field;
	    every other connection sees `??` (the server masks the value to
	    `null` for them)
	  - a value set by the owner is visible to the owner; after the draw the
	    number stays visible to everyone
	The right-hand tap zone focuses the initiative field (chooseInitiative)
	or ends the character's turn (playTurns), like the reference; tapping
	the row opens the stat menu.
-->
<script lang="ts">
	import type { GameSession } from '$lib/state/session';
	import type { CharacterItem, MonsterInstance } from '$lib/protocol/types';
	import { classInfo, isObjectiveOrEscort, type ClassData } from '$lib/data/classInfo';
	import { classColor, classIcon, summonImage } from '$lib/game/art';
	import { conditionChipClass, conditionLabel } from '$lib/game/conditions';
	import { standeeFigureId } from '$lib/game/stats';
	import type { FigureRef } from './StatusMenu.svelte';

	let {
		session,
		item,
		onOpenMenu,
		onAddSummon
	}: {
		session: GameSession;
		item: CharacterItem;
		onOpenMenu: (figure: FigureRef) => void;
		/** Open the add-summon menu for this character. */
		onAddSummon: () => void;
	} = $props();

	const seats = $derived(session.seats);
	const player = $derived(session.player);
	const gameState = $derived(session.state);
	const mySession = $derived(session.session); // my own {id, role} (store)

	let cls = $state<ClassData | null>(null);
	$effect(() => {
		let alive = true;
		classInfo(item.edition, item.characterClass)
			.then((c) => {
				if (alive) cls = c;
			})
			.catch(() => {});
		return () => {
			alive = false;
		};
	});

	const cs = $derived(item.characterState);
	const alive = $derived(cs.health > 0);
	const roundState = $derived($gameState?.roundState ?? 0);
	const turnState = $derived(item.turnState);
	const isObjective = $derived(isObjectiveOrEscort(item.characterClass));

	const owner = $derived($seats[item.id] ?? null);
	const mine = $derived(!!$player && owner != null && owner === $mySession?.id);
	const unclaimed = $derived(owner === null);

	const currentTurn = $derived(roundState === 1 && turnState === 1);
	const done = $derived(roundState === 1 && turnState === 2);

	const canEnterInit = $derived(
		roundState === 0 && alive && !isObjective && mine && $gameState != null
	);

	let initInput = $state<HTMLInputElement | null>(null);
	let initDraft = $state('');

	// Seed the editable field from (re)synced state so a reload or reconnect
	// doesn't blank an entry we already made. Skip while the field is focused
	// (clicking it clears it for re-entry) so we never clobber a live edit.
	$effect(() => {
		const v = cs.initiative;
		if (v == null || v <= 0) return;
		if (initInput && document.activeElement === initInput) return;
		if (String(v) !== initDraft) initDraft = String(v);
	});

	// `??` for a value masked by the server; the revealed number otherwise.
	const initDisplay = $derived(
		cs.initiative === null ? '??' : cs.initiative > 0 ? String(cs.initiative) : ''
	);
	const showInitNumber = $derived(
		roundState === 0 ? alive && !canEnterInit : alive
	);

	function onInitInput() {
		const value = initDraft.trim();
		if (
			/^\d{1,2}$/.test(value) &&
			value !== '0' &&
			cs.initiative !== Number(value)
		) {
			session.command('setInit', { characterId: item.id, init: Number(value) });
		}
	}

	function onTurnTap() {
		if (roundState === 0) {
			initInput?.focus();
			return;
		}
		if (roundState === 1 && alive && $player) {
			session.command('turnDone', { id: item.id });
		}
	}

	const figure: FigureRef = $derived({
		title: cs.display,
		figureId: item.id,
		ownerId: null,
		kind: 'character',
		rowId: item.id,
		isObjective
	});

	/** A figure ref for one of the character's summon standees. */
	function summonFigureFor(s: MonsterInstance): FigureRef {
		return {
			title: s.name || cs.display,
			figureId: standeeFigureId(s),
			ownerId: item.id,
			kind: 'summon',
			rowId: item.id,
			isObjective: false
		};
	}
</script>

{#if !isObjective}
	<div class="summons">
		{#each cs.summonList as s (standeeFigureId(s))}
			<button
				class="summon__box"
				class:summon__box--dead={s.health <= 0}
				onclick={() => onOpenMenu(summonFigureFor(s))}
				aria-label={s.name || 'summon'}
			>
				<img src={summonImage(s.gfx)} alt="" loading="lazy" />
				{#if s.standeeNr > 0}
					<span class="summon__box-nr">{s.standeeNr}</span>
				{/if}
				<span class="summon__box-hp">{s.health}</span>
			</button>
		{/each}
		<button class="summon__add" onclick={onAddSummon} aria-label="Add Summon">
			⚔+
		</button>
	</div>
{/if}

<div
	class="row row--character"
	class:row--current={currentTurn}
	class:row--done={done}
	class:row--dead={!alive}
>
	<div
		class="row__bg"
		style:background-color={cls ? classColor(cls.color) : '#3a3f47'}
	></div>

	<div class="row__inner">
		<div
			class="row__art"
			role="button"
			tabindex="0"
			onclick={() => onOpenMenu(figure)}
			onkeydown={(e) => {
				if (e.key === 'Enter' || e.key === ' ') onOpenMenu(figure);
			}}
		>
			<img src={cls ? classIcon(cls.name) : ''} alt={cs.display} loading="lazy" />
		</div>

		{#if canEnterInit}
			<input
				class="init__input"
				bind:this={initInput}
				bind:value={initDraft}
				oninput={onInitInput}
				onclick={() => (initDraft = '')}
				maxlength={2}
				inputmode="numeric"
				autocomplete="off"
				aria-label="Initiative"
			/>
		{:else if showInitNumber}
			<div class="init" class:init__secret={cs.initiative === null}>{initDisplay}</div>
		{/if}

		<div
			style="overflow: hidden; min-width: 0; flex: 1"
			role="button"
			tabindex="0"
			onclick={() => onOpenMenu(figure)}
			onkeydown={(e) => {
				if (e.key === 'Enter' || e.key === ' ') onOpenMenu(figure);
			}}
		>
			<div class="row__name">{cs.display}</div>
			<div class="row__hp">
				{cs.health} / {cs.maxHealth}
				<span class="conds">
					{#each cs.conditions as condIndex}
						<span class={conditionChipClass(condIndex)}>{conditionLabel(condIndex)}</span>
					{/each}
				</span>
			</div>
		</div>

		{#if !isObjective}
			<span class="row__xp">XP {cs.xp}</span>
		{/if}
		<span class="row__level">{cs.level}</span>

		<!-- Right-hand tap zone: initiative focus / turn done (reference). -->
		<div
			style="width: 70px; height: 100%; flex: 0 0 auto"
			role="button"
			tabindex="0"
			aria-label={roundState === 0 ? 'Initiative' : 'Turn done'}
			onclick={onTurnTap}
			onkeydown={(e) => {
				if (e.key === 'Enter' || e.key === ' ') onTurnTap();
			}}
		></div>
	</div>

	<!-- The claim chip anchors to the row (position:relative) so it sits in
	     the row's corner; kept inside `.row` so it moves with it. -->
	{#if unclaimed}
		<button class="chip chip--claim" onclick={() => session.claim(item.id)}>Claim</button>
	{/if}
</div>

<style>
	/* The summon standees sit above the row, like the reference. */
	.summons {
		display: flex;
		align-items: stretch;
		gap: 4px;
		width: calc(100% - 6px);
		margin: 2px 3px 0;
	}
	.summon__box {
		position: relative;
		width: 34px;
		height: 44px;
		padding: 0;
		border: 1px solid #2196f3;
		border-radius: 4px;
		background: rgba(0, 0, 0, 0.45);
		overflow: hidden;
		cursor: pointer;
	}
	.summon__box:focus-visible {
		outline: 2px solid #7ec8ff;
	}
	.summon__box img {
		width: 100%;
		height: 100%;
		object-fit: cover;
	}
	.summon__box--dead {
		filter: grayscale(1);
		opacity: 0.6;
	}
	.summon__box-hp {
		position: absolute;
		right: 2px;
		bottom: 1px;
		font-size: 11px;
		font-weight: 700;
		color: #fff;
		text-shadow: 0 0 2px #000;
	}
	.summon__box-nr {
		position: absolute;
		left: 2px;
		top: 1px;
		font-size: 10px;
		font-weight: 700;
		color: #fff;
		text-shadow: 0 0 2px #000;
	}
	.summon__add {
		width: 34px;
		height: 44px;
		border: 1px dashed rgba(255, 255, 255, 0.4);
		border-radius: 4px;
		background: rgba(0, 0, 0, 0.3);
		color: inherit;
		font-size: 12px;
		cursor: pointer;
	}
	.summon__add:hover {
		background: rgba(255, 255, 255, 0.12);
	}
</style>