From c4697637e0d2076c68ebe5cbb977773023875d84 Mon Sep 17 00:00:00 2001 From: WBHarry Date: Fri, 8 Aug 2025 17:51:10 +0200 Subject: [PATCH] Added rerolls for damage dice in chat --- module/applications/ui/chatLog.mjs | 31 +++++++----- module/dice/damageRoll.mjs | 63 +++++++++++++++++++++++++ styles/less/ui/chat/chat.less | 8 +++- templates/ui/chat/parts/damage-part.hbs | 7 ++- 4 files changed, 95 insertions(+), 14 deletions(-) diff --git a/module/applications/ui/chatLog.mjs b/module/applications/ui/chatLog.mjs index fd9ab096..65560194 100644 --- a/module/applications/ui/chatLog.mjs +++ b/module/applications/ui/chatLog.mjs @@ -228,19 +228,28 @@ export default class DhpChatLog extends foundry.applications.sidebar.tabs.ChatLo } const target = event.target.closest('[data-die-index]'); - let originalRoll_parsed = message.rolls.map(roll => JSON.parse(roll))[0]; - const rollClass = - game.system.api.dice[ - message.type === 'dualityRoll' ? 'DualityRoll' : target.dataset.type === 'damage' ? 'DHRoll' : 'D20Roll' - ]; - if (!game.modules.get('dice-so-nice')?.active) foundry.audio.AudioHelper.play({ src: CONFIG.sounds.dice }); + if (target.dataset.type === 'damage') { + game.system.api.dice.DamageRoll.reroll(target, message); + } else { + let originalRoll_parsed = message.rolls.map(roll => JSON.parse(roll))[0]; + const rollClass = + game.system.api.dice[ + message.type === 'dualityRoll' + ? 'DualityRoll' + : target.dataset.type === 'damage' + ? 'DHRoll' + : 'D20Roll' + ]; - const { newRoll, parsedRoll } = await rollClass.reroll(originalRoll_parsed, target, message); + if (!game.modules.get('dice-so-nice')?.active) foundry.audio.AudioHelper.play({ src: CONFIG.sounds.dice }); - await game.messages.get(message._id).update({ - 'system.roll': newRoll, - 'rolls': [parsedRoll] - }); + const { newRoll, parsedRoll } = await rollClass.reroll(originalRoll_parsed, target, message); + + await game.messages.get(message._id).update({ + 'system.roll': newRoll, + 'rolls': [parsedRoll] + }); + } } } diff --git a/module/dice/damageRoll.mjs b/module/dice/damageRoll.mjs index 999a4bb8..e84a333b 100644 --- a/module/dice/damageRoll.mjs +++ b/module/dice/damageRoll.mjs @@ -134,4 +134,67 @@ export default class DamageRoll extends DHRoll { } return (part.roll._formula = this.constructor.getFormula(part.roll.terms)); } + + static async reroll(target, message) { + const { damageType, part, dice, result } = target.dataset; + const rollPart = message.system.damage[damageType].parts[part]; + + let parsedRoll = game.system.api.dice.DamageRoll.fromData({ + ...rollPart.roll, + terms: rollPart.roll.terms.map((term, index) => ({ + ...term, + ...(term.class === 'Die' ? { results: rollPart.dice[index].results } : {}) + })), + class: 'DamageRoll', + evaluated: false + }); + + const term = parsedRoll.terms[dice]; + const termResult = parsedRoll.terms[dice].results[result]; + + const newIndex = parsedRoll.terms[dice].results.length; + await term.reroll(`/r1=${termResult.result}`); + + if (game.modules.get('dice-so-nice')?.active) { + const newResult = parsedRoll.terms[dice].results[newIndex]; + const diceSoNiceRoll = { + _evaluated: true, + dice: [ + new foundry.dice.terms.Die({ + ...term, + total: newResult.result, + faces: term._faces, + results: [newResult] + }) + ], + options: { appearance: {} } + }; + + await game.dice3d.showForRoll(diceSoNiceRoll, game.user, true); + } + + await parsedRoll.evaluate(); + + const results = parsedRoll.dice[dice].results.map(result => ({ + ...result, + discarded: !result.active + })); + const newResult = results.splice(results.length - 1, 1); + results.splice(Number(result) + 1, 0, newResult[0]); + + const rerolledDice = { + dice: parsedRoll.dice[dice].denomination, + total: parsedRoll.dice[dice].total, + results: results + }; + + await game.messages.get(message._id).update({ + [`system.damage.${damageType}.parts.${part}`]: { + ...rollPart, + total: parsedRoll.total, + dice: [rerolledDice], + [`dice.${dice}`]: rerolledDice + } + }); + } } diff --git a/styles/less/ui/chat/chat.less b/styles/less/ui/chat/chat.less index 9afa32d3..d0b32bc6 100644 --- a/styles/less/ui/chat/chat.less +++ b/styles/less/ui/chat/chat.less @@ -522,10 +522,14 @@ .roll-die { display: grid; grid-template-areas: - ". a a" - "c b b"; + '. a a' + 'c b b'; gap: 3px; + .reroll-button:hover { + filter: drop-shadow(0 0 3px @golden); + } + label { text-align: center; height: var(--font-size-12); diff --git a/templates/ui/chat/parts/damage-part.hbs b/templates/ui/chat/parts/damage-part.hbs index 7ec8bfd8..10c8a6f5 100644 --- a/templates/ui/chat/parts/damage-part.hbs +++ b/templates/ui/chat/parts/damage-part.hbs @@ -29,7 +29,12 @@ {{#each results}} {{#unless discarded}}
-
{{result}}
+
+ {{result}} +
{{/unless}} {{/each}}