Refactor elfs data
This commit is contained in:
@@ -1,82 +1,56 @@
|
||||
import { readdirSync, readFileSync, readJsonFileSync } from '../fs'
|
||||
import { compareVersion } from '../../../lib/string'
|
||||
import { ElfData } from '../../../lib/honkai3rd/elfs'
|
||||
import yaml from 'yaml'
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
import { omit } from 'ramda'
|
||||
|
||||
const elfFileNameList = readdirSync('honkai3rd/elfs')
|
||||
const elfDataList = elfFileNameList
|
||||
.map((fileName) => {
|
||||
const filePathname = 'honkai3rd/elfs/' + fileName
|
||||
const data = readJsonFileSync(filePathname) as ElfData
|
||||
let cachedData: any = null
|
||||
|
||||
const krDataFilePath = `honkai3rd/ko-KR/elfs/${data.id}.md`
|
||||
try {
|
||||
const krData = parseElfData(readFileSync(krDataFilePath).toString())
|
||||
data.krName = krData.name
|
||||
data.skillRows.forEach((skillRow, skillRowIndex) => {
|
||||
skillRow.forEach((skill, skillIndex) => {
|
||||
if (krData.skillRows?.[skillRowIndex]?.[skillIndex]?.name != null) {
|
||||
skill.krName = krData.skillRows?.[skillRowIndex]?.[skillIndex]?.name
|
||||
}
|
||||
if (
|
||||
krData.skillRows?.[skillRowIndex]?.[skillIndex]?.description != null
|
||||
) {
|
||||
skill.krDescription =
|
||||
krData.skillRows?.[skillRowIndex]?.[skillIndex]?.description
|
||||
}
|
||||
})
|
||||
})
|
||||
} catch (error) {
|
||||
// console.warn('Failed to read', krDataFilePath)
|
||||
// console.warn(error)
|
||||
}
|
||||
export function listElfs(locale?: string): ElfData[] {
|
||||
if (cachedData == null) {
|
||||
cachedData = yaml.parse(
|
||||
fs
|
||||
.readFileSync(path.join(process.cwd(), 'data/honkai3rd/elfs.yaml'))
|
||||
.toString('utf-8')
|
||||
)
|
||||
}
|
||||
const elfs = cachedData
|
||||
|
||||
return data
|
||||
})
|
||||
.sort((a, b) => {
|
||||
let compareResult = 0
|
||||
|
||||
compareResult = compareVersion(b.version, a.version)
|
||||
|
||||
if (compareResult !== 0) {
|
||||
return compareResult
|
||||
}
|
||||
compareResult = a.name
|
||||
.replace(/ \(.\)/, '')
|
||||
.localeCompare(b.name.replace(/ \(.\)/, ''))
|
||||
|
||||
return compareResult
|
||||
})
|
||||
|
||||
const elfMap = elfDataList.reduce((map, elf) => {
|
||||
map.set(elf.id, elf)
|
||||
return map
|
||||
}, new Map<string, ElfData>())
|
||||
|
||||
export function listElfs() {
|
||||
return elfDataList
|
||||
}
|
||||
|
||||
export function getElfById(id: string) {
|
||||
return elfMap.get(id)
|
||||
}
|
||||
|
||||
function parseElfData(rawData: string) {
|
||||
const [name, ...skillRowSections] = rawData.split('\n## ')
|
||||
const skillRows = skillRowSections.map((skillRowSection) => {
|
||||
const skillSections = skillRowSection.split('\n### ')
|
||||
return skillSections.map((skillSection) => {
|
||||
const [name, description] = skillSection
|
||||
.split('\n\n')
|
||||
.map((data) => data.replace(/#+\s/, '').trim())
|
||||
const localized = (elfs as any[])
|
||||
.map((elf) => {
|
||||
return {
|
||||
name,
|
||||
description: description.replace(/\\\*/g, '*'),
|
||||
}
|
||||
...omit(['krName', 'krDescription'], elf),
|
||||
name: locale === 'ko-KR' ? elf.krName : elf.name,
|
||||
skillRows: elf.skillRows.map((row: any[]) => row.map(localizeSkill)),
|
||||
} as any
|
||||
})
|
||||
})
|
||||
.sort((a, b) => {
|
||||
let compareResult = 0
|
||||
|
||||
return {
|
||||
name: name.replace(/#+\s/, '').trim(),
|
||||
skillRows,
|
||||
compareResult = compareVersion(b.version, a.version)
|
||||
|
||||
if (compareResult !== 0) {
|
||||
return compareResult
|
||||
}
|
||||
compareResult = a.name
|
||||
.replace(/ \(.\)/, '')
|
||||
.localeCompare(b.name.replace(/ \(.\)/, ''))
|
||||
|
||||
return compareResult
|
||||
})
|
||||
|
||||
return localized
|
||||
|
||||
function localizeSkill(skill: any) {
|
||||
return {
|
||||
...omit(['krName', 'krDescription'], skill),
|
||||
name: locale === 'ko-KR' ? skill.krName : skill.name,
|
||||
description: locale === 'ko-KR' ? skill.krDescription : skill.description,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function getElfById(id: string, locale?: string) {
|
||||
return listElfs(locale).find((elf) => elf.id === id, locale)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user