-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.js
116 lines (95 loc) · 2.4 KB
/
utils.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
const config = require('./config');
module.exports = {
handler100s,
handler100,
pick,
formatRoll,
parseRoll,
random_1_based,
levelOfSuccess
};
/**
* Default handler for 0-n arguments
* @param userCommand string e.g. 'fumble' (often ignored)
* @param args array of strings
* @param utilsIgnored
* @param command command object
* @returns {string}
*/
function handler100s(userCommand, args, utilsIgnored, command) {
if (!args.length)
args = ['x'];
return args.map((x) => handler100(userCommand, x, command)).join('\n');
}
/**
* Default Handler for a single roll
* @param userCommand e.g. "fumble", ignored
* @param rollString e.g. "57"
* @param command command object
* @returns {string}
*/
function handler100(userCommand, rollString, command) {
let roll = parseRoll(rollString);
let effect = pick(command.data, roll);
let nicelyFormattedRoll = formatRoll(roll);
return `${command.name} #${nicelyFormattedRoll}: ${effect}`;
}
/**
* Pick an effect, handling '"' ("ditto")
* @param effects array
* @param roll 1-based
* @returns {string}
*/
function pick(effects, roll) {
let effect = effects[roll];
while (effect === '"')
effect = effects[--roll];
return effect;
}
/**
* Formats a roll to two characters, e.g. 5 => "05", 100 => "00"
* @param roll
* @returns {string}
*/
function formatRoll(roll) {
return ('0' + roll).substr(-2);
}
/**
* Parse an incoming string representing a die roll
* - 0 gets converted to 100
* - non-numberic input, e.g. "x", will usually get converted to a random number, 1..max
* @param rollString
* @param max
* @returns {number}
*/
function parseRoll(rollString, max = 100) {
let roll = parseInt(rollString, 10);
if (!roll) {
if (roll === 0)
roll = 100;
else if (max > 0)
roll = random_1_based(max);
}
return roll;
}
/**
* Select a random number 1..max (inclusive)
* @param max {number} defaults to 100
* @returns {number}
*/
function random_1_based(max = 100) {
return Math.floor(Math.random() * max) + 1;
}
/**
* Translates aliases and shortcuts to "standard" levels of success
* e.g. "slash" -> "critical"
* @param input {string}
* @returns {string} or null if nothing matches
*/
function levelOfSuccess(input) {
for (let [level, aliases] of Object.entries(config.successAliases)) {
if (aliases.includes(input))
return level;
}
return null;
}