38 lines
750 B
JavaScript
38 lines
750 B
JavaScript
'use strict';
|
|
|
|
const Chance = require("chance");
|
|
|
|
module.exports = class Stat {
|
|
/**
|
|
* @type {number{4}}
|
|
*/
|
|
dices;
|
|
|
|
/**
|
|
* @type {number}
|
|
*/
|
|
result
|
|
|
|
constructor(diceString) {
|
|
this.dices = Stat.chance(diceString);
|
|
// this.dices = Array.from({length: 4}, _ => Math.floor(Math.random() * 6 + 1));
|
|
|
|
this.dices.sort().reverse();
|
|
|
|
this.result = this.dices.reduce((a, s) => s + a, 0) - Math.min(...this.dices);
|
|
}
|
|
|
|
toString() {
|
|
return `${this.result} [${this.dices.join(',')}]`;
|
|
}
|
|
|
|
static #chance;
|
|
|
|
static chance(diceString) {
|
|
if (!this.#chance) {
|
|
this.#chance = new Chance();
|
|
}
|
|
|
|
return this.#chance.rpg(diceString);
|
|
}
|
|
} |