Skip to content

Commit

Permalink
feat: support parsing levels
Browse files Browse the repository at this point in the history
  • Loading branch information
littlebtc committed Nov 21, 2020
1 parent 4c25898 commit 729e264
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 6 deletions.
2 changes: 1 addition & 1 deletion 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": "0.3.0",
"version": "0.3.1",
"license": "MIT",
"main": "index.js",
"types": "index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/dx_intl/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { default as parsePlayer } from './player'
export { default as parseScores } from './scores'

export default (): void => {}
export default (): void => {}
12 changes: 12 additions & 0 deletions src/dx_intl/scores.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@

import { assertNonEmpty, assertBetween } from '../utils'

const LEVELS = [
'1', '2', '3', '4', '5', '6', '7', '7+', '8',
'8+', '9', '9+', '10', '10+', '11', '11+',
'12', '12+', '13', '13+', '14', '14+', '15'] as const

export interface ScoresParseEntry {
category: number
title: string
Expand All @@ -9,6 +14,7 @@ export interface ScoresParseEntry {
score: number
combo_flag: '' | 'fc' | 'fc+' | 'ap' | 'ap+'
sync_flag: '' | 'fs' | 'fs+' | 'fdx' | 'fdx+'
level: typeof LEVELS[number]
}

const parseScores = (content: string | HTMLDocument, categoryFrom: number = 1, categoryTo: number = 6): ScoresParseEntry[] => {
Expand Down Expand Up @@ -40,16 +46,21 @@ const parseScores = (content: string | HTMLDocument, categoryFrom: number = 1, c
const category = prev.currentCategory
const title = curr.querySelector('.music_name_block')?.textContent ?? ''
const difficulty = ['basic', 'advanced', 'expert', 'master', 'remaster'].indexOf(rawMusicDifficulty)
const rawLevel = curr.querySelector('.music_lv_block')?.textContent ?? ''
// If only one type: determine by .music_kind_icon (standard.png vs dx.png)
// Two types: determine by id (sta_xx vs dx_xx)
const rawDeluxe = curr.querySelector('.music_kind_icon')?.getAttribute('src') ?? ((curr.id.match(/sta|dx/) ?? [''])[0])
assertBetween(category, categoryFrom, categoryTo, 'category')
assertNonEmpty(title, 'title')
assertBetween(difficulty, 0, 4, 'difficulty')
assertNonEmpty(rawDeluxe, 'deluxe')
assertNonEmpty(rawLevel, 'level')
const score = parseFloat(rawScore)
const deluxe = (rawDeluxe === 'dx' || rawDeluxe.includes('dx.png'))
assertBetween(score, 0, 101, 'score')
const levelIndex = (LEVELS as readonly string[]).indexOf(rawLevel)
if (levelIndex < 0) { throw new Error('Level is not matched!') }
const level = LEVELS[levelIndex]

const flagImages = [...curr.querySelectorAll('img.f_r').values()]
const flags = flagImages.reduce<{
Expand Down Expand Up @@ -89,6 +100,7 @@ const parseScores = (content: string | HTMLDocument, categoryFrom: number = 1, c
deluxe,
difficulty,
score,
level,
...flags
}
]
Expand Down
12 changes: 9 additions & 3 deletions tests/dx_intl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const scoresContent = `
<div class="w_450 m_15 p_r f_0">
<div class="music_master_score_back pointer p_3">
<form>
<div class="music_lv_block f_r t_c f_14">12</div>
<div class="music_name_block t_l f_13 break">Test song 1</div>
<div class="music_score_block w_120 t_r f_l f_12">99.4738%</div>
<img src="https://maimaidx-eng.com/maimai-mobile/img/music_icon_back.png" class="h_30 f_r">
Expand All @@ -41,6 +42,7 @@ const scoresContent = `
<div class="w_450 m_15 p_r f_0" id="dx_1" style="margin-top:30px">
<div class="music_master_score_back pointer p_3">
<form>
<div class="music_lv_block f_r t_c f_14">13</div>
<div class="music_name_block t_l f_13 break">Test song 2</div>
<div class="music_score_block w_120 t_r f_l f_12">100.0208%</div>
<img src="https://maimaidx-eng.com/maimai-mobile/img/music_icon_fs.png" class="h_30 f_r">
Expand All @@ -52,6 +54,7 @@ const scoresContent = `
<div class="w_450 m_15 p_r f_0" id="sta_1" style="margin-top:30px">
<div class="music_master_score_back pointer p_3">
<form>
<div class="music_lv_block f_r t_c f_14">11</div>
<div class="music_name_block t_l f_13 break">Test song 2</div>
<div class="music_score_block w_120 t_r f_l f_12">99.1234%</div>
<img src="https://maimaidx-eng.com/maimai-mobile/img/music_icon_back.png" class="h_30 f_r">
Expand Down Expand Up @@ -82,23 +85,26 @@ test('Score should parse successfully', () => {
difficulty: 3,
score: 99.4738,
combo_flag: 'fc',
sync_flag: ''
sync_flag: '',
level: '12'
}, {
category: 1,
title: 'Test song 2',
deluxe: true,
difficulty: 3,
score: 100.0208,
combo_flag: 'fc+',
sync_flag: 'fs'
sync_flag: 'fs',
level: '13'
}, {
category: 1,
title: 'Test song 2',
deluxe: false,
difficulty: 3,
score: 99.1234,
combo_flag: '',
sync_flag: ''
sync_flag: '',
level: '11'
}
])
})

0 comments on commit 729e264

Please sign in to comment.