import { DS4 } from "../config"; /** * Provides default values for all arguments the `CheckFactory` expects. */ class DefaultCheckOptions implements DS4CheckFactoryOptions { readonly maxCritSuccess = 1; readonly minCritFailure = 20; readonly useSlayingDice = false; readonly rollMode: Const.DiceRollMode = "roll"; mergeWith(other: Partial): DS4CheckFactoryOptions { return { ...this, ...other }; } } /** * Singleton reference for default value extraction. */ const defaultCheckOptions = new DefaultCheckOptions(); /** * Most basic class responsible for generating the chat formula and passing it to the chat as roll. */ class CheckFactory { constructor( private checkTargetValue: number, private gmModifier: number, passedOptions: Partial = {}, ) { this.checkOptions = defaultCheckOptions.mergeWith(passedOptions); } private checkOptions: DS4CheckFactoryOptions; async execute(): Promise { const rollCls = CONFIG.Dice.rolls[0]; const innerFormula = ["ds", this.createTargetValueTerm(), this.createCritTerm()].filterJoin(""); const formula = this.checkOptions.useSlayingDice ? `{${innerFormula}}x` : innerFormula; const roll = new rollCls(formula); const rollModeTemplate = this.checkOptions.rollMode; return roll.toMessage({}, { rollMode: rollModeTemplate, create: true }); } // Term generators createTargetValueTerm(): string | null { if (this.checkTargetValue !== null) { return "v" + (this.checkTargetValue + this.gmModifier); } else { return null; } } createCritTerm(): string | null { const minCritRequired = this.checkOptions.minCritFailure !== defaultCheckOptions.minCritFailure; const maxCritRequired = this.checkOptions.maxCritSuccess !== defaultCheckOptions.maxCritSuccess; if (minCritRequired || maxCritRequired) { return "c" + (this.checkOptions.maxCritSuccess ?? "") + ":" + (this.checkOptions.minCritFailure ?? ""); } else { return null; } } } /** * Asks the user for all unknown/necessary information and passes them on to perform a roll. * @param targetValue - The Check Target Number ("CTN") * @param options - Options changing the behavior of the roll and message. */ export async function createCheckRoll( targetValue: number, options: Partial = {}, ): Promise { // 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, useSlayingDice: gmModifierData.useSlayingDice ?? options.useSlayingDice ?? undefined, rollMode: gmModifierData.rollMode ?? options.rollMode ?? undefined, }; // Create Factory const cf = new CheckFactory(newTargetValue, gmModifier, newOptions); // Possibly additional processing // Execute roll return cf.execute(); } /** * Responsible for rendering the modal interface asking for the modifier specified by GM and (currently) additional data. * * @notes * At the moment, this asks for more data than it will do after some iterations. * * @returns The data given by the user. */ async function askGmModifier( targetValue: number, options: Partial = {}, { template, title }: { template?: string; title?: string } = {}, ): 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"); const templateData = { cssClass: "roll-option", title: usedTitle, checkTargetValue: targetValue, maxCritSuccess: options.maxCritSuccess ?? defaultCheckOptions.maxCritSuccess, minCritFailure: options.minCritFailure ?? defaultCheckOptions.minCritFailure, rollMode: options.rollMode, config: DS4, }; const renderedHtml = await renderTemplate(usedTemplate, templateData); const dialogPromise = new Promise((resolve) => { new Dialog( { title: usedTitle, content: renderedHtml, buttons: { ok: { icon: '', label: game.i18n.localize("DS4.GenericOkButton"), callback: (html) => { if (!("jquery" in html)) { throw new Error( game.i18n.format("DS4.ErrorUnexpectedHtmlType", { exType: "JQuery", realType: "HTMLElement", }), ); } else { const innerForm = html[0].querySelector("form"); if (!innerForm) { throw new Error( game.i18n.format("DS4.ErrorCouldNotFindHtmlElement", { htmlElement: "form" }), ); } resolve(innerForm); } }, }, cancel: { icon: '', label: game.i18n.localize("DS4.GenericCancelButton"), }, }, default: "ok", }, { jQuery: true }, ).render(true); }); const dialogForm = await dialogPromise; return parseDialogFormData(dialogForm); } /** * Extracts Dialog data from the returned DOM element. * @param formData - The filed dialog */ function parseDialogFormData(formData: HTMLFormElement): Partial { return { checkTargetValue: parseInt(formData["ctv"]?.value), gmModifier: parseInt(formData["gmmod"]?.value), maxCritSuccess: parseInt(formData["maxcoup"]?.value), minCritFailure: parseInt(formData["minfumble"]?.value), rollMode: formData["visibility"]?.value, }; } /** * Contains data that needs retrieval from an interactive Dialog. */ interface GmModifierData { gmModifier: number; rollMode: Const.DiceRollMode; } /** * Contains *CURRENTLY* necessary Data for drafting a roll. * * @deprecated * Quite a lot of this information is requested due to a lack of automation: * - maxCritSuccess * - minCritFailure * - useSlayingDice * - checkTargetValue * * They will and should be removed once effects and data retrieval is in place. * If a "raw" roll dialog is necessary, create another pre-porcessing Dialog * class asking for the required information. * This interface should then be replaced with the `GmModifierData`. */ interface IntermediateGmModifierData extends GmModifierData { checkTargetValue: number; gmModifier: number; maxCritSuccess: number; minCritFailure: number; // TODO: In final version from system settings useSlayingDice: boolean; rollMode: Const.DiceRollMode; } /** * The minimum behavioural options that need to be passed to the factory. */ export interface DS4CheckFactoryOptions { maxCritSuccess: number; minCritFailure: number; useSlayingDice: boolean; rollMode: Const.DiceRollMode; }