Skip to content

Commit

Permalink
fix race abilities auto fill
Browse files Browse the repository at this point in the history
  • Loading branch information
pyanderson committed Nov 10, 2023
1 parent 45df205 commit b1746f4
Showing 1 changed file with 30 additions and 21 deletions.
51 changes: 30 additions & 21 deletions src/features/races.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,6 @@ function loadRaceAutoComplete({ iframe, races, characterId }) {
root: iframe,
path: ['div.sheet-left-container', 'div.sheet-header-info'],
});
const abilitiesContainer = pathQuerySelector({
root: iframe,
path: ['div.sheet-left-container', 'div.sheet-powers-and-abilities'],
});
const sizeAndMoveContainer = pathQuerySelector({
root: iframe,
path: ['div.sheet-left-container', 'div.sheet-size-and-move-container'],
Expand Down Expand Up @@ -105,21 +101,36 @@ function loadRaceAutoComplete({ iframe, races, characterId }) {
await waitForCondition({
checkFn: () => character.attribs.models.length > 0,
});
const toAdd = race.abilities.reduce((acc, a) => [...acc, a.name], []);
const toRemove = races
.filter((r) => r.name !== race.name)
.map((r) => r.abilities)
.reduce(
(acc, abilities) => [...acc, ...abilities.map((a) => a.name)],
(acc, abilities) => [
...acc,
...abilities
.filter((a) => toAdd.indexOf(a.name) === -1)
.map((a) => a.name),
],
[],
);
const regex =
/^(repeating_abilities|repeating_powers)_(.+)_(nameability|namepower)$/;
const currentAttrs = character.attribs.models.filter((x) =>
regex.test(x.get('name')),
);

const getAttributes = () => {
const regex =
/^(repeating_abilities|repeating_powers)_(?<id>.+)_(nameability|namepower)$/;
return character.attribs.models
.filter((x) => regex.test(x.get('name')))
.map((attribute) => ({
name: attribute.get('current'),
id: attribute.get('name').match(regex)?.groups.id,
}));
};

const currentAttrs = getAttributes();

// add the race abilities
for (const ability of race.abilities) {
if (!currentAttrs.find((x) => x.get('current') === race.name)) {
if (!currentAttrs.find((x) => x.name === race.name)) {
addRepItem({
character,
groupName,
Expand Down Expand Up @@ -149,25 +160,23 @@ function loadRaceAutoComplete({ iframe, races, characterId }) {
],
});
}
const updatedAttrs = character.attribs.models.filter((x) =>
regex.test(x.get('name')),
);
const updatedAttrs = getAttributes();
// remove the other races abilities
for (const attribute of updatedAttrs) {
if (toRemove.includes(attribute.get('current').trim())) {
if (toRemove.includes(attribute.name.trim())) {
delRepItem({
character,
groupName,
rowId: attribute.get('id'), // TODO: Fix this ID
rowId: attribute.id,
});
}
}
};
addEventObserver({
el: input,
eventName: 'input',
eventHandler: updateAbilities,
});
// addEventObserver({
// el: input,
// eventName: 'input',
// eventHandler: updateAbilities,
// });
addEventObserver({
el: input,
eventName: 'change',
Expand Down

0 comments on commit b1746f4

Please sign in to comment.