From 0f279727c76c9d88f9a1d04c5ad9d15161c5084b Mon Sep 17 00:00:00 2001 From: Johannes Loher Date: Sun, 7 Feb 2021 17:38:05 +0100 Subject: [PATCH] Simplify --- README.md | 3 ++- src/lang/de.json | 4 +++- src/lang/en.json | 4 +++- src/module.json | 2 +- src/module/risk-dice.ts | 48 +++++++++++++++++++++++++++++++++-------- 5 files changed, 48 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 761674b..0256d0d 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,8 @@ An implementation of risk dice as found for example in the Splittermond game system. This module provides a new dice type which represents a risk die. Rolling a risk -die ist just rolling 4 10 sided dice. The total result is calculated by: +die ist just rolling a number of dice combined with s special way of calculating +the total: - If there are either two `1`s or a `1` and a `2` in the results, the total sum is `1 + 1` or `1 + 2` respectively. - Otherwise, the total result is the sum of the 2 highest results. diff --git a/src/lang/de.json b/src/lang/de.json index 0967ef4..069831f 100644 --- a/src/lang/de.json +++ b/src/lang/de.json @@ -1 +1,3 @@ -{} +{ + "RiskDice.ErrorNumberMustBeAtLeast3": "Die Anzahl an Würfeln muss mindestens 3 sein." +} diff --git a/src/lang/en.json b/src/lang/en.json index 0967ef4..fbf72b3 100644 --- a/src/lang/en.json +++ b/src/lang/en.json @@ -1 +1,3 @@ -{} +{ + "RiskDice.ErrorNumberMustBeAtLeast3": "The number of dice must be at least 3." +} diff --git a/src/module.json b/src/module.json index 73133ea..5052f5f 100644 --- a/src/module.json +++ b/src/module.json @@ -15,7 +15,7 @@ }, { "lang": "de", - "name": "German", + "name": "Deutsch", "path": "lang/de.json" } ], diff --git a/src/module/risk-dice.ts b/src/module/risk-dice.ts index 88985fe..d0a82db 100644 --- a/src/module/risk-dice.ts +++ b/src/module/risk-dice.ts @@ -1,26 +1,56 @@ class RiskDie extends DiceTerm { - constructor() { - super({ number: 4, faces: 10 }); + constructor({ modifiers, ...other }: Partial) { + 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 }); } - get total() { + /** + * 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; - let total; if (this.values.includes(1) && this.values.includes(2)) { - total = 3; + return 3; } else if (this.values.filter((res) => res === 1).length > 1) { - total = 2; + return 2; } else { - total = this.values + return this.values .sort((a, b) => b - a) .slice(0, 2) .reduce((acc, e) => acc + e, 0); } - return total; } + + 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() { +function registerRiskDie(): void { CONFIG.Dice.types.push(RiskDie); CONFIG.Dice.terms.r = RiskDie; }