Skip to content

Commit

Permalink
feat: deal with legacy rating
Browse files Browse the repository at this point in the history
  • Loading branch information
littlebtc committed Jul 26, 2021
1 parent 0fda452 commit ab180cb
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 3 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
21 changes: 21 additions & 0 deletions src/dx_intl/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand All @@ -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 => {
Expand All @@ -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")
Expand All @@ -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)
Expand Down Expand Up @@ -83,6 +102,7 @@ const parsePlayer = (
title,
trophy,
rating,
rating_legacy: isLegacyRating(rating, ratingImageRef),
grade,
}
}
Expand All @@ -99,6 +119,7 @@ const parsePlayer = (
title,
trophy,
rating,
rating_legacy: isLegacyRating(rating, ratingImageRef),
course_rank: courseRank,
class_rank: classRank,
}
Expand Down
50 changes: 50 additions & 0 deletions tests/dx_intl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,42 @@ const playerContentNewVersion = `
</div>
`

const playerContentNewVersionAndRating = `
<div class="see_through_block m_15 m_t_0 p_10 p_r t_l f_0">
<div class="basic_block p_10 f_0">
<img src="https://maimaidx.jp/maimai-mobile/img/Icon/513dc5cb9f0ca1c7.png" class="w_112 f_l"/>
<div class="p_l_10 f_l">
<div class="trophy_block trophy_Bronze p_3 t_c f_0">
<div class="trophy_inner_block f_13">
<span>京都府勢</span>
</div>
</div>
<div class="m_b_5">
<div class="name_block f_l f_16">KOINU</div>
<div class="f_r t_r f_0">
<div class="p_r p_3">
<img src="https://maimaidx.jp/maimai-mobile/img/rating_base_gold.png?ver=1.17" class="h_30 f_r"/>
<div class="rating_block f_11">14323</div>
</div>
</div>
<div class="clearfix"></div>
</div>
<img src="https://maimaidx.jp/maimai-mobile/img/line_01.png" class="user_data_block_line" />
<div class="clearfix"></div>
<img src="https://maimaidx.jp/maimai-mobile/img/course/course_rank_01T7GHJvGe.png" class="h_35 f_l"/>
<img src="https://maimaidx.jp/maimai-mobile/img/class/class_rank_s_04ZqZmdpb8.png" class="p_l_10 h_35 f_l">
<div class="p_l_10 f_l f_14"><img src="https://maimaidx.jp/maimai-mobile/img/icon_star.png" class="h_30 m_3 v_m"/>×3</div>
</div>
<div class="clearfix"></div>
</div>
<img src="https://maimaidx.jp/maimai-mobile/img/Chara/a58681dff962bf96.png" class="w_120 m_t_10 f_r"/>
<div class="comment_block f_l f_12">
</div>
<div class="clearfix"></div>
</div>
`

const scoresContent = `
<div class="wrapper main_wrapper t_c">
<div class="screw_block m_15 f_15">POPS &amp; ANIME</div>
Expand Down Expand Up @@ -129,6 +165,7 @@ test("Player should parse successfully", () => {
title: "Test Title",
trophy: "gold",
rating: 8500,
rating_legacy: true,
grade: 13,
})
})
Expand All @@ -138,6 +175,7 @@ test("Player should parse successfully if title is a marquee", () => {
title: "打打打打打打打打打打打打打打打打打打打打打打打打",
trophy: "bronze",
rating: 10100,
rating_legacy: true,
grade: 21,
})
})
Expand All @@ -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,
})
Expand Down

0 comments on commit ab180cb

Please sign in to comment.