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-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;
|
|
|
|
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 !== CheckFactory.defaultCheckOptions.minCritFailure;
|
|
|
|
const maxCritRequired = this.checkOptions.maxCritSuccess !== CheckFactory.defaultCheckOptions.maxCritSuccess;
|
|
|
|
|
|
|
|
if (minCritRequired || maxCritRequired) {
|
|
|
|
return "c" + (this.checkOptions.maxCritSuccess ?? "") + "," + (this.checkOptions.minCritFailure ?? "");
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
createSlayingDiceTerm(): string {
|
|
|
|
return this.checkOptions.useSlayingDice ? "x" : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
static defaultCheckOptions = new DefaultCheckOptions();
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
const gmModifier = await askGmModifier();
|
|
|
|
|
|
|
|
// Create Factory
|
|
|
|
const cf = new CheckFactory(targetValue, gmModifier, options);
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
*
|
|
|
|
* @returns {Promise<number>} The number by the user.
|
|
|
|
*/
|
2021-01-09 23:14:31 +01:00
|
|
|
async function askGmModifier(): Promise<number> {
|
|
|
|
// Render model interface and return value
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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-09 23:21:57 +01:00
|
|
|
export type DS4RollMode = "roll" | "gmroll" | "blindroll" | "selfroll";
|