From 527ad9cc64388d4e8ca291e1d435d54e2a210f44 Mon Sep 17 00:00:00 2001 From: George1044 Date: Thu, 21 Sep 2023 11:20:26 +0300 Subject: [PATCH] Fix manual rolls for damage rolls This was done on the PF2e system and their rolling data structures, not sure if this fix works for other systems. --- df-manual-rolls/src/ManualRolls.ts | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/df-manual-rolls/src/ManualRolls.ts b/df-manual-rolls/src/ManualRolls.ts index d9b6c17..21970ce 100644 --- a/df-manual-rolls/src/ManualRolls.ts +++ b/df-manual-rolls/src/ManualRolls.ts @@ -36,6 +36,27 @@ export default class ManualRolls { libWrapper.unregister(SETTINGS.MOD_NAME, 'DiceTerm.prototype.roll', false); } + static async setRollPromptRecursively(obj: any, rollPrompt: RollPrompt, termsToRoll: RollTerm[]) { + if (obj instanceof RollTerm) { + termsToRoll.push(obj); + } + if (obj instanceof PoolTerm){ + return; + } + // If the object is a DiceTerm, set the rollPrompt + if (obj instanceof DiceTerm) { + (obj).rollPrompt = rollPrompt; + + } else if (typeof obj === 'object') { + // If the object is an object, recursively check its properties + for (const key in obj) { + if (obj.hasOwnProperty(key)) { + await ManualRolls.setRollPromptRecursively(obj[key], rollPrompt, termsToRoll); + } + } + } + } + private static async _Roll_evaluate(this: Roll, wrapper: (arg: any) => any, { minimize = false, maximize = false } = {}): Promise { // Ignore Min/Max requests and if we are disabled if (!ManualRolls.shouldRollManually || minimize || maximize) { @@ -65,15 +86,14 @@ export default class ManualRolls { /****** DF MANUAL ROLLS MODIFICATION ******/ // @ts-ignore const rollPrompt = new RollPrompt({}, this.options.flavor ? { title: this.options.flavor } : {}); - + const termsToRoll: (RollTerm)[] = []; for (const term of this.terms) { - if (!(term instanceof DiceTerm)) continue; - (term).rollPrompt = rollPrompt; + await ManualRolls.setRollPromptRecursively(term, rollPrompt, termsToRoll); } // Step 3 - Evaluate remaining terms const promises: Promise[] = []; - for (const term of this.terms) { + for (const term of termsToRoll) { // @ts-ignore if (term._evaluated) continue; promises.push(term.evaluate({ minimize, maximize, async: true })); @@ -81,6 +101,7 @@ export default class ManualRolls { await rollPrompt.render(true); await Promise.all(promises); /************ END MODIFICATION ************/ + // Step 4 - Evaluate the final expression this._total = this._evaluateTotal();