-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.js
More file actions
39 lines (36 loc) · 1.09 KB
/
utils.js
File metadata and controls
39 lines (36 loc) · 1.09 KB
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
// Randomly pick an item from a list, attempting to avoid items that have previously been picked
const RETRIES = 5;
let picks = [];
function pickRandom(arr) {
let i = 0;
while (i < RETRIES) {
let pick = arr[Math.floor(Math.random() * arr.length)];
if (! picks.includes(pick)) {
picks.push(pick);
return pick;
}
i++;
}
let pick = arr[Math.floor(Math.random() * arr.length)];
//console.log("<<retry limit reached: reusing item>>");
return pick;
}
function resetPicks() {
picks = [];
}
// String template magic
function t(strings, ...tokens) {
return function (context) {
let result = [strings[0]];
tokens.forEach(function(token, i) {
let value = token instanceof Array ? token : context[token];
result.push(value instanceof Function ? value() : value instanceof Array ? pickRandom(value) : value, strings[i + 1]);
});
//console.log(result.join(""));
return result.join("");
}
}
// Die roller
function rollD(n) {
return Math.floor(Math.random()*n)+1;
}