From 159f5b852941ce5825eab68365a58bb507031dcf Mon Sep 17 00:00:00 2001
From: Johannes Loher <johannes.loher@fg4f.de>
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 c972120..57e0ac5 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<DS4CheckFactoryOptions> = {},
     ) {
-        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<DS4CheckFactoryOptions>} 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<DS4CheckFactoryOptions> = {
         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<IntermediateGmModifierData>} The data given by the user.
+ * @returns The data given by the user.
  */
 async function askGmModifier(
     targetValue: number,
     options: Partial<DS4CheckFactoryOptions> = {},
     { template, title }: { template?: string; title?: string } = {},
-): Promise<IntermediateGmModifierData> {
+): Promise<Partial<IntermediateGmModifierData>> {
     // 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<IntermediateGmModifierData> {
     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 8c3e7dc..b842c3a 100644
--- a/src/templates/roll/roll-options.hbs
+++ b/src/templates/roll/roll-options.hbs
@@ -9,8 +9,10 @@
     <input id="minfumble" data-type="Number" type="number" name="minfumble" value="{{minCritFailure}}" />
     <label for="visibility">{{localize "DS4.RollDialogVisibilityLabel"}}</label>
     <select id="visibility" data-type="String">
-        {{#each rollModes as |rollMode|}}
-            <option value="{{rollMode}}">{{lookup ../config.chatVisibilities rollMode}}</option>
+        {{#select rollMode}}
+        {{#each config.chatVisibilities as |rollModeValue rollModeKey|}}
+        <option value="{{rollModeKey}}">{{rollModeValue}}</option>
         {{/each}}
+        {{/select}}
     </select>
-</form>
+</form>
\ No newline at end of file

From 637af7c4d0ad7f1bd4a9406ac5f9a859d40b0535 Mon Sep 17 00:00:00 2001
From: Johannes Loher <johannes.loher@fg4f.de>
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 57e0ac5..44339b4 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>): DS4CheckFactoryOptions {
         return { ...this, ...other } as DS4CheckFactoryOptions;