diff --git a/package-lock.json b/package-lock.json index 1fd0ea9..5f477bf 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@otohime-site/parser", - "version": "21.7.1", + "version": "21.7.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@otohime-site/parser", - "version": "21.7.1", + "version": "21.7.2", "license": "MIT", "devDependencies": { "@types/jest": "^26.0.20", diff --git a/package.json b/package.json index f6d65df..13d1ab6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@otohime-site/parser", - "version": "21.7.1", + "version": "21.7.2", "license": "MIT", "main": "index.js", "types": "index.d.ts", diff --git a/src/dx_intl/player.ts b/src/dx_intl/player.ts index e99e699..9811e18 100644 --- a/src/dx_intl/player.ts +++ b/src/dx_intl/player.ts @@ -3,6 +3,9 @@ import { assertNonEmpty, assertBetween } from "../utils" interface PlayerParseResultBase { card_name: string rating: number + // Who did not play the new version yet + // will stay the previous rating + rating_legacy: boolean title: string trophy: "normal" | "bronze" | "silver" | "gold" | "rainbow" } @@ -16,6 +19,16 @@ export interface PlayerParseResultNew extends PlayerParseResultBase { class_rank: number } +const isLegacyRating = (rating: number, ref: string): boolean => + (ref === "green" && rating < 3000) || + (ref === "orange" && rating < 4000) || + (ref === "red" && rating < 7000) || + (ref === "purple" && rating < 10000) || + (ref === "bronze" && rating < 12000) || + (ref === "silver" && rating < 13000) || + (ref === "gold" && rating < 14000) || + (ref === "rainbow" && rating < 15000) + const parsePlayer = ( content: string | HTMLDocument ): PlayerParseResultLegacy | PlayerParseResultNew => { @@ -37,6 +50,10 @@ const parsePlayer = ( ])[0].toLowerCase() const ratingBlock = document.querySelector(".rating_block") const rawRating = ratingBlock?.textContent ?? "" + const ratingImageRef = (document + .querySelector('img[src *= "/img/rating_base_"]') + ?.getAttribute("src") + ?.match(/_([a-z]+)\.png/) ?? ["", ""])[1] const rawCourseRank = (document .querySelector('img[src *= "/img/course/"]') ?.getAttribute("src") @@ -53,6 +70,8 @@ const parsePlayer = ( assertNonEmpty(title, "title") assertNonEmpty(trophy, "trophy") assertNonEmpty(rawRating, "rating") + assertNonEmpty(ratingImageRef, "ratingImageRef") + if ( rawGrade.length === 0 && (rawCourseRank.length === 0 || rawClassRank.length === 0) @@ -83,6 +102,7 @@ const parsePlayer = ( title, trophy, rating, + rating_legacy: isLegacyRating(rating, ratingImageRef), grade, } } @@ -99,6 +119,7 @@ const parsePlayer = ( title, trophy, rating, + rating_legacy: isLegacyRating(rating, ratingImageRef), course_rank: courseRank, class_rank: classRank, } diff --git a/tests/dx_intl.test.ts b/tests/dx_intl.test.ts index a309700..68dec5a 100644 --- a/tests/dx_intl.test.ts +++ b/tests/dx_intl.test.ts @@ -79,6 +79,42 @@ const playerContentNewVersion = ` ` +const playerContentNewVersionAndRating = ` +
+
+ +
+
+
+ 京都府勢 +
+
+
+
KOINU
+
+
+ +
14323
+
+
+
+
+ +
+ + +
×3
+
+
+
+ +
+ +
+
+
+` + const scoresContent = `
POPS & ANIME
@@ -129,6 +165,7 @@ test("Player should parse successfully", () => { title: "Test Title", trophy: "gold", rating: 8500, + rating_legacy: true, grade: 13, }) }) @@ -138,6 +175,7 @@ test("Player should parse successfully if title is a marquee", () => { title: "打打打打打打打打打打打打打打打打打打打打打打打打", trophy: "bronze", rating: 10100, + rating_legacy: true, grade: 21, }) }) @@ -147,6 +185,18 @@ test("We should be able to parse the new version player", () => { title: "京都府勢", trophy: "bronze", rating: 5316, + rating_legacy: true, + course_rank: 1, + class_rank: 4, + }) +}) +test("We should be able to parse the new version player and rating", () => { + expect(parsePlayer(playerContentNewVersionAndRating)).toEqual({ + card_name: "KOINU", + title: "京都府勢", + trophy: "bronze", + rating: 14323, + rating_legacy: false, course_rank: 1, class_rank: 4, })