forked from derekjones562/5e-spelljammer.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil-generate-tables-data.js
315 lines (265 loc) · 7.99 KB
/
util-generate-tables-data.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
"use strict";
if (typeof module !== "undefined") {
require("../js/utils");
require("../js/render");
}
class UtilGenTables {
static _getTableSectionIndex (sectionOrders, chapterName, sectionName) {
((sectionOrders[chapterName] =
sectionOrders[chapterName] || {})[sectionName] =
sectionOrders[chapterName][sectionName] || 1);
const val = sectionOrders[chapterName][sectionName];
sectionOrders[chapterName][sectionName]++;
return val;
}
/**
* @param opts
* @param opts.sectionOrders
* @param opts.path
* @param opts.tmpMeta
* @param opts.section
* @param opts.data
* @param opts.stacks
* @param opts.isRequireIncludes
*/
static _doSearch (opts) {
const {sectionOrders, path, tmpMeta, section, data, stacks, isRequireIncludes} = opts;
if (data.data && data.data.tableIgnore) return;
if (data.entries) {
const nxtSection = data.name || section;
if (data.name || data.page) {
const {name, page} = data;
path.push({name, page});
}
data.entries.forEach(ent => this._doSearch({...opts, section: nxtSection, data: ent}));
if (data.name || data.page) path.pop();
} else if (data.items) {
if (data.name || data.page) {
const {name, page} = data;
path.push({name, page});
}
data.items.forEach(item => this._doSearch({...opts, data: item}));
if (data.name || data.page) path.pop();
} else if (data.type === "table" || data.type === "tableGroup") {
if (isRequireIncludes && !(data.data && data.data[VeCt.ENTDATA_TABLE_INCLUDE])) return;
const cpy = MiscUtil.copy(data);
cpy._tmpMeta = MiscUtil.copy(tmpMeta);
cpy.path = MiscUtil.copy(path);
cpy.section = section;
cpy.sectionIndex = this._getTableSectionIndex(sectionOrders, tmpMeta.name, section);
stacks[data.type].push(cpy);
}
}
static _getCleanSectionName (name) {
return name.replace(/^(?:Step )?[-\d]+[:.]?\s*/, "");
}
static _isSectionInTitle (sections, title) {
const lowTitle = title.toLowerCase();
return sections.some(section => lowTitle.includes(section.toLowerCase()));
}
static _mutDataAddPage (table) {
for (let i = table.path.length - 1; i >= 0; --i) {
if (table.path[i].page) {
table.page = table.path[i].page;
break;
}
}
}
static _mutCleanData (table) {
delete table.path;
delete table.section;
delete table.sectionIndex;
if (table._tmpMeta.metaType === "adventure-book") {
table.chapter = table._tmpMeta;
delete table._tmpMeta;
// clean chapter
if (table.data && table.data.tableChapterIgnore) {
delete table.chapter;
} else {
const chapterOut = {};
chapterOut.name = table.chapter.name;
chapterOut.ordinal = table.chapter.ordinal;
chapterOut.index = table.chapter.index;
table.chapter = chapterOut;
}
} else if (table._tmpMeta.metaType === "class") {
table.class = {name: table._tmpMeta.className, source: table._tmpMeta.classSource};
delete table._tmpMeta;
} else if (table._tmpMeta.metaType === "subclass") {
table.subclass = {
name: table._tmpMeta.subclassName,
source: table._tmpMeta.subclassSource,
className: table._tmpMeta.className,
classSource: table._tmpMeta.classSource,
};
delete table._tmpMeta;
} else if (table._tmpMeta.metaType === "variantrule") {
table.variantrule = {name: table._tmpMeta.name, source: table._tmpMeta.source};
delete table._tmpMeta;
}
if (table.type === "table") delete table.type;
delete table.data;
if (table.name) table.name = Renderer.stripTags(table.name);
}
/**
* @param doc
* @param opts
* @param opts.headProp
* @param opts.bodyProp
* @param [opts.isRequireIncludes]
*/
static getAdventureBookTables (doc, opts) {
const sectionOrders = {};
const stacks = {table: [], tableGroup: []};
if (!(doc[opts.headProp] && doc[opts.bodyProp])) return;
doc[opts.bodyProp].data.forEach((chapter, ixChapter) => {
const tmpMeta = MiscUtil.copy(doc[opts.headProp].contents[ixChapter]);
tmpMeta.index = ixChapter;
tmpMeta.metaType = "adventure-book";
const path = [];
this._doSearch({
sectionOrders,
path,
tmpMeta: tmpMeta,
section: doc[opts.headProp].name,
data: chapter,
stacks: stacks,
isRequireIncludes: opts.isRequireIncludes,
});
});
stacks.table.forEach(tbl => {
const cleanSectionNames = tbl.path.filter(ent => ent.name).map(ent => this._getCleanSectionName(ent.name));
if (tbl.data && tbl.data.tableNamePrefix && tbl.caption) {
tbl.name = `${tbl.data.tableNamePrefix}; ${tbl.caption}`;
} else if (tbl.data && tbl.data.tableName) {
tbl.name = tbl.data.tableName;
} else if (tbl.caption) {
if (this._isSectionInTitle(cleanSectionNames, tbl.caption) || (tbl.data && tbl.data.skipSectionPrefix)) {
tbl.name = tbl.caption;
} else {
tbl.name = `${cleanSectionNames.last()}; ${tbl.caption}`;
}
} else {
// If this is the only table in this section, remove the numerical suffix
if (tbl.sectionIndex === 1 && sectionOrders[tbl._tmpMeta.name][tbl.section] === 2) {
tbl.name = cleanSectionNames.last();
} else {
tbl.name = `${cleanSectionNames.last()}; ${tbl.sectionIndex}`;
}
}
this._mutDataAddPage(tbl);
tbl.source = doc[opts.headProp].id;
this._mutCleanData(tbl);
});
stacks.tableGroup.forEach(tg => {
const cleanSections = tg.path.filter(ent => ent.name).map(ent => this._getCleanSectionName(ent.name));
if (!tg.name) throw new Error("Group had no name!");
if (!this._isSectionInTitle(cleanSections, tg.name)) {
tg.name = `${cleanSections.last()}; ${tg.name}`;
}
this._mutDataAddPage(tg);
tg.source = doc[opts.headProp].id;
this._mutCleanData(tg);
});
return stacks;
}
static getClassTables (cls) {
const sectionOrders = {};
const stacks = {table: [], tableGroup: []};
const path = [];
cls.classFeatures.forEach((lvl, lvlI) => {
const tmpMeta = {
metaType: "class",
className: cls.name,
classSource: cls.source || SRC_PHB,
level: lvlI + 1,
};
lvl.forEach(feat => this._doSearch({
sectionOrders,
path,
tmpMeta,
section: cls.name,
data: feat,
stacks: stacks,
isRequireIncludes: true,
// Used to deduplicate headers
name: cls.name,
}));
});
stacks.table.forEach(it => {
it.name = it.caption;
it.source = it._tmpMeta.subclassSource || it._tmpMeta.classSource;
this._mutDataAddPage(it);
this._mutCleanData(it);
});
return stacks;
}
static getSubclassTables (sc) {
const sectionOrders = {};
const stacks = {table: [], tableGroup: []};
const path = [];
sc.subclassFeatures.forEach(lvl => {
const level = lvl[0].level;
const tmpMeta = {
metaType: "subclass",
className: sc.className,
classSource: sc.classSource || SRC_PHB,
level,
subclassName: sc.name,
subclassSource: sc.source || sc.classSource || SRC_PHB,
// Used to deduplicate headers
name: sc.name,
};
lvl.forEach(feat => this._doSearch({
sectionOrders,
path,
tmpMeta,
section: sc.className,
data: feat,
stacks: stacks,
isRequireIncludes: true,
}));
});
stacks.table.forEach(it => {
it.name = it.caption;
it.source = it._tmpMeta.subclassSource || it._tmpMeta.classSource;
this._mutDataAddPage(it);
this._mutCleanData(it);
});
return stacks;
}
static getGenericTables (entity, metaType, ...props) {
const sectionOrders = {};
const stacks = {table: [], tableGroup: []};
const path = [];
props.forEach(prop => {
if (!entity[prop]) return;
const tmpMeta = {
metaType: metaType,
name: entity.name,
source: entity.source,
};
entity[prop].forEach(ent => {
this._doSearch({
sectionOrders,
path,
tmpMeta,
section: entity.name,
data: ent,
stacks: stacks,
isRequireIncludes: true,
// Used to deduplicate headers
name: entity.name,
})
})
});
stacks.table.forEach(it => {
it.name = it.caption;
it.source = it._tmpMeta.source;
this._mutDataAddPage(it);
this._mutCleanData(it);
});
return stacks;
}
}
if (typeof module !== "undefined") module.exports = UtilGenTables;