-
Notifications
You must be signed in to change notification settings - Fork 1
/
diceRoller.js
96 lines (80 loc) · 3 KB
/
diceRoller.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
class DiceRoller {
constructor(dice, symbolHash) {
this.dice = dice;
this.symbolList = [];
if (!symbolHash) {
var newConfig = JSON.parse(JSON.stringify(require('./dice.js')));
symbolHash = newConfig.symbols;
}
Object.keys(symbolHash).forEach(key => {
this.symbolList.push(symbolHash[key]);
});
this.rollResult = [];
}
roll(poolString, symbolString) {
var diceToRoll = poolString.split('');
var dice = this.dice;
var results = [];
diceToRoll.forEach(function(die) {
die = die.toLocaleLowerCase();
if (dice[die]) {
var roll = Math.floor(Math.random() * dice[die].length);
results = results.concat(dice[die][roll]);
}
});
this.additionalSymbols = symbolString;
this.rollResult = results;
}
static get symbolTypes() {
return ['Success', 'Failure', 'Threat', 'Advantage', 'Despair', 'Triumph', 'Light', 'Dark', 'Blank'];
}
static cancelSymbols(symbols) {
symbols.sort();
var cancelledSymbols = [];
var symbolCounts = this.countSymbols(symbols);
this.removeSymbols(symbols, 'Success', Math.min(symbolCounts['Failure'], symbolCounts['Success']));
this.removeSymbols(symbols, 'Failure', Math.min(symbolCounts['Failure'], symbolCounts['Success']));
this.removeSymbols(symbols, 'Advantage', Math.min(symbolCounts['Advantage'], symbolCounts['Threat']));
this.removeSymbols(symbols, 'Threat', Math.min(symbolCounts['Advantage'], symbolCounts['Threat']));
this.removeSymbols(symbols, 'Blank', symbolCounts['Blank']);
return symbols;
}
static removeSymbols(symbols, symbolToRemove, number) {
var index = symbols.indexOf(symbolToRemove);
if (index > -1) {
symbols.splice(index, number);
}
return symbols;
}
static countSymbols(symbols) {
var symbolTypes = this.symbolTypes;
var counts = {};
symbolTypes.forEach(function(type){
counts[type] = symbols.filter(symbol => symbol == type).length;
});
return counts;
}
get symbolResults() {
var symbolResults = [];
this.rollResult.forEach(function(result) {
symbolResults = symbolResults.concat(result.results)
});
if (this.additionalSymbols){
var additionalSymbols = this.additionalSymbols.split('');
additionalSymbols.forEach(addSymbol => {
var newSymbol = this.symbolList.find(symbol => {
return symbol.character == addSymbol;
});
symbolResults.push(newSymbol.name);
});
}
return symbolResults;
}
get diceResults() {
return this.rollResult;
}
get cancelledSymbols() {
return DiceRoller.cancelSymbols(this.symbolResults);
}
}
module.exports = DiceRoller;