diff options
Diffstat (limited to 'resources/js/memory.js')
-rw-r--r-- | resources/js/memory.js | 48 |
1 files changed, 39 insertions, 9 deletions
diff --git a/resources/js/memory.js b/resources/js/memory.js index 6023e49..35da905 100644 --- a/resources/js/memory.js +++ b/resources/js/memory.js @@ -6,9 +6,9 @@ const Memory = { /** - * @returns {string} + * @returns {(Object|State)} */ - save () { + saveToObject () { const prepareSaveData = (saveData) => { const prepareMonster = (monster) => { if (monster.statusEffect && monster.statusEffect.slug === 'lifeleech') { @@ -62,14 +62,25 @@ const Memory = { saveData.opponent.activeMonsterIdx = Memory.state.opponent.monsters.indexOf(Memory.state.opponent.activeMonster); + return saveData; + }, + + /** + * @returns {string} + */ + saveToString () { // hash - return btoa(JSON.stringify(saveData)); + return btoa(JSON.stringify(Memory.saveToObject())); + }, + + saveToLocalStorage () { + localStorage.setItem('state', Memory.saveToString()); }, /** - * @param {string} saveData + * @param {(Object|State)} saveData */ - async load (saveData) { + async loadFromObject (saveData) { /** * @param {Area} areaData */ @@ -169,7 +180,7 @@ const Memory = { /** * @type {State} */ - const loadedState = JSON.parse(atob(saveData)); + const loadedState = saveData; Memory.state.Settings.language = loadedState.Settings.language; await fetchTranslation(Memory.state.Settings.language); @@ -177,6 +188,10 @@ const Memory = { Memory.state.Settings.currency = loadedState.Settings.currency; Memory.state.Settings.logMaxLength = loadedState.Settings.logMaxLength; + if (loadedState.Game) { // backwards compat: TODO: remove check later + Memory.state.Game.isInBattle = loadedState.Game.isInBattle; + } + for (const areaSlug of Object.keys(loadedState.areaProgress)) { const areaData = loadedState.areaProgress[areaSlug]; Memory.state.areaProgress[areaSlug] = await loadArea(areaData); @@ -199,10 +214,25 @@ const Memory = { Memory.state.activeTechnique = await loadTechnique(loadedState.activeTechnique); Memory.state.activeBall = await loadInventoryItem(loadedState.activeBall); + // draw game + if (!Game.isTown(Memory.state.currentArea)) { + UI.drawOpponentMonster(); + UI.drawActiveMonster(); + UI.drawActiveTechniques(); + } UI.drawArea(); UI.drawStatus(); - UI.drawOpponentMonster(); - UI.drawActiveMonster(); - UI.drawActiveTechniques(); + UI.closeAllPopups(); + }, + + /** + * @param {string} saveData + */ + loadFromString (saveData) { + Memory.loadFromObject(JSON.parse(atob(saveData))); + }, + + loadFromLocalStorage () { + Memory.loadFromString(localStorage.getItem('state')); }, }; |