summaryrefslogtreecommitdiff
path: root/web/src/lib/ui/menus/SelectScenarioMenu.svelte
blob: 14add4132f3e49ca28bd896202a84cc38046afff (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
<!--
	Set Scenario (the reference `SelectScenarioMenu`): pick a campaign, then
	a scenario from it. Selecting sends `setCampaign` (when the campaign
	changed) followed by `setScenario` — the server applies them in order.
-->
<script lang="ts">
	import type { GameSession } from '$lib/state/session';
	import { gameData, type CampaignData } 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 campaigns = $state<string[] | null>(null);
	let campaign = $state('');
	let data = $state<CampaignData | null>(null);
	let error = $state<string | null>(null);

	// Load the campaign list and default to the current campaign.
	$effect(() => {
		if (!open) return;
		let alive = true;
		campaigns = null;
		error = null;
		gameData
			.campaigns()
			.then((list) => {
				if (!alive) return;
				campaigns = list;
				campaign = $gameState?.currentCampaign && list.includes($gameState.currentCampaign)
					? $gameState.currentCampaign
					: list[0] ?? '';
			})
			.catch(() => {
				if (alive) error = 'failed to load campaigns';
			});
		return () => {
			alive = false;
		};
	});

	// Load the selected campaign's scenarios.
	$effect(() => {
		const c = campaign;
		if (!open || !c) return;
		let alive = true;
		data = null;
		gameData
			.loadCampaign(c)
			.then((d) => {
				if (alive) data = d;
			})
			.catch(() => {
				if (alive) error = 'failed to load campaign';
			});
		return () => {
			alive = false;
		};
	});

	function pickScenario(scenario: string) {
		if (!$player) return;
		if ($gameState && $gameState.currentCampaign !== campaign) {
			session.command('setCampaign', { campaign });
		}
		session.command('setScenario', { scenario, section: false });
		open = false;
	}
</script>

<Menu open={open} title={t('menuSetScenario')}>
	{#if error}
		<p style="padding: 12px; opacity: 0.8">{error}</p>
	{:else if campaigns}
		<div class="campaignbtns">
			{#each campaigns as c (c)}
				<button class:button--active={c === campaign} onclick={() => (campaign = c)}>
					{c}
				</button>
			{/each}
		</div>
		{#if data}
			{#if data.scenarios.length === 0}
				<p style="padding: 12px; opacity: 0.7">{t('noResultsFound')}</p>
			{:else}
				<ul class="menulist">
					{#each data.scenarios as s (s.name)}
						<li>
							<button class="menulist__item" disabled={!$player} onclick={() => pickScenario(s.name)}>
								{s.name}
							</button>
						</li>
					{/each}
				</ul>
			{/if}
		{:else}
			<p style="padding: 12px; opacity: 0.7">{t('connecting')}</p>
		{/if}
	{/if}
</Menu>