From b6b0cfbb9b6a61183cbce83aabf08b3bd709a12e Mon Sep 17 00:00:00 2001 From: Stefan Zermatten Date: Sat, 19 Nov 2022 18:12:51 +0200 Subject: [PATCH] Fixed triggers on attribute reset on rest --- .../methods/damageProperty.js | 4 +- .../creatures/methods/restCreature.js | 60 ++++++++----------- 2 files changed, 28 insertions(+), 36 deletions(-) diff --git a/app/imports/api/creature/creatureProperties/methods/damageProperty.js b/app/imports/api/creature/creatureProperties/methods/damageProperty.js index 507d82bd..f129a562 100644 --- a/app/imports/api/creature/creatureProperties/methods/damageProperty.js +++ b/app/imports/api/creature/creatureProperties/methods/damageProperty.js @@ -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 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 prop.damage = damage; prop.value = newValue; + logFunction?.(newValue); } else if (operation === 'increment') { let currentValue = prop.value || 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 prop.damage += increment; prop.value -= increment; + logFunction?.(increment); } applyTriggers(actionContext.triggers?.damageProperty?.after, prop, actionContext); diff --git a/app/imports/api/creature/creatures/methods/restCreature.js b/app/imports/api/creature/creatures/methods/restCreature.js index 7ca7c16c..7e7edb1e 100644 --- a/app/imports/api/creature/creatures/methods/restCreature.js +++ b/app/imports/api/creature/creatures/methods/restCreature.js @@ -6,6 +6,7 @@ import { assertEditPermission } from '/imports/api/creature/creatures/creaturePe import { union } from 'lodash'; import ActionContext from '/imports/api/engine/actions/ActionContext.js'; import { applyTriggers } from '/imports/api/engine/actions/applyTriggers.js'; +import { damagePropertyWork } from '/imports/api/creature/creatureProperties/methods/damageProperty.js'; const restCreature = new ValidatedMethod({ name: 'creature.methods.rest', @@ -84,23 +85,20 @@ export function resetProperties(creatureId, resetFilter, actionContext) { type: 'attribute', damage: { $ne: 0 }, } - CreatureProperties.find(attributeFilter, { - fields: { name: 1, damage: 1 } - }).forEach(prop => { - actionContext.addLog({ - name: prop.name, - value: prop.damage >= 0 ? `Restored ${prop.damage}` : `Removed ${-prop.damage}` + CreatureProperties.find(attributeFilter).forEach(prop => { + damagePropertyWork({ + prop, + operation: 'increment', + value: -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 const actionFilter = { ...filter, @@ -135,13 +133,6 @@ function resetHitDice(creatureId, actionContext) { attributeType: 'hitDice', removed: { $ne: true }, inactive: { $ne: true }, - }, { - fields: { - name: 1, - hitDiceSize: 1, - damage: 1, - total: 1, - } }).fetch(); // Use a collator to do sorting in natural order let collator = new Intl.Collator('en', { @@ -155,24 +146,23 @@ function resetHitDice(creatureId, actionContext) { let resetMultiplier = actionContext.creature.settings.hitDiceResetMultiplier || 0.5; let recoverableHd = Math.max(Math.floor(totalHd * resetMultiplier), 1); // recover each hit dice in turn until the recoverable amount is used up - let amountToRecover, resultingDamage; + let amountToRecover; hitDice.forEach(hd => { if (!recoverableHd) return; - amountToRecover = Math.min(recoverableHd, hd.damage || 0); + amountToRecover = Math.min(recoverableHd, hd.damage ?? 0); if (!amountToRecover) return; recoverableHd -= amountToRecover; - resultingDamage = hd.damage - amountToRecover; - actionContext.addLog({ - name: hd.name, - value: amountToRecover >= 0 ? `Restored ${amountToRecover} hit dice` : `Removed ${-amountToRecover} hit dice` - }); - CreatureProperties.update(hd._id, { - $set: { - damage: resultingDamage, - dirty: true, + damagePropertyWork({ + prop: hd, + operation: 'increment', + value: -amountToRecover, + actionContext, + logFunction(increment) { + actionContext.addLog({ + name: hd.name, + value: increment < 0 ? `Restored ${-increment} hit dice` : `Removed ${increment} hit dice` + }); } - }, { - selector: { type: 'attribute' }, }); }); }