From d5859704c5ec2d624ef7996c247217bd4c233c57 Mon Sep 17 00:00:00 2001 From: WBHarry Date: Sun, 21 Dec 2025 22:16:23 +0100 Subject: [PATCH] Fixed so that we do not run separate actor.modifyResource calls during actions and dice rolls --- daggerheart.mjs | 2 - .../applications/sheets/actors/character.mjs | 15 ++- module/data/action/baseAction.mjs | 53 ++++++++++- module/data/fields/action/costField.mjs | 2 +- module/dice/dhRoll.mjs | 78 --------------- module/dice/dualityRoll.mjs | 95 ++++++++++++++++++- module/documents/actor.mjs | 9 +- 7 files changed, 158 insertions(+), 96 deletions(-) diff --git a/daggerheart.mjs b/daggerheart.mjs index eeed29dc..f1fe602e 100644 --- a/daggerheart.mjs +++ b/daggerheart.mjs @@ -17,7 +17,6 @@ import { socketRegistration } from './module/systemRegistration/_module.mjs'; import { placeables } from './module/canvas/_module.mjs'; -import { registerRollDiceHooks } from './module/dice/dhRoll.mjs'; import './node_modules/@yaireo/tagify/dist/tagify.css'; import TemplateManager from './module/documents/templateManager.mjs'; @@ -177,7 +176,6 @@ Hooks.on('ready', async () => { ui.compendiumBrowser = new applications.ui.ItemBrowser(); socketRegistration.registerSocketHooks(); - registerRollDiceHooks(); socketRegistration.registerUserQueries(); if (!game.user.getFlag(CONFIG.DH.id, CONFIG.DH.FLAGS.userFlags.welcomeMessage)) { diff --git a/module/applications/sheets/actors/character.mjs b/module/applications/sheets/actors/character.mjs index 953a0cf6..51df2fc9 100644 --- a/module/applications/sheets/actors/character.mjs +++ b/module/applications/sheets/actors/character.mjs @@ -675,16 +675,21 @@ export default class CharacterSheet extends DHBaseActorSheet { roll: { trait: button.dataset.attribute }, - hasRoll: true - }; - const result = await this.document.diceRoll({ - ...config, + hasRoll: true, actionType: 'action', headerTitle: `${game.i18n.localize('DAGGERHEART.GENERAL.dualityRoll')}: ${this.actor.name}`, title: game.i18n.format('DAGGERHEART.UI.Chat.dualityRoll.abilityCheckTitle', { ability: abilityLabel }) - }); + }; + const result = await this.document.diceRoll(config); + + /* This could be avoided by baking config.costs into config.resourceUpdates. Didn't feel like messing with it at the time */ + const costResources = result.costs + .filter(x => x.enabled) + .map(cost => ({ ...cost, value: -cost.value, total: -cost.total })); + config.resourceUpdates.addResources(costResources); + await config.resourceUpdates.updateResources(); } //TODO: redo toggleEquipItem method diff --git a/module/data/action/baseAction.mjs b/module/data/action/baseAction.mjs index b15d6c4e..abb1ca25 100644 --- a/module/data/action/baseAction.mjs +++ b/module/data/action/baseAction.mjs @@ -173,6 +173,12 @@ export default class DHBaseAction extends ActionMixin(foundry.abstract.DataModel return actorData; } + joinResourceUpdates(updates) { + return updates.reduce((acc, update) => { + if (acc) return acc; + }, []); + } + /** * Execute each part of the Action workflow in order, calling a specific event before and after each part. * @param {object} config Config object usually created from prepareConfig method @@ -206,6 +212,7 @@ export default class DHBaseAction extends ActionMixin(foundry.abstract.DataModel // Execute the Action Worflow in order based of schema fields await this.executeWorkflow(config); + await config.resourceUpdates.updateResources(); if (Hooks.call(`${CONFIG.DH.id}.postUseAction`, this, config) === false) return; @@ -239,8 +246,10 @@ export default class DHBaseAction extends ActionMixin(foundry.abstract.DataModel isDirect: !!this.damage?.direct, selectedRollMode: game.settings.get('core', 'rollMode'), data: this.getRollData(), - evaluate: this.hasRoll + evaluate: this.hasRoll, + resourceUpdates: new ResourceUpdateMap(this.actor.uuid) }; + DHBaseAction.applyKeybindings(config); return config; } @@ -322,10 +331,46 @@ export default class DHBaseAction extends ActionMixin(foundry.abstract.DataModel * @returns {string[]} An array of localized tag strings. */ _getTags() { - const tags = [ - game.i18n.localize(`DAGGERHEART.ACTIONS.TYPES.${this.type}.name`), - ]; + const tags = [game.i18n.localize(`DAGGERHEART.ACTIONS.TYPES.${this.type}.name`)]; return tags; } } + +export class ResourceUpdateMap extends Map { + #actorUuid; + + constructor(actorUuid) { + super(); + this.#actorUuid = actorUuid; + } + + addResources(resources) { + for (const resource of resources) { + if (!resource.key) continue; + + const existing = this.get(resource.key); + if (existing) { + this.set(resource.key, { + ...existing, + value: existing.value + (resource.value ?? 0), + total: existing.total + (resource.total ?? 0) + }); + } else { + this.set(resource.key, resource); + } + } + } + + #getResources() { + return Array.from(this.values()); + } + + async updateResources() { + const actor = await foundry.utils.fromUuid(this.#actorUuid); + if (actor) { + const target = actor.system.partner ?? actor; + await target.modifyResource(this.#getResources()); + } + } +} diff --git a/module/data/fields/action/costField.mjs b/module/data/fields/action/costField.mjs index 656edee3..9271f6b0 100644 --- a/module/data/fields/action/costField.mjs +++ b/module/data/fields/action/costField.mjs @@ -75,7 +75,7 @@ export default class CostField extends fields.ArrayField { } }, []); - await actor.modifyResource(resources); + config.resourceUpdates.addResources(resources); } /** diff --git a/module/dice/dhRoll.mjs b/module/dice/dhRoll.mjs index ec289941..ea24f238 100644 --- a/module/dice/dhRoll.mjs +++ b/module/dice/dhRoll.mjs @@ -236,81 +236,3 @@ export default class DHRoll extends Roll { return {}; } } - -async function automateHopeFear(config) { - const automationSettings = game.settings.get(CONFIG.DH.id, CONFIG.DH.SETTINGS.gameSettings.Automation); - const hopeFearAutomation = automationSettings.hopeFear; - if ( - !config.source?.actor || - (game.user.isGM ? !hopeFearAutomation.gm : !hopeFearAutomation.players) || - config.actionType === 'reaction' || - config.tagTeamSelected || - config.skips?.resources - ) - return; - const actor = await fromUuid(config.source.actor); - let updates = []; - if (!actor) return; - - if (config.rerolledRoll) { - if (config.roll.result.duality != config.rerolledRoll.result.duality) { - const hope = - (config.roll.isCritical || config.roll.result.duality === 1 ? 1 : 0) - - (config.rerolledRoll.isCritical || config.rerolledRoll.result.duality === 1 ? 1 : 0); - const stress = (config.roll.isCritical ? 1 : 0) - (config.rerolledRoll.isCritical ? 1 : 0); - const fear = - (config.roll.result.duality === -1 ? 1 : 0) - (config.rerolledRoll.result.duality === -1 ? 1 : 0); - - if (hope !== 0) updates.push({ key: 'hope', value: hope, total: -1 * hope, enabled: true }); - if (stress !== 0) updates.push({ key: 'stress', value: -1 * stress, total: stress, enabled: true }); - if (fear !== 0) updates.push({ key: 'fear', value: fear, total: -1 * fear, enabled: true }); - } - } else { - if (config.roll.isCritical || config.roll.result.duality === 1) - updates.push({ key: 'hope', value: 1, total: -1, enabled: true }); - if (config.roll.isCritical) updates.push({ key: 'stress', value: -1, total: 1, enabled: true }); - if (config.roll.result.duality === -1) updates.push({ key: 'fear', value: 1, total: -1, enabled: true }); - } - - if (updates.length) { - const target = actor.system.partner ?? actor; - if (!['dead', 'defeated', 'unconscious'].some(x => actor.statuses.has(x))) { - await target.modifyResource(updates); - } - } -} - -export const registerRollDiceHooks = () => { - Hooks.on(`${CONFIG.DH.id}.postRollDuality`, async (config, message) => { - const automationSettings = game.settings.get(CONFIG.DH.id, CONFIG.DH.SETTINGS.gameSettings.Automation); - if ( - automationSettings.countdownAutomation && - config.actionType !== 'reaction' && - !config.tagTeamSelected && - !config.skips?.updateCountdowns - ) { - const { updateCountdowns } = game.system.api.applications.ui.DhCountdowns; - - if (config.roll.result.duality === -1) { - await updateCountdowns( - CONFIG.DH.GENERAL.countdownProgressionTypes.actionRoll.id, - CONFIG.DH.GENERAL.countdownProgressionTypes.fear.id - ); - } else { - await updateCountdowns(CONFIG.DH.GENERAL.countdownProgressionTypes.actionRoll.id); - } - } - - await automateHopeFear(config); - - if (!config.roll.hasOwnProperty('success') && !config.targets?.length) return; - - const rollResult = config.roll.success || config.targets.some(t => t.hit), - looseSpotlight = !rollResult || config.roll.result.duality === -1; - - if (looseSpotlight && game.combat?.active) { - const currentCombatant = game.combat.combatants.get(game.combat.current?.combatantId); - if (currentCombatant?.actorId == actor.id) ui.combat.setCombatantSpotlight(currentCombatant.id); - } - }); -}; diff --git a/module/dice/dualityRoll.mjs b/module/dice/dualityRoll.mjs index 4d305c6c..2d3933d6 100644 --- a/module/dice/dualityRoll.mjs +++ b/module/dice/dualityRoll.mjs @@ -2,6 +2,7 @@ import D20RollDialog from '../applications/dialogs/d20RollDialog.mjs'; import D20Roll from './d20Roll.mjs'; import { setDiceSoNiceForDualityRoll } from '../helpers/utils.mjs'; import { getDiceSoNicePresets } from '../config/generalConfig.mjs'; +import { ResourceUpdateMap } from '../data/action/baseAction.mjs'; export default class DualityRoll extends D20Roll { _advantageFaces = 6; @@ -219,6 +220,88 @@ export default class DualityRoll extends D20Roll { return data; } + static async buildPost(roll, config, message) { + await super.buildPost(roll, config, message); + + await DualityRoll.dualityUpdate(config); + } + + static async automateHopeFear(config) { + const automationSettings = game.settings.get(CONFIG.DH.id, CONFIG.DH.SETTINGS.gameSettings.Automation); + const hopeFearAutomation = automationSettings.hopeFear; + if ( + !config.source?.actor || + (game.user.isGM ? !hopeFearAutomation.gm : !hopeFearAutomation.players) || + config.actionType === 'reaction' || + config.tagTeamSelected || + config.skips?.resources + ) + return; + const actor = await fromUuid(config.source.actor); + let updates = []; + if (!actor) return; + + if (config.rerolledRoll) { + if (config.roll.result.duality != config.rerolledRoll.result.duality) { + const hope = + (config.roll.isCritical || config.roll.result.duality === 1 ? 1 : 0) - + (config.rerolledRoll.isCritical || config.rerolledRoll.result.duality === 1 ? 1 : 0); + const stress = (config.roll.isCritical ? 1 : 0) - (config.rerolledRoll.isCritical ? 1 : 0); + const fear = + (config.roll.result.duality === -1 ? 1 : 0) - (config.rerolledRoll.result.duality === -1 ? 1 : 0); + + if (hope !== 0) updates.push({ key: 'hope', value: hope, total: -1 * hope, enabled: true }); + if (stress !== 0) updates.push({ key: 'stress', value: -1 * stress, total: stress, enabled: true }); + if (fear !== 0) updates.push({ key: 'fear', value: fear, total: -1 * fear, enabled: true }); + } + } else { + if (config.roll.isCritical || config.roll.result.duality === 1) + updates.push({ key: 'hope', value: 1, total: -1, enabled: true }); + if (config.roll.isCritical) updates.push({ key: 'stress', value: -1, total: 1, enabled: true }); + if (config.roll.result.duality === -1) updates.push({ key: 'fear', value: 1, total: -1, enabled: true }); + } + + if (updates.length) { + // const target = actor.system.partner ?? actor; + if (!['dead', 'defeated', 'unconscious'].some(x => actor.statuses.has(x))) { + config.resourceUpdates.addResources(updates); + } + } + } + + static async dualityUpdate(config) { + const automationSettings = game.settings.get(CONFIG.DH.id, CONFIG.DH.SETTINGS.gameSettings.Automation); + if ( + automationSettings.countdownAutomation && + config.actionType !== 'reaction' && + !config.tagTeamSelected && + !config.skips?.updateCountdowns + ) { + const { updateCountdowns } = game.system.api.applications.ui.DhCountdowns; + + if (config.roll.result.duality === -1) { + await updateCountdowns( + CONFIG.DH.GENERAL.countdownProgressionTypes.actionRoll.id, + CONFIG.DH.GENERAL.countdownProgressionTypes.fear.id + ); + } else { + await updateCountdowns(CONFIG.DH.GENERAL.countdownProgressionTypes.actionRoll.id); + } + } + + await DualityRoll.automateHopeFear(config); + + if (!config.roll.hasOwnProperty('success') && !config.targets?.length) return; + + const rollResult = config.roll.success || config.targets.some(t => t.hit), + looseSpotlight = !rollResult || config.roll.result.duality === -1; + + if (looseSpotlight && game.combat?.active) { + const currentCombatant = game.combat.combatants.get(game.combat.current?.combatantId); + if (currentCombatant?.actorId == actor.id) ui.combat.setCombatantSpotlight(currentCombatant.id); + } + } + static async reroll(rollString, target, message) { let parsedRoll = game.system.api.dice.DualityRoll.fromData({ ...rollString, evaluated: false }); const term = parsedRoll.terms[target.dataset.dieIndex]; @@ -257,13 +340,19 @@ export default class DualityRoll extends D20Roll { newRoll.extra = newRoll.extra.slice(2); const tagTeamSettings = game.settings.get(CONFIG.DH.id, CONFIG.DH.SETTINGS.gameSettings.TagTeamRoll); - Hooks.call(`${CONFIG.DH.id}.postRollDuality`, { + + const config = { source: { actor: message.system.source.actor ?? '' }, targets: message.system.targets, tagTeamSelected: Object.values(tagTeamSettings.members).some(x => x.messageId === message._id), roll: newRoll, - rerolledRoll: message.system.roll - }); + rerolledRoll: message.system.roll, + resourceUpdates: new ResourceUpdateMap(message.system.source.actor ?? '') + }; + + await DualityRoll.automateHopeFear(config); + await config.resourceUpdates.updateResources(); + return { newRoll, parsedRoll }; } } diff --git a/module/documents/actor.mjs b/module/documents/actor.mjs index 35ab5cc6..6340e7f4 100644 --- a/module/documents/actor.mjs +++ b/module/documents/actor.mjs @@ -3,6 +3,7 @@ import { LevelOptionType } from '../data/levelTier.mjs'; import DHFeature from '../data/item/feature.mjs'; import { createScrollText, damageKeyToNumber } from '../helpers/utils.mjs'; import DhCompanionLevelUp from '../applications/levelup/companionLevelup.mjs'; +import { ResourceUpdateMap } from '../data/action/baseAction.mjs'; export default class DhpActor extends Actor { parties = new Set(); @@ -477,6 +478,7 @@ export default class DhpActor extends Actor { async diceRoll(config) { config.source = { ...(config.source ?? {}), actor: this.uuid }; config.data = this.getRollData(); + config.resourceUpdates = new ResourceUpdateMap(this.uuid); const rollClass = config.roll.lite ? CONFIG.Dice.daggerheart['DHRoll'] : this.rollClass; return await rollClass.build(config); } @@ -765,9 +767,10 @@ export default class DhpActor extends Actor { } convertDamageToThreshold(damage) { - const massiveDamageEnabled=game.settings.get(CONFIG.DH.id, CONFIG.DH.SETTINGS.gameSettings.variantRules).massiveDamage.enabled; - if (massiveDamageEnabled && damage >= (this.system.damageThresholds.severe * 2)) { - return 4; + const massiveDamageEnabled = game.settings.get(CONFIG.DH.id, CONFIG.DH.SETTINGS.gameSettings.variantRules) + .massiveDamage.enabled; + if (massiveDamageEnabled && damage >= this.system.damageThresholds.severe * 2) { + return 4; } return damage >= this.system.damageThresholds.severe ? 3 : damage >= this.system.damageThresholds.major ? 2 : 1; }