-
Notifications
You must be signed in to change notification settings - Fork 3
/
rules.js
121 lines (94 loc) · 2.96 KB
/
rules.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
var fs = require("fs");
var rulesNames = [];
fs.readFile("5esrd.json", (error, data) => {
if (error) {
console.error(error);
return;
}
const srd = JSON.parse(data);
fs.readFile("srd_rule_includes.json", (error, data2) => {
if (error) {
console.error(error);
return;
}
const includes = JSON.parse(data2);
const rules = nestedProcessRules(includes, srd);
// console.log(rules);
fs.writeFile("rulesData.json", JSON.stringify(rules, null, " "), (err) => {
if (err) {
console.error(err);
}
});
});
});
function nestedProcessRules(includes, srd, parents) {
let rules = [];
parents = parents || [];
for (let rule in includes) {
if (includes[rule] === false) continue;
if (!srd[rule]) {
console.log("Rule " + rule + " does not exist under " + parents.join(", "));
continue;
}
let ruleName = rule;
if (rulesNames.includes(ruleName)) {
ruleName = rule + " (" + parents.slice(-1)[0] + ")";
console.log("Duplicate rule " + rule + " in " + parents.join(", ") + ". Renaming to " + ruleName);
if (rulesNames.includes(ruleName)) {
console.log("Renamed rule duplicated. Skipping rule.");
continue;
}
}
const parsedRule = {
"name": ruleName,
"content": [],
"recordType": "rule"
};
parsedRule.searchString = rule.toLowerCase().replace(/[^\w]/g, "");
parsedRule.searchStrings = rule.toLowerCase().replace(/[^\w ]/g, "").split(" ");
if (parents.length > 0) {
parsedRule.parents = parents;
}
if (typeof srd[rule] == "string" || srd[rule] instanceof Array || srd[rule].hasOwnProperty("table")) {
parsedRule.content.push(srd[rule]);
rules.push(parsedRule);
rulesNames.push(ruleName);
} else {
if (srd[rule].content) {
if (srd[rule].content instanceof Array) {
parsedRule.content = parsedRule.content.concat(srd[rule].content);
} else {
parsedRule.content.push(srd[rule].content);
}
}
parsedRule.children = [];
for (let child in includes[rule]) {
if (includes[rule][child] === false) {
parsedRule.content.push("");
parsedRule.content.push("**" + child + "**");
parsedRule.content.push("");
if (srd[rule][child].content instanceof Array) {
parsedRule.content = parsedRule.content.concat(srd[rule][child].content);
} else {
parsedRule.content.push(srd[rule][child].content);
}
} else {
parsedRule.children.push(child);
}
}
if (parsedRule.children.length == 0) {
delete parsedRule.children;
}
const newParents = parents.slice(0);
if (parsedRule.content.length > 0) {
rules.push(parsedRule);
rulesNames.push(ruleName);
newParents.push(ruleName);
} else {
console.log("Skipping rule " + rule + " under " + parents.join(", ") + " as it has no content");
}
rules = rules.concat(nestedProcessRules(includes[rule], srd[rule], newParents));
}
}
return rules;
}