summaryrefslogtreecommitdiff
path: root/resources/js/db.js
blob: 96b971a8eacd2b2beff8ea6916691c78f614e8a4 (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
const DB = {
  allMonsters: [],
  allAnimations: {},
  monsters: {},
  shapes: {},
  elements: {},
  techniques: {},
  statusEffects: {},
};

async function initializeDB () {
  DB.allMonsters = await fetch('/db/all-monsters.json').then((response) => response.json());
  DB.allAnimations = await fetch('/db/animations.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());
  }
}

/**
 * @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);
}