Added a updateArmorValue function that updates armoreffects according to an auto order

This commit is contained in:
WBHarry 2026-02-09 21:51:58 +01:00
parent fb9f89fa9d
commit 9dc5830e18
2 changed files with 75 additions and 5 deletions

View file

@ -115,6 +115,34 @@ export default class ArmorEffect extends foundry.data.ActiveEffectTypeDataModel
await this.parent.update({ 'system.changes': newChanges });
}
static orderEffectsForAutoChange(armorEffects, increasing) {
const getEffectWeight = effect => {
switch (effect.parent.type) {
case 'loot':
case 'consumable':
return 2;
case 'class':
case 'subclass':
case 'ancestry':
case 'community':
case 'feature':
case 'domainCard':
return 3;
case 'weapon':
case 'armor':
return 4;
case 'character':
return 5;
default:
return 1;
}
};
return armorEffects.sort((a, b) =>
increasing ? getEffectWeight(b) - getEffectWeight(a) : getEffectWeight(a) - getEffectWeight(b)
);
}
/* Overrides */
prepareBaseData() {

View file

@ -458,11 +458,6 @@ export default class DhCharacter extends BaseDataActor {
return this.parent.items.find(x => x.type === 'armor' && x.system.equipped);
}
/* TODO: Prep datastructure to be useful when applying automatic armor damage order */
get armorEffects() {
return Array.from(this.parent.allApplicableEffects());
}
get activeBeastform() {
return this.parent.effects.find(x => x.type === 'beastform');
}
@ -512,6 +507,53 @@ export default class DhCharacter extends BaseDataActor {
}
}
async updateArmorValue(armorChange) {
if (armorChange === 0) return;
const increasing = armorChange >= 0;
let remainingChange = Math.abs(armorChange);
const armorEffects = Array.from(this.parent.allApplicableEffects()).filter(x => x.type === 'armor');
const orderedEffects = game.system.api.data.activeEffects.ArmorEffect.orderEffectsForAutoChange(
armorEffects,
increasing
);
const embeddedUpdates = [];
for (const armorEffect of orderedEffects) {
let usedArmorChange = 0;
if (increasing) {
const remainingArmor = armorEffect.system.armorChange.max - armorEffect.system.armorChange.value;
usedArmorChange = Math.min(remainingChange, remainingArmor);
remainingChange -= usedArmorChange;
} else {
const changeChange = Math.min(armorEffect.system.armorChange.value, remainingChange);
usedArmorChange -= changeChange;
remainingChange -= changeChange;
}
if (!usedArmorChange) continue;
else {
if (!embeddedUpdates[armorEffect.parent.id])
embeddedUpdates[armorEffect.parent.id] = { doc: armorEffect.parent, updates: [] };
embeddedUpdates[armorEffect.parent.id].updates.push({
'_id': armorEffect.id,
'system.changes': [
{
...armorEffect.system.armorChange,
value: armorEffect.system.armorChange.value + usedArmorChange
}
]
});
}
if (remainingChange === 0) break;
}
for (const { doc, updates } of Object.values(embeddedUpdates))
doc.updateEmbeddedDocuments('ActiveEffect', updates);
}
get sheetLists() {
const ancestryFeatures = [],
communityFeatures = [],