-
Notifications
You must be signed in to change notification settings - Fork 0
/
dice-pool.js
45 lines (35 loc) · 1.14 KB
/
dice-pool.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
const _ = require('lodash');
class DicePool {
constructor(diceCount, rerollThreshold, rote = false) {
this.diceCount = diceCount;
this.rerollThreshold = rerollThreshold;
this.rote = rote;
}
getDescription() {
let noun = (this.diceCount === 1) ? 'die' : 'dice';
let threshold = this.rerollThreshold || 'no 10';
let roteLabel = (this.rote) ? ', rote' : '';
return `${this.diceCount} ${noun}, ${threshold}-again${roteLabel}`;
}
roll() {
return rollDice(this.diceCount, this.rerollThreshold, this.rote);
}
}
function rollDice(diceCount, rerollThreshold, rote) {
let successCount = 0;
// Roll each die once.
_.times(diceCount, () => {
// Get a random result between 1 and 10.
let result = Math.floor(Math.random() * 10) + 1;
let isSuccess = result >= 8;
// If the die is a success, increment the succces count.
if (isSuccess) successCount += 1;
// Re-roll if the die exceeds the reroll threshold, or if it failed
// and the pool has the rote quality.
if (result >= rerollThreshold || (!isSuccess && rote)) {
successCount += rollDice(1, rerollThreshold, false);
}
})
return successCount;
}
module.exports = DicePool;