summaryrefslogtreecommitdiff
path: root/resources/js/memory.js
blob: 1732472ff842b4cdcf29fb687e716360b72a7d29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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.language = loadedState.language;
    await fetchTranslation(Memory.state.language);

    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();
  },
};