-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathchapter.js
305 lines (284 loc) · 9.23 KB
/
chapter.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
exports.parseChapter = parseChapter;
let parsers = {
Introduction: extractText,
"Unlock Requirements": parseUnlockReq,
"Clear Rewards": parseClearRewards,
"3-Star Rewards": parse3StarRewards,
"Enemy Level": parseEnermyLevel,
"Base XP (info)": parseBaseXP,
"Required Battles": extractLeadingDigits,
"Boss Kills to Clear": extractLeadingDigits,
"Star Conditions": parseStarCon,
"Air Supremacy (info)": parseAirSuprem,
"Fleet Restrictions": parseFleetRestriction,
"Stat Restrictions": parseStatsRestriction,
"Map Drops": parseMapDrops,
"Additional Notes": extractText,
"Equipment Blueprint Drops": parseEQBPDrops,
"Ship Drops": parseShipDrops,
"Node Map": parseNodeMap,
};
let betterNames = {
checkAndReplace: function (text) {
if (this[text]) return this[text];
else return text;
},
"3-StarRewards": "threeStarRewards",
};
function parseChapter(doc, index, names) {
let chapter = {};
let boxes = doc.getElementsByClassName("mapbox");
let hasHardMode = boxes.length > 5;
chapter["names"] = names[index] || {};
for (let i = 1; i <= 4; i++) {
chapter[i] = {
names: {
en: names[index + "-" + i] ? names[index + "-" + i].en : undefined,
cn: names[index + "-" + i] ? names[index + "-" + i].cn : undefined,
jp: names[index + "-" + i] ? names[index + "-" + i].jp : undefined,
},
normal: hasHardMode
? parseMap(doc.querySelector("div[title='" + index + "-" + i + "']"), index + "-" + i)
: parseMap(boxes[i - 1], index + "-" + i),
hard: hasHardMode
? parseMap(doc.querySelector("div[title='" + index + "-" + i + " Hard']"), index + "-" + i)
: null,
};
}
return chapter;
}
function parseMap(div, code, names) {
if (!div) return null;
let name_list = div.getElementsByTagName("th");
let data_list = div.getElementsByTagName("td");
let map = {};
map.title = div.querySelector(".mapbox-header").textContent;
map.code = code;
for (let i = 0; i < name_list.length; i++) {
let parser = parsers[name_list[i].textContent];
if (!parser) console.log("\n(" + i + ") Missing Parser: " + name_list[i].textContent);
map[betterNames.checkAndReplace(camelize(name_list[i].textContent.replace(/\(.+\)/, "")))] =
parser(data_list[i]);
if (name_list[i].textContent === "Node Map") break; // 100% the last valid head
}
return map;
}
function extractText(div) {
return div.textContent.trim();
}
function extractLeadingDigits(div) {
return parseInt(div.textContent.replace(/(^\d+).+/g, "$1"));
}
function parseUnlockReq(div) {
let value = div.textContent.trim();
return {
text: value,
requiredLevel: parseInt(value.substring(7)),
};
}
function parseClearRewards(div) {
if (div.childNodes[4].textContent === ", ")
return {
cube: parseInt(div.childNodes[0].textContent.replace(/[^\d]+/g, "")),
coin: parseInt(div.childNodes[2].textContent.replace(/[^\d]+/g, "")),
ship: div.childNodes[5].title,
};
else
return {
cube: parseInt(div.childNodes[0].textContent.replace(/[^\d]+/g, "")),
coin: parseInt(div.childNodes[2].textContent.replace(/[^\d]+/g, "")),
oil: parseInt(div.childNodes[4].textContent.replace(/[^\d]+/g, "")),
};
}
const ITEM_NAME_WITH_AMOUNT_REGEX = /^(?:,\s+)?(\d+)x ?(.+)$/g;
function parse3StarRewards(div) {
let reward = {};
let rewards = [];
for (let i = 0; i < div.childNodes.length; i++) {
if (div.childNodes[i].nodeType === 3) {
// text node
let text = div.childNodes[i].textContent;
if (text.includes(",")) {
rewards.push(reward);
reward = {};
text = text.replace(",", "");
}
if (text.trim().length === 0) continue;
if (isNaN(text.replace(/[,\s]+/g, ""))) {
if (ITEM_NAME_WITH_AMOUNT_REGEX.test(text)) {
let args = text.replace(ITEM_NAME_WITH_AMOUNT_REGEX, "$1|$2").split("|");
reward.count = parseInt(args[0]);
reward.item = args[1].trim();
} else reward.item = text.trim();
} else {
reward.count = parseInt(text.replace(/[^\d]+/g, ""));
}
} else {
reward.item = div.childNodes[i].title;
}
}
if (Object.keys(reward).length !== 0) rewards.push(reward);
return rewards;
}
function parseEnermyLevel(div) {
let info = {
mobLevel: parseInt(div.childNodes[1].textContent.replace(/[^\d]+/g, "")),
bossLevel: parseInt(div.childNodes[4].textContent.replace(/[^\d]+/g, "")),
};
if (div.childNodes.length === 7) info.boss = div.childNodes[5].textContent;
else if (div.childNodes.length > 7)
info.boss = [...div.childNodes].filter((n) => n.tagName === "A").map((e) => e.title);
else info.boss = div.childNodes[4].textContent.replace(/[^()]+\((.+)\)/, "$1");
return info;
}
function parseBaseXP(div) {
return {
smallFleet: parseInt(div.childNodes[1].textContent.replace(/[^\d]+/g, "")),
mediumFleet: parseInt(div.childNodes[3].textContent.replace(/[^\d]+/g, "")),
largeFleet: parseInt(div.childNodes[5].textContent.replace(/[^\d]+/g, "")),
bossFleet: parseInt(div.childNodes[7].textContent.replace(/[^\d]+/g, "")),
};
}
function parseStarCon(div) {
return [
div.childNodes[0].textContent,
div.childNodes[2].textContent,
div.childNodes[4].textContent,
];
}
function parseAirSuprem(div) {
let air = {
actual: parseInt(div.childNodes[1].textContent.replace(/[^\d]+/g, "")),
};
for (let i = 4; i < div.childNodes.length; i += 2) {
if (!div.childNodes[i].title) i -= 1;
else
air[camelize(div.childNodes[i].title)] = parseInt(
div.childNodes[i + 1].textContent.replace(/[^\d]+/g, "")
);
}
return air;
}
function parseFleetRestriction(div) {
let fleet_1_res = {};
let i = 0;
for (; div.childNodes[i] && div.childNodes[i + 1]; ) {
if (div.childNodes[i].tagName === "HR" || div.childNodes[i + 1].tagName === "HR") break;
if (div.childNodes[i].tagName === "B") {
i++;
continue;
}
fleet_1_res[camelize(div.childNodes[i + 1].title.replace(/\(.+\)/g, "").trim())] = parseInt(
div.childNodes[i].textContent.replace(/[^\d]/g, "")
);
i += 2;
}
i += 3;
let fleet_2_res = {};
for (; div.childNodes[i] && div.childNodes[i + 1]; ) {
if (div.childNodes[i].tagName === "B") {
i++;
continue;
}
fleet_2_res[camelize(div.childNodes[i + 1].title.replace(/\(.+\)/g, "").trim())] = parseInt(
div.childNodes[i].textContent.replace(/[^\d]/g, "")
);
i += 2;
}
return {
fleet1: fleet_1_res,
fleet2: fleet_2_res,
};
}
function parseStatsRestriction(div) {
let restrictions = {};
div.childNodes.forEach((n) => {
if (n.textContent === ": Not available") return;
if (n.nodeType === 3) {
let restriction = n.textContent.replace(/, $/, "").replace(":", "").trim();
let data = restriction
.replace(/(?:Total )?(.+)(?: >|(?: total value)? greater than) (\d+)/, "$1|$2")
.split("|");
data[0] = data[0].replace(/(total value|stat)/gi, "").trim();
restrictions[camelize(data[0])] = parseInt(data[1]);
}
});
return restrictions;
}
function parseMapDrops(div) {
let rewards = [];
div.childNodes.forEach((n) => {
if (n.nodeType === 3) rewards.push(n.textContent.replace(",", "").trim());
});
return rewards;
}
function parseEQBPDrops(div) {
let names = div.querySelectorAll(".alicon .alittxt");
let tiers = div.querySelectorAll(".alicon .alintr");
let eqs = [];
for (let i = 0; i < names.length; i++)
eqs[i] = {
name: names[i].textContent,
tier: tiers[i].textContent,
};
return eqs;
}
function parseShipDrops(div) {
let ships = [];
for (let container of div.getElementsByClassName("alicon")) {
let ship = {};
ship.name = container.getElementsByClassName("alittxt")[0].textContent;
if (container.getElementsByClassName("alintr")[0]) {
ship.note = container.getElementsByClassName("alintr")[0].textContent;
ships.push(ship);
} else ships.push(ship.name);
}
return ships;
}
const BGCOLOR_DICT = {
"rgb(0, 68, 0)": "Land",
"rgb(170, 221, 221)": "Sea",
};
function parseNodeMap(div) {
let nodemap = {};
nodemap.preview =
"https://azurlane.koumakan.jp" +
galleryThumbnailUrlToActualUrl(div.getElementsByTagName("img")[0].src);
let nodes = [];
let map = [];
let rows = div.querySelectorAll(".nodemap tr");
let width,
height = 0;
for (let row of rows) {
let cols = row.getElementsByTagName("td");
if (cols.length === 0) continue;
let map_cols = [];
if (!width) width = cols.length;
for (let i = 0; i < cols.length; i++) {
if (cols[i].children.length !== 0) map_cols[i] = cols[i].children[0].title;
else map_cols[i] = BGCOLOR_DICT[cols[i].style.backgroundColor];
if (map_cols[i] !== "Sea")
nodes.push({
x: i,
y: height,
node: map_cols[i],
});
}
map.push(map_cols);
height++;
}
nodemap.width = width;
nodemap.height = height;
nodemap.map = map;
nodemap.nodes = nodes;
return nodemap;
}
function galleryThumbnailUrlToActualUrl(tdir) {
return tdir.replace(/\/images\/thumb\/(.\/..)\/([^\/]+)\/.+/g, "/images/$1/$2");
}
function camelize(str) {
return str.replace(/(?:^\w|[A-Z]|\b\w|\s+)/g, (match, i) => {
if (+match === 0) return "";
return i == 0 ? match.toLowerCase() : match.toUpperCase();
});
}