From d25212d9d7f68470bddbfa593e6536736d8f6662 Mon Sep 17 00:00:00 2001 From: IrkTheImp Date: Tue, 1 Jul 2025 21:29:52 -0500 Subject: [PATCH] update command to use new dice roll. --- daggerheart.mjs | 46 +++++----------------- module/applications/chatMessage.mjs | 8 +++- module/applications/roll.mjs | 44 +++++++++++++++------ module/data/action/action.mjs | 61 +++++++++++++++++------------ module/helpers/utils.mjs | 4 +- 5 files changed, 87 insertions(+), 76 deletions(-) diff --git a/daggerheart.mjs b/daggerheart.mjs index 52d54d77..80f070f4 100644 --- a/daggerheart.mjs +++ b/daggerheart.mjs @@ -215,46 +215,20 @@ Hooks.on('chatMessage', (_, message) => { const hopeAndFearRoll = `1${rollCommand.hope ?? 'd12'}+1${rollCommand.fear ?? 'd12'}`; const advantageRoll = `${advantageState === true ? '+d6' : advantageState === false ? '-d6' : ''}`; const attributeRoll = `${trait?.value ? `${trait.value > 0 ? `+${trait.value}` : `${trait.value}`}` : ''}`; - const roll = new DualityRoll(`${hopeAndFearRoll}${advantageRoll}${attributeRoll}`); - await roll.evaluate(); + const formula = `${hopeAndFearRoll}${advantageRoll}${attributeRoll}`; - console.log('roll result', roll.result); - - setDiceSoNiceForDualityRoll(roll, advantageState); - - resolve({ - roll, - trait: trait - ? { - value: trait.value, - label: `${game.i18n.localize(abilities[traitValue].label)} ${trait.value >= 0 ? `+` : ``}${trait.value}` - } - : undefined, - title - }); - }).then(async ({ roll, trait, title }) => { - const cls = getDocumentClass('ChatMessage'); - const systemData = new DHDualityRoll({ + const config = { title: title, - origin: target?.id, - roll: roll, - modifiers: trait ? [trait] : [], - hope: { dice: rollCommand.hope ?? 'd12', value: roll.dice[0].total }, - fear: { dice: rollCommand.fear ?? 'd12', value: roll.dice[1].total }, - advantage: advantageState !== null ? { dice: 'd6', value: roll.dice[2].total } : undefined, - advantageState - }); - - const msgData = { - type: 'dualityRoll', - sound: CONFIG.sounds.dice, - system: systemData, - user: game.user.id, - content: 'systems/daggerheart/templates/chat/duality-roll.hbs', - rolls: [roll] + roll: { formula }, + targets: target, + hasSave: false, + dialog: { configure: false }, + evaluate: true }; - cls.create(msgData); + await CONFIG.Dice.daggerheart['DualityRoll'].build(config); + + resolve(); }); } diff --git a/module/applications/chatMessage.mjs b/module/applications/chatMessage.mjs index 1328de57..ef76d18f 100644 --- a/module/applications/chatMessage.mjs +++ b/module/applications/chatMessage.mjs @@ -1,6 +1,10 @@ export default class DhpChatMessage extends foundry.documents.ChatMessage { async renderHTML() { - if(this.system.messageTemplate) this.content = await foundry.applications.handlebars.renderTemplate(this.system.messageTemplate, this.system); + if (this.system.messageTemplate) + this.content = await foundry.applications.handlebars.renderTemplate( + this.system.messageTemplate, + this.system + ); /* We can change to fully implementing the renderHTML function if needed, instead of augmenting it. */ const html = await super.renderHTML(); @@ -14,7 +18,7 @@ export default class DhpChatMessage extends foundry.documents.ChatMessage { break; case -1: html.classList.add('fear'); - break; + break; default: html.classList.add('critical'); break; diff --git a/module/applications/roll.mjs b/module/applications/roll.mjs index caf3dd70..a81ac071 100644 --- a/module/applications/roll.mjs +++ b/module/applications/roll.mjs @@ -1,6 +1,7 @@ import DHDamageRoll from '../data/chat-message/damageRoll.mjs'; import D20RollDialog from '../dialogs/d20RollDialog.mjs'; import DamageDialog from '../dialogs/damageDialog.mjs'; +import { setDiceSoNiceForDualityRoll } from '../helpers/utils.mjs'; /* - Damage & other resources roll @@ -54,7 +55,6 @@ export class DHRoll extends Roll { } static async buildPost(roll, config, message) { - console.log(config); for (const hook of config.hooks) { if (Hooks.call(`${SYSTEM.id}.postRoll${hook.capitalize()}`, config, message) === false) return null; } @@ -95,7 +95,8 @@ export class DHRoll extends Roll { } static applyKeybindings(config) { - config.dialog.configure ??= !(config.event.shiftKey || config.event.altKey || config.event.ctrlKey); + if (config.event) + config.dialog.configure ??= !(config.event.shiftKey || config.event.altKey || config.event.ctrlKey); } constructFormula(config) { @@ -161,12 +162,20 @@ export class D20Roll extends DHRoll { } static applyKeybindings(config) { - const keys = { - normal: config.event.shiftKey || config.event.altKey || config.event.ctrlKey, - advantage: config.event.altKey, - disadvantage: config.event.ctrlKey + let keys = { + normal: true, + advantage: false, + disadvantage: false }; + if (config.event) { + keys = { + normal: config.event.shiftKey || config.event.altKey || config.event.ctrlKey, + advantage: config.event.altKey, + disadvantage: config.event.ctrlKey + }; + } + // Should the roll configuration dialog be displayed? config.dialog.configure ??= !Object.values(keys).some(k => k); @@ -372,12 +381,23 @@ export class DualityRoll extends D20Roll { } applyBaseBonus() { - this.options.roll.modifiers = [ - { - label: 'HI', - value: Roll.replaceFormulaData(`@traits.${this.options.roll.trait}.total`, this.data) - } - ]; + if (this.options.roll.trait) { + this.options.roll.modifiers = [ + { + label: `DAGGERHEART.Abilities.${this.options.roll.trait}.name`, + value: Roll.replaceFormulaData(`@traits.${this.options.roll.trait}.total`, this.data) + } + ]; + } else { + this.options.roll.modifiers = []; + } + } + + static async buildEvaluate(roll, config = {}, message = {}) { + if (config.evaluate !== false) await roll.evaluate(); + const advantageState = this.hasAdvantage ? true : this.hasDisadvantage ? false : null; + setDiceSoNiceForDualityRoll(roll, advantageState); + this.postEvaluate(roll, config); } static postEvaluate(roll, config = {}) { diff --git a/module/data/action/action.mjs b/module/data/action/action.mjs index af5e061e..fed7a890 100644 --- a/module/data/action/action.mjs +++ b/module/data/action/action.mjs @@ -1,6 +1,6 @@ import CostSelectionDialog from '../../applications/costSelectionDialog.mjs'; import { DHActionDiceData, DHActionRollData, DHDamageData, DHDamageField } from './actionDice.mjs'; -import DhpActor from '../../documents/actor.mjs'; +//import DhpActor from '../../documents/actor.mjs'; import D20RollDialog from '../../dialogs/d20RollDialog.mjs'; const fields = foundry.data.fields; @@ -129,13 +129,13 @@ export class DHBaseAction extends foundry.abstract.DataModel { return this.parent.parent; } - get actor() { - return this.item instanceof DhpActor - ? this.item - : this.item?.parent instanceof DhpActor - ? this.item.parent - : this.item?.actor; - } + // get actor() { + // return this.item instanceof DhpActor + // ? this.item + // : this.item?.parent instanceof DhpActor + // ? this.item.parent + // : this.item?.actor; + // } get chatTemplate() { return 'systems/daggerheart/templates/chat/duality-roll.hbs'; @@ -402,11 +402,18 @@ export class DHBaseAction extends foundry.abstract.DataModel { hasCost(costs) { const realCosts = this.getRealCosts(costs), hasFearCost = realCosts.findIndex(c => c.type === 'fear'); - if(hasFearCost > -1) { + if (hasFearCost > -1) { const fearCost = realCosts.splice(hasFearCost, 1); - if(!game.user.isGM || fearCost[0].total > game.settings.get(SYSTEM.id, SYSTEM.SETTINGS.gameSettings.Resources.Fear)) return false; + if ( + !game.user.isGM || + fearCost[0].total > game.settings.get(SYSTEM.id, SYSTEM.SETTINGS.gameSettings.Resources.Fear) + ) + return false; } - return realCosts.reduce((a, c) => a && this.actor.system.resources[c.type]?.value >= (c.total ?? c.value), true); + return realCosts.reduce( + (a, c) => a && this.actor.system.resources[c.type]?.value >= (c.total ?? c.value), + true + ); } /* COST */ @@ -498,19 +505,25 @@ export class DHBaseAction extends foundry.abstract.DataModel { /* SAVE */ async rollSave(target, event, message) { - if(!target?.actor) return; - return target.actor.diceRoll({ - event, - title: 'Roll Save', - roll: { - trait: this.save.trait, - difficulty: this.save.difficulty, - type: "reaction" - }, - data: target.actor.getRollData() - }).then(async (result) => { - if(result) this.updateChatMessage(message, target.id, {result: result.roll.total, success: result.roll.success}); - }) + if (!target?.actor) return; + return target.actor + .diceRoll({ + event, + title: 'Roll Save', + roll: { + trait: this.save.trait, + difficulty: this.save.difficulty, + type: 'reaction' + }, + data: target.actor.getRollData() + }) + .then(async result => { + if (result) + this.updateChatMessage(message, target.id, { + result: result.roll.total, + success: result.roll.success + }); + }); } async updateChatMessage(message, targetId, changes, chain = true) { diff --git a/module/helpers/utils.mjs b/module/helpers/utils.mjs index 7816d0f4..0d31e80a 100644 --- a/module/helpers/utils.mjs +++ b/module/helpers/utils.mjs @@ -124,8 +124,8 @@ export const getCommandTarget = () => { export const setDiceSoNiceForDualityRoll = (rollResult, advantageState) => { const diceSoNicePresets = getDiceSoNicePresets(); - rollResult.dice[0].options.appearance = diceSoNicePresets.hope; - rollResult.dice[1].options.appearance = diceSoNicePresets.fear; + rollResult.dice[0].options = { appearance: diceSoNicePresets.hope }; + rollResult.dice[1].options = { appearance: diceSoNicePresets.fear }; //diceSoNicePresets.fear; if (rollResult.dice[2]) { if (advantageState === true) { rollResult.dice[2].options.appearance = diceSoNicePresets.advantage;