More cleanup
This commit is contained in:
parent
0f279727c7
commit
bea124c3b8
2 changed files with 52 additions and 51 deletions
|
@ -1,54 +1,4 @@
|
||||||
class RiskDie extends DiceTerm {
|
import { RiskDie } from './risk-die';
|
||||||
constructor({ modifiers, ...other }: Partial<DiceTerm.TermData>) {
|
|
||||||
const numberModifier = modifiers?.filter((m) => m[0] === 'n')[0];
|
|
||||||
const numberRegex = new RegExp('n([0-9]+)?');
|
|
||||||
const numberMatch = numberModifier?.match(numberRegex);
|
|
||||||
const [numberString] = numberMatch?.slice(1) || [`${4}`];
|
|
||||||
const number = parseInt(numberString);
|
|
||||||
|
|
||||||
const facesModifier = modifiers?.filter((m) => m[0] === 'f')[0];
|
|
||||||
const facesRegex = new RegExp('f([0-9]+)?');
|
|
||||||
const facesMatch = facesModifier?.match(facesRegex);
|
|
||||||
const [facesString] = facesMatch?.slice(1) || [`${10}`];
|
|
||||||
const faces = parseInt(facesString);
|
|
||||||
|
|
||||||
if (number < 3) {
|
|
||||||
throw new Error(game.i18n.localize('RiskDice.ErrorNumberMustBeAtLeast3'));
|
|
||||||
}
|
|
||||||
|
|
||||||
super({ ...other, modifiers, number, faces });
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return the dice expression portion of the full term formula, excluding any flavor text.
|
|
||||||
*/
|
|
||||||
get expression() {
|
|
||||||
return `dr${this.modifiers.join('')}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
get total(): number | null {
|
|
||||||
if (!this._evaluated) return null;
|
|
||||||
if (this.values.includes(1) && this.values.includes(2)) {
|
|
||||||
return 3;
|
|
||||||
} else if (this.values.filter((res) => res === 1).length > 1) {
|
|
||||||
return 2;
|
|
||||||
} else {
|
|
||||||
return this.values
|
|
||||||
.sort((a, b) => b - a)
|
|
||||||
.slice(0, 2)
|
|
||||||
.reduce((acc, e) => acc + e, 0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
noop(): void {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
static MODIFIERS = {
|
|
||||||
n: 'noop', // Modifier is consumed in constructor for number
|
|
||||||
f: 'noop', // Modifier is consumed in constructor for faces
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function registerRiskDie(): void {
|
function registerRiskDie(): void {
|
||||||
CONFIG.Dice.types.push(RiskDie);
|
CONFIG.Dice.types.push(RiskDie);
|
||||||
|
|
51
src/module/risk-die.ts
Normal file
51
src/module/risk-die.ts
Normal file
|
@ -0,0 +1,51 @@
|
||||||
|
export class RiskDie extends DiceTerm {
|
||||||
|
constructor({ modifiers, ...other }: Partial<DiceTerm.TermData>) {
|
||||||
|
const numberModifier = modifiers?.filter((m) => m[0] === 'n')[0];
|
||||||
|
const numberRegex = new RegExp('n([0-9]+)?');
|
||||||
|
const numberMatch = numberModifier?.match(numberRegex);
|
||||||
|
const [numberString] = numberMatch?.slice(1) || [`${4}`];
|
||||||
|
const number = parseInt(numberString);
|
||||||
|
|
||||||
|
const facesModifier = modifiers?.filter((m) => m[0] === 'f')[0];
|
||||||
|
const facesRegex = new RegExp('f([0-9]+)?');
|
||||||
|
const facesMatch = facesModifier?.match(facesRegex);
|
||||||
|
const [facesString] = facesMatch?.slice(1) || [`${10}`];
|
||||||
|
const faces = parseInt(facesString);
|
||||||
|
|
||||||
|
if (number < 3) {
|
||||||
|
throw new Error(game.i18n.localize('RiskDice.ErrorNumberMustBeAtLeast3'));
|
||||||
|
}
|
||||||
|
|
||||||
|
super({ ...other, modifiers, number, faces });
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the dice expression portion of the full term formula, excluding any flavor text.
|
||||||
|
*/
|
||||||
|
get expression(): string {
|
||||||
|
return `d${RiskDie.DENOMINATION}${this.modifiers.join('')}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
get total(): number | null {
|
||||||
|
if (!this._evaluated) return null;
|
||||||
|
if (this.values.includes(1) && this.values.includes(2)) {
|
||||||
|
return 3;
|
||||||
|
} else if (this.values.filter((res) => res === 1).length > 1) {
|
||||||
|
return 2;
|
||||||
|
} else {
|
||||||
|
return this.values
|
||||||
|
.sort((a, b) => b - a)
|
||||||
|
.slice(0, 2)
|
||||||
|
.reduce((acc, e) => acc + e, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @override */
|
||||||
|
static MODIFIERS = {
|
||||||
|
n: (): void => undefined, // Modifier is consumed in constructor for number
|
||||||
|
f: (): void => undefined, // Modifier is consumed in constructor for faces
|
||||||
|
};
|
||||||
|
|
||||||
|
/** @override */
|
||||||
|
static DENOMINATION = 'r';
|
||||||
|
}
|
Reference in a new issue