From 516c04c8de4b8786a0bec20affc2158688577291 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20R=C3=BCmpelein?= Date: Sun, 10 Jan 2021 21:03:11 +0100 Subject: [PATCH] First proper parsing without much "nice-ity". --- src/module/rolls/check-factory.ts | 81 +++++++++++++++++++++-------- src/templates/roll/roll-options.hbs | 8 +-- 2 files changed, 63 insertions(+), 26 deletions(-) diff --git a/src/module/rolls/check-factory.ts b/src/module/rolls/check-factory.ts index 24c563c0..8d560ea0 100644 --- a/src/module/rolls/check-factory.ts +++ b/src/module/rolls/check-factory.ts @@ -27,7 +27,6 @@ class CheckFactory { ) { this.checkOptions = new DefaultCheckOptions().mergeWith(passedOptions); } - private checkOptions: DS4CheckFactoryOptions; async execute(): Promise { @@ -42,13 +41,14 @@ class CheckFactory { const roll = new rollCls(formula); const rollModeTemplate = this.checkOptions.rollMode; + console.log(rollModeTemplate); return roll.toMessage({}, { rollMode: rollModeTemplate, create: true }); } // Term generators createTargetValueTerm(): string { if (this.checkTargetValue != null) { - return "v" + this.checkTargetValue; + return "v" + (this.checkTargetValue + this.gmModifier); } else { return null; } @@ -80,8 +80,15 @@ export async function createCheckRoll(targetValue: number, options: Partial = { + maxCritSuccess: gmModifierData.maxCritSuccess ?? options.maxCritSuccess ?? undefined, + minCritFailure: gmModifierData.minCritFailure ?? options.minCritFailure ?? undefined, + useSlayingDice: gmModifierData.useSlayingDice ?? options.useSlayingDice ?? undefined, + rollMode: gmModifierData.rollMode ?? options.rollMode ?? undefined, + }; + // Create Factory - const cf = new CheckFactory(targetValue, gmModifierData.gmModifier, options); + const cf = new CheckFactory(gmModifierData.checkTargetValue, gmModifierData.gmModifier, newOptions); // Possibly additional processing @@ -115,30 +122,60 @@ async function askGmModifier( const renderedHtml = await renderTemplate(usedTemplate, templateData); // TODO: Localize - const dialogData: DialogData = { - title: title ?? "Roll Options", - close: (html) => null, - content: renderedHtml, - buttons: { - ok: { - label: "OK", - callback: (html) => null, + const dialogPromise = new Promise((resolve) => { + new Dialog( + { + title: title ?? "Roll Options", + close: (html: JQuery) => resolve(null), + content: renderedHtml, + buttons: { + ok: { + label: "OK", + callback: (html: HTMLElement | JQuery) => { + if (!("jquery" in html)) { + throw new Error("Internal Type Error"); + } else { + const innerForm = html[0].querySelector("form"); + resolve(innerForm); + } + }, + }, + cancel: { + label: "Cancel", + callback: (html: JQuery) => resolve(null), + }, + }, + default: "ok", }, - cancel: { - label: "Cancel", - callback: (html) => null, - }, - }, - default: "ok", - }; - - const dialog = new Dialog(dialogData, {}).render(true); - - return { gmModifier: 0 }; + {}, + ).render(true); + }); + const dialogForm = await dialogPromise; + return parseDialogFormData(dialogForm, targetValue); } +function parseDialogFormData(formData: HTMLFormElement, targetValue: number): GmModifierData { + const parsedData = { + 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, + }; + console.log("Data", parsedData); + return parsedData; +} + +// TODO: Remove unnecessary data step by step interface GmModifierData { + checkTargetValue: number; gmModifier: number; + maxCritSuccess: number; + minCritFailure: number; + // TODO: In final version from system settings + useSlayingDice: boolean; + rollMode: DS4RollMode; } export interface DS4CheckFactoryOptions { diff --git a/src/templates/roll/roll-options.hbs b/src/templates/roll/roll-options.hbs index 4cab330e..9e2b320e 100644 --- a/src/templates/roll/roll-options.hbs +++ b/src/templates/roll/roll-options.hbs @@ -1,12 +1,12 @@
- + - + - + - +