// TODO: Rename to something sane. /** * Provides default values for all arguments the `CheckFactory` expects. */ class DefaultCheckOptions implements DS4CheckFactoryOptions { maxCritSuccess = 1; minCritFailure = 20; useSlayingDice = false; rollMode: DS4RollMode = "roll"; mergeWith(other: Partial): DS4CheckFactoryOptions { return { ...this, ...other } as DS4CheckFactoryOptions; } } 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 = new DefaultCheckOptions().mergeWith(passedOptions); } private checkOptions: DS4CheckFactoryOptions; async execute(): Promise { const rollCls: typeof Roll = CONFIG.Dice.rolls[0]; const formula = [ "ds", this.createTargetValueTerm(), this.createCritTerm(), this.createSlayingDiceTerm(), ].filterJoin(""); const roll = new rollCls(formula); const rollModeTemplate = this.checkOptions.rollMode; return roll.toMessage({}, { rollMode: rollModeTemplate, create: true }); } // Term generators createTargetValueTerm(): string { if (this.checkTargetValue != null) { return "v" + this.checkTargetValue; } else { return null; } } createCritTerm(): string { 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; } } createSlayingDiceTerm(): string { return this.checkOptions.useSlayingDice ? "x" : null; } } // TODO: Figure out return of roll (void should be Ok, tough?) /** * 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. */ export async function createCheckRoll(targetValue: number, options: Partial): Promise { // Ask for additional required data; const gmModifierData = await askGmModifier(targetValue, options); // Create Factory const cf = new CheckFactory(targetValue, gmModifierData.gmModifier, options); // Possibly additional processing // Execute roll await cf.execute(); } /** * Responsible for rendering the modal interface asking for the modifier specified by GM. * * @notes * At the moment, this asks for more data than it will do after some iterations. * * @returns {Promise} The number 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 templateData = { cssClass: "roll-option", title: title ?? "Roll Options", checkTargetValue: targetValue, maxCritSuccess: options.maxCritSuccess ?? defaultCheckOptions.maxCritSuccess, minCritFailure: options.minCritFailure ?? defaultCheckOptions.minCritFailure, rollModes: rollModes, }; 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, }, cancel: { label: "Cancel", callback: (html) => null, }, }, default: "ok", }; const dialog = new Dialog(dialogData, {}).render(true); return { gmModifier: 0 }; } interface GmModifierData { gmModifier: number; } export interface DS4CheckFactoryOptions { maxCritSuccess: number; minCritFailure: number; useSlayingDice: boolean; rollMode: DS4RollMode; } const rollModes = ["roll", "gmroll", "blindroll", "selfroll"] as const; type DS4RollModeTuple = typeof rollModes; export type DS4RollMode = DS4RollModeTuple[number];