55 lines
2.3 KiB
TypeScript
55 lines
2.3 KiB
TypeScript
import {
|
|
updateCharacterActionBindings,
|
|
updateCharacterProgression,
|
|
type StorageLike,
|
|
} from "./accountRepository";
|
|
import { selectedCharacter, useShellStore } from "./shellStore";
|
|
import { useCombatStore } from "../game/combatStore";
|
|
import { activateCharacterUi } from "../game/uiLayoutStore";
|
|
|
|
// Reuse the character editors without mounting the dungeon's simulation bridge.
|
|
export function beginMainMenuCharacterEditing(storage?: StorageLike): (() => void) | null {
|
|
const shell = useShellStore.getState();
|
|
const character = selectedCharacter(shell);
|
|
if (shell.phase !== "main-menu" || !shell.session || !character
|
|
|| character.ownerId !== shell.session.ownerId) return null;
|
|
|
|
const ownerId = shell.session.ownerId;
|
|
const characterId = character.id;
|
|
useCombatStore.getState().initializeCharacter(character);
|
|
activateCharacterUi(character);
|
|
|
|
return useCombatStore.subscribe((combat, previous) => {
|
|
const current = useShellStore.getState();
|
|
const profile = selectedCharacter(current);
|
|
if (current.phase !== "main-menu" || current.session?.ownerId !== ownerId
|
|
|| profile?.id !== characterId || profile.ownerId !== ownerId) return;
|
|
if (combat.talentRanks === previous.talentRanks && combat.actionBindings === previous.actionBindings) return;
|
|
|
|
try {
|
|
let updated = profile;
|
|
if (combat.talentRanks !== previous.talentRanks) {
|
|
const saved = updateCharacterProgression(ownerId, characterId, {
|
|
// Menu edits cannot award experience or alter the character's level.
|
|
level: profile.level,
|
|
experience: profile.experience,
|
|
talentRanks: combat.talentRanks,
|
|
}, storage);
|
|
if (!saved) throw new Error("Character storage is unavailable.");
|
|
updated = saved;
|
|
}
|
|
if (combat.actionBindings !== previous.actionBindings) {
|
|
const saved = updateCharacterActionBindings(ownerId, characterId, combat.actionBindings, storage);
|
|
if (!saved) throw new Error("Character storage is unavailable.");
|
|
updated = saved;
|
|
}
|
|
useShellStore.setState((state) => ({
|
|
characters: state.characters.map((entry) => entry.id === characterId ? updated : entry),
|
|
notice: "",
|
|
}));
|
|
} catch {
|
|
useShellStore.getState().setNotice("Could not save character changes. Check that profile storage is available and try again.");
|
|
}
|
|
});
|
|
}
|