summaryrefslogtreecommitdiff
path: root/web/src/lib/ui/StatusMenu.svelte
blob: b14e2b0da27cabaf9b50d08de7d7c5fdc5a0416e (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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
<!--
	The per-figure stat menu (the reference `StatusMenu`): ± steppers for
	health, max health, xp and the bless/curse/empower/enfeeble counters,
	condition toggles, and turn-done. Values are read live from the state so
	the dialog stays in sync while open. All actions are commands to the
	server (players only — the server rejects spectators).
-->
<script lang="ts">
	import type { GameSession } from '$lib/state/session';
	import type {
		CharacterItem,
		ModifierDeck,
		MonsterInstance,
		MonsterItem
	} from '$lib/protocol/types';
	import {
		characterStatCounters,
		standeeFigureId,
		standeeStatCounters
	} from '$lib/game/stats';
	import { isObjectiveOrEscort } from '$lib/data/classInfo';
	import { CONDITIONS } from '$lib/game/conditions';
	import { t } from '$lib/i18n';
	import { clientSettings } from '$lib/settings';
	import type { CampaignData } from '$lib/data/gameData';
	import Menu from './Menu.svelte';
	import FigureNoteMenu from './menus/FigureNoteMenu.svelte';

	/**
	 * The "custom content" conditions, hidden when the `showCustomContent`
	 * client setting is off (the reference `showCustomContent` gate in the
	 * status-menu condition panels: dodge / infect / rupture / poison x2).
	 */
	const CUSTOM_CONDITIONS = new Set(['dodge', 'infect', 'rupture', 'poison2']);

	export interface FigureRef {
		title: string;
		/** Wire `figureId`: the character id, or `name+gfx+standeeNr`. */
		figureId: string;
		ownerId: string | null;
		kind: 'character' | 'monster' | 'summon';
		/** The row's id (for turn-done / activate). */
		rowId: string;
		/** True for Escort / Objective characters (no xp, no initiative). */
		isObjective: boolean;
	}

	let {
		session,
		figure,
		campaignData,
		onAddNoteRow,
		open = $bindable(true)
	}: {
		session: GameSession;
		figure: FigureRef;
		/** Campaign data (monster model lookup for the Ice Wraith gate). */
		campaignData?: CampaignData | null;
		/** Open the note-row editor with a preset link target. */
		onAddNoteRow: (presetLinkId: string) => void;
		open?: boolean;
	} = $props();

	const gameState = session.state;
	const player = session.player;

	function character(): CharacterItem | null {
		if (figure.kind !== 'character' || !$gameState) return null;
		return $gameState.currentList.find(
			(item) => item.id === figure.figureId && 'characterState' in item
		) as CharacterItem | null;
	}

	function ownerItem(): MonsterItem | CharacterItem | null {
		if (!figure.ownerId || !$gameState) return null;
		return $gameState.currentList.find((item) => item.id === figure.ownerId) as
			| MonsterItem
			| CharacterItem
			| null;
	}

	function standee() {
		const owner = ownerItem();
		if (!owner) return null;
		if ('monsterInstances' in owner) {
			return owner.monsterInstances.find(
				(s: MonsterInstance) => standeeFigureId(s) === figure.figureId
			);
		}
		if ('characterState' in owner) {
			return owner.characterState.summonList.find(
				(s: MonsterInstance) => standeeFigureId(s) === figure.figureId
			);
		}
		return null;
	}

	const liveCharacter = $derived(figure.kind === 'character' ? character() : null);
	const liveStandee = $derived(figure.kind !== 'character' ? standee() : null);
	const liveOwner = $derived(figure.kind !== 'character' ? ownerItem() : null);

	const health = $derived(
		liveCharacter?.characterState.health ?? liveStandee?.health ?? null
	);
	const maxHealth = $derived(
		liveCharacter?.characterState.maxHealth ?? liveStandee?.maxHealth ?? null
	);
	const xp = $derived(
		figure.kind === 'character' && !figure.isObjective
			? liveCharacter?.characterState.xp ?? 0
			: null
	);

	// Summons use the owning character's modifier deck; monsters use the
	// scenario (global) deck — mirroring the reference's `deck` getter.
	const counters = $derived.by(() => {
		if (figure.kind === 'character') {
			const c = liveCharacter;
			return c ? characterStatCounters(c) : null;
		}
		const owner = liveOwner;
		if (!owner) return null;
		const deck: ModifierDeck | undefined = 'characterState' in owner
			? owner.characterState.modifierDeck
			: $gameState?.modifierDeck;
		return deck ? standeeStatCounters(deck) : null;
	});

	const conditions = $derived(
		liveCharacter?.characterState.conditions ?? liveStandee?.conditions ?? []
	);

	const roundState = $derived($gameState?.roundState ?? 0);
	const canAct = $derived(!!$player && ($gameState != null));

	function cmd(name: string, args: Record<string, unknown>) {
		session.command(name, args);
	}

	function change(name: string, delta: number, extra: Record<string, unknown> = {}) {
		cmd(name, { change: delta, figureId: figure.figureId, ...extra });
	}

	function toggleCondition(index: number, name: string) {
		const args: Record<string, unknown> = { condition: name, figureId: figure.figureId };
		if (figure.ownerId) args.ownerId = figure.ownerId;
		if (conditions.includes(index)) cmd('removeCondition', args);
		else cmd('addCondition', args);
	}

	// --- Phase 5 extras ------------------------------------------------------

	/** The owning monster's model (Ice Wraith gate), for monster figures. */
	const monsterModel = $derived.by(() => {
		if (figure.kind !== 'monster' || !campaignData) return null;
		const owner = liveOwner;
		if (owner == null || !('monsterInstances' in owner)) return null;
		return (
			campaignData.monsters.find(
				(m) => m.name === owner.type || m.display === owner.type
			) ?? null
		);
	});

	const isIceWraith = $derived(monsterModel?.deck === 'Ice Wraith');
	const isElite = $derived(liveStandee?.type === 1);

	function switchForm() {
		// The command takes the *current* elite state and toggles it.
		cmd('iceWraithChangeForm', {
			isElite,
			ownerId: figure.ownerId ?? '',
			figureId: figure.figureId
		});
	}

	// The summon mark (the reference `StatusMenuSummonButton`).
	const summonMarked = $derived(liveStandee?.roundSummoned !== -1);

	function toggleSummon() {
		cmd('setAsSummon', {
			summoned: !summonMarked,
			figureId: figure.figureId,
			ownerId: figure.ownerId ?? ''
		});
	}

	// The figure's quick note (characters and monsters; not summons).
	const noteFigure = $derived.by(() => {
		if (figure.kind === 'character') return liveCharacter;
		if (figure.kind === 'monster')
			return liveOwner && 'note' in liveOwner ? liveOwner : null;
		return null;
	});

	let noteOpen = $state(false);

	function openNote() {
		noteOpen = true;
	}

	/** The link target preset when adding a note row from here. */
	function presetLinkTarget(): string {
		return figure.ownerId ?? figure.figureId;
	}

	// --- Character level + name (the reference `SetCharacterLevelMenu`) ------

	/** The character's class model (its per-level health bounds the level range). */
	const charClass = $derived.by(() => {
		if (figure.kind !== 'character' || !liveCharacter || !campaignData)
			return null;
		return (
			campaignData.classes.find(
				(c) =>
					c.id === liveCharacter.characterClass ||
					c.name === liveCharacter.characterClass
			) ?? null
		);
	});

	const maxLevel = $derived(charClass?.health.length ?? 9);
	const currentLevel = $derived(liveCharacter?.characterState.level ?? 1);

	function setLevel(nr: number) {
		if (nr === currentLevel) return;
		cmd('setCharacterLevel', { level: nr, characterId: figure.figureId });
	}

	// The name field mirrors the server's display, resetting only when the
	// display itself changes (so a live edit is never clobbered by an
	// unrelated state update).
	let nameDraft = $state('');
	let lastSeenDisplay = $state('');
	$effect(() => {
		const display = liveCharacter?.characterState.display ?? '';
		if (display !== lastSeenDisplay) {
			lastSeenDisplay = display;
			nameDraft = display;
		}
	});

	function submitName() {
		const name = nameDraft.trim();
		if (name && name !== liveCharacter?.characterState.display) {
			cmd('changeName', { name, characterId: figure.figureId });
		}
	}
</script>

<Menu open={open} title={figure.title}>
	{#if health != null && maxHealth != null}
		<div class="statrow">
			<span class="statrow__label">Health</span>
			<span class="statrow__value">{health} / {maxHealth}</span>
			<button class="stepbtn" disabled={!canAct} onclick={() => change('changeHealth', 1)}>+</button>
			<button class="stepbtn" disabled={!canAct || health <= 0} onclick={() => change('changeHealth', -1)}>−</button>
		</div>
		<div class="statrow">
			<span class="statrow__label">Max Health</span>
			<span class="statrow__value">{maxHealth}</span>
			<button class="stepbtn" disabled={!canAct} onclick={() => change('changeMaxHealth', 1)}>+</button>
			<button class="stepbtn" disabled={!canAct || health >= maxHealth} onclick={() => change('changeMaxHealth', -1)}>−</button>
		</div>
	{/if}

	{#if xp != null}
		<div class="statrow">
			<span class="statrow__label">XP</span>
			<span class="statrow__value">{xp}</span>
			<button class="stepbtn" disabled={!canAct} onclick={() => change('changeXP', 10)}>+</button>
			<button class="stepbtn" disabled={!canAct || xp <= 0} onclick={() => change('changeXP', -10)}>−</button>
		</div>
	{/if}

	<!-- Level + name (the reference `SetCharacterLevelMenu`); characters only. -->
	{#if figure.kind === 'character' && !figure.isObjective && liveCharacter}
		<div class="statrow statrow--wrap">
			<span class="statrow__label">{t('web:level')}</span>
			<div class="levelbtns">
				{#each Array(maxLevel) as _, i}
					<button
						class="pill"
						class:pill--on={i + 1 === currentLevel}
						disabled={!canAct || i + 1 === currentLevel}
						onclick={() => setLevel(i + 1)}
					>{i + 1}</button>
				{/each}
			</div>
		</div>
		<div class="statrow">
			<span class="statrow__label">{t('changeName')}</span>
			<input
				class="search"
				style="margin: 0; width: auto; flex: 1"
				value={nameDraft}
				oninput={(e) => (nameDraft = e.currentTarget.value)}
				onkeydown={(e) => {
					if (e.key === 'Enter') submitName();
				}}
			/>
		</div>
	{/if}

	{#if counters}
		<div class="statrow">
			<span class="statrow__label">{t('cmdAddBless')}</span>
			<span class="statrow__value">{counters.bless}</span>
			<button class="stepbtn" disabled={!canAct} onclick={() => change('changeBless', 1)}>+</button>
			<button class="stepbtn" disabled={!canAct || counters.bless <= 0} onclick={() => change('changeBless', -1)}>−</button>
		</div>
		<div class="statrow">
			<span class="statrow__label">{t('cmdAddCurse')}</span>
			<span class="statrow__value">{counters.curse}</span>
			<button class="stepbtn" disabled={!canAct} onclick={() => change('changeCurse', 1)}>+</button>
			<button class="stepbtn" disabled={!canAct || counters.curse <= 0} onclick={() => change('changeCurse', -1)}>−</button>
		</div>
		<div class="statrow">
			<span class="statrow__label">{t('cmdAddEmpower')}</span>
			<span class="statrow__value">{counters.empower}</span>
			<button class="stepbtn" disabled={!canAct} onclick={() => change('changeEmpower', 1, { gfx: 'in-empower' })}>+</button>
			<button class="stepbtn" disabled={!canAct || counters.empower <= 0} onclick={() => change('changeEmpower', -1, { gfx: 'in-empower' })}>−</button>
		</div>
		<div class="statrow">
			<span class="statrow__label">{t('cmdAddEnfeeble')}</span>
			<span class="statrow__value">{counters.enfeeble}</span>
			<button class="stepbtn" disabled={!canAct} onclick={() => change('changeEnfeeble', 1, { gfx: 'in-enfeeble' })}>+</button>
			<button class="stepbtn" disabled={!canAct || counters.enfeeble <= 0} onclick={() => change('changeEnfeeble', -1, { gfx: 'in-enfeeble' })}>−</button>
		</div>
	{/if}

	<!-- The custom-content conditions (the reference `showCustomContent`
	     gate) are hidden when "show custom content" is off. -->
	<div class="condgrid">
		{#each CONDITIONS as c, i}
			{#if $clientSettings.showCustomContent || !CUSTOM_CONDITIONS.has(c.name)}
				<button
					class:condgrid--on={conditions.includes(i)}
					disabled={!canAct}
					onclick={() => toggleCondition(i, c.name)}
				>
					{c.label}
				</button>
			{/if}
		{/each}
	</div>

	<div class="dialog__body" style="padding-top: 8px">
		{#if isIceWraith}
			<button
				class="stepbtn"
				style="width: 100%; height: 36px"
				disabled={!canAct}
				onclick={switchForm}
			>
				Switch Form
			</button>
		{/if}
		{#if figure.kind === 'monster' && liveStandee}
			<button
				class="stepbtn"
				class:stepbtn--on={summonMarked}
				style="width: 100%; height: 36px"
				disabled={!canAct}
				onclick={toggleSummon}
			>
				{summonMarked ? t('cmdRemoveSummonMark') : t('cmdMarkAsSummon')}
			</button>
		{/if}
		{#if noteFigure}
			<button
				class="stepbtn"
				style="width: 100%; height: 36px"
				disabled={!canAct}
				onclick={openNote}
			>
				{noteFigure.note || t('characterNoteQuickAdd')}
			</button>
		{/if}
		<button
			class="stepbtn"
			style="width: 100%; height: 36px"
			disabled={!canAct}
			onclick={() => onAddNoteRow(presetLinkTarget())}
		>
			{t('noteRowAddFromMenu')}
		</button>
	</div>

	{#if roundState === 1}
		<div class="dialog__body" style="padding-top: 8px">
			<button
				class="stepbtn"
				style="width: 100%; height: 40px"
				disabled={!canAct}
				onclick={() => {
					cmd('turnDone', { id: figure.rowId });
					open = false;
				}}
			>
				{t('cmdTurnDone', { id: figure.title })}
			</button>
		</div>
	{/if}
</Menu>

{#if noteOpen && noteFigure}
	<FigureNoteMenu
		{session}
		figureId={figure.kind === 'character' ? figure.figureId : (figure.ownerId ?? figure.figureId)}
		initial={noteFigure.note}
		bind:open={noteOpen}
	/>
{/if}

<style>
	.statrow--wrap {
		flex-wrap: wrap;
	}
	.levelbtns {
		display: flex;
		flex-wrap: wrap;
		gap: 4px;
		flex: 1;
		justify-content: flex-end;
	}
	.pill {
		min-width: 26px;
		padding: 3px 6px;
		font-size: 13px;
		border: none;
		border-radius: 4px;
		background: rgba(255, 255, 255, 0.12);
		color: inherit;
		cursor: pointer;
	}
	.pill:hover {
		background: rgba(255, 255, 255, 0.25);
	}
	.pill--on {
		background: #2196f3;
		color: #fff;
	}
	.pill:disabled {
		opacity: 0.4;
		cursor: default;
	}
</style>