const Memory = { /** * @type {State} */ state: new State(), /** * @returns {string} */ save () { const prepareSaveData = (saveData) => { const prepareMonster = (monster) => { if (monster.statusEffect && monster.statusEffect.slug === 'lifeleech') { monster.statusEffect = null; } return monster; }; for (const idx in saveData.monsters) { saveData.monsters[idx] = prepareMonster(saveData.monsters[idx]); } for (const idx in saveData.player.monsters) { saveData.player.monsters[idx] = prepareMonster(saveData.player.monsters[idx]); } for (const idx in saveData.opponent.monsters) { saveData.opponent.monsters[idx] = prepareMonster(saveData.opponent.monsters[idx]); } return JSON.parse(JSON.stringify(saveData)); }; const saveMonster = (monsterData, monsterState) => { monsterData.level = monsterState.level; monsterData.hp = monsterState.hp; return monsterData; }; /** * @type {State} */ const saveData = prepareSaveData(Object.assign({}, Memory.state)); // monsters for (const idx in saveData.monsters) { saveData.monsters[idx] = saveMonster(saveData.monsters[idx], Memory.state.monsters[idx]); } // player - monsters for (const idx in saveData.player.monsters) { saveData.player.monsters[idx] = saveMonster(saveData.player.monsters[idx], Memory.state.player.monsters[idx]); } saveData.player.activeMonsterIdx = Memory.state.player.monsters.indexOf(Memory.state.player.activeMonster); // opponent - monsters for (const idx in saveData.opponent.monsters) { saveData.opponent.monsters[idx] = saveMonster(saveData.opponent.monsters[idx], Memory.state.opponent.monsters[idx]); } saveData.opponent.activeMonsterIdx = Memory.state.opponent.monsters.indexOf(Memory.state.opponent.activeMonster); // hash return btoa(JSON.stringify(saveData)); }, /** * @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 */ const loadMonster = async (monsterData) => { const monster = await fetchMonster(monsterData.slug); monster.level = monsterData.level; monster.hp = monsterData.hp; monster.exp = monsterData.exp; monster.tasteWarm = monsterData.tasteWarm; monster.tasteCold = monsterData.tasteCold; monster.gender = monsterData.gender; monster.heldItem = await loadItem(monsterData.heldItem); monster.statusEffect = await loadStatusEffect(monsterData.statusEffect); monster.statModifiers = monsterData.statModifiers; monster.experienceModifier = monsterData.experienceModifier; monster.moneyModifier = monsterData.moneyModifier; monster.activeTechniques = await Promise.all(monsterData.activeTechniques.map(async (technique) => { return loadTechnique(technique); })); return monster; }; /** * @param {InventoryItem} inventoryItemData */ const loadInventoryItem = async (inventoryItemData) => { const inventoryItem = new InventoryItem(await fetchItem(inventoryItemData.item.slug)); inventoryItem.quantity = inventoryItemData.quantity; return inventoryItem; }; /** * @param {Item} itemData */ const loadItem = async (itemData) => { if (!itemData) { return null; } const item = await fetchItem(itemData.slug); return item; }; /** * @param {StatusEffect} statusEffectData */ const loadStatusEffect = async (statusEffectData) => { if (!statusEffectData) { return null; } const statusEffect = await fetchStatusEffect(statusEffectData.slug); statusEffect.turnsLeft = statusEffectData.turnsLeft; return statusEffect; }; /** * @param {Trainer} trainerData */ const loadTrainer = async (trainerData) => { const trainer = new Trainer(trainerData); return trainer; }; /** * @param {Technique} techniqueData */ const loadTechnique = async (techniqueData) => { const technique = await fetchTechnique(techniqueData.slug); technique.turnLastUse = techniqueData.turnLastUse; return technique; }; /** * @type {State} */ const loadedState = JSON.parse(atob(saveData)); Memory.state.Settings.language = loadedState.Settings.language; await fetchTranslation(Memory.state.Settings.language); applyTranslation(); 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.lastVisitedTown = loadedState.lastVisitedTown; 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(); UI.drawStatus(); UI.drawOpponentMonster(); UI.drawActiveMonster(); UI.drawActiveTechniques(); }, };