Skip to content

Commit

Permalink
feat(active_effects): improve active effect implementation
Browse files Browse the repository at this point in the history
* update encumbrance for gear quantity on actor

#1215
  • Loading branch information
wrycu committed May 23, 2023
1 parent 352c0bc commit d8ba053
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
6 changes: 4 additions & 2 deletions modules/actors/actor-sheet-ffg.js
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,8 @@ export class ActorSheetFFG extends ActorSheet {
item = await ImportHelpers.findCompendiumEntityById("Item", itemId);
}
}
item.update({ ["data.quantity.value"]: item.system.quantity.value + 1 });
await item.update({ ["data.quantity.value"]: item.system.quantity.value + 1 });
await ModifierHelpers.updateActiveEffectForGear(this.actor, item, 'increase');
});

html.find(".item-quantity .quantity.decrease").click(async (ev) => {
Expand All @@ -738,7 +739,8 @@ export class ActorSheetFFG extends ActorSheet {
}
}
let count = item.system.quantity.value - 1 > 0 ? item.system.quantity.value - 1 : 0;
item.update({ ["data.quantity.value"]: count });
await item.update({ ["data.quantity.value"]: count });
await ModifierHelpers.updateActiveEffectForGear(this.actor, item, 'decrease');
});

// Roll Skill
Expand Down
55 changes: 55 additions & 0 deletions modules/helpers/modifiers.js
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,61 @@ export default class ModifierHelpers {
return value;
}

/**
* Update the encumbrance value for gear on an actor when the +/- buttons are clicked
* @param actor - actor the gear is on
* @param item - item being +/-'ed
* @param mode - "increase" for + and "decrease" for -
* @returns {Promise<void>}
*/
static async updateActiveEffectForGear(actor, item, mode) {
let effects = actor.effects.contents;
let uuid = `Actor.${actor.id}.Item.${item.id}`;
let current_value = item.system.quantity.value;
let previous_value;
if (mode === 'increase') {
previous_value = current_value - 1;
} else if (mode === 'decrease') {
previous_value = current_value + 1;
}
let filtered_effects = effects.filter(i => i.origin === uuid && i.label === 'Encumbrance (Current)');
if (filtered_effects.length === 0) {
console.log("found no matching effects")
return;
}

if (current_value > 0 && previous_value !== 0) {
for (let effect of filtered_effects) {
if (effect.changes.length === 0) {
// don't attempt to do things if there aren't any changes
break;
}
let original_value = parseInt(effect.changes[0].value) / previous_value
await effect.update({
changes: [{
key: "system.stats.encumbrance.value",
mode: CONST.ACTIVE_EFFECT_MODES.ADD,
priority: null,
value: original_value * current_value,
}],
});
}
}
else {
// toggle suspending the AE instead
for (let effect of filtered_effects) {
if (effect.changes.length === 0) {
console.log("found no matching effects")
// don't attempt to do things if there aren't any changes
break;
}
await effect.update({
disabled: !effect.disabled,
});
}
}
}

/**
* Look up (active) active effects modifying a particular skill, e.g. "add boost"
* @param actor - actor to search for matching Active Effects on
Expand Down

0 comments on commit d8ba053

Please sign in to comment.