Fixed triggers on attribute reset on rest

This commit is contained in:
Stefan Zermatten
2022-11-19 18:12:51 +02:00
parent 428aeef635
commit b6b0cfbb9b
2 changed files with 28 additions and 36 deletions

View File

@@ -59,7 +59,7 @@ const damageProperty = new ValidatedMethod({
}, },
}); });
export function damagePropertyWork({ prop, operation, value, actionContext }) { export function damagePropertyWork({ prop, operation, value, actionContext, logFunction }) {
// Save the value to the scope before applying the before triggers // Save the value to the scope before applying the before triggers
if (operation === 'increment') { if (operation === 'increment') {
@@ -105,6 +105,7 @@ export function damagePropertyWork({ prop, operation, value, actionContext }) {
// Also write it straight to the prop so that it is updated in the actionContext // Also write it straight to the prop so that it is updated in the actionContext
prop.damage = damage; prop.damage = damage;
prop.value = newValue; prop.value = newValue;
logFunction?.(newValue);
} else if (operation === 'increment') { } else if (operation === 'increment') {
let currentValue = prop.value || 0; let currentValue = prop.value || 0;
let currentDamage = prop.damage || 0; let currentDamage = prop.damage || 0;
@@ -125,6 +126,7 @@ export function damagePropertyWork({ prop, operation, value, actionContext }) {
// Also write it straight to the prop so that it is updated in the actionContext // Also write it straight to the prop so that it is updated in the actionContext
prop.damage += increment; prop.damage += increment;
prop.value -= increment; prop.value -= increment;
logFunction?.(increment);
} }
applyTriggers(actionContext.triggers?.damageProperty?.after, prop, actionContext); applyTriggers(actionContext.triggers?.damageProperty?.after, prop, actionContext);

View File

@@ -6,6 +6,7 @@ import { assertEditPermission } from '/imports/api/creature/creatures/creaturePe
import { union } from 'lodash'; import { union } from 'lodash';
import ActionContext from '/imports/api/engine/actions/ActionContext.js'; import ActionContext from '/imports/api/engine/actions/ActionContext.js';
import { applyTriggers } from '/imports/api/engine/actions/applyTriggers.js'; import { applyTriggers } from '/imports/api/engine/actions/applyTriggers.js';
import { damagePropertyWork } from '/imports/api/creature/creatureProperties/methods/damageProperty.js';
const restCreature = new ValidatedMethod({ const restCreature = new ValidatedMethod({
name: 'creature.methods.rest', name: 'creature.methods.rest',
@@ -84,23 +85,20 @@ export function resetProperties(creatureId, resetFilter, actionContext) {
type: 'attribute', type: 'attribute',
damage: { $ne: 0 }, damage: { $ne: 0 },
} }
CreatureProperties.find(attributeFilter, { CreatureProperties.find(attributeFilter).forEach(prop => {
fields: { name: 1, damage: 1 } damagePropertyWork({
}).forEach(prop => { prop,
actionContext.addLog({ operation: 'increment',
name: prop.name, value: -prop.damage,
value: prop.damage >= 0 ? `Restored ${prop.damage}` : `Removed ${-prop.damage}` actionContext,
logFunction(increment) {
actionContext.addLog({
name: prop.name,
value: increment < 0 ? `Restored ${-increment}` : `Removed ${-increment}`
});
}
}); });
}); });
CreatureProperties.update(attributeFilter, {
$set: {
damage: 0,
dirty: true,
}
}, {
selector: { type: 'attribute' },
multi: true,
});
// Update all action-like properties' usesUsed // Update all action-like properties' usesUsed
const actionFilter = { const actionFilter = {
...filter, ...filter,
@@ -135,13 +133,6 @@ function resetHitDice(creatureId, actionContext) {
attributeType: 'hitDice', attributeType: 'hitDice',
removed: { $ne: true }, removed: { $ne: true },
inactive: { $ne: true }, inactive: { $ne: true },
}, {
fields: {
name: 1,
hitDiceSize: 1,
damage: 1,
total: 1,
}
}).fetch(); }).fetch();
// Use a collator to do sorting in natural order // Use a collator to do sorting in natural order
let collator = new Intl.Collator('en', { let collator = new Intl.Collator('en', {
@@ -155,24 +146,23 @@ function resetHitDice(creatureId, actionContext) {
let resetMultiplier = actionContext.creature.settings.hitDiceResetMultiplier || 0.5; let resetMultiplier = actionContext.creature.settings.hitDiceResetMultiplier || 0.5;
let recoverableHd = Math.max(Math.floor(totalHd * resetMultiplier), 1); let recoverableHd = Math.max(Math.floor(totalHd * resetMultiplier), 1);
// recover each hit dice in turn until the recoverable amount is used up // recover each hit dice in turn until the recoverable amount is used up
let amountToRecover, resultingDamage; let amountToRecover;
hitDice.forEach(hd => { hitDice.forEach(hd => {
if (!recoverableHd) return; if (!recoverableHd) return;
amountToRecover = Math.min(recoverableHd, hd.damage || 0); amountToRecover = Math.min(recoverableHd, hd.damage ?? 0);
if (!amountToRecover) return; if (!amountToRecover) return;
recoverableHd -= amountToRecover; recoverableHd -= amountToRecover;
resultingDamage = hd.damage - amountToRecover; damagePropertyWork({
actionContext.addLog({ prop: hd,
name: hd.name, operation: 'increment',
value: amountToRecover >= 0 ? `Restored ${amountToRecover} hit dice` : `Removed ${-amountToRecover} hit dice` value: -amountToRecover,
}); actionContext,
CreatureProperties.update(hd._id, { logFunction(increment) {
$set: { actionContext.addLog({
damage: resultingDamage, name: hd.name,
dirty: true, value: increment < 0 ? `Restored ${-increment} hit dice` : `Removed ${increment} hit dice`
});
} }
}, {
selector: { type: 'attribute' },
}); });
}); });
} }