Skip to content

Commit 8ebdb9a

Browse files
authored
regex hack
1 parent 5649771 commit 8ebdb9a

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

src/chapters/chapter.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,4 @@ export type AirSupremacy = {
4747
superiority: number;
4848
supremacy: number;
4949
} | { actual: number; denial: number; parity: number; superiority: number; supremacy: number; };
50+
export type MapDrop = 'string' | { min?: number, max?: number, amm?: string, item: string, guaranteed: boolean };

src/chapters/parser.ts

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
EnemyLevel,
66
MapPart,
77
Reward,
8+
MapDrop,
89
StarConditions,
910
} from "./chapter";
1011
import { camelize } from "../utils";
@@ -61,10 +62,10 @@ export function parseChapter(doc: Document, index: number, names: any) {
6162
jp: names[index + "-" + i] ? names[index + "-" + i].jp : null,
6263
},
6364
normal: hasHardMode
64-
? parseMap(boxes[(i-1)*2], index + "-" + i)
65+
? parseMap(boxes[(i - 1) * 2], index + "-" + i)
6566
: parseMap(boxes[i - 1], index + "-" + i),
6667
hard: hasHardMode
67-
? parseMap(boxes[(i-1)*2+1], index + "-" + i)
68+
? parseMap(boxes[(i - 1) * 2 + 1], index + "-" + i)
6869
: null,
6970
};
7071
}
@@ -198,7 +199,7 @@ function parseAirSuprem(div: Element): AirSupremacy {
198199
parity: parseInt(div.childNodes[8].textContent.replace(/[^\d]+/g, "")),
199200
superiority: parseInt(div.childNodes[10].textContent.replace(/[^\d]+/g, "")),
200201
supremacy: parseInt(div.childNodes[12].textContent.replace(/[^\d]+/g, ""))
201-
}:{
202+
} : {
202203
actual: parseInt(div.childNodes[1].textContent.replace(/[^\d]+/g, "")),
203204
superiority: parseInt(div.childNodes[6].textContent.replace(/[^\d]+/g, "")),
204205
supremacy: parseInt(div.childNodes[8].textContent.replace(/[^\d]+/g, "")),
@@ -261,10 +262,31 @@ function parseStatsRestriction(div: Element) {
261262
return restrictions;
262263
}
263264

264-
function parseMapDrops(div: Element): string[] {
265-
let rewards: string[] = [];
265+
const MAP_DROP_REGEX = /^\s*(?<amm>(?<min>\d+)[-](?<max>\d+)|\d+)x? (?<reward>[^,;.]+)(?:[,;.]|$)/
266+
function parseMapDrops(div: Element): MapDrop[] {
267+
let rewards: MapDrop[] = [];
268+
let type: 'summary' | 'clear' | 'chance' = 'summary'
269+
let buf = "";
270+
let m: RegExpMatchArray | null = null;
266271
div.childNodes.forEach((n) => {
267-
if (n.nodeType === 3) rewards.push(n.textContent.replace(",", "").trim());
272+
if (n.textContent === "Clear bonus:") return type = "clear";
273+
if (n.textContent === "Chance of getting:") return type = "chance";
274+
if (type === "summary") {
275+
if (n.nodeType === 3) rewards.push(n.textContent.replace(",", "").trim());
276+
} else {
277+
buf.push(n?.firstChild?.alt ?? n?.firstChild?.title ?? n?.alt ?? n?.title)
278+
let m = buf.match(MAP_DROP_REGEX)
279+
if (m) {
280+
buf = buf.slice(m[0].length)
281+
rewards.push({
282+
min: m.groups?.min ? parseInt(m.groups?.min) : undefined,
283+
max: m.groups?.max ? parseInt(m.groups?.max) : undefined,
284+
amm: !(m.groups?.min && m.groups?.max) ? m.groups?.amm : undefined,
285+
item: m.groups?.reward,
286+
guaranteed: type === "clear"
287+
})
288+
}
289+
}
268290
});
269291
return rewards;
270292
}

0 commit comments

Comments
 (0)