summaryrefslogtreecommitdiff
path: root/resources/js/memory.js
blob: 6023e4985c47a9c24227b76d096667cedba2742c (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
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();
  },
};