Refactor elfs data

This commit is contained in:
Sakura Knoll
2022-07-04 15:51:52 +09:00 Unverified
parent 91b45512ee
commit fd0be46e25
5 changed files with 57 additions and 98 deletions
+3 -9
View File
@@ -1,22 +1,16 @@
/** @jsxImportSource theme-ui */
import { Box, Card, Text } from '@theme-ui/components'
import { useRouter } from 'next/router'
import { assetsBucketBaseUrl } from '../../lib/consts'
import { ElfData } from '../../lib/honkai3rd/elfs'
import { translate } from '../../lib/i18n'
import PageLink from '../atoms/PageLink'
import SquareImageBox from '../atoms/SquareImageBox'
interface ElfCardProps {
elf: Pick<ElfData, 'id' | 'name' | 'krName'>
elf: Pick<ElfData, 'id' | 'name'>
hidden?: boolean
}
const ElfCard = ({ elf }: ElfCardProps) => {
const { locale } = useRouter()
const elfName = translate(locale, { 'ko-KR': elf.krName }, elf.name)
return (
<Card
sx={{
@@ -30,7 +24,7 @@ const ElfCard = ({ elf }: ElfCardProps) => {
>
<PageLink href={`/honkai3rd/elfs/${elf.id}`}>
<SquareImageBox
alt={elfName}
alt={elf.name}
src={`${assetsBucketBaseUrl}/honkai3rd/elfs/icon-${elf.id}.png`}
size={[100]}
/>
@@ -43,7 +37,7 @@ const ElfCard = ({ elf }: ElfCardProps) => {
textAlign: 'center',
}}
>
<Text>{elfName}</Text>
<Text>{elf.name}</Text>
</Box>
</PageLink>
</Card>
-3
View File
@@ -1,16 +1,13 @@
export interface ElfSkill {
type: 'passive' | 'basic' | 'ultimate' | 'team'
name: string
krName?: string
requiredRank: number
description: string
krDescription?: string
}
export interface ElfData {
id: string
name: string
krName?: string
baseRank: number
features: string[]
skillRows: [ElfSkill[], ElfSkill[], ElfSkill[], ElfSkill[]]
+6 -12
View File
@@ -30,12 +30,10 @@ const ElfShowPage = ({ elf }: ElfShowPageProps) => {
const { locale } = useRouter()
const { t } = useTranslation()
const elfName = translate(locale, { 'ko-KR': elf.krName }, elf.name)
return (
<Honkai3rdLayout>
<Head
title={`${t('common.honkai-3rd')}: ${elfName} - ${t(
title={`${t('common.honkai-3rd')}: ${elf.name} - ${t(
'common.abyss-lab'
)}`}
description={`${t('common.honkai-3rd')} ${t(
@@ -63,12 +61,12 @@ const ElfShowPage = ({ elf }: ElfShowPageProps) => {
{ href: '/honkai3rd/elfs', label: t('common.elfs') },
{
href: `/honkai3rd/elfs/${elf.id}`,
label: elfName,
label: elf.name,
},
]}
/>
<Heading as='h1'>{elfName}</Heading>
<Heading as='h1'>{elf.name}</Heading>
<Box mb={3}>
<Box
@@ -111,7 +109,7 @@ const ElfShowPage = ({ elf }: ElfShowPageProps) => {
as={columnIndex === 0 ? 'h2' : 'h3'}
sx={{ mb: 1 }}
>
{translate(locale, { 'ko-KR': item.krName }, item.name)}
{item.name}
</Heading>
<SecondaryLabel>
{t(`elfs-show.${item.type}`)}
@@ -125,11 +123,7 @@ const ElfShowPage = ({ elf }: ElfShowPageProps) => {
whiteSpace: 'pre-wrap',
}}
>
{translate(
locale,
{ 'ko-KR': item.krDescription },
item.description
)}
{item.description}
</Paragraph>
</React.Fragment>
)
@@ -148,7 +142,7 @@ export async function getStaticProps({
params,
locale,
}: NextPageContext & { params: { elfId: string } }) {
const elf = getElfById(params.elfId)
const elf = getElfById(params.elfId, locale)
return {
props: { elf, ...(await getI18NProps(locale)) },
}
+2 -2
View File
@@ -12,7 +12,7 @@ import Head from '../../../components/atoms/Head'
import Honkai3rdLayout from '../../../components/layouts/Honkai3rdLayout'
interface ElfListPageProps {
elfs: Pick<ElfData, 'id' | 'name' | 'krName'>[]
elfs: Pick<ElfData, 'id' | 'name'>[]
}
const ElfListPage = ({ elfs }: ElfListPageProps) => {
@@ -56,7 +56,7 @@ export default ElfListPage
export async function getStaticProps({ locale }: NextPageContext) {
return {
props: {
elfs: listElfs().map((elf) => pick(['id', 'name', 'krName'], elf)),
elfs: listElfs(locale).map((elf) => pick(['id', 'name', 'krName'], elf)),
...(await getI18NProps(locale)),
},
}
+46 -72
View File
@@ -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)
}