forked from derekjones562/5e-spelljammer.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-dmscreen-reference.js
77 lines (64 loc) · 1.5 KB
/
generate-dmscreen-reference.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
const fs = require("fs");
const utB = require("./util-book-reference");
const index = utB.UtilBookReference.getIndex(
{
name: "Quick Reference",
id: "bookref-quick",
tag: "quickref",
},
{
name: "DM Reference",
id: "bookref-dmscreen",
tag: "dmref",
},
);
fs.writeFileSync("data/generated/bookref-dmscreen.json", JSON.stringify(index).replace(/\s+\u2014\s+?/g, "\\u2014"), "utf8");
function flattenReferenceIndex (ref, skipHeaders) {
const outMeta = {
name: {},
id: {},
section: {},
};
const meta = {
name: {},
id: {},
section: {},
};
const out = [];
let nameId = 1;
let idId = 1;
let sectionId = 1;
let indexId = 1;
Object.values(ref).forEach(book => {
if (!meta.name[book.name]) {
outMeta.name[nameId] = book.name;
meta.name[book.name] = nameId++;
}
if (!meta.id[book.id]) {
outMeta.id[idId] = book.id;
meta.id[book.id] = idId++;
}
book.contents.forEach((c, i) => {
if (!meta.section[c.name]) {
outMeta.section[sectionId] = c.name;
meta.section[c.name] = sectionId++;
}
if (skipHeaders) return;
(c.headers || []).forEach(h => {
out.push({
id: indexId++,
b: meta.id[book.id], // book
s: meta.section[c.name], // section name
p: i, // section index
h, // header name
})
});
});
});
return {
_meta: outMeta,
data: out,
};
}
fs.writeFileSync("data/generated/bookref-dmscreen-index.json", JSON.stringify(flattenReferenceIndex(index.reference)), "utf8");
console.log("Updated DM Screen references.");