From cc685bfe02b42b592987117fa008a4461785f53c Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Thu, 17 Aug 2023 02:53:14 +0200 Subject: refactor --- resources/js/db.js | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 resources/js/db.js (limited to 'resources/js/db.js') diff --git a/resources/js/db.js b/resources/js/db.js new file mode 100644 index 0000000..96b971a --- /dev/null +++ b/resources/js/db.js @@ -0,0 +1,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} + */ +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} + */ +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} + */ +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); +} -- cgit v1.2.3