Refactor weapon data
This commit is contained in:
@@ -4,11 +4,11 @@ import path from 'path'
|
||||
import yaml from 'yaml'
|
||||
import { omit } from 'ramda'
|
||||
|
||||
let parsedData: any = null
|
||||
let cachedData: any = null
|
||||
|
||||
export function listBattlesuits(locale?: string): BattlesuitData[] {
|
||||
if (parsedData == null) {
|
||||
parsedData = yaml.parse(
|
||||
if (cachedData == null) {
|
||||
cachedData = yaml.parse(
|
||||
fs
|
||||
.readFileSync(
|
||||
path.join(process.cwd(), 'data/honkai3rd/battlesuits.yaml')
|
||||
@@ -16,7 +16,7 @@ export function listBattlesuits(locale?: string): BattlesuitData[] {
|
||||
.toString('utf-8')
|
||||
)
|
||||
}
|
||||
const battlesuits = parsedData
|
||||
const battlesuits = cachedData
|
||||
|
||||
const localized = (battlesuits as any[]).map((battlesuit) => {
|
||||
return {
|
||||
|
||||
@@ -1,87 +1,71 @@
|
||||
import { readdirSync, readFileSync, readJsonFileSync } from '../fs'
|
||||
import { compareVersion } from '../../../lib/string'
|
||||
import { WeaponData } from '../../../lib/honkai3rd/weapons'
|
||||
import yaml from 'yaml'
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
import { omit } from 'ramda'
|
||||
|
||||
const weaponsFileNameList = readdirSync('honkai3rd/weapons')
|
||||
const weaponDataList = weaponsFileNameList
|
||||
.map((fileName) => {
|
||||
const filePathname = 'honkai3rd/weapons/' + fileName
|
||||
const data = readJsonFileSync(filePathname) as WeaponData
|
||||
let cachedData: any = null
|
||||
|
||||
const krDataFilePath = `honkai3rd/ko-KR/weapons/${data.id}.md`
|
||||
try {
|
||||
const krData = parseWeaponData(readFileSync(krDataFilePath).toString())
|
||||
export function listWeapons(locale?: string): WeaponData[] {
|
||||
if (cachedData == null) {
|
||||
cachedData = yaml.parse(
|
||||
fs
|
||||
.readFileSync(path.join(process.cwd(), 'data/honkai3rd/weapons.yaml'))
|
||||
.toString('utf-8')
|
||||
)
|
||||
}
|
||||
const weapons = cachedData
|
||||
|
||||
data.krName = krData.name
|
||||
data.skills.forEach((skill, index) => {
|
||||
skill.krName = krData.skills[index].name
|
||||
skill.krDescription = krData.skills[index].description
|
||||
})
|
||||
} catch (error) {
|
||||
// console.warn('Failed to read', krDataFilePath)
|
||||
// console.warn(error)
|
||||
}
|
||||
const localized = (weapons as any[])
|
||||
.map((weapon) => {
|
||||
return {
|
||||
...omit(['krName', 'krDescription'], weapon),
|
||||
name: locale === 'ko-KR' ? weapon.krName : weapon.name,
|
||||
skills: weapon.skills.map((skill: any) => localizeSkill(skill)),
|
||||
} as any
|
||||
})
|
||||
.sort((a, b) => {
|
||||
let compareResult = 0
|
||||
|
||||
return data
|
||||
})
|
||||
.sort((a, b) => {
|
||||
let compareResult = 0
|
||||
compareResult = compareVersion(b.version || '0.0', a.version || '0.0')
|
||||
if (compareResult !== 0) {
|
||||
return compareResult
|
||||
}
|
||||
|
||||
compareResult = b.rarity - a.rarity
|
||||
if (compareResult !== 0) {
|
||||
return compareResult
|
||||
}
|
||||
|
||||
compareResult = a.name
|
||||
.replace(/ \(.\)/, '')
|
||||
.localeCompare(b.name.replace(/ \(.\)/, ''))
|
||||
|
||||
compareResult = compareVersion(b.version || '0.0', a.version || '0.0')
|
||||
if (compareResult !== 0) {
|
||||
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,
|
||||
}
|
||||
|
||||
compareResult = b.rarity - a.rarity
|
||||
if (compareResult !== 0) {
|
||||
return compareResult
|
||||
}
|
||||
|
||||
compareResult = a.name
|
||||
.replace(/ \(.\)/, '')
|
||||
.localeCompare(b.name.replace(/ \(.\)/, ''))
|
||||
|
||||
return compareResult
|
||||
})
|
||||
const weaponMap = weaponDataList.reduce((map, weapon) => {
|
||||
map.set(weapon.id, weapon)
|
||||
return map
|
||||
}, new Map<string, WeaponData>())
|
||||
|
||||
export function listWeapons() {
|
||||
return weaponDataList
|
||||
}
|
||||
}
|
||||
|
||||
export function getWeaponById(id: string) {
|
||||
return weaponMap.get(id)
|
||||
export function getWeaponById(id: string, locale?: string) {
|
||||
return listWeapons(locale).find((weapon) => weapon.id === id, locale)
|
||||
}
|
||||
|
||||
export function getWeaponMapByIds(idList: string[]) {
|
||||
export function getWeaponMapByIds(idList: string[], locale?: string) {
|
||||
return idList.reduce<{ [key: string]: WeaponData }>((map, id) => {
|
||||
const weapon = getWeaponById(id)
|
||||
const weapon = getWeaponById(id, locale)
|
||||
if (weapon != null) {
|
||||
map[id] = weapon
|
||||
}
|
||||
return map
|
||||
}, {})
|
||||
}
|
||||
|
||||
function parseWeaponData(rawData: string) {
|
||||
const [name, ...skillSections] = rawData
|
||||
.replace(/\\\*/g, '*')
|
||||
.split('## ')
|
||||
.map((data) => data.replace(/#+\s/, '').trim())
|
||||
const skills = skillSections.map((skillSection) => {
|
||||
const [skillName, skillDescription] = skillSection.split('\n\n')
|
||||
|
||||
return {
|
||||
name: skillName,
|
||||
description: skillDescription,
|
||||
}
|
||||
})
|
||||
|
||||
return {
|
||||
name,
|
||||
skills,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user