Android build v1.1.39
This commit is contained in:
@@ -101,14 +101,20 @@ export type Iwt2OnlineSaveResult = {
|
||||
save: Iwt2Save | null
|
||||
}
|
||||
|
||||
const IWT2_SAVE_KEY = 'i-want-to-heal-2:save:v1'
|
||||
export const IWT2_LOCAL_SAVE_SLOTS = [1, 2, 3] as const
|
||||
export type Iwt2LocalSaveSlot = (typeof IWT2_LOCAL_SAVE_SLOTS)[number]
|
||||
export type Iwt2LocalSaveSlots = Record<Iwt2LocalSaveSlot, Iwt2Save | null>
|
||||
|
||||
export function createDefaultIwt2Save(): Iwt2Save {
|
||||
const IWT2_LEGACY_SAVE_KEY = 'i-want-to-heal-2:save:v1'
|
||||
const IWT2_SAVE_MIGRATION_KEY = 'i-want-to-heal-2:save-slots:migrated:v1'
|
||||
const IWT2_ACTIVE_SAVE_SLOT_KEY = 'i-want-to-heal-2:active-save-slot:v1'
|
||||
|
||||
export function createDefaultIwt2Save(characterName = 'Healer'): Iwt2Save {
|
||||
return {
|
||||
version: 2,
|
||||
updatedAt: Date.now(),
|
||||
character: {
|
||||
name: 'Healer',
|
||||
name: normalizeCharacterName(characterName),
|
||||
level: 1,
|
||||
experience: 0,
|
||||
healerStyle: 'dawnweaver',
|
||||
@@ -153,12 +159,38 @@ function normalizeSave(value: unknown): Iwt2Save {
|
||||
}
|
||||
}
|
||||
|
||||
export function loadIwt2Save(): Iwt2Save {
|
||||
return loadExistingIwt2Save() ?? createDefaultIwt2Save()
|
||||
function iwt2SaveSlotKey(slot: Iwt2LocalSaveSlot) {
|
||||
return `i-want-to-heal-2:save:slot-${slot}:v1`
|
||||
}
|
||||
|
||||
export function loadExistingIwt2Save(): Iwt2Save | null {
|
||||
const serialized = window.localStorage.getItem(IWT2_SAVE_KEY)
|
||||
function migrateLegacyIwt2Save() {
|
||||
if (window.localStorage.getItem(IWT2_SAVE_MIGRATION_KEY)) return
|
||||
const legacySave = window.localStorage.getItem(IWT2_LEGACY_SAVE_KEY)
|
||||
const firstSlotKey = iwt2SaveSlotKey(1)
|
||||
if (legacySave && !window.localStorage.getItem(firstSlotKey)) {
|
||||
window.localStorage.setItem(firstSlotKey, legacySave)
|
||||
}
|
||||
window.localStorage.setItem(IWT2_SAVE_MIGRATION_KEY, 'true')
|
||||
}
|
||||
|
||||
export function activeIwt2SaveSlot(): Iwt2LocalSaveSlot {
|
||||
const stored = Number(window.localStorage.getItem(IWT2_ACTIVE_SAVE_SLOT_KEY))
|
||||
return IWT2_LOCAL_SAVE_SLOTS.includes(stored as Iwt2LocalSaveSlot)
|
||||
? stored as Iwt2LocalSaveSlot
|
||||
: 1
|
||||
}
|
||||
|
||||
export function selectActiveIwt2SaveSlot(slot: Iwt2LocalSaveSlot) {
|
||||
window.localStorage.setItem(IWT2_ACTIVE_SAVE_SLOT_KEY, String(slot))
|
||||
}
|
||||
|
||||
export function loadIwt2Save(slot = activeIwt2SaveSlot()): Iwt2Save {
|
||||
return loadExistingIwt2Save(slot) ?? createDefaultIwt2Save()
|
||||
}
|
||||
|
||||
export function loadExistingIwt2Save(slot = activeIwt2SaveSlot()): Iwt2Save | null {
|
||||
migrateLegacyIwt2Save()
|
||||
const serialized = window.localStorage.getItem(iwt2SaveSlotKey(slot))
|
||||
if (!serialized) return null
|
||||
try {
|
||||
return normalizeSave(JSON.parse(serialized))
|
||||
@@ -167,6 +199,14 @@ export function loadExistingIwt2Save(): Iwt2Save | null {
|
||||
}
|
||||
}
|
||||
|
||||
export function loadIwt2SaveSlots(): Iwt2LocalSaveSlots {
|
||||
return {
|
||||
1: loadExistingIwt2Save(1),
|
||||
2: loadExistingIwt2Save(2),
|
||||
3: loadExistingIwt2Save(3),
|
||||
}
|
||||
}
|
||||
|
||||
export async function loadIwt2OnlineSave(): Promise<Iwt2OnlineSaveResult> {
|
||||
const result = await requestGameApiJson<{ save: unknown | null }>('/api/iwt2/sync-save')
|
||||
return {
|
||||
@@ -185,12 +225,12 @@ export async function writeIwt2OnlineSave(save: Iwt2Save): Promise<Iwt2OnlineSav
|
||||
}
|
||||
}
|
||||
|
||||
export function writeIwt2Save(save: Iwt2Save) {
|
||||
replaceIwt2Save(save)
|
||||
export function writeIwt2Save(save: Iwt2Save, slot = activeIwt2SaveSlot()) {
|
||||
replaceIwt2Save(save, slot)
|
||||
}
|
||||
|
||||
export function replaceIwt2Save(save: Iwt2Save) {
|
||||
window.localStorage.setItem(IWT2_SAVE_KEY, JSON.stringify(normalizeSave(save)))
|
||||
export function replaceIwt2Save(save: Iwt2Save, slot = activeIwt2SaveSlot()) {
|
||||
window.localStorage.setItem(iwt2SaveSlotKey(slot), JSON.stringify(normalizeSave(save)))
|
||||
}
|
||||
|
||||
export function recordIwt2BossKill(
|
||||
|
||||
Reference in New Issue
Block a user