Implement CTN as modifier.
This commit is contained in:
parent
c89278992f
commit
3dca6a5ae4
1 changed files with 25 additions and 20 deletions
|
@ -12,16 +12,25 @@ export class DS4Roll extends DiceTerm {
|
|||
constructor(termData: Partial<TermData>) {
|
||||
super({
|
||||
number: 1,
|
||||
faces: termData.number ?? -1,
|
||||
faces: termData.faces,
|
||||
modifiers: termData.modifiers ?? [],
|
||||
options: termData.options ?? {},
|
||||
});
|
||||
console.log("This @ constructor: ", termData);
|
||||
|
||||
const targetValueModifier = this.modifiers.filter((m) => m[0] === "v")[0] ?? "v" + DS4Roll.DEFAULT_TARGET_VALUE;
|
||||
const rgx = new RegExp("v([0-9]+)?");
|
||||
const match = targetValueModifier.match(rgx);
|
||||
if (match) {
|
||||
const [parseTargetValue] = match.slice(1);
|
||||
this.targetValue = parseTargetValue ? parseInt(parseTargetValue) : DS4Roll.DEFAULT_TARGET_VALUE;
|
||||
}
|
||||
}
|
||||
|
||||
success = null;
|
||||
failure = null;
|
||||
number = 1;
|
||||
targetValue = DS4Roll.DEFAULT_TARGET_VALUE;
|
||||
|
||||
/**
|
||||
* @override
|
||||
|
@ -30,7 +39,7 @@ export class DS4Roll extends DiceTerm {
|
|||
* @private_notes This gets only called once for the first roll.
|
||||
*/
|
||||
roll({ minimize = false, maximize = false } = {}): RollResult {
|
||||
console.log(`This faces @ roll: ${this.faces}`);
|
||||
console.log(`This targetValue @ roll: ${this.targetValue}`);
|
||||
const rollResult = this.rollWithDifferentBorders(1, 20, { minimize, maximize });
|
||||
this.results.push(rollResult);
|
||||
if (rollResult.status == RollResultStatus.CRITICAL_SUCCESS) {
|
||||
|
@ -46,17 +55,14 @@ export class DS4Roll extends DiceTerm {
|
|||
minCritFail: number,
|
||||
{ minimize = false, maximize = false } = {},
|
||||
): RollResult {
|
||||
const targetValueToUse = this.targetValue;
|
||||
if (minimize) {
|
||||
return new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [20], true);
|
||||
} else if (maximize) {
|
||||
return new RollResult(
|
||||
this.faces,
|
||||
RollResultStatus.CRITICAL_SUCCESS,
|
||||
Array(Math.ceil(this.faces / 20)).fill(1),
|
||||
true,
|
||||
);
|
||||
const numberOfDice = Array(Math.ceil(targetValueToUse / 20)).fill(1);
|
||||
return new RollResult(targetValueToUse, RollResultStatus.CRITICAL_SUCCESS, numberOfDice, true);
|
||||
} else {
|
||||
return ds4roll(this.faces, { maxCritSucc: maxCritSuccess, minCritFail: minCritFail });
|
||||
return ds4roll(targetValueToUse, { maxCritSucc: maxCritSuccess, minCritFail: minCritFail });
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -72,15 +78,11 @@ export class DS4Roll extends DiceTerm {
|
|||
}, []);
|
||||
}
|
||||
|
||||
get expression(): string {
|
||||
return `${this.faces}ds${this.modifiers.join("")}`;
|
||||
}
|
||||
|
||||
get formula(): string {
|
||||
return this.expression;
|
||||
}
|
||||
|
||||
/** Term Modifiers */
|
||||
noop(): this {
|
||||
return this;
|
||||
}
|
||||
|
||||
crits(modifier: string): this {
|
||||
const rgx = new RegExp("c([0-9]+)?,([0-9]+)?");
|
||||
const match = modifier.match(rgx);
|
||||
|
@ -90,8 +92,9 @@ export class DS4Roll extends DiceTerm {
|
|||
const maxCritSuccess = parseCritSucc ? parseInt(parseCritSucc) : 1;
|
||||
const minCritFail = parsedCritFail ? parseInt(parsedCritFail) : 20;
|
||||
|
||||
// noinspection UnnecessaryLocalVariableJS
|
||||
const newResults: Array<RollResult> = (this.results as Array<RollResult>).map((r) => {
|
||||
return ds4roll(this.faces, { minCritFail: minCritFail, maxCritSucc: maxCritSuccess }, r.dice);
|
||||
return ds4roll(this.targetValue, { minCritFail: minCritFail, maxCritSucc: maxCritSuccess }, r.dice);
|
||||
});
|
||||
|
||||
this.results = newResults;
|
||||
|
@ -124,14 +127,16 @@ export class DS4Roll extends DiceTerm {
|
|||
this.results.push(newRoll);
|
||||
}
|
||||
|
||||
if (checked > 1000) throw new Error("Maximum recursion depth for explodign dice roll exceeded");
|
||||
if (checked > 1000) throw new Error("Maximum recursion depth for exploding dice roll exceeded");
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: To Types
|
||||
static DEFAULT_TARGET_VALUE = 10;
|
||||
// TODO: add to Type declarations
|
||||
static DENOMINATION = "s";
|
||||
static MODIFIERS = {
|
||||
x: "explode",
|
||||
c: "crits",
|
||||
v: "noop", // Modifier is consumed in constructor for target value
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue