Fixed multiple dice

This commit is contained in:
WBHarry 2025-08-08 19:07:19 +02:00
parent c4697637e0
commit b44d4695ad

View file

@ -139,24 +139,36 @@ export default class DamageRoll extends DHRoll {
const { damageType, part, dice, result } = target.dataset; const { damageType, part, dice, result } = target.dataset;
const rollPart = message.system.damage[damageType].parts[part]; const rollPart = message.system.damage[damageType].parts[part];
let diceIndex = 0;
let parsedRoll = game.system.api.dice.DamageRoll.fromData({ let parsedRoll = game.system.api.dice.DamageRoll.fromData({
...rollPart.roll, ...rollPart.roll,
terms: rollPart.roll.terms.map((term, index) => ({ terms: rollPart.roll.terms.map(term => {
...term, const isDie = term.class === 'Die';
...(term.class === 'Die' ? { results: rollPart.dice[index].results } : {}) const fixedTerm = {
})), ...term,
...(isDie ? { results: rollPart.dice[diceIndex].results } : {})
};
if (isDie) diceIndex++;
return fixedTerm;
}),
class: 'DamageRoll', class: 'DamageRoll',
evaluated: false evaluated: false
}); });
const term = parsedRoll.terms[dice]; const parsedDiceTerms = Object.keys(parsedRoll.terms).reduce((acc, key) => {
const termResult = parsedRoll.terms[dice].results[result]; const term = parsedRoll.terms[key];
if (term instanceof CONFIG.Dice.termTypes.DiceTerm) acc[Object.keys(acc).length] = term;
return acc;
}, {});
const term = parsedDiceTerms[dice];
const termResult = parsedDiceTerms[dice].results[result];
const newIndex = parsedRoll.terms[dice].results.length; const newIndex = parsedDiceTerms[dice].results.length;
await term.reroll(`/r1=${termResult.result}`); await term.reroll(`/r1=${termResult.result}`);
if (game.modules.get('dice-so-nice')?.active) { if (game.modules.get('dice-so-nice')?.active) {
const newResult = parsedRoll.terms[dice].results[newIndex]; const newResult = parsedDiceTerms[dice].results[newIndex];
const diceSoNiceRoll = { const diceSoNiceRoll = {
_evaluated: true, _evaluated: true,
dice: [ dice: [
@ -182,18 +194,25 @@ export default class DamageRoll extends DHRoll {
const newResult = results.splice(results.length - 1, 1); const newResult = results.splice(results.length - 1, 1);
results.splice(Number(result) + 1, 0, newResult[0]); results.splice(Number(result) + 1, 0, newResult[0]);
const rerolledDice = { const rerolledDice = parsedRoll.dice.map((x, index) => {
dice: parsedRoll.dice[dice].denomination, if (index !== Number(dice)) return { ...x, dice: x.denomination };
total: parsedRoll.dice[dice].total, return {
results: results dice: parsedRoll.dice[dice].denomination,
}; total: parsedRoll.dice[dice].total,
results: results
};
});
await game.messages.get(message._id).update({ const updateMessage = game.messages.get(message._id);
[`system.damage.${damageType}.parts.${part}`]: { await updateMessage.update({
...rollPart, [`system.damage.${damageType}`]: {
...updateMessage,
total: parsedRoll.total, total: parsedRoll.total,
dice: [rerolledDice], [`parts.${part}`]: {
[`dice.${dice}`]: rerolledDice ...rollPart,
total: parsedRoll.total,
dice: rerolledDice
}
} }
}); });
} }