Skip to content

Commit

Permalink
[WIP] improve spell module
Browse files Browse the repository at this point in the history
  • Loading branch information
pyanderson committed Nov 11, 2023
1 parent 256a2cc commit a1a027e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 53 deletions.
16 changes: 4 additions & 12 deletions src/features/character-sheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,18 +263,10 @@ export class CharacterSheet {
this._equipmentsContainer = null;
this._headerContainer = null;
this.character.getAttributes = (filterFn, transformFn = (a) => a) =>
this.character.attribs.models.filter(filterFn).map(transformFn);
this.character.getAbilities = () => {
const regex =
/^(repeating_abilities|repeating_powers)_(?<id>.+)_(nameability|namepower)$/;
return this.character.getAttributes(
(a) => regex.test(a.get('name')),
(a) => ({
name: a.get('current'),
id: a.get('name').match(regex)?.groups.id,
}),
);
};
this.character.attribs.models
.filter(filterFn)
.map(transformFn)
.reduce((acc, a) => ({ ...acc, [a.get('name')]: a }), {});
this.spellSheet = new SpellSheet(
this.iframe,
this.spellsContainer,
Expand Down
70 changes: 29 additions & 41 deletions src/features/spells.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,17 +245,18 @@ export class SpellSheet {
this.spellsContainer = spellsContainer;
this.db = db;
this.character = character;
this.character.getSpellAttributes = (circle, id) => {
this.character.getSpellAttributes = (id, circle) => {
const regex = new RegExp(`^repeating_spells${circle}_${id}_`);
return this.character.getAttributes((a) => regex.test(a.get('name')));
};
this.character.setSpellAttributes = (circle, id, attributes) => {
const attribs = this.character.getSpellAttributes(circle, id);
attributes.forEach(({ name, value }) => {
this.character.updateSpell = (id, circle, spellName) => {
const attribsMap = this.character.getSpellAttributes(id, circle);
const attributes = this.getSpellAttributes(circle, spellName);
Object.entries(attributes).forEach(([name, current]) => {
const attrName = `repeating_spells${circle}_${id}_${name}`;
const attr = attribs.find((a) => a.get('name') === attrName);
if (attr) attr.save({ current: value });
else this.character.attribs.create({ name: attrName, current: value });
const attr = attribsMap[attrName];
if (attr) attr.save({ current });
else this.character.attribs.create({ name: attrName, current });
});
};
}
Expand Down Expand Up @@ -293,15 +294,15 @@ export class SpellSheet {
closeText: '',
buttons: {
Confirmar: () => {
this.updateContainerSpell(container, circle, input.value);
this.updateSpell(container, circle, input.value);
dialog.dialog('close');
},
Cancelar: () => dialog.dialog('close'),
},
});
input.addEventObserver('keydown', (e) => {
if (e.keyCode === 13) {
this.updateContainerSpell(container, circle, input.value);
this.updateSpell(container, circle, input.value);
dialog.dialog('close');
}
});
Expand All @@ -317,45 +318,32 @@ export class SpellSheet {
const spell = this.db.spells[circle][spellName];
if (!spell) return [];
const spellCD = this.iframe.getValue('input.spell-cd-total');
return [
{ name: 'namespell', value: spell.name },
{ name: 'spelltipo', value: spell.type },
{ name: 'spellexecucao', value: spell.execution },
{ name: 'spellalcance', value: spell.range },
{ name: 'spellduracao', value: spell.duration },
{
name: 'spellalvoarea',
value: spell.target || spell.area || spell.effect,
},
{ name: 'spellresistencia', value: spell.resistance },
{
name: 'spelldescription',
value: `${spell.description}${
spell.implements.length > 0 ? '\n\n' : ''
}${spell.implements
.map((implement) => `${implement.cost}: ${implement.description}`)
.join('\n\n')}`,
},
...(spellCD
? [
{
name: 'spellcd',
value: spell.resistance !== '' ? spellCD : '',
},
]
: []),
];
return {
namespell: spell.name,
spelltipo: spell.type,
spellexecucao: spell.execution,
spellalcance: spell.range,
spellduracao: spell.duration,
spellalvoarea: spell.target || spell.area || spell.effect,
spellresistencia: spell.resistance,
spelldescription: `${spell.description}${
spell.implements.length > 0 ? '\n\n' : ''
}${spell.implements
.map((implement) => `${implement.cost}: ${implement.description}`)
.join('\n\n')}`,
...(spellCD ? { spellcd: spell.resistance !== '' ? spellCD : '' } : {}),
};
}

async updateContainerSpell(container, circle, spellName) {
async updateSpell(container, circle, spellName) {
this.character.attribs.fetch();
await waitForCondition({
checkFn: () => this.character.attribs.models.length > 0,
});
this.character.setSpellAttributes(
circle,
this.character.updateSpell(
container.parentNode.parentNode.getAttribute('data-reprowid'),
this.getSpellAttributes(circle, spellName),
circle,
spellName,
);
}
}

0 comments on commit a1a027e

Please sign in to comment.