From 1f6d13c49dd48502960e07a8e88182f5616490a4 Mon Sep 17 00:00:00 2001 From: Johannes Loher Date: Sat, 26 Jun 2021 14:29:49 +0200 Subject: [PATCH] Fix issue with CheckFactory parsing empty strings as NaN for check related values --- src/module/rolls/check-factory.ts | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/module/rolls/check-factory.ts b/src/module/rolls/check-factory.ts index 6e90977b..ae02aa78 100644 --- a/src/module/rolls/check-factory.ts +++ b/src/module/rolls/check-factory.ts @@ -46,7 +46,8 @@ class CheckFactory { } createCheckTargetNumberModifier(): string { - return "v" + Math.max(this.checkTargetNumber + this.gmModifier, 0); + const totalCheckTargetNumber = Math.max(this.checkTargetNumber + this.gmModifier, 0); + return `v${totalCheckTargetNumber}`; } createCoupFumbleModifier(): string | null { @@ -55,7 +56,7 @@ class CheckFactory { const isMaximumCoupResultRequired = this.options.maximumCoupResult !== defaultCheckOptions.maximumCoupResult; if (isMinimumFumbleResultRequired || isMaximumCoupResultRequired) { - return "c" + (this.options.maximumCoupResult ?? "") + ":" + (this.options.minimumFumbleResult ?? ""); + return `c${this.options.maximumCoupResult ?? ""}:${this.options.minimumFumbleResult ?? ""}`; } else { return null; } @@ -162,12 +163,20 @@ async function askGmModifier( * @param formData - The filed dialog */ function parseDialogFormData(formData: HTMLFormElement): Partial { + const chosenCheckTargetNumber = parseInt(formData["check-target-number"]?.value); + const chosenGMModifier = parseInt(formData["gm-modifier"]?.value); + const chosenMaximumCoupResult = parseInt(formData["maximum-coup-result"]?.value); + const chosenMinimumFumbleResult = parseInt(formData["minimum-fumble-result"]?.value); + const chosenRollMode = formData["roll-mode"]?.value; + + const invalidNumbers = [NaN, Infinity, -Infinity]; + return { - checkTargetNumber: parseInt(formData["check-target-number"]?.value), - gmModifier: parseInt(formData["gm-modifier"]?.value), - maximumCoupResult: parseInt(formData["maximum-coup-result"]?.value), - minimumFumbleResult: parseInt(formData["minimum-fumble-result"]?.value), - rollMode: formData["roll-mode"]?.value, + checkTargetNumber: invalidNumbers.includes(chosenCheckTargetNumber) ? undefined : chosenCheckTargetNumber, + gmModifier: invalidNumbers.includes(chosenGMModifier) ? undefined : chosenGMModifier, + maximumCoupResult: invalidNumbers.includes(chosenMaximumCoupResult) ? undefined : chosenMaximumCoupResult, + minimumFumbleResult: invalidNumbers.includes(chosenMinimumFumbleResult) ? undefined : chosenMinimumFumbleResult, + rollMode: Object.values(CONST.DICE_ROLL_MODES).includes(chosenRollMode) ? chosenRollMode : undefined, }; }