From 159f5b852941ce5825eab68365a58bb507031dcf Mon Sep 17 00:00:00 2001 From: Johannes Loher Date: Sun, 24 Jan 2021 03:19:31 +0100 Subject: [PATCH 1/2] make it possible to pass rollMode to createCheckRoll --- src/module/rolls/check-factory.ts | 35 ++++++++++++++--------------- src/templates/roll/roll-options.hbs | 8 ++++--- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/src/module/rolls/check-factory.ts b/src/module/rolls/check-factory.ts index c972120c..57e0ac56 100644 --- a/src/module/rolls/check-factory.ts +++ b/src/module/rolls/check-factory.ts @@ -28,7 +28,7 @@ class CheckFactory { private gmModifier: number, passedOptions: Partial = {}, ) { - this.checkOptions = new DefaultCheckOptions().mergeWith(passedOptions); + this.checkOptions = defaultCheckOptions.mergeWith(passedOptions); } private checkOptions: DS4CheckFactoryOptions; @@ -45,7 +45,6 @@ class CheckFactory { const roll = new rollCls(formula); const rollModeTemplate = this.checkOptions.rollMode; - console.log(rollModeTemplate); return roll.toMessage({}, { rollMode: rollModeTemplate, create: true }); } @@ -76,8 +75,8 @@ class CheckFactory { /** * Asks the user for all unknown/necessary information and passes them on to perform a roll. - * @param targetValue {number} The Check Target Number ("CTN") - * @param options {Partial} Options changing the behaviour of the roll and message. + * @param targetValue The Check Target Number ("CTN") + * @param options Options changing the behaviour of the roll and message. */ export async function createCheckRoll( targetValue: number, @@ -86,6 +85,8 @@ export async function createCheckRoll( // Ask for additional required data; const gmModifierData = await askGmModifier(targetValue, options); + const newTargetValue = gmModifierData.checkTargetValue ?? targetValue; + const gmModifier = gmModifierData.gmModifier ?? 0; const newOptions: Partial = { maxCritSuccess: gmModifierData.maxCritSuccess ?? options.maxCritSuccess ?? undefined, minCritFailure: gmModifierData.minCritFailure ?? options.minCritFailure ?? undefined, @@ -94,7 +95,7 @@ export async function createCheckRoll( }; // Create Factory - const cf = new CheckFactory(gmModifierData.checkTargetValue, gmModifierData.gmModifier, newOptions); + const cf = new CheckFactory(newTargetValue, gmModifier, newOptions); // Possibly additional processing @@ -108,13 +109,13 @@ export async function createCheckRoll( * @notes * At the moment, this asks for more data than it will do after some iterations. * - * @returns {Promise} The data given by the user. + * @returns The data given by the user. */ async function askGmModifier( targetValue: number, options: Partial = {}, { template, title }: { template?: string; title?: string } = {}, -): Promise { +): Promise> { // Render model interface and return value const usedTemplate = template ?? "systems/ds4/templates/roll/roll-options.hbs"; const usedTitle = title ?? game.i18n.localize("DS4.RollDialogDefaultTitle"); @@ -124,7 +125,7 @@ async function askGmModifier( checkTargetValue: targetValue, maxCritSuccess: options.maxCritSuccess ?? defaultCheckOptions.maxCritSuccess, minCritFailure: options.minCritFailure ?? defaultCheckOptions.minCritFailure, - rollModes: rollModes, + rollMode: options.rollMode, config: DS4, }; const renderedHtml = await renderTemplate(usedTemplate, templateData); @@ -167,22 +168,20 @@ async function askGmModifier( ).render(true); }); const dialogForm = await dialogPromise; - return parseDialogFormData(dialogForm, targetValue); + return parseDialogFormData(dialogForm); } /** * Extracts Dialog data from the returned DOM element. - * @param formData {HTMLFormElement} The filed dialog - * @param targetValue {number} The previously known target value (slated for removal once data automation is available) + * @param formData The filed dialog */ -function parseDialogFormData(formData: HTMLFormElement, targetValue: number): IntermediateGmModifierData { +function parseDialogFormData(formData: HTMLFormElement): Partial { return { - checkTargetValue: parseInt(formData["ctv"]?.value) ?? targetValue, - gmModifier: parseInt(formData["gmmod"]?.value) ?? 0, - maxCritSuccess: parseInt(formData["maxcoup"]?.value) ?? defaultCheckOptions.maxCritSuccess, - minCritFailure: parseInt(formData["minfumble"]?.value) ?? defaultCheckOptions.minCritFailure, - useSlayingDice: false, - rollMode: formData["visibility"]?.value ?? defaultCheckOptions.rollMode, + checkTargetValue: parseInt(formData["ctv"]?.value), + gmModifier: parseInt(formData["gmmod"]?.value), + maxCritSuccess: parseInt(formData["maxcoup"]?.value), + minCritFailure: parseInt(formData["minfumble"]?.value), + rollMode: formData["visibility"]?.value, }; } diff --git a/src/templates/roll/roll-options.hbs b/src/templates/roll/roll-options.hbs index 8c3e7dc3..b842c3a1 100644 --- a/src/templates/roll/roll-options.hbs +++ b/src/templates/roll/roll-options.hbs @@ -9,8 +9,10 @@ - + \ No newline at end of file From 637af7c4d0ad7f1bd4a9406ac5f9a859d40b0535 Mon Sep 17 00:00:00 2001 From: Johannes Loher Date: Mon, 25 Jan 2021 01:25:45 +0100 Subject: [PATCH 2/2] make DefaultCheckOptions --- src/module/rolls/check-factory.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/module/rolls/check-factory.ts b/src/module/rolls/check-factory.ts index 57e0ac56..44339b47 100644 --- a/src/module/rolls/check-factory.ts +++ b/src/module/rolls/check-factory.ts @@ -4,10 +4,10 @@ import { DS4 } from "../config"; * Provides default values for all arguments the `CheckFactory` expects. */ class DefaultCheckOptions implements DS4CheckFactoryOptions { - maxCritSuccess = 1; - minCritFailure = 20; - useSlayingDice = false; - rollMode: DS4RollMode = "roll"; + readonly maxCritSuccess = 1; + readonly minCritFailure = 20; + readonly useSlayingDice = false; + readonly rollMode: DS4RollMode = "roll"; mergeWith(other: Partial): DS4CheckFactoryOptions { return { ...this, ...other } as DS4CheckFactoryOptions;