2021-01-09 23:14:31 +01:00
|
|
|
// TODO: Rename to something sane.
|
|
|
|
|
2021-01-09 23:21:57 +01:00
|
|
|
/**
|
|
|
|
* Provides default values for all arguments the `CheckFactory` expects.
|
|
|
|
*/
|
|
|
|
class DefaultCheckOptions implements DS4CheckFactoryOptions {
|
2021-01-09 23:14:31 +01:00
|
|
|
maxCritSuccess = 1;
|
|
|
|
minCritFailure = 20;
|
|
|
|
useSlayingDice = false;
|
2021-01-09 23:21:57 +01:00
|
|
|
rollMode: DS4RollMode = "roll";
|
2021-01-09 23:14:31 +01:00
|
|
|
|
2021-01-09 23:21:57 +01:00
|
|
|
mergeWith(other: Partial<DS4CheckFactoryOptions>): DS4CheckFactoryOptions {
|
|
|
|
return { ...this, ...other } as DS4CheckFactoryOptions;
|
2021-01-09 23:14:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-10 16:40:11 +01:00
|
|
|
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.
|
|
|
|
*/
|
2021-01-09 23:14:31 +01:00
|
|
|
class CheckFactory {
|
|
|
|
constructor(
|
|
|
|
private checkTargetValue: number,
|
|
|
|
private gmModifier: number,
|
2021-01-09 23:21:57 +01:00
|
|
|
passedOptions: Partial<DS4CheckFactoryOptions> = {},
|
2021-01-09 23:14:31 +01:00
|
|
|
) {
|
|
|
|
this.checkOptions = new DefaultCheckOptions().mergeWith(passedOptions);
|
|
|
|
}
|
2021-01-09 23:21:57 +01:00
|
|
|
private checkOptions: DS4CheckFactoryOptions;
|
2021-01-09 23:14:31 +01:00
|
|
|
|
|
|
|
async execute(): Promise<void> {
|
|
|
|
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;
|
2021-01-10 21:03:11 +01:00
|
|
|
console.log(rollModeTemplate);
|
2021-01-09 23:14:31 +01:00
|
|
|
return roll.toMessage({}, { rollMode: rollModeTemplate, create: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
// Term generators
|
|
|
|
createTargetValueTerm(): string {
|
|
|
|
if (this.checkTargetValue != null) {
|
2021-01-10 21:03:11 +01:00
|
|
|
return "v" + (this.checkTargetValue + this.gmModifier);
|
2021-01-09 23:14:31 +01:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
createCritTerm(): string {
|
2021-01-10 16:40:11 +01:00
|
|
|
const minCritRequired = this.checkOptions.minCritFailure !== defaultCheckOptions.minCritFailure;
|
|
|
|
const maxCritRequired = this.checkOptions.maxCritSuccess !== defaultCheckOptions.maxCritSuccess;
|
2021-01-09 23:14:31 +01:00
|
|
|
|
|
|
|
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?)
|
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 targetValue {number} The Check Target Number ("CTN")
|
|
|
|
* @param options {Partial<DS4CheckFactoryOptions>} Options changing the behaviour of the roll and message.
|
|
|
|
*/
|
|
|
|
export async function createCheckRoll(targetValue: number, options: Partial<DS4CheckFactoryOptions>): Promise<void> {
|
2021-01-09 23:14:31 +01:00
|
|
|
// Ask for additional required data;
|
2021-01-10 16:40:11 +01:00
|
|
|
const gmModifierData = await askGmModifier(targetValue, options);
|
2021-01-09 23:14:31 +01:00
|
|
|
|
2021-01-10 21:03:11 +01:00
|
|
|
const newOptions: Partial<DS4CheckFactoryOptions> = {
|
|
|
|
maxCritSuccess: gmModifierData.maxCritSuccess ?? options.maxCritSuccess ?? undefined,
|
|
|
|
minCritFailure: gmModifierData.minCritFailure ?? options.minCritFailure ?? undefined,
|
|
|
|
useSlayingDice: gmModifierData.useSlayingDice ?? options.useSlayingDice ?? undefined,
|
|
|
|
rollMode: gmModifierData.rollMode ?? options.rollMode ?? undefined,
|
|
|
|
};
|
|
|
|
|
2021-01-09 23:14:31 +01:00
|
|
|
// Create Factory
|
2021-01-10 21:03:11 +01:00
|
|
|
const cf = new CheckFactory(gmModifierData.checkTargetValue, gmModifierData.gmModifier, newOptions);
|
2021-01-09 23:14:31 +01:00
|
|
|
|
|
|
|
// Possibly additional processing
|
|
|
|
|
|
|
|
// Execute roll
|
|
|
|
await cf.execute();
|
|
|
|
}
|
|
|
|
|
2021-01-09 23:21:57 +01:00
|
|
|
/**
|
|
|
|
* Responsible for rendering the modal interface asking for the modifier specified by GM.
|
|
|
|
*
|
2021-01-10 16:40:11 +01:00
|
|
|
* @notes
|
|
|
|
* At the moment, this asks for more data than it will do after some iterations.
|
|
|
|
*
|
2021-01-09 23:21:57 +01:00
|
|
|
* @returns {Promise<number>} The number by the user.
|
|
|
|
*/
|
2021-01-10 16:40:11 +01:00
|
|
|
async function askGmModifier(
|
|
|
|
targetValue: number,
|
|
|
|
options: Partial<DS4CheckFactoryOptions>,
|
|
|
|
{ template, title }: { template?: string; title?: string } = {},
|
|
|
|
): Promise<GmModifierData> {
|
2021-01-09 23:14:31 +01:00
|
|
|
// Render model interface and return value
|
2021-01-10 16:40:11 +01:00
|
|
|
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
|
2021-01-10 21:03:11 +01:00
|
|
|
const dialogPromise = new Promise<HTMLFormElement>((resolve) => {
|
|
|
|
new Dialog(
|
|
|
|
{
|
|
|
|
title: title ?? "Roll Options",
|
|
|
|
close: (html: JQuery) => resolve(null),
|
|
|
|
content: renderedHtml,
|
|
|
|
buttons: {
|
|
|
|
ok: {
|
|
|
|
label: "OK",
|
|
|
|
callback: (html: HTMLElement | JQuery) => {
|
|
|
|
if (!("jquery" in html)) {
|
|
|
|
throw new Error("Internal Type Error");
|
|
|
|
} else {
|
|
|
|
const innerForm = html[0].querySelector("form");
|
|
|
|
resolve(innerForm);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
cancel: {
|
|
|
|
label: "Cancel",
|
|
|
|
callback: (html: JQuery) => resolve(null),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
default: "ok",
|
2021-01-10 16:40:11 +01:00
|
|
|
},
|
2021-01-10 21:03:11 +01:00
|
|
|
{},
|
|
|
|
).render(true);
|
|
|
|
});
|
|
|
|
const dialogForm = await dialogPromise;
|
|
|
|
return parseDialogFormData(dialogForm, targetValue);
|
|
|
|
}
|
2021-01-10 16:40:11 +01:00
|
|
|
|
2021-01-10 21:03:11 +01:00
|
|
|
function parseDialogFormData(formData: HTMLFormElement, targetValue: number): GmModifierData {
|
|
|
|
const parsedData = {
|
|
|
|
checkTargetValue: parseInt(formData["ctv"]?.value) ?? targetValue,
|
|
|
|
gmModifier: parseInt(formData["gmmod"]?.value) ?? 0,
|
|
|
|
maxCritSuccess: parseInt(formData["maxcoup"]?.value) ?? defaultCheckOptions.maxCritSuccess,
|
|
|
|
minCritFailure: parseInt(formData["minfumble"]?.value) ?? defaultCheckOptions.minCritFailure,
|
|
|
|
useSlayingDice: false,
|
|
|
|
rollMode: formData["visibility"]?.value ?? defaultCheckOptions.rollMode,
|
|
|
|
};
|
|
|
|
console.log("Data", parsedData);
|
|
|
|
return parsedData;
|
2021-01-10 16:40:11 +01:00
|
|
|
}
|
|
|
|
|
2021-01-10 21:03:11 +01:00
|
|
|
// TODO: Remove unnecessary data step by step
|
2021-01-10 16:40:11 +01:00
|
|
|
interface GmModifierData {
|
2021-01-10 21:03:11 +01:00
|
|
|
checkTargetValue: number;
|
2021-01-10 16:40:11 +01:00
|
|
|
gmModifier: number;
|
2021-01-10 21:03:11 +01:00
|
|
|
maxCritSuccess: number;
|
|
|
|
minCritFailure: number;
|
|
|
|
// TODO: In final version from system settings
|
|
|
|
useSlayingDice: boolean;
|
|
|
|
rollMode: DS4RollMode;
|
2021-01-09 23:14:31 +01:00
|
|
|
}
|
|
|
|
|
2021-01-09 23:21:57 +01:00
|
|
|
export interface DS4CheckFactoryOptions {
|
2021-01-09 23:14:31 +01:00
|
|
|
maxCritSuccess: number;
|
|
|
|
minCritFailure: number;
|
|
|
|
useSlayingDice: boolean;
|
2021-01-09 23:21:57 +01:00
|
|
|
rollMode: DS4RollMode;
|
2021-01-09 23:14:31 +01:00
|
|
|
}
|
|
|
|
|
2021-01-10 16:40:11 +01:00
|
|
|
const rollModes = ["roll", "gmroll", "blindroll", "selfroll"] as const;
|
|
|
|
type DS4RollModeTuple = typeof rollModes;
|
|
|
|
export type DS4RollMode = DS4RollModeTuple[number];
|