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
|
<!--
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, MonsterInstance, MonsterItem } from '$lib/protocol/types';
import {
characterStatCounters,
standeeFigureId,
standeeStatCounters,
type ModifierDeckJson
} from '$lib/game/stats';
import { isObjectiveOrEscort } from '$lib/data/classInfo';
import { CONDITIONS } from '$lib/game/conditions';
import { t } from '$lib/i18n';
import Menu from './Menu.svelte';
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,
open = $bindable(true)
}: {
session: GameSession;
figure: FigureRef;
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 = 'characterState' in owner
? (owner.characterState.modifierDeck as ModifierDeckJson | undefined)
: ($gameState?.modifierDeck as ModifierDeckJson | undefined);
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);
}
</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}
{#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}
<div class="condgrid">
{#each CONDITIONS as c, i}
<button
class:condgrid--on={conditions.includes(i)}
disabled={!canAct}
onclick={() => toggleCondition(i, c.name)}
>
{c.label}
</button>
{/each}
</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>
|