ds4/src/dice/check-factory.js

308 lines
13 KiB
JavaScript
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
2022-05-13 19:12:28 +02:00
import { DialogWithListeners } from "../apps/dialog-with-listeners";
import { DS4 } from "../config";
2022-11-04 21:47:18 +01:00
import { getGame } from "../utils/utils";
2021-07-07 19:22:35 +02:00
/** @typedef {"publicroll" | "gmroll" | "gmroll" | "selfroll"} RollModes */
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 {
/**
* @param {number} checkTargetNumber The check target number for this check factory
* @param {number} checkModifier The check modifier for this check factory
* @param {Partial<DS4CheckFactoryOptions>} [options] Options for this check factory
*/
constructor(checkTargetNumber, checkModifier, options = {}) {
this.#checkTargetNumber = checkTargetNumber;
this.#checkModifier = checkModifier;
this.#options = foundry.utils.mergeObject(this.constructor.defaultOptions, options);
}
2021-01-13 17:11:07 +01:00
/**
* The check target number for this check factory.
* @type {number}
*/
#checkTargetNumber;
/**
* The check modifier for this check factory.
* @type {number}
*/
#checkModifier;
/**
* The options for this check factory.
* @type {DS4CheckFactoryOptions}
*/
#options;
/**
* The default options of thos CheckFactory class. Upon instantiation, they are merged with the explicitly provided options.
* @type {DS4CheckFactoryOptions}
*/
static get defaultOptions() {
return {
maximumCoupResult: 1,
minimumFumbleResult: 20,
useSlayingDice: false,
rollMode: "publicroll",
};
}
/**
* Execute this check factory.
* @returns {Promise<ChatMessage | undefined>} A promise that resolves to the created chat message for the roll */
async execute() {
2021-03-18 08:58:35 +01:00
const innerFormula = ["ds", this.createCheckTargetNumberModifier(), this.createCoupFumbleModifier()].filterJoin(
"",
);
const formula = this.#options.useSlayingDice ? `{${innerFormula}}x` : innerFormula;
const roll = Roll.create(formula);
const speaker = this.#options.speaker ?? ChatMessage.getSpeaker();
2021-06-30 04:32:10 +02:00
2021-07-01 02:56:09 +02:00
return roll.toMessage(
{
speaker,
flavor: this.#options.flavor,
flags: this.#options.flavorData ? { ds4: { flavorData: this.#options.flavorData } } : undefined,
},
{ rollMode: this.#options.rollMode, create: true },
2021-03-18 08:52:02 +01:00
);
}
/**
* Create the check target number modifier for this roll.
* @returns string
*/
createCheckTargetNumberModifier() {
const totalCheckTargetNumber = this.#checkTargetNumber + this.#checkModifier;
return totalCheckTargetNumber >= 0 ? `v${this.#checkTargetNumber + this.#checkModifier}` : "v0";
}
/**
* Create the coup fumble modifier for this roll.
* @returns {string | null}
*/
createCoupFumbleModifier() {
2021-03-18 08:52:02 +01:00
const isMinimumFumbleResultRequired =
this.#options.minimumFumbleResult !== this.constructor.defaultOptions.minimumFumbleResult;
const isMaximumCoupResultRequired =
this.#options.maximumCoupResult !== this.constructor.defaultOptions.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.
* @param {number} checkTargetNumber The Check Target Number ("CTN")
* @param {Partial<DS4CheckFactoryOptions>} [options={}] Options changing the behavior of the roll and message.
* @returns {Promise<ChateMessage|undefined>} A promise that resolves to the chat message created by the roll
2021-01-09 23:21:57 +01:00
*/
export async function createCheckRoll(checkTargetNumber, options = {}) {
// Ask for additional required data;
2022-05-13 19:12:28 +02:00
const interactiveRollData = await askForInteractiveRollData(checkTargetNumber, options);
2022-05-13 19:12:28 +02:00
const newTargetValue = interactiveRollData.checkTargetNumber ?? checkTargetNumber;
const checkModifier = interactiveRollData.checkModifier ?? 0;
/** @type {Partial<DS4CheckFactoryOptions>} */
const newOptions = {
2022-05-13 19:12:28 +02:00
maximumCoupResult: interactiveRollData.maximumCoupResult ?? options.maximumCoupResult,
minimumFumbleResult: interactiveRollData.minimumFumbleResult ?? options.minimumFumbleResult,
2021-07-07 19:22:35 +02:00
useSlayingDice: getGame().settings.get("ds4", "useSlayingDiceForAutomatedChecks"),
2022-05-13 19:12:28 +02:00
rollMode: interactiveRollData.rollMode ?? options.rollMode,
2021-03-18 08:52:02 +01:00
flavor: options.flavor,
flavorData: options.flavorData,
speaker: options.speaker,
};
// Create Factory
2022-05-13 19:12:28 +02:00
const cf = new CheckFactory(newTargetValue, checkModifier, newOptions);
// Possibly additional processing
// Execute roll
return cf.execute();
}
2021-01-09 23:21:57 +01:00
/**
2022-05-13 19:12:28 +02:00
* Responsible for rendering the modal interface asking for the check modifier 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.
*
* @param {number} checkTargetNumber The check target number
* @param {Partial<DS4CheckFactoryOptions>} [options={}] Predefined roll options
* @param {template?: string | undefined; title?: string | undefined} [additionalOptions={}] Additional options to use for the dialog
* @returns {Promise<Partial<IntermediateInteractiveRollData>>} A promise that resolves to the data given by the user.
2021-01-09 23:21:57 +01:00
*/
async function askForInteractiveRollData(checkTargetNumber, options = {}, { template, title } = {}) {
2021-04-13 21:40:52 +02:00
const usedTemplate = template ?? "systems/ds4/templates/dialogs/roll-options.hbs";
2021-07-07 19:22:35 +02:00
const usedTitle = title ?? getGame().i18n.localize("DS4.DialogRollOptionsDefaultTitle");
const id = foundry.utils.randomID();
const templateData = {
2021-01-13 18:02:22 +01:00
title: usedTitle,
checkTargetNumber: checkTargetNumber,
maximumCoupResult: options.maximumCoupResult ?? this.constructor.defaultOptions.maximumCoupResult,
minimumFumbleResult: options.minimumFumbleResult ?? this.constructor.defaultOptions.minimumFumbleResult,
2021-07-07 19:22:35 +02:00
rollMode: options.rollMode ?? getGame().settings.get("core", "rollMode"),
rollModes: CONFIG.Dice.rollModes,
2022-05-13 19:12:28 +02:00
checkModifiers: Object.entries(DS4.i18n.checkModifiers).map(([key, translation]) => {
if (key in DS4.checkModifiers) {
const value = DS4.checkModifiers[key];
2022-05-13 19:12:28 +02:00
const label = `${translation} (${value >= 0 ? `+${value}` : value})`;
return { value, label };
}
return { value: key, label: translation };
}),
id,
};
const renderedHtml = await renderTemplate(usedTemplate, templateData);
const dialogPromise = new Promise((resolve) => {
2022-05-13 19:12:28 +02:00
new DialogWithListeners(
{
title: usedTitle,
content: renderedHtml,
buttons: {
ok: {
icon: '<i class="fas fa-check"></i>',
label: getGame().i18n.localize("DS4.GenericOkButton"),
callback: (html) => {
if (!("jquery" in html)) {
2021-02-07 13:51:20 +01:00
throw new Error(
2022-05-13 19:12:28 +02:00
getGame().i18n.format("DS4.ErrorUnexpectedHtmlType", {
exType: "JQuery",
realType: "HTMLElement",
}),
2021-02-07 13:51:20 +01:00
);
2022-05-13 19:12:28 +02:00
} else {
const innerForm = html[0]?.querySelector("form");
if (!innerForm) {
throw new Error(
getGame().i18n.format("DS4.ErrorCouldNotFindHtmlElement", {
htmlElement: "form",
}),
);
}
resolve(innerForm);
2021-02-07 13:51:20 +01:00
}
2022-05-13 19:12:28 +02:00
},
},
cancel: {
icon: '<i class="fas fa-times"></i>',
label: getGame().i18n.localize("DS4.GenericCancelButton"),
},
},
2022-05-13 19:12:28 +02:00
default: "ok",
},
{
activateAdditionalListeners: (html, app) => {
const checkModifierCustomFormGroup = html
.find(`#check-modifier-custom-${id}`)
.parent(".form-group");
html.find(`#check-modifier-${id}`).on("change", (event) => {
2022-05-13 19:12:28 +02:00
if (
event.currentTarget.value === "custom" &&
2022-05-13 19:12:28 +02:00
checkModifierCustomFormGroup.hasClass("ds4-hidden")
) {
checkModifierCustomFormGroup.removeClass("ds4-hidden");
app.setPosition({ height: "auto" });
} else if (!checkModifierCustomFormGroup.hasClass("ds4-hidden")) {
checkModifierCustomFormGroup.addClass("ds4-hidden");
app.setPosition({ height: "auto" });
}
});
},
id,
},
2022-05-13 19:12:28 +02:00
).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.
* @param {HTMLFormElement} formData The filed dialog
* @returns {Partial<IntermediateInteractiveRollData>}
2021-01-13 18:56:19 +01:00
*/
function parseDialogFormData(formData) {
const chosenCheckTargetNumber = parseInt(formData["check-target-number"]?.value);
2022-05-13 19:12:28 +02:00
const chosenCheckModifier = formData["check-modifier"]?.value;
const chosenCheckModifierValue =
chosenCheckModifier === "custom"
? parseInt(formData["check-modifier-custom"]?.value)
: parseInt(chosenCheckModifier);
const chosenMaximumCoupResult = parseInt(formData["maximum-coup-result"]?.value);
const chosenMinimumFumbleResult = parseInt(formData["minimum-fumble-result"]?.value);
const chosenRollMode = formData["roll-mode"]?.value;
2021-01-13 18:02:22 +01:00
return {
checkTargetNumber: Number.isSafeInteger(chosenCheckTargetNumber) ? chosenCheckTargetNumber : undefined,
2022-05-13 19:12:28 +02:00
checkModifier: Number.isSafeInteger(chosenCheckModifierValue) ? chosenCheckModifierValue : undefined,
maximumCoupResult: Number.isSafeInteger(chosenMaximumCoupResult) ? chosenMaximumCoupResult : undefined,
minimumFumbleResult: Number.isSafeInteger(chosenMinimumFumbleResult) ? chosenMinimumFumbleResult : undefined,
rollMode: Object.keys(CONFIG.Dice.rollModes).includes(chosenRollMode) ? chosenRollMode : undefined,
};
}
2021-01-13 18:56:19 +01:00
/**
* Contains data that needs retrieval from an interactive Dialog.
* @typedef {object} InteractiveRollData
* @property {number} checkModifier
* @property {RollModes} rollMode
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
* - 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 {@link InteractiveRollData}.
* @typedef {object} IntermediateInteractiveRollData
* @property {number} checkTargetNumber
* @property {number} maximumCoupResult
* @property {number} minimumFumbleResult
2021-01-13 18:56:19 +01:00
*/
2021-01-13 18:56:19 +01:00
/**
* The minimum behavioral options that need to be passed to the factory.
* @typedef {object} DS4CheckFactoryOptions
* @property {number} maximumCoupResult
* @property {number} minimumFumbleResult
* @property {boolean} useSlayingDice
* @property {RollModes} rollMode
* @property {string} [flavor]
* @property {Record<string, string | number | null>} [flavorData]
* @property {ChatSpeakerData} [speaker]
*/
/**
* @typedef {object} ChatSpeakerData
* @property {string | null} [scene]
* @property {string | null} [actor]
* @property {string | null} [token]
* @property {string | null} [alias]
2021-01-13 18:56:19 +01:00
*/