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
|
<!--
Add Character (the reference `AddCharacterMenu`): the current campaign's
class list (hidden classes surface when the search matches their name),
a level selector, and the class icon. Selecting sends `addCharacter` —
the server assigns the new character to the session that sent it.
-->
<script lang="ts">
import type { GameSession } from '$lib/state/session';
import { gameData, type ClassData } from '$lib/data/gameData';
import { isObjectiveOrEscort } from '$lib/data/classInfo';
import { classColor, classIcon } from '$lib/game/art';
import { t } from '$lib/i18n';
import Menu from '../Menu.svelte';
import SaveCharacterMenu from './SaveCharacterMenu.svelte';
let {
session,
open = $bindable(true)
}: {
session: GameSession;
open?: boolean;
} = $props();
const gameState = session.state;
let campaignData = $state<ClassData[] | null>(null);
let search = $state('');
let level = $state(1);
let error = $state<string | null>(null);
$effect(() => {
if (!open) return;
const c = $gameState?.currentCampaign;
if (!c) {
campaignData = [];
return;
}
let alive = true;
campaignData = null;
error = null;
gameData
.loadCampaign(c)
.then((d) => {
if (alive) campaignData = d.classes;
})
.catch(() => {
if (alive) error = 'failed to load campaign';
});
return () => {
alive = false;
};
});
const q = $derived(search.trim().toLowerCase());
/** Hidden classes are listed only when the search matches them. */
const classes = $derived(
(campaignData ?? []).filter((c) => !c.hidden || q.includes(c.name.toLowerCase()))
);
// The character items in the current list (their `characterClass` is the
// class id — the same key the reference's `_characterAlreadyAdded`
// compares on).
const existingClasses = $derived.by(() => {
const set = new Set<string>();
for (const item of $gameState?.currentList ?? []) {
if ('characterClass' in item) set.add(item.characterClass);
}
return set;
});
/**
* The reference disables a class once it has been added — except
* Objective/Escort, which may be added repeatedly under a numbered
* display name.
*/
function alreadyAdded(cls: ClassData): boolean {
if (isObjectiveOrEscort(cls.name)) return false;
return existingClasses.has(cls.id);
}
/** The reference's display numbering for a repeated objective/escort. */
function objectiveDisplay(cls: ClassData): string | null {
if (!isObjectiveOrEscort(cls.name)) return null;
let count = 1;
for (const item of $gameState?.currentList ?? []) {
if ('characterClass' in item && item.characterClass === cls.name) count++;
}
return count > 1 ? `${cls.name} ${count}` : null;
}
let charsOpen = $state(false);
// No player gate: the server lets a spectator add a character, and the
// adding session owns it — this is how a fresh session becomes a player
// on an empty table (PLAN.md section 3.3).
function pick(cls: ClassData) {
const display = objectiveDisplay(cls);
session.command('addCharacter', {
id: cls.id,
edition: cls.edition,
level,
...(display != null ? { display } : {})
});
open = false;
}
</script>
<Menu open={open} title={t('menuAddCharacter')}>
{#if error}
<p style="padding: 12px; opacity: 0.8">{error}</p>
{:else}
<input class="search" placeholder={t('addCharacterHint')} bind:value={search} />
<div class="statrow">
<span class="statrow__label">{t('web:level')}</span>
<button class="stepbtn" disabled={level <= 1} onclick={() => (level -= 1)}>−</button>
<span class="statrow__value">{level}</span>
<button class="stepbtn" disabled={level >= 10} onclick={() => (level += 1)}>+</button>
</div>
{#if campaignData === null}
<p style="padding: 12px; opacity: 0.7">{t('connecting')}</p>
{:else if classes.length === 0}
<p style="padding: 12px; opacity: 0.7">{t('noResultsFound')}</p>
{:else}
<ul class="menulist">
{#each classes as cls (cls.id)}
<li>
<button class="menulist__item" disabled={alreadyAdded(cls)} onclick={() => pick(cls)}>
<img src={classIcon(cls.name)} alt="" loading="lazy" style="background: {classColor(cls.color)}" />
{cls.name}
</button>
</li>
{/each}
</ul>
{/if}
{/if}
<!-- Character saves (the reference `SaveCharacterMenu` entry). -->
<button class="saveentry" onclick={() => (charsOpen = true)}>
{t('loadOrSaveCharacters')}
</button>
</Menu>
{#if charsOpen}
<SaveCharacterMenu {session} bind:open={charsOpen} />
{/if}
<style>
.saveentry {
display: block;
width: calc(100% - 24px);
margin: 8px 12px 4px;
padding: 6px 12px;
font-size: 13px;
border: none;
border-radius: 4px;
background: rgba(255, 255, 255, 0.12);
color: inherit;
cursor: pointer;
text-align: left;
}
.saveentry:hover {
background: rgba(255, 255, 255, 0.25);
}
</style>
|