Implement CTN as modifier.

This commit is contained in:
Oliver Rümpelein 2021-01-08 21:07:15 +01:00
parent c89278992f
commit 3dca6a5ae4

View file

@ -12,16 +12,25 @@ export class DS4Roll extends DiceTerm {
constructor(termData: Partial<TermData>) { constructor(termData: Partial<TermData>) {
super({ super({
number: 1, number: 1,
faces: termData.number ?? -1, faces: termData.faces,
modifiers: termData.modifiers ?? [], modifiers: termData.modifiers ?? [],
options: termData.options ?? {}, options: termData.options ?? {},
}); });
console.log("This @ constructor: ", termData); 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; success = null;
failure = null; failure = null;
number = 1; number = 1;
targetValue = DS4Roll.DEFAULT_TARGET_VALUE;
/** /**
* @override * @override
@ -30,7 +39,7 @@ export class DS4Roll extends DiceTerm {
* @private_notes This gets only called once for the first roll. * @private_notes This gets only called once for the first roll.
*/ */
roll({ minimize = false, maximize = false } = {}): RollResult { 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 }); const rollResult = this.rollWithDifferentBorders(1, 20, { minimize, maximize });
this.results.push(rollResult); this.results.push(rollResult);
if (rollResult.status == RollResultStatus.CRITICAL_SUCCESS) { if (rollResult.status == RollResultStatus.CRITICAL_SUCCESS) {
@ -46,17 +55,14 @@ export class DS4Roll extends DiceTerm {
minCritFail: number, minCritFail: number,
{ minimize = false, maximize = false } = {}, { minimize = false, maximize = false } = {},
): RollResult { ): RollResult {
const targetValueToUse = this.targetValue;
if (minimize) { if (minimize) {
return new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [20], true); return new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [20], true);
} else if (maximize) { } else if (maximize) {
return new RollResult( const numberOfDice = Array(Math.ceil(targetValueToUse / 20)).fill(1);
this.faces, return new RollResult(targetValueToUse, RollResultStatus.CRITICAL_SUCCESS, numberOfDice, true);
RollResultStatus.CRITICAL_SUCCESS,
Array(Math.ceil(this.faces / 20)).fill(1),
true,
);
} else { } 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 */ /** Term Modifiers */
noop(): this {
return this;
}
crits(modifier: string): this { crits(modifier: string): this {
const rgx = new RegExp("c([0-9]+)?,([0-9]+)?"); const rgx = new RegExp("c([0-9]+)?,([0-9]+)?");
const match = modifier.match(rgx); const match = modifier.match(rgx);
@ -90,8 +92,9 @@ export class DS4Roll extends DiceTerm {
const maxCritSuccess = parseCritSucc ? parseInt(parseCritSucc) : 1; const maxCritSuccess = parseCritSucc ? parseInt(parseCritSucc) : 1;
const minCritFail = parsedCritFail ? parseInt(parsedCritFail) : 20; const minCritFail = parsedCritFail ? parseInt(parsedCritFail) : 20;
// noinspection UnnecessaryLocalVariableJS
const newResults: Array<RollResult> = (this.results as Array<RollResult>).map((r) => { 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; this.results = newResults;
@ -124,14 +127,16 @@ export class DS4Roll extends DiceTerm {
this.results.push(newRoll); 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 DENOMINATION = "s";
static MODIFIERS = { static MODIFIERS = {
x: "explode", x: "explode",
c: "crits", c: "crits",
v: "noop", // Modifier is consumed in constructor for target value
}; };
} }