summaryrefslogtreecommitdiff
path: root/resources/js/db.js
blob: e084818d57b7a020920d92e36fab116eb2ef3677 (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
/**
 * @typedef {Object} DB_Evolution
 * @property {string} path
 * @property {string} monster_slug
 * @property {string} at_level
 * @property {string} item
 */

//

const DB = {
  /**
   * @type {string[]}
   */
  allMonsters: [],

  /**
   * @typedef {Object.<string, string[]>} DB_Animation
   *
   * @type {DB_Animation}
   */
  allAnimations: {},

  /**
   * @type {string[]}
   */
  allItems: [],

  /**
   * @typedef {Object} DB_Monster
   *
   * @type {Object.<MonsterSlug, DB_Monster>}
   */
  monsters: {},

  /**
   * @typedef {Object} DB_Shape
   *
   * @type {Object.<string, DB_Shape>}
   */
  shapes: {},

  /**
   * @typedef {Object} DB_Element
   *
   * @type {Object.<ElementType, DB_Element>}
   */
  elements: {},

  /**
   * @typedef {Object} DB_Technique
   *
   * @type {Object.<TechniqueSlug, DB_Technique>}
   */
  techniques: {},

  /**
   * @typedef {Object} DB_StatusEffect
   *
   * @type {Object.<string, DB_StatusEffect>}
   */
  statusEffects: {},

  /**
   * @typedef {Object} DB_Item
   *
   * @type {Object.<string, DB_Item>}
   */
  items: {},

  translations: {},
};

async function initializeDB () {
  DB.allMonsters = await fetch('/db/_generated/all-monsters.json').then((response) => response.json());
  DB.allAnimations = await fetch('/db/_generated/animations.json').then((response) => response.json());
  DB.allItems = await fetch('/db/_generated/all-items.json').then((response) => response.json());

  DB.shapes = await fetch('/modules/tuxemon/mods/tuxemon/db/shape/shapes.json').then((response) => response.json());

  for (const element of Object.keys(ElementType)) {
    DB.elements[element] = await fetch(`/modules/tuxemon/mods/tuxemon/db/element/${element}.json`).then((response) => response.json());
  }

  await fetchTranslation(Memory.state.language);
}

/**
 * @param {MonsterSlug} slug
 *
 * @returns {Promise<Monster>}
 */
async function fetchMonster (slug) {
  if (! DB.monsters[slug]) {
    DB.monsters[slug] = await fetch(`/modules/tuxemon/mods/tuxemon/db/monster/${slug}.json`).then((response) => response.json());
  }

  const monster = new Monster(slug);
  await monster.initialize();

  return monster;
}

/**
 * @param {TechniqueSlug} slug
 *
 * @returns {Promise<Technique>}
 */
async function fetchTechnique (slug) {
  if (! DB.techniques[slug]) {
    DB.techniques[slug] = await fetch(`/modules/tuxemon/mods/tuxemon/db/technique/${slug}.json`).then((response) => response.json());
  }

  return new Technique(slug);
}

/**
 * @param {string} slug
 *
 * @returns {Promise<StatusEffect>}
 */
async function fetchStatusEffect (slug) {
  if (! DB.statusEffects[slug]) {
    DB.statusEffects[slug] = await fetch(`/modules/tuxemon/mods/tuxemon/db/technique/status_${slug}.json`).then((response) => response.json());
  }

  return new StatusEffect(slug);
}

/**
 * @param {string} slug
 *
 * @returns {Promise<Item>}
 */
async function fetchItem (slug) {
  if (! DB.items[slug]) {
    DB.items[slug] = await fetch(`/modules/tuxemon/mods/tuxemon/db/item/${slug}.json`).then((response) => response.json());
  }

  return new Item(slug);
}

/**
 * @param {string} locale
 *
 * @returns {Promise<Object>}
 */
async function fetchTranslation (locale) {
  if (! DB.translations[locale]) {
    DB.translations[locale] = await fetch(`/db/_generated/i18n/${locale}.json`).then((response) => response.json());
  }

  return DB.translations[locale];
}