-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwikiscrape.js
More file actions
131 lines (111 loc) · 3.06 KB
/
Copy pathwikiscrape.js
File metadata and controls
131 lines (111 loc) · 3.06 KB
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
import https from "https";
export const getWikiSource = (url) =>
new Promise((resolve, reject) => {
const options = {
headers: { "user-agent": "riktenx/filterscape" },
};
https.get(`${url}?action=raw`, options, (resp) => {
if (resp.statusCode !== 200) {
return reject(new Error("http " + resp.statusCode));
}
let data = "";
resp.on("data", (d) => {
data += d;
});
resp.on("end", () => resolve(data));
});
});
const computeMapArea = (points, plane) => {
if (points.length === 0) {
return [-1, -1, -1, -1, -1, -1];
}
let x0 = points[0].x;
let y0 = points[0].y;
let x1 = points[0].x;
let y1 = points[0].y;
for (const point of points) {
x0 = Math.min(x0, point.x);
y0 = Math.min(y0, point.y);
x1 = Math.max(x1, point.x);
y1 = Math.max(y1, point.y);
}
return [x0, y0, plane, x1, y1, plane];
};
const parseLocLineProperty = (line) => {
const [_, value] = line.substring(1).split("=");
return value.trim();
};
const parseLocLinePoints = (line) => {
var pointDecls = line.substring(1);
var parts = pointDecls.split("|");
return parts.map((part) => {
const [xDecl, yDecl] = part.split(",");
const [_, xStr] = xDecl.split(":");
const [__, yStr] = yDecl.split(":");
const x = parseInt(xStr);
const y = parseInt(yStr);
return { x, y };
});
};
export const buildMapAreas = (wikiSource) => {
const lines = wikiSource.split("\n");
let areas = {};
let inLocLine = false;
let current;
for (const line of lines) {
if (line === "{{LocLine") {
inLocLine = true;
current = { name: "", plane: -1, points: [] };
}
if (!inLocLine) {
continue;
}
if (line.startsWith("|location")) {
current.name = parseLocLineProperty(line);
} else if (line.startsWith("|plane")) {
current.plane = parseInt(parseLocLineProperty(line));
} else if (line.startsWith("|x")) {
current.points = parseLocLinePoints(line);
}
if (line === "}}" && inLocLine) {
// flush
inLocLine = false;
areas[current.name] = computeMapArea(current.points, current.plane);
}
}
return areas;
};
export const buildDropTable = (wikiSource) => {
const lines = wikiSource.split("\n");
let currentList;
let lists = {};
for (const line of lines) {
if (line.startsWith("====") && line.endsWith("====")) {
currentList = line.substring(4, line.length - 4).trim();
if (!lists[currentList]) {
lists[currentList] = [];
}
continue;
}
if (line.startsWith("===") && line.endsWith("===")) {
currentList = line.substring(3, line.length - 3).trim();
if (!lists[currentList]) {
lists[currentList] = [];
}
continue;
}
const [preamble, nameDecl] = line.split("|");
if (
preamble !== "{{DropsLine" ||
!nameDecl ||
!nameDecl.startsWith("name=")
) {
continue;
}
const [_, name] = nameDecl.split("=");
if (!lists[currentList].includes(name)) {
lists[currentList].push(name);
}
}
return lists;
};