diff options
Diffstat (limited to 'web/src/lib/ui/menus')
| -rw-r--r-- | web/src/lib/ui/menus/AddCharacterMenu.svelte | 51 |
1 files changed, 45 insertions, 6 deletions
diff --git a/web/src/lib/ui/menus/AddCharacterMenu.svelte b/web/src/lib/ui/menus/AddCharacterMenu.svelte index 8383f13..06a24f8 100644 --- a/web/src/lib/ui/menus/AddCharacterMenu.svelte +++ b/web/src/lib/ui/menus/AddCharacterMenu.svelte @@ -7,6 +7,7 @@ <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'; @@ -21,7 +22,6 @@ } = $props(); const gameState = session.state; - const player = session.player; let campaignData = $state<ClassData[] | null>(null); let search = $state(''); @@ -57,11 +57,50 @@ (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) { - if (!$player) return; - session.command('addCharacter', { id: cls.id, edition: cls.edition, level }); + const display = objectiveDisplay(cls); + session.command('addCharacter', { + id: cls.id, + edition: cls.edition, + level, + ...(display != null ? { display } : {}) + }); open = false; } </script> @@ -74,9 +113,9 @@ <div class="statrow"> <span class="statrow__label">{t('web:level')}</span> - <button class="stepbtn" disabled={!$player || level <= 1} onclick={() => (level -= 1)}>−</button> + <button class="stepbtn" disabled={level <= 1} onclick={() => (level -= 1)}>−</button> <span class="statrow__value">{level}</span> - <button class="stepbtn" disabled={!$player || level >= 10} onclick={() => (level += 1)}>+</button> + <button class="stepbtn" disabled={level >= 10} onclick={() => (level += 1)}>+</button> </div> {#if campaignData === null} @@ -87,7 +126,7 @@ <ul class="menulist"> {#each classes as cls (cls.id)} <li> - <button class="menulist__item" disabled={!$player} onclick={() => pick(cls)}> + <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> |
