summaryrefslogtreecommitdiff
path: root/web/src/lib/ui/menus/AddSectionMenu.svelte
blob: 8fdef55a8a4b5d2882da08cafa46b26a56cabda8 (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
<!--
	Add Section (the reference `AddSectionMenu`): lists the addable sections
	of the current scenario (its rooms after the start room, minus spawns)
	and adds one via `setScenario { scenario, section: true }`.

	Reference behaviors ported here:
	- case-insensitive search filter over the section names;
	- ordering by the section number (`findNrFromScenarioName`) with a
	  string-compare fallback;
	- sections already in the scenario are greyed out and tapping them is a
	  no-op;
	- the search label is "Add Random Dungeon Card" when the scenario is
	  "#Random Dungeon".
-->
<script lang="ts">
	import type { GameSession } from '$lib/state/session';
	import { gameData } from '$lib/data/gameData';
	import { t } from '$lib/i18n';
	import Menu from '../Menu.svelte';

	let {
		session,
		open = $bindable(true)
	}: {
		session: GameSession;
		open?: boolean;
	} = $props();

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

	let sections = $state<string[] | null>(null);
	let search = $state('');

	/** Port of the reference `GameMethods.findNrFromScenarioName`: drop the
	 * first character, read until a space or `.`, and parse an int. Null when
	 * the remainder is not a number. */
	function sectionNr(name: string): number | null {
		const rest = name.slice(1);
		const end = rest.search(/[ .]/);
		const nr = end === -1 ? rest : rest.slice(0, end);
		if (nr === '') return null;
		const n = Number(nr);
		return Number.isInteger(n) ? n : null;
	}

	$effect(() => {
		if (!open) return;
		const s = $gameState?.scenario ?? '';
		const c = $gameState?.currentCampaign ?? '';
		search = '';
		if (!s || !c) {
			sections = [];
			return;
		}
		let alive = true;
		sections = null;
		gameData
			.sections(c, s)
			.then((list) => {
				if (alive) sections = list;
			})
			.catch(() => {
				if (alive) sections = [];
			});
		return () => {
			alive = false;
		};
	});

	const shown = $derived.by(() => {
		const all = sections ?? [];
		const q = search.trim().toLowerCase();
		const filtered = q
			? all.filter((n) => n.toLowerCase().includes(q))
			: all;
		return [...filtered].sort((a, b) => {
			const aNr = sectionNr(a);
			const bNr = sectionNr(b);
			if (aNr != null && bNr != null) return aNr - bNr;
			return a < b ? -1 : a > b ? 1 : 0;
		});
	});

	const added = $derived($gameState?.scenarioSectionsAdded ?? []);

	function isAdded(section: string): boolean {
		return added.includes(section);
	}

	function pick(section: string) {
		if (!$player || isAdded(section)) return;
		// For sections the wire `scenario` field carries the *section name*;
		// the server matches it against the current scenario's sections.
		session.command('setScenario', { scenario: section, section: true });
		open = false;
	}

	/** The reference relabels the search field on the random dungeon. */
	const searchLabel = $derived(
		$gameState?.scenario === '#Random Dungeon'
			? t('menuAddRandomDungeonCard')
			: t('menuAddSection')
	);
</script>

<Menu open={open} title={t('menuAddSection')}>
	{#if !$gameState?.scenario}
		<p class="empty">{t('web:noScenario')}</p>
	{:else}
		<input class="search" placeholder={searchLabel} bind:value={search} />
		{#if sections === null}
			<p class="empty">{t('connecting')}</p>
		{:else if shown.length === 0}
			<p class="empty">{t('noResultsFound')}</p>
		{:else}
			<ul class="menulist">
				{#each shown as section (section)}
					<li>
						<button
							class="menulist__item"
							class:menulist__item--added={isAdded(section)}
							disabled={!$player || isAdded(section)}
							onclick={() => pick(section)}
						>
							{section}
						</button>
					</li>
				{/each}
			</ul>
		{/if}
	{/if}
</Menu>

<style>
	.empty {
		padding: 12px;
		opacity: 0.7;
	}
	/* The reference greys added sections (Colors.blueGrey on a light theme). */
	.menulist__item--added {
		color: #90a4ae;
	}
</style>