Skip to content

Commit

Permalink
add speels and powers to the book and update the data handle in the a…
Browse files Browse the repository at this point in the history
…pplication
  • Loading branch information
pyanderson committed Oct 31, 2023
1 parent 2da89a4 commit 1da1539
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 51 deletions.
5 changes: 1 addition & 4 deletions src/common/constants.js
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
5 changes: 2 additions & 3 deletions src/common/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@
* @typedef T20Data
* @type {object}
* @property {SpellData} spells
* @property {PowerData} powers
* @property {string[]} powersOptions
* @property {PowerData} abilitiesAndPowers
*/

/**
Expand All @@ -54,5 +53,5 @@

/**
* @typedef PowerData
* @type {object.<string, object.<string, Power>>}
* @type {object.<string, Power>}
*/
9 changes: 2 additions & 7 deletions src/features/character-sheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
14 changes: 7 additions & 7 deletions src/features/powers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"]');
Expand All @@ -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');
},
Expand All @@ -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');
}
Expand Down
3 changes: 2 additions & 1 deletion src/features/spells.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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: '',
Expand Down
45 changes: 16 additions & 29 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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 */
Expand All @@ -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 });
});
});
});

0 comments on commit 1da1539

Please sign in to comment.