-
Notifications
You must be signed in to change notification settings - Fork 0
/
randomizer.js
40 lines (34 loc) · 998 Bytes
/
randomizer.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
module.exports.fill = fill;
var Mustache = require("mustache");
var tableCollection = require("./tables").tableCollection;
var dice = require("./dice");
var templateMap = createMustacheTemplates(tableCollection)
function fill(pattern) {
while(pattern.indexOf('{{') >= 0) {
pattern = Mustache.render(pattern, templateMap);
}
return pattern;
}
function createMustacheTemplates(tableCollection) {
var templateMap = [];
for(i = 0; i < tableCollection.length; i++) {
var table = tableCollection[i];
addMustacheTemplate(templateMap, table);
}
return templateMap;
}
function addMustacheTemplate(templateMap, table) {
templateMap[table.name] = function() {
return pickFrom(table);
}
}
function pickFrom(randomContent) {
var choice;
if (randomContent.rules) {
choice = randomContent.rules();
} else {
var numerOfOptions = Object.keys(randomContent.table).length;
choice = dice.rollDie(numerOfOptions)
}
return randomContent.table[choice];
}