Improve CheckFactory
This commit is contained in:
parent
a2014c53d4
commit
d5b872e74b
5 changed files with 63 additions and 47 deletions
|
@ -6,6 +6,7 @@ class DefaultCheckOptions implements DS4CheckFactoryOptions {
|
|||
readonly minimumFumbleResult = 20;
|
||||
readonly useSlayingDice = false;
|
||||
readonly rollMode: Const.DiceRollMode = "roll";
|
||||
readonly flavor: undefined;
|
||||
|
||||
mergeWith(other: Partial<DS4CheckFactoryOptions>): DS4CheckFactoryOptions {
|
||||
return { ...this, ...other };
|
||||
|
@ -22,41 +23,37 @@ const defaultCheckOptions = new DefaultCheckOptions();
|
|||
*/
|
||||
class CheckFactory {
|
||||
constructor(
|
||||
private checkTargetValue: number,
|
||||
private checkTargetNumber: number,
|
||||
private gmModifier: number,
|
||||
passedOptions: Partial<DS4CheckFactoryOptions> = {},
|
||||
options: Partial<DS4CheckFactoryOptions> = {},
|
||||
) {
|
||||
this.checkOptions = defaultCheckOptions.mergeWith(passedOptions);
|
||||
this.options = defaultCheckOptions.mergeWith(options);
|
||||
}
|
||||
|
||||
private checkOptions: DS4CheckFactoryOptions;
|
||||
private options: DS4CheckFactoryOptions;
|
||||
|
||||
async execute(): Promise<ChatMessage | unknown> {
|
||||
const innerFormula = ["ds", this.createTargetValueTerm(), this.createCritTerm()].filterJoin("");
|
||||
const formula = this.checkOptions.useSlayingDice ? `{${innerFormula}}x` : innerFormula;
|
||||
async execute(): Promise<ChatMessage> {
|
||||
const innerFormula = ["ds", this.createTargetNumberModifier(), this.createCoupFumbleModifier()].filterJoin("");
|
||||
const formula = this.options.useSlayingDice ? `{${innerFormula}}x` : innerFormula;
|
||||
const roll = Roll.create(formula);
|
||||
|
||||
const rollModeTemplate = this.checkOptions.rollMode;
|
||||
return roll.toMessage({}, { rollMode: rollModeTemplate, create: true });
|
||||
return roll.toMessage(
|
||||
{ speaker: ChatMessage.getSpeaker(), flavor: this.options.flavor },
|
||||
{ rollMode: this.options.rollMode, create: true },
|
||||
);
|
||||
}
|
||||
|
||||
// Term generators
|
||||
createTargetValueTerm(): string | null {
|
||||
if (this.checkTargetValue !== null) {
|
||||
return "v" + (this.checkTargetValue + this.gmModifier);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
createTargetNumberModifier(): string | null {
|
||||
return "v" + (this.checkTargetNumber + this.gmModifier);
|
||||
}
|
||||
|
||||
createCritTerm(): string | null {
|
||||
const minCritRequired = this.checkOptions.minimumFumbleResult !== defaultCheckOptions.minimumFumbleResult;
|
||||
const maxCritRequired = this.checkOptions.maximumCoupResult !== defaultCheckOptions.maximumCoupResult;
|
||||
createCoupFumbleModifier(): string | null {
|
||||
const isMinimumFumbleResultRequired =
|
||||
this.options.minimumFumbleResult !== defaultCheckOptions.minimumFumbleResult;
|
||||
const isMaximumCoupResultRequired = this.options.maximumCoupResult !== defaultCheckOptions.maximumCoupResult;
|
||||
|
||||
if (minCritRequired || maxCritRequired) {
|
||||
return (
|
||||
"c" + (this.checkOptions.maximumCoupResult ?? "") + ":" + (this.checkOptions.minimumFumbleResult ?? "")
|
||||
);
|
||||
if (isMinimumFumbleResultRequired || isMaximumCoupResultRequired) {
|
||||
return "c" + (this.options.maximumCoupResult ?? "") + ":" + (this.options.minimumFumbleResult ?? "");
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
@ -65,23 +62,24 @@ class CheckFactory {
|
|||
|
||||
/**
|
||||
* 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.
|
||||
* @param checkTargetNumber - The Check Target Number ("CTN")
|
||||
* @param options - Options changing the behavior of the roll and message.
|
||||
*/
|
||||
export async function createCheckRoll(
|
||||
targetValue: number,
|
||||
checkTargetNumber: number,
|
||||
options: Partial<DS4CheckFactoryOptions> = {},
|
||||
): Promise<ChatMessage | unknown> {
|
||||
// Ask for additional required data;
|
||||
const gmModifierData = await askGmModifier(targetValue, options);
|
||||
const gmModifierData = await askGmModifier(checkTargetNumber, options);
|
||||
|
||||
const newTargetValue = gmModifierData.checkTargetNumber ?? targetValue;
|
||||
const newTargetValue = gmModifierData.checkTargetNumber ?? checkTargetNumber;
|
||||
const gmModifier = gmModifierData.gmModifier ?? 0;
|
||||
const newOptions: Partial<DS4CheckFactoryOptions> = {
|
||||
maximumCoupResult: gmModifierData.maximumCoupResult ?? options.maximumCoupResult ?? undefined,
|
||||
minimumFumbleResult: gmModifierData.minimumFumbleResult ?? options.minimumFumbleResult ?? undefined,
|
||||
useSlayingDice: gmModifierData.useSlayingDice ?? options.useSlayingDice ?? undefined,
|
||||
rollMode: gmModifierData.rollMode ?? options.rollMode ?? undefined,
|
||||
maximumCoupResult: gmModifierData.maximumCoupResult ?? options.maximumCoupResult,
|
||||
minimumFumbleResult: gmModifierData.minimumFumbleResult ?? options.minimumFumbleResult,
|
||||
useSlayingDice: game.settings.get("ds4", "useSlayingDiceForAutomatedChecks") ?? false,
|
||||
rollMode: gmModifierData.rollMode ?? options.rollMode,
|
||||
flavor: options.flavor,
|
||||
};
|
||||
|
||||
// Create Factory
|
||||
|
@ -106,7 +104,6 @@ async function askGmModifier(
|
|||
options: Partial<DS4CheckFactoryOptions> = {},
|
||||
{ template, title }: { template?: string; title?: string } = {},
|
||||
): Promise<Partial<IntermediateGmModifierData>> {
|
||||
// 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 = {
|
||||
|
@ -114,7 +111,7 @@ async function askGmModifier(
|
|||
checkTargetNumber: checkTargetNumber,
|
||||
maximumCoupResult: options.maximumCoupResult ?? defaultCheckOptions.maximumCoupResult,
|
||||
minimumFumbleResult: options.minimumFumbleResult ?? defaultCheckOptions.minimumFumbleResult,
|
||||
rollMode: options.rollMode,
|
||||
rollMode: options.rollMode ?? game.settings.get("core", "rollMode"),
|
||||
rollModes: CONFIG.Dice.rollModes,
|
||||
};
|
||||
const renderedHtml = await renderTemplate(usedTemplate, templateData);
|
||||
|
@ -200,12 +197,8 @@ interface GmModifierData {
|
|||
*/
|
||||
interface IntermediateGmModifierData extends GmModifierData {
|
||||
checkTargetNumber: number;
|
||||
gmModifier: number;
|
||||
maximumCoupResult: number;
|
||||
minimumFumbleResult: number;
|
||||
// TODO: In final version from system settings
|
||||
useSlayingDice: boolean;
|
||||
rollMode: Const.DiceRollMode;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -216,4 +209,5 @@ export interface DS4CheckFactoryOptions {
|
|||
minimumFumbleResult: number;
|
||||
useSlayingDice: boolean;
|
||||
rollMode: Const.DiceRollMode;
|
||||
flavor?: string;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue