diff --git a/src/common/constants.js b/src/common/constants.js index 7ec9a4b..0638bc8 100644 --- a/src/common/constants.js +++ b/src/common/constants.js @@ -1,10 +1,7 @@ 'use strict'; /* eslint-disable no-unused-vars */ // Static Data -const BOOK_PATH = 'data/book.json'; -const SPELLS_PATH = 'data/spells.json'; -const POWERS_PATH = 'data/powers.json'; -const TABLES_PATH = 'data/tables.json'; +const DB_PATH = 'data/db.json'; const CHARACTER_SHEET_CSS_PATH = 'css/sheet.css'; const ICON_PATH = 'images/32.png'; diff --git a/src/common/types.js b/src/common/types.js index 6606f90..ed4d3d5 100644 --- a/src/common/types.js +++ b/src/common/types.js @@ -43,8 +43,7 @@ * @typedef T20Data * @type {object} * @property {SpellData} spells - * @property {PowerData} powers - * @property {string[]} powersOptions + * @property {PowerData} abilitiesAndPowers */ /** @@ -54,5 +53,5 @@ /** * @typedef PowerData - * @type {object.>} + * @type {object.} */ diff --git a/src/features/character-sheet.js b/src/features/character-sheet.js index 17cc674..bab16ec 100644 --- a/src/features/character-sheet.js +++ b/src/features/character-sheet.js @@ -131,14 +131,9 @@ function loadSheetExtraCSS({ iframe }) { * @param {string} props.characterId - The character ID in the Roll20 game. */ // eslint-disable-next-line no-unused-vars -function loadSheetEnhancement({ spells, powers, characterId }) { +function loadSheetEnhancement({ spells, abilitiesAndPowers, characterId }) { // Fetch the Tormenta20 data - const data = { spells, powers, powersOptions: [] }; - for (const type of Object.keys(data.powers)) { - for (const name of Object.keys(data.powers[type])) { - data.powersOptions.push(`${type} - ${name}`); - } - } + const data = { spells, abilitiesAndPowers }; // Load the functionalities const iframe = document.querySelector(`iframe[name="iframe_${characterId}"]`); if (!iframe) { diff --git a/src/features/powers.js b/src/features/powers.js index 90abee9..4ea77fe 100644 --- a/src/features/powers.js +++ b/src/features/powers.js @@ -42,7 +42,11 @@ function renderPowerButton({ container, data }) { innerHTML: 'Escolher', }), ); - container.prepend(createPowerDialog({ options: data.powersOptions })); + container.prepend( + createPowerDialog({ + options: Object.keys(data?.abilitiesAndPowers || {}), + }), + ); container.style.flexDirection = 'column'; container.style.gap = '8px'; const button = container.querySelector('button[name="choose-power"]'); @@ -54,11 +58,9 @@ function renderPowerButton({ container, data }) { closeText: '', buttons: { Confirmar: () => { - const items = input.value.split(' - '); - if (items.length <= 1) return false; fillPowerContainer({ container, - power: data.powers[items[0]][items[1]], + power: data.abilitiesAndPowers[input.value], }); dialog.dialog('close'); }, @@ -75,11 +77,9 @@ function renderPowerButton({ container, data }) { eventName: 'keydown', eventHandler: (e) => { if (e.keyCode === 13) { - const items = input.value.split(' - '); - if (items.length <= 1) return false; fillPowerContainer({ container, - power: data.powers[items[0]][items[1]], + power: data.abilitiesAndPowers[input.value], }); dialog.dialog('close'); } diff --git a/src/features/spells.js b/src/features/spells.js index 44ef746..99ef0e6 100644 --- a/src/features/spells.js +++ b/src/features/spells.js @@ -72,7 +72,7 @@ function fillSpellContainer({ iframe, container, spell }) { }); setInputValue({ selector: 'input[name="attr_spellalvoarea"]', - value: spell.target || spell.area, + value: spell.target || spell.area || spell.effect, origin: container, }); setInputValue({ @@ -132,6 +132,7 @@ function renderSpellButton({ iframe, container, circle, data }) { const button = container.querySelector('button[name="choose-spell"]'); const form = container.querySelector('form[name="spell-form"]'); const input = form.querySelector('input[name="spell-name"]'); + // TODO: Use the dialog manager const dialog = $(container.querySelector('div[name="spell-dialog"]')).dialog({ autoOpen: false, closeText: '', diff --git a/src/index.js b/src/index.js index 1b3fe22..f4e02d3 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,6 @@ 'use strict'; /* common/constants vars */ -/* global BOOK_PATH,SPELLS_PATH,POWERS_PATH,TABLES_PATH */ +/* global DB_PATH */ /* features/character-sheet vars */ /* global loadSheetEnhancement */ /* features/book vars */ @@ -10,33 +10,20 @@ $(document).ready(() => { // Loading all game data in one place to avoid loading this multiple times through the extension. - const t20Data = { book: [], spells: {}, powers: {}, tables: {} }; - const promisses = [ - [BOOK_PATH, 'book'], - [SPELLS_PATH, 'spells'], - [POWERS_PATH, 'powers'], - [TABLES_PATH, 'tables'], - ].map(([path, key]) => - fetch(chrome.runtime.getURL(path)) - .then((response) => response.json()) - .then((data) => { - t20Data[key] = data; - }), - ); - - $(window).on('message', (e) => { - const data = e.originalEvent.data; - // only add the sheet improvements when a character sheet is opened - if (data.type === 'loaded') - loadSheetEnhancement({ - spells: t20Data.spells, - powers: t20Data.powers, - characterId: data.characterId, + fetch(chrome.runtime.getURL(DB_PATH)) + .then((response) => response.json()) + .then((db) => { + loadBook({ bookItems: db.book }); + loadChatEnhancement({ bookItems: db.book }); + $(window).on('message', (e) => { + const data = e.originalEvent.data; + // only add the sheet improvements when a character sheet is opened + if (data.type === 'loaded') + loadSheetEnhancement({ + spells: db.spells, + abilitiesAndPowers: db.abilities_and_powers, + characterId: data.characterId, + }); }); - }); - - Promise.all(promisses).then(() => { - loadBook({ bookItems: [...t20Data.book, t20Data.tables] }); - loadChatEnhancement({ bookItems: t20Data.book }); - }); + }); });