This commit is contained in:
Johannes Loher 2021-02-07 17:38:05 +01:00
parent 54323bd300
commit 0f279727c7
5 changed files with 48 additions and 13 deletions

View file

@ -4,7 +4,8 @@ An implementation of risk dice as found for example in the Splittermond game
system. system.
This module provides a new dice type which represents a risk die. Rolling a risk 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 - 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. is `1 + 1` or `1 + 2` respectively.
- Otherwise, the total result is the sum of the 2 highest results. - Otherwise, the total result is the sum of the 2 highest results.

View file

@ -1 +1,3 @@
{} {
"RiskDice.ErrorNumberMustBeAtLeast3": "Die Anzahl an Würfeln muss mindestens 3 sein."
}

View file

@ -1 +1,3 @@
{} {
"RiskDice.ErrorNumberMustBeAtLeast3": "The number of dice must be at least 3."
}

View file

@ -15,7 +15,7 @@
}, },
{ {
"lang": "de", "lang": "de",
"name": "German", "name": "Deutsch",
"path": "lang/de.json" "path": "lang/de.json"
} }
], ],

View file

@ -1,26 +1,56 @@
class RiskDie extends DiceTerm { class RiskDie extends DiceTerm {
constructor() { constructor({ modifiers, ...other }: Partial<DiceTerm.TermData>) {
super({ number: 4, faces: 10 }); 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'));
} }
get total() { 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._evaluated) return null;
let total;
if (this.values.includes(1) && this.values.includes(2)) { if (this.values.includes(1) && this.values.includes(2)) {
total = 3; return 3;
} else if (this.values.filter((res) => res === 1).length > 1) { } else if (this.values.filter((res) => res === 1).length > 1) {
total = 2; return 2;
} else { } else {
total = this.values return this.values
.sort((a, b) => b - a) .sort((a, b) => b - a)
.slice(0, 2) .slice(0, 2)
.reduce((acc, e) => acc + e, 0); .reduce((acc, e) => acc + e, 0);
} }
return total;
}
} }
function registerRiskDie() { 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 {
CONFIG.Dice.types.push(RiskDie); CONFIG.Dice.types.push(RiskDie);
CONFIG.Dice.terms.r = RiskDie; CONFIG.Dice.terms.r = RiskDie;
} }