Refactor stigmata data
This commit is contained in:
@@ -1,183 +1,136 @@
|
||||
import { readdirSync, readFileSync, readJsonFileSync } from '../fs'
|
||||
import { compareVersion } from '../../../lib/string'
|
||||
import { StigmataData, StigmataSet } from '../../../lib/honkai3rd/stigmata'
|
||||
import { StigmataData } from '../../../lib/honkai3rd/stigmata'
|
||||
import yaml from 'yaml'
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
import { omit } from 'ramda'
|
||||
|
||||
const stigmataDataFileNameList = readdirSync('honkai3rd/stigmata')
|
||||
const stigmataList = stigmataDataFileNameList
|
||||
.map((fileName) => {
|
||||
const filePathname = 'honkai3rd/stigmata/' + fileName
|
||||
const data = readJsonFileSync(filePathname) as StigmataData
|
||||
let cachedStigmataData: any = null
|
||||
|
||||
const krDataFilePath = `honkai3rd/ko-KR/stigmata/${data.id}.md`
|
||||
try {
|
||||
const krData = parseStigmataData(readFileSync(krDataFilePath).toString())
|
||||
|
||||
data.krName = krData.name
|
||||
data.skill.krName = krData.skill.name
|
||||
data.skill.krDescription = krData.skill.description
|
||||
} catch (error) {
|
||||
// console.warn('Failed to read', krDataFilePath)
|
||||
// console.warn(error)
|
||||
}
|
||||
|
||||
return data
|
||||
})
|
||||
.filter((stigmataData) => !stigmataData.hidden)
|
||||
.sort((a, b) => {
|
||||
let compareResult = b.rarity - a.rarity
|
||||
if (compareResult !== 0) {
|
||||
return compareResult
|
||||
}
|
||||
compareResult = compareVersion(b.version || '0.0', a.version || '0.0')
|
||||
if (compareResult !== 0) {
|
||||
return compareResult
|
||||
}
|
||||
compareResult = (a.set || 'ZZZZZ').localeCompare(b.set || 'ZZZZZ')
|
||||
if (compareResult !== 0) {
|
||||
return compareResult
|
||||
}
|
||||
compareResult = -a.type.localeCompare(b.type)
|
||||
if (compareResult !== 0) {
|
||||
return compareResult
|
||||
}
|
||||
compareResult = a.name
|
||||
.replace(/ \(.\)/, '')
|
||||
.localeCompare(b.name.replace(/ \(.\)/, ''))
|
||||
|
||||
return compareResult
|
||||
})
|
||||
const stigmataMap = stigmataList.reduce((map, stigmata) => {
|
||||
map.set(stigmata.id, stigmata)
|
||||
return map
|
||||
}, new Map<string, StigmataData>())
|
||||
|
||||
const stigmataSetStigmataDataListMap = stigmataList.reduce((map, stigmata) => {
|
||||
if (stigmata.set != null) {
|
||||
let stigmataList = map.get(stigmata.set)
|
||||
if (stigmataList == null) {
|
||||
stigmataList = []
|
||||
map.set(stigmata.set, stigmataList)
|
||||
}
|
||||
stigmataList.push(stigmata)
|
||||
export function listStigmata(locale?: string): StigmataData[] {
|
||||
if (cachedStigmataData == null) {
|
||||
cachedStigmataData = yaml.parse(
|
||||
fs
|
||||
.readFileSync(path.join(process.cwd(), 'data/honkai3rd/stigmata.yaml'))
|
||||
.toString('utf-8')
|
||||
)
|
||||
}
|
||||
return map
|
||||
}, new Map<string, StigmataData[]>())
|
||||
const stigmata = cachedStigmataData
|
||||
|
||||
const stigmataSetFileNameList = readdirSync('honkai3rd/stigmata-sets')
|
||||
const stigmataSetList = stigmataSetFileNameList
|
||||
.map((fileName) => {
|
||||
const filePathname = 'honkai3rd/stigmata-sets/' + fileName
|
||||
const data = readJsonFileSync(filePathname) as StigmataSet
|
||||
const localized = (stigmata as any[])
|
||||
.map((stigma) => {
|
||||
return {
|
||||
...omit(['krName', 'krDescription'], stigma),
|
||||
name: locale === 'ko-KR' ? stigma.krName : stigma.name,
|
||||
skill: localizeSkill(stigma.skill),
|
||||
} as any
|
||||
})
|
||||
.filter((stigmataData) => !stigmataData.hidden)
|
||||
.sort((a, b) => {
|
||||
let compareResult = b.rarity - a.rarity
|
||||
if (compareResult !== 0) {
|
||||
return compareResult
|
||||
}
|
||||
compareResult = compareVersion(b.version || '0.0', a.version || '0.0')
|
||||
if (compareResult !== 0) {
|
||||
return compareResult
|
||||
}
|
||||
compareResult = (a.set || 'ZZZZZ').localeCompare(b.set || 'ZZZZZ')
|
||||
if (compareResult !== 0) {
|
||||
return compareResult
|
||||
}
|
||||
compareResult = -a.type.localeCompare(b.type)
|
||||
if (compareResult !== 0) {
|
||||
return compareResult
|
||||
}
|
||||
compareResult = a.name
|
||||
.replace(/ \(.\)/, '')
|
||||
.localeCompare(b.name.replace(/ \(.\)/, ''))
|
||||
|
||||
const krDataFilePath = `honkai3rd/ko-KR/stigmata-sets/${data.id}.md`
|
||||
try {
|
||||
const krData = parseStigmataSetData(
|
||||
readFileSync(krDataFilePath).toString()
|
||||
)
|
||||
|
||||
data.krName = krData.name
|
||||
data.krAltName = krData.altName
|
||||
data.twoSetSkill.krName = krData.twoSetSkill.name
|
||||
data.twoSetSkill.krDescription = krData.twoSetSkill.description
|
||||
data.threeSetSkill.krName = krData.threeSetSkill.name
|
||||
data.threeSetSkill.krDescription = krData.threeSetSkill.description
|
||||
} catch (error) {
|
||||
// console.warn('Failed to read', krDataFilePath)
|
||||
// console.warn(error)
|
||||
}
|
||||
|
||||
return data
|
||||
})
|
||||
.filter((stigmataSet) => !stigmataSet.hidden)
|
||||
.sort((a, b) => {
|
||||
let compareResult = b.rarity - a.rarity
|
||||
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 = compareVersion(b.version || '0.0', a.version || '0.0')
|
||||
if (compareResult !== 0) {
|
||||
}
|
||||
}
|
||||
|
||||
export function getStigmaById(id: string, locale?: string) {
|
||||
return listStigmata(locale).find((stigma) => stigma.id === id, locale)
|
||||
}
|
||||
|
||||
let cachedStigmataSetsData: any = null
|
||||
|
||||
export function listStigmataSet(locale?: string): StigmataData[] {
|
||||
if (cachedStigmataSetsData == null) {
|
||||
cachedStigmataSetsData = yaml.parse(
|
||||
fs
|
||||
.readFileSync(
|
||||
path.join(process.cwd(), 'data/honkai3rd/stigmata-sets.yaml')
|
||||
)
|
||||
.toString('utf-8')
|
||||
)
|
||||
}
|
||||
const stigmataSets = cachedStigmataSetsData as any[]
|
||||
const localized = stigmataSets
|
||||
.map((stigmataSet) => {
|
||||
return {
|
||||
...omit(['krName', 'krDescription'], stigmataSet),
|
||||
name: locale === 'ko-KR' ? stigmataSet.krName : stigmataSet.name,
|
||||
altName:
|
||||
locale === 'ko-KR' ? stigmataSet.krAltName : stigmataSet.altName,
|
||||
twoSetSkill: localizeSkill(stigmataSet.twoSetSkill),
|
||||
threeSetSkill: localizeSkill(stigmataSet.threeSetSkill),
|
||||
} as any
|
||||
})
|
||||
.filter((stigmataSet) => !stigmataSet.hidden)
|
||||
.sort((a, b) => {
|
||||
let compareResult = b.rarity - a.rarity
|
||||
if (compareResult !== 0) {
|
||||
return compareResult
|
||||
}
|
||||
compareResult = compareVersion(b.version || '0.0', a.version || '0.0')
|
||||
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,
|
||||
}
|
||||
compareResult = a.name
|
||||
.replace(/ \(.\)/, '')
|
||||
.localeCompare(b.name.replace(/ \(.\)/, ''))
|
||||
}
|
||||
}
|
||||
|
||||
return compareResult
|
||||
export function getStigmataSetBySetId(setId: string, locale?: string) {
|
||||
return listStigmataSet(locale).find((set) => set.id === setId, locale)
|
||||
}
|
||||
|
||||
export function getStigmataListBySetId(setId: string, locale?: string) {
|
||||
return ['top', 'mid', 'bot'].map((type) => {
|
||||
return getStigmaById(setId + '-' + type, locale)!
|
||||
})
|
||||
|
||||
const stigmataSetMap = stigmataSetList.reduce((map, stigmataSet) => {
|
||||
map.set(stigmataSet.id, stigmataSet)
|
||||
return map
|
||||
}, new Map<string, StigmataSet>())
|
||||
|
||||
export function listStigmata() {
|
||||
return stigmataList
|
||||
}
|
||||
|
||||
export function getStigmataById(id: string) {
|
||||
return stigmataMap.get(id)
|
||||
}
|
||||
|
||||
export function listStigmataSet() {
|
||||
return stigmataSetList
|
||||
}
|
||||
|
||||
export function getStigmataListBySetId(setId: string) {
|
||||
return stigmataSetStigmataDataListMap.get(setId) || []
|
||||
}
|
||||
|
||||
export function getStigmataSetBySetId(setId: string) {
|
||||
return stigmataSetMap.get(setId)
|
||||
}
|
||||
|
||||
export function getStigmataMapByIds(idList: string[]) {
|
||||
export function getStigmataMapByIds(idList: string[], locale?: string) {
|
||||
return idList.reduce<{ [key: string]: StigmataData }>((map, id) => {
|
||||
const stigmata = getStigmataById(id)
|
||||
const stigmata = getStigmaById(id, locale)
|
||||
if (stigmata != null) {
|
||||
map[id] = stigmata
|
||||
}
|
||||
return map
|
||||
}, {})
|
||||
}
|
||||
|
||||
function parseStigmataData(rawData: string) {
|
||||
const [name, skillName, skillDescription] = rawData
|
||||
.replace(/\\\*/g, '*')
|
||||
.split('\n\n')
|
||||
.map((data) => data.replace(/#+\s/, '').trim())
|
||||
|
||||
return {
|
||||
name,
|
||||
skill: {
|
||||
name: skillName,
|
||||
description: skillDescription,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
function parseStigmataSetData(rawData: string) {
|
||||
const [
|
||||
name,
|
||||
altName,
|
||||
twoSetSkillName,
|
||||
twoSetSkillDescription,
|
||||
threeSetSkillName,
|
||||
threeSetSkillDescription,
|
||||
] = rawData
|
||||
.replace(/\\\*/g, '*')
|
||||
.split('\n\n')
|
||||
.map((data) => data.replace(/#+\s/, '').trim())
|
||||
|
||||
return {
|
||||
name,
|
||||
altName,
|
||||
twoSetSkill: {
|
||||
name: twoSetSkillName,
|
||||
description: twoSetSkillDescription,
|
||||
},
|
||||
threeSetSkill: {
|
||||
name: threeSetSkillName,
|
||||
description: threeSetSkillDescription,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user