From fd0be46e25b8d9e0aaf2264451e58be226f18614 Mon Sep 17 00:00:00 2001 From: Sakura Knoll Date: Mon, 4 Jul 2022 15:51:52 +0900 Subject: [PATCH] Refactor elfs data --- src/components/molecules/ElfCard.tsx | 12 +-- src/lib/honkai3rd/elfs.ts | 3 - src/pages/honkai3rd/elfs/[elfId].tsx | 18 ++-- src/pages/honkai3rd/elfs/index.tsx | 4 +- src/server/data/honkai3rd/elfs.ts | 118 +++++++++++---------------- 5 files changed, 57 insertions(+), 98 deletions(-) diff --git a/src/components/molecules/ElfCard.tsx b/src/components/molecules/ElfCard.tsx index 7451a8ec..c3d0e615 100644 --- a/src/components/molecules/ElfCard.tsx +++ b/src/components/molecules/ElfCard.tsx @@ -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 + elf: Pick hidden?: boolean } const ElfCard = ({ elf }: ElfCardProps) => { - const { locale } = useRouter() - const elfName = translate(locale, { 'ko-KR': elf.krName }, elf.name) - return ( { > @@ -43,7 +37,7 @@ const ElfCard = ({ elf }: ElfCardProps) => { textAlign: 'center', }} > - {elfName} + {elf.name} diff --git a/src/lib/honkai3rd/elfs.ts b/src/lib/honkai3rd/elfs.ts index e997c321..817665f3 100644 --- a/src/lib/honkai3rd/elfs.ts +++ b/src/lib/honkai3rd/elfs.ts @@ -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[]] diff --git a/src/pages/honkai3rd/elfs/[elfId].tsx b/src/pages/honkai3rd/elfs/[elfId].tsx index f487e82d..c04212f7 100644 --- a/src/pages/honkai3rd/elfs/[elfId].tsx +++ b/src/pages/honkai3rd/elfs/[elfId].tsx @@ -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 ( { { href: '/honkai3rd/elfs', label: t('common.elfs') }, { href: `/honkai3rd/elfs/${elf.id}`, - label: elfName, + label: elf.name, }, ]} /> - {elfName} + {elf.name} { as={columnIndex === 0 ? 'h2' : 'h3'} sx={{ mb: 1 }} > - {translate(locale, { 'ko-KR': item.krName }, item.name)} + {item.name} {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} ) @@ -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)) }, } diff --git a/src/pages/honkai3rd/elfs/index.tsx b/src/pages/honkai3rd/elfs/index.tsx index 42a71e5a..27aee0b6 100644 --- a/src/pages/honkai3rd/elfs/index.tsx +++ b/src/pages/honkai3rd/elfs/index.tsx @@ -12,7 +12,7 @@ import Head from '../../../components/atoms/Head' import Honkai3rdLayout from '../../../components/layouts/Honkai3rdLayout' interface ElfListPageProps { - elfs: Pick[] + elfs: Pick[] } 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)), }, } diff --git a/src/server/data/honkai3rd/elfs.ts b/src/server/data/honkai3rd/elfs.ts index c9f97830..5ebcd78a 100644 --- a/src/server/data/honkai3rd/elfs.ts +++ b/src/server/data/honkai3rd/elfs.ts @@ -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()) - -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) +}