Fixed DamageReroll

This commit is contained in:
WBHarry 2026-05-27 19:01:47 +02:00
parent 06c70b7e58
commit 126a8e4660
4 changed files with 59 additions and 6 deletions

View file

@ -108,7 +108,7 @@ export default class DhpChatLog extends foundry.applications.sidebar.tabs.ChatLo
icon: '<i class="fa-solid fa-dice"></i>', icon: '<i class="fa-solid fa-dice"></i>',
visible: li => { visible: li => {
const message = game.messages.get(li.dataset.messageId); const message = game.messages.get(li.dataset.messageId);
return game.user.isGM || message.isAuthor; return message.system.hasRoll && (game.user.isGM || message.isAuthor);
}, },
callback: async li => { callback: async li => {
const message = game.messages.get(li.dataset.messageId); const message = game.messages.get(li.dataset.messageId);
@ -128,10 +128,8 @@ export default class DhpChatLog extends foundry.applications.sidebar.tabs.ChatLo
}, },
callback: async li => { callback: async li => {
const message = game.messages.get(li.dataset.messageId); const message = game.messages.get(li.dataset.messageId);
const damageRoll = new game.system.api.dice.DamageRoll(message.system.damage.hitPoints.roll); const update = await message.system.getRerolledDamage();
await message.system.action.workflow.get('damage')?.execute(message.system, message.id, true); message.update(update);
// message.update({ rolls: [reroll] });
console.log('test');
} }
} }
]; ];

View file

@ -130,6 +130,41 @@ export default class DHActorRoll extends foundry.abstract.TypeDataModel {
}); });
} }
/* TODO: Change how damage data is stored somehow to enable better rerolling */
async getRerolledDamage() {
if (!this.damage) return;
const rerolls = [];
const update = { system: { damage: {} } };
for (const partKey in this.damage) {
const part = this.damage[partKey];
const testRoll = Roll.fromData(part.parts[0].roll);
const rerolled = await testRoll.reroll();
rerolls.push(rerolled);
if (!update.system.damage[partKey]) update.system.damage[partKey] = { parts: [part.parts[0]] };
const partData = update.system.damage[partKey].parts[0];
update.system.damage[partKey].total = rerolled.total;
partData.modifierTotal = rerolled.terms.reduce((acc, x) => {
if (x.isDeterministic && !x.operator) acc += x.total;
return acc;
}, 0);
partData.dice = rerolled.dice;
partData.total = rerolled.total;
partData.roll = rerolled.toJSON();
}
if (game.modules.get('dice-so-nice')?.active) {
for (const roll of rerolls) {
await game.dice3d.showForRoll(roll, game.user, true);
}
} else {
foundry.audio.AudioHelper.play({ src: CONFIG.sounds.dice });
}
return update;
}
registerTargetHook() { registerTargetHook() {
if (!this.parent.isAuthor || !this.hasTarget) return; if (!this.parent.isAuthor || !this.hasTarget) return;
if (this.targetMode && this.parent.targetHook !== null) { if (this.targetMode && this.parent.targetHook !== null) {

View file

@ -224,4 +224,19 @@ export default class D20Roll extends DHRoll {
resetFormula() { resetFormula() {
return (this._formula = this.constructor.getFormula(this.terms)); return (this._formula = this.constructor.getFormula(this.terms));
} }
async reroll(options) {
const result = await super.reroll(options);
if (this instanceof game.system.api.dice.DualityRoll) return result;
if (options?.liveRoll) {
if (game.modules.get('dice-so-nice')?.active) {
await game.dice3d.showForRoll(result, game.user, true);
} else {
foundry.audio.AudioHelper.play({ src: CONFIG.sounds.dice });
}
}
return result;
}
} }

View file

@ -18,7 +18,12 @@ export default class DamageRoll extends DHRoll {
if (config.evaluate !== false) for (const roll of config.roll) await roll.roll.evaluate(); if (config.evaluate !== false) for (const roll of config.roll) await roll.roll.evaluate();
roll._evaluated = true; roll._evaluated = true;
const parts = config.roll.map(r => this.postEvaluate(r));
const parts = [];
for (const roll of config.roll) {
parts.push(this.postEvaluate(roll));
roll.roll = JSON.stringify(roll.roll.toJSON());
}
config.damage = this.unifyDamageRoll(parts); config.damage = this.unifyDamageRoll(parts);
} }