summaryrefslogtreecommitdiff
path: root/resources/js/memory.js
diff options
context:
space:
mode:
Diffstat (limited to 'resources/js/memory.js')
-rw-r--r--resources/js/memory.js149
1 files changed, 149 insertions, 0 deletions
diff --git a/resources/js/memory.js b/resources/js/memory.js
new file mode 100644
index 0000000..81acf89
--- /dev/null
+++ b/resources/js/memory.js
@@ -0,0 +1,149 @@
+const Memory = {
+ /**
+ * @type {State}
+ */
+ state: new State(),
+
+
+ /**
+ * @returns {string}
+ */
+ save () {
+ const saveMonster = (monsterData, monsterState) => {
+ monsterData.level = monsterState.level;
+ monsterData.hp = monsterState.hp;
+
+ if (monsterData.statusEffect && monsterData.statusEffect.slug === 'lifeleech') {
+ monsterData.statusEffect = null;
+ }
+
+ return monsterData;
+ };
+
+ /**
+ * @type {State}
+ */
+ const saveData = JSON.parse(JSON.stringify(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 {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 {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.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.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.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.activeTechnique = await loadTechnique(loadedState.activeTechnique);
+
+ UI.drawOpponentMonster();
+ UI.drawActiveMonster();
+ UI.drawActiveTechniques();
+ },
+};