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,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]
});
}
}
}

View file

@ -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
}
});
}
}

View file

@ -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);

View file

@ -29,7 +29,12 @@
{{#each results}}
{{#unless discarded}}
<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>
{{/unless}}
{{/each}}