summaryrefslogtreecommitdiff
path: root/web/src/lib/ui/menus/AddSectionMenu.svelte
blob: 991b6a5d0de82e07094073871d9755beeece22da (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
<!--
	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 }`.
-->
<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);

	$effect(() => {
		if (!open) return;
		const s = $gameState?.scenario ?? '';
		const c = $gameState?.currentCampaign ?? '';
		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;
		};
	});

	function pick(section: string) {
		if (!$player) 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;
	}
</script>

<Menu open={open} title={t('menuAddSection')}>
	{#if !$gameState?.scenario}
		<p style="padding: 12px; opacity: 0.7">{t('web:noScenario')}</p>
	{:else if sections === null}
		<p style="padding: 12px; opacity: 0.7">{t('connecting')}</p>
	{:else if sections.length === 0}
		<p style="padding: 12px; opacity: 0.7">{t('noResultsFound')}</p>
	{:else}
		<ul class="menulist">
			{#each sections as section (section)}
				<li>
					<button
						class="menulist__item"
						disabled={!$player}
						onclick={() => pick(section)}
					>
						{section}
					</button>
				</li>
			{/each}
		</ul>
	{/if}
</Menu>