summaryrefslogtreecommitdiff
path: root/web/src/lib/ui/menus/SaveCharacterMenu.svelte
blob: c237b8a5d61a285ae8f7f8982e7f6f174756c51a (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<!--
	Character saves (the reference `SaveCharacterMenu`, Phase 6):

	- "Add new Save:" — one class icon per character in the current list;
	  tapping opens the modal in save-only mode with a suggested name
	  (display name + the next free number, the reference numbering).
	- "Load Character:" — the stored character saves, each with its class
	  icon; tapping opens the modal with Load / Delete.

	As with game-state saves, the content lives on the server only; the
	messages are `saveCharacter` / `loadCharacterSave` / `deleteCharacterSave`
	(player-gated). Loading restores the character's identity (level /
	display / perks) with a fresh battle state — the reference
	`CharacterState.fromSave`.
-->
<script lang="ts">
	import type { GameSession } from '$lib/state/session';
	import { t } from '$lib/i18n';
	import { serverSettings } from '$lib/settings';
	import { isCharacter, type CharacterItem } from '$lib/protocol/types';
	import { classIcon } from '$lib/game/art';
	import Menu from '../Menu.svelte';
	import SaveCharacterModalMenu from './SaveCharacterModalMenu.svelte';

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

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

	/** The characters in the current list. */
	const characters = $derived.by((): CharacterItem[] =>
		($gameState?.currentList ?? []).filter((i): i is CharacterItem => isCharacter(i))
	);

	/**
	 * The suggested name for a character (the reference `SaveCharacterMenu`):
	 * its display name plus the next number among the existing saves whose
	 * name starts with that display name (no number when it is the first).
	 */
	function suggestedName(char: CharacterItem): string {
		const display = char.characterState.display || char.characterClass;
		let nr = 1;
		for (const s of $ss?.characterSaves ?? []) {
			if (s.name.startsWith(display)) nr++;
		}
		return display + (nr > 1 ? nr.toString() : '');
	}

	let modal = $state<{ name: string; saveId: string; characterId: string | null } | null>(
		null
	);
	let modalOpen = $state(false);

	function openFor(char: CharacterItem) {
		const name = suggestedName(char);
		modal = { name, saveId: `${name}\n${char.id}`, characterId: char.id };
		modalOpen = true;
	}

	function openExisting(name: string, characterId: string) {
		modal = { name, saveId: `${name}\n${characterId}`, characterId: null };
		modalOpen = true;
	}
</script>

<Menu open={open} title={t('loadSaveDeleteCharacters')}>
	<div class="sect">{t('addNewSaveLabel')}</div>
	{#if characters.length > 0}
		<div class="icons">
			{#each characters as char (char.id)}
				<button
					class="iconbtn"
					disabled={!$player}
					aria-label={char.characterState.display || char.characterClass}
					onclick={() => openFor(char)}
				>
					<img src={classIcon(char.characterClass)} alt="" loading="lazy" />
				</button>
			{/each}
		</div>
	{:else}
		<p class="empty">{t('noResultsFound')}</p>
	{/if}

	<div class="sect">{t('loadCharacter')}</div>
	{#each $ss?.characterSaves ?? [] as s (s.name + s.characterId)}
		<button class="savebtn" disabled={!$player} onclick={() => openExisting(s.name, s.characterId)}>
			<img src={classIcon(s.characterId)} alt="" loading="lazy" />
			{s.name}
		</button>
	{/each}
	{#if $ss != null && ($ss?.characterSaves?.length ?? 0) === 0}
		<p class="empty">{t('noResultsFound')}</p>
	{/if}
</Menu>

{#if modalOpen && modal}
	{@const m = modal}
	<SaveCharacterModalMenu
		{session}
		name={m.name}
		saveId={m.saveId}
		characterId={m.characterId}
		bind:open={modalOpen}
	/>
{/if}

<style>
	.sect {
		padding: 8px 12px 4px;
		font-size: 14px;
		font-weight: 600;
	}
	.icons {
		display: flex;
		flex-wrap: wrap;
		gap: 4px;
		padding: 0 12px 4px;
	}
	.iconbtn {
		width: 36px;
		height: 36px;
		padding: 0;
		border: none;
		border-radius: 4px;
		background: none;
		cursor: pointer;
	}
	.iconbtn:disabled {
		opacity: 0.4;
		cursor: default;
	}
	.iconbtn img {
		width: 100%;
		height: 100%;
		object-fit: contain;
	}
	.savebtn {
		display: flex;
		align-items: center;
		gap: 8px;
		width: calc(100% - 24px);
		margin: 4px 12px;
		padding: 6px 8px;
		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;
	}
	.savebtn img {
		width: 24px;
		height: 24px;
		object-fit: contain;
	}
	.empty {
		padding: 4px 12px;
		opacity: 0.7;
		font-size: 13px;
	}
</style>