summaryrefslogtreecommitdiff
path: root/web/src/lib/ui/menus/SaveCharacterMenu.svelte
diff options
context:
space:
mode:
Diffstat (limited to 'web/src/lib/ui/menus/SaveCharacterMenu.svelte')
-rw-r--r--web/src/lib/ui/menus/SaveCharacterMenu.svelte177
1 files changed, 177 insertions, 0 deletions
diff --git a/web/src/lib/ui/menus/SaveCharacterMenu.svelte b/web/src/lib/ui/menus/SaveCharacterMenu.svelte
new file mode 100644
index 0000000..c237b8a
--- /dev/null
+++ b/web/src/lib/ui/menus/SaveCharacterMenu.svelte
@@ -0,0 +1,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>