From c0354b250f84d578b609a7f25d71dee7fc24e9ca Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Tue, 22 Aug 2023 15:01:03 +0200 Subject: currency, save/load --- resources/js/memory.js | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) (limited to 'resources/js/memory.js') diff --git a/resources/js/memory.js b/resources/js/memory.js index 550ea55..47cde88 100644 --- a/resources/js/memory.js +++ b/resources/js/memory.js @@ -52,6 +52,19 @@ const Memory = { * @param {string} saveData */ async load (saveData) { + /** + * @param {Area} areaData + */ + const loadArea = async (areaData) => { + const area = await fetchArea(areaData.slug); + + area.monsterProgress = areaData.monsterProgress; + area.trainerProgress = areaData.trainerProgress; + area.isCompleted = areaData.isCompleted; + + return area; + }; + /** * @param {Monster} monsterData */ @@ -115,6 +128,15 @@ const Memory = { return statusEffect; }; + /** + * @param {Trainer} trainerData + */ + const loadTrainer = async (trainerData) => { + const trainer = new Trainer(trainerData); + + return trainer; + }; + /** * @param {Technique} techniqueData */ @@ -131,20 +153,34 @@ const Memory = { */ const loadedState = JSON.parse(atob(saveData)); - Memory.state.Settings.language = loadedState.language; + Memory.state.Settings.language = loadedState.Settings.language; await fetchTranslation(Memory.state.Settings.language); + Memory.state.Settings.currency = loadedState.Settings.currency; + Memory.state.Settings.logMaxLength = loadedState.Settings.logMaxLength; + + for (const areaSlug of Object.keys(loadedState.areaProgress)) { + const areaData = loadedState.areaProgress[areaSlug]; + Memory.state.areaProgress[areaSlug] = await loadArea(areaData); + } + Memory.state.currentArea = await loadArea(loadedState.currentArea); Memory.state.turn = loadedState.turn; Memory.state.money = loadedState.money; Memory.state.monsters = await Promise.all(loadedState.monsters.map(async (monsterData) => await loadMonster(monsterData))); + Memory.state.player = await loadTrainer(loadedState.player); Memory.state.player.monsters = await Promise.all(loadedState.player.monsters.map(async (monsterData) => await loadMonster(monsterData))); Memory.state.player.inventory = await Promise.all(loadedState.player.inventory.map(async (itemData) => await loadInventoryItem(itemData))); Memory.state.player.activeMonster = Memory.state.player.monsters[loadedState.player.activeMonsterIdx]; + Memory.state.opponent = await loadTrainer(loadedState.opponent); Memory.state.opponent.monsters = await Promise.all(loadedState.opponent.monsters.map(async (monsterData) => await loadMonster(monsterData))); Memory.state.opponent.inventory = await Promise.all(loadedState.opponent.inventory.map(async (itemData) => await loadInventoryItem(itemData))); Memory.state.opponent.activeMonster = Memory.state.opponent.monsters[loadedState.opponent.activeMonsterIdx]; + Memory.state.rivalMonster = loadedState.rivalMonster; Memory.state.activeTechnique = await loadTechnique(loadedState.activeTechnique); + Memory.state.activeBall = await loadInventoryItem(loadedState.activeBall); + UI.drawArea(Memory.state.currentArea); + UI.drawStatus(); UI.drawOpponentMonster(); UI.drawActiveMonster(); UI.drawActiveTechniques(); -- cgit v1.2.3