Added rerolls for damage dice in chat

This commit is contained in:
WBHarry 2025-08-08 17:51:10 +02:00
parent a25007b994
commit c4697637e0
4 changed files with 95 additions and 14 deletions

View file

@ -228,10 +228,18 @@ export default class DhpChatLog extends foundry.applications.sidebar.tabs.ChatLo
} }
const target = event.target.closest('[data-die-index]'); const target = event.target.closest('[data-die-index]');
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]; let originalRoll_parsed = message.rolls.map(roll => JSON.parse(roll))[0];
const rollClass = const rollClass =
game.system.api.dice[ game.system.api.dice[
message.type === 'dualityRoll' ? 'DualityRoll' : target.dataset.type === 'damage' ? 'DHRoll' : 'D20Roll' 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 (!game.modules.get('dice-so-nice')?.active) foundry.audio.AudioHelper.play({ src: CONFIG.sounds.dice });
@ -243,4 +251,5 @@ export default class DhpChatLog extends foundry.applications.sidebar.tabs.ChatLo
'rolls': [parsedRoll] 'rolls': [parsedRoll]
}); });
} }
}
} }

View file

@ -134,4 +134,67 @@ export default class DamageRoll extends DHRoll {
} }
return (part.roll._formula = this.constructor.getFormula(part.roll.terms)); 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
}
});
}
} }

View file

@ -522,10 +522,14 @@
.roll-die { .roll-die {
display: grid; display: grid;
grid-template-areas: grid-template-areas:
". a a" '. a a'
"c b b"; 'c b b';
gap: 3px; gap: 3px;
.reroll-button:hover {
filter: drop-shadow(0 0 3px @golden);
}
label { label {
text-align: center; text-align: center;
height: var(--font-size-12); height: var(--font-size-12);

View file

@ -29,7 +29,12 @@
{{#each results}} {{#each results}}
{{#unless discarded}} {{#unless discarded}}
<div class="roll-die{{#unless @../first}} has-plus{{/unless}}"> <div class="roll-die{{#unless @../first}} has-plus{{/unless}}">
<div class="dice {{../dice}}">{{result}}</div> <div
class="dice reroll-button {{../dice}}"
data-die-index="0" data-type="damage" data-damage-type="{{@../../../key}}" data-part="{{@../../key}}" data-dice="{{@../key}}" data-result="{{@key}}"
>
{{result}}
</div>
</div> </div>
{{/unless}} {{/unless}}
{{/each}} {{/each}}