summaryrefslogtreecommitdiff
path: root/web/src/lib/ui/menus/SaveMenu.svelte
blob: 50268fc7cb2704baf56aee7e4187ffa80b46f4ff (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
<!--
	Save states (the reference `SaveMenu`, Phase 6): the named game-state
	saves plus "Add new Save". Tapping an entry opens the
	{@link SaveModalMenu} with Load / Save / Delete and a rename field.

	The save *content* lives on the server only (PLAN.md section 4 — it never
	crosses the wire); this menu works with names via the `save` /
	`loadSave` / `deleteSave` messages, which are player-gated on the server
	(loading a save is an undoable action, so it advances the command index).
-->
<script lang="ts">
	import type { GameSession } from '$lib/state/session';
	import { t } from '$lib/i18n';
	import { serverSettings } from '$lib/settings';
	import Menu from '../Menu.svelte';
	import SaveModalMenu from './SaveModalMenu.svelte';

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

	const gameState = $derived(session.state);
	const player = $derived(session.player);
	const ss = serverSettings;

	/**
	 * The suggested name (the reference `getSuggestedSaveName`): the campaign
	 * name plus the next free number (no space, as in the reference).
	 */
	const suggestedName = $derived.by(() => {
		const saves = $ss?.saves ?? [];
		const campaign = $gameState?.currentCampaign ?? '';
		let nr = saves.length + 1;
		while (saves.includes(campaign + nr.toString())) nr++;
		return campaign + nr.toString();
	});

	let modalName = $state<string | null>(null);
	let modalSaveOnly = $state(false);
	let modalOpen = $state(false);

	function openModal(name: string, saveOnly: boolean) {
		modalName = name;
		modalSaveOnly = saveOnly;
		modalOpen = true;
	}
</script>

<Menu open={open} title={t('loadAddDeleteSaves')}>
	<p class="note">{t('saveStateNote')}</p>

	{#if $player}
		<button class="pill" onclick={() => openModal(suggestedName, true)}>
			{t('addNewSave')}
		</button>
	{/if}

	{#each $ss?.saves ?? [] as name (name)}
		<button class="savebtn" disabled={!$player} onclick={() => openModal(name, false)}>
			{name}
		</button>
	{/each}
	{#if $ss != null && ($ss?.saves?.length ?? 0) === 0}
		<p class="empty">{t('noResultsFound')}</p>
	{/if}
</Menu>

{#if modalOpen && modalName != null}
	<SaveModalMenu
		{session}
		name={modalName}
		saveOnly={modalSaveOnly}
		bind:open={modalOpen}
	/>
{/if}

<style>
	.note {
		margin: 0 0 12px;
		padding: 0 12px;
		font-size: 13px;
		opacity: 0.85;
	}
	.pill {
		margin: 0 12px 8px;
		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;
	}
	.pill:hover {
		background: rgba(255, 255, 255, 0.25);
	}
	.savebtn {
		display: block;
		width: calc(100% - 24px);
		margin: 0 12px 4px;
		padding: 8px 12px;
		font-size: 14px;
		border: none;
		border-radius: 4px;
		background: rgba(127, 127, 127, 0.15);
		color: inherit;
		cursor: pointer;
		text-align: left;
	}
	.savebtn:hover:not(:disabled) {
		background: rgba(127, 127, 127, 0.3);
	}
	.savebtn:disabled {
		opacity: 0.4;
		cursor: default;
	}
	.empty {
		padding: 12px;
		opacity: 0.7;
		font-size: 13px;
	}
</style>