ds4/src/module/rolls/check-factory.ts

227 lines
8.5 KiB
TypeScript
Raw Normal View History

2021-06-26 22:02:00 +02:00
// SPDX-FileCopyrightText: 2021 Johannes Loher
// SPDX-FileCopyrightText: 2021 Oliver Rümpelein
//
// SPDX-License-Identifier: MIT
2021-01-09 23:21:57 +01:00
/**
* Provides default values for all arguments the `CheckFactory` expects.
*/
class DefaultCheckOptions implements DS4CheckFactoryOptions {
readonly maximumCoupResult = 1;
readonly minimumFumbleResult = 20;
2021-01-25 01:25:45 +01:00
readonly useSlayingDice = false;
2021-02-20 17:30:17 +01:00
readonly rollMode: Const.DiceRollMode = "roll";
2021-03-18 08:52:02 +01:00
readonly flavor: undefined;
2021-01-09 23:21:57 +01:00
mergeWith(other: Partial<DS4CheckFactoryOptions>): DS4CheckFactoryOptions {
2021-02-05 02:52:55 +01:00
return { ...this, ...other };
}
}
2021-01-13 18:56:19 +01:00
/**
* Singleton reference for default value extraction.
*/
const defaultCheckOptions = new DefaultCheckOptions();
2021-01-09 23:21:57 +01:00
/**
* Most basic class responsible for generating the chat formula and passing it to the chat as roll.
*/
class CheckFactory {
constructor(
2021-03-18 08:52:02 +01:00
private checkTargetNumber: number,
private gmModifier: number,
2021-03-18 08:52:02 +01:00
options: Partial<DS4CheckFactoryOptions> = {},
) {
2021-03-18 08:52:02 +01:00
this.options = defaultCheckOptions.mergeWith(options);
}
2021-01-13 17:11:07 +01:00
2021-03-18 08:52:02 +01:00
private options: DS4CheckFactoryOptions;
2021-03-18 08:52:02 +01:00
async execute(): Promise<ChatMessage> {
2021-03-18 08:58:35 +01:00
const innerFormula = ["ds", this.createCheckTargetNumberModifier(), this.createCoupFumbleModifier()].filterJoin(
"",
);
2021-03-18 08:52:02 +01:00
const formula = this.options.useSlayingDice ? `{${innerFormula}}x` : innerFormula;
const roll = Roll.create(formula);
2021-03-18 08:52:02 +01:00
return roll.toMessage(
{ speaker: ChatMessage.getSpeaker(), flavor: this.options.flavor },
{ rollMode: this.options.rollMode, create: true },
);
}
createCheckTargetNumberModifier(): string {
const totalCheckTargetNumber = Math.max(this.checkTargetNumber + this.gmModifier, 0);
return `v${totalCheckTargetNumber}`;
}
2021-03-18 08:52:02 +01:00
createCoupFumbleModifier(): string | null {
const isMinimumFumbleResultRequired =
this.options.minimumFumbleResult !== defaultCheckOptions.minimumFumbleResult;
const isMaximumCoupResultRequired = this.options.maximumCoupResult !== defaultCheckOptions.maximumCoupResult;
2021-03-18 08:52:02 +01:00
if (isMinimumFumbleResultRequired || isMaximumCoupResultRequired) {
return `c${this.options.maximumCoupResult ?? ""}:${this.options.minimumFumbleResult ?? ""}`;
} else {
return null;
}
}
}
2021-01-09 23:21:57 +01:00
/**
* Asks the user for all unknown/necessary information and passes them on to perform a roll.
2021-03-18 08:52:02 +01:00
* @param checkTargetNumber - The Check Target Number ("CTN")
* @param options - Options changing the behavior of the roll and message.
2021-01-09 23:21:57 +01:00
*/
export async function createCheckRoll(
2021-03-18 08:52:02 +01:00
checkTargetNumber: number,
options: Partial<DS4CheckFactoryOptions> = {},
): Promise<ChatMessage | unknown> {
// Ask for additional required data;
2021-03-18 08:52:02 +01:00
const gmModifierData = await askGmModifier(checkTargetNumber, options);
2021-03-18 08:52:02 +01:00
const newTargetValue = gmModifierData.checkTargetNumber ?? checkTargetNumber;
const gmModifier = gmModifierData.gmModifier ?? 0;
const newOptions: Partial<DS4CheckFactoryOptions> = {
2021-03-18 08:52:02 +01:00
maximumCoupResult: gmModifierData.maximumCoupResult ?? options.maximumCoupResult,
minimumFumbleResult: gmModifierData.minimumFumbleResult ?? options.minimumFumbleResult,
2021-03-22 09:04:45 +01:00
useSlayingDice: game.settings.get("ds4", "useSlayingDiceForAutomatedChecks"),
2021-03-18 08:52:02 +01:00
rollMode: gmModifierData.rollMode ?? options.rollMode,
flavor: options.flavor,
};
// Create Factory
const cf = new CheckFactory(newTargetValue, gmModifier, newOptions);
// Possibly additional processing
// Execute roll
return cf.execute();
}
2021-01-09 23:21:57 +01:00
/**
2021-01-13 18:56:19 +01:00
* Responsible for rendering the modal interface asking for the modifier specified by GM and (currently) additional data.
2021-01-09 23:21:57 +01:00
*
* @notes
* At the moment, this asks for more data than it will do after some iterations.
*
* @returns The data given by the user.
2021-01-09 23:21:57 +01:00
*/
async function askGmModifier(
checkTargetNumber: number,
options: Partial<DS4CheckFactoryOptions> = {},
{ template, title }: { template?: string; title?: string } = {},
): Promise<Partial<IntermediateGmModifierData>> {
2021-04-13 21:40:52 +02:00
const usedTemplate = template ?? "systems/ds4/templates/dialogs/roll-options.hbs";
2021-05-13 21:39:42 +02:00
const usedTitle = title ?? game.i18n.localize("DS4.DialogRollOptionsDefaultTitle");
const templateData = {
2021-01-13 18:02:22 +01:00
title: usedTitle,
checkTargetNumber: checkTargetNumber,
maximumCoupResult: options.maximumCoupResult ?? defaultCheckOptions.maximumCoupResult,
minimumFumbleResult: options.minimumFumbleResult ?? defaultCheckOptions.minimumFumbleResult,
2021-03-18 08:52:02 +01:00
rollMode: options.rollMode ?? game.settings.get("core", "rollMode"),
rollModes: CONFIG.Dice.rollModes,
};
const renderedHtml = await renderTemplate(usedTemplate, templateData);
const dialogPromise = new Promise<HTMLFormElement>((resolve) => {
new Dialog({
title: usedTitle,
content: renderedHtml,
buttons: {
ok: {
icon: '<i class="fas fa-check"></i>',
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) {
2021-02-07 13:51:20 +01:00
throw new Error(
game.i18n.format("DS4.ErrorCouldNotFindHtmlElement", { htmlElement: "form" }),
2021-02-07 13:51:20 +01:00
);
}
resolve(innerForm);
}
},
},
cancel: {
icon: '<i class="fas fa-times"></i>',
label: game.i18n.localize("DS4.GenericCancelButton"),
},
},
default: "ok",
}).render(true);
});
const dialogForm = await dialogPromise;
return parseDialogFormData(dialogForm);
}
2021-01-13 18:56:19 +01:00
/**
* Extracts Dialog data from the returned DOM element.
2021-02-07 11:51:36 +01:00
* @param formData - The filed dialog
2021-01-13 18:56:19 +01:00
*/
function parseDialogFormData(formData: HTMLFormElement): Partial<IntermediateGmModifierData> {
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];
2021-01-13 18:02:22 +01:00
return {
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,
};
}
2021-01-13 18:56:19 +01:00
/**
* Contains data that needs retrieval from an interactive Dialog.
*/
interface GmModifierData {
2021-01-13 18:56:19 +01:00
gmModifier: number;
2021-02-20 17:30:17 +01:00
rollMode: Const.DiceRollMode;
2021-01-13 18:56:19 +01:00
}
/**
* Contains *CURRENTLY* necessary Data for drafting a roll.
*
* @deprecated
* Quite a lot of this information is requested due to a lack of automation:
* - maximumCoupResult
* - minimumFumbleResult
2021-01-13 18:56:19 +01:00
* - useSlayingDice
* - checkTargetNumber
2021-01-13 18:56:19 +01:00
*
* They will and should be removed once effects and data retrieval is in place.
* If a "raw" roll dialog is necessary, create another pre-processing Dialog
2021-01-13 18:56:19 +01:00
* class asking for the required information.
* This interface should then be replaced with the `GmModifierData`.
*/
interface IntermediateGmModifierData extends GmModifierData {
checkTargetNumber: number;
maximumCoupResult: number;
minimumFumbleResult: number;
}
2021-01-13 18:56:19 +01:00
/**
* The minimum behavioral options that need to be passed to the factory.
2021-01-13 18:56:19 +01:00
*/
2021-01-09 23:21:57 +01:00
export interface DS4CheckFactoryOptions {
maximumCoupResult: number;
minimumFumbleResult: number;
useSlayingDice: boolean;
2021-02-20 17:30:17 +01:00
rollMode: Const.DiceRollMode;
2021-03-18 08:52:02 +01:00
flavor?: string;
}