From 91b45512eead1f74ec19d1865cba75c14cba289b Mon Sep 17 00:00:00 2001 From: Sakura Knoll Date: Mon, 4 Jul 2022 12:14:32 +0900 Subject: [PATCH] Refactor stigmata data --- src/components/molecules/StigmataCard.tsx | 19 +- src/components/molecules/StigmataSetCard.tsx | 19 +- src/components/pages/SingleStigmataPage.tsx | 66 +---- src/components/pages/StigmataSetPage.tsx | 65 +--- src/lib/honkai3rd/stigmata.ts | 5 - .../honkai3rd/battlesuits/[battlesuitId].tsx | 2 +- .../prints/battlesuits/[battlesuitId].tsx | 4 +- .../prints/stigmata/[stigmataId].tsx | 78 ++--- src/pages/honkai3rd/stigmata/[stigmataId].tsx | 20 +- src/pages/honkai3rd/stigmata/index.tsx | 12 +- src/scripts/migrations/stigmata-set.ts | 39 --- src/scripts/migrations/stigmata.ts | 41 --- src/server/data/honkai3rd/stigmata.ts | 277 ++++++++---------- 13 files changed, 187 insertions(+), 460 deletions(-) delete mode 100644 src/scripts/migrations/stigmata-set.ts delete mode 100644 src/scripts/migrations/stigmata.ts diff --git a/src/components/molecules/StigmataCard.tsx b/src/components/molecules/StigmataCard.tsx index f5410a5b..be8b2624 100644 --- a/src/components/molecules/StigmataCard.tsx +++ b/src/components/molecules/StigmataCard.tsx @@ -1,25 +1,16 @@ /** @jsxImportSource theme-ui */ import { Box, Card, Text, Flex } from '@theme-ui/components' -import { useRouter } from 'next/router' import { assetsBucketBaseUrl } from '../../lib/consts' import { StigmataData } from '../../lib/honkai3rd/stigmata' -import { translate } from '../../lib/i18n' import PageLink from '../atoms/PageLink' import SquareImageBox from '../atoms/SquareImageBox' interface StigmataCardProps { - stigmata: Pick + stigmata: Pick size?: 'sm' | 'default' } const StigmataCard = ({ stigmata, size }: StigmataCardProps) => { - const { locale } = useRouter() - const stigmataName = translate( - locale, - { 'ko-KR': stigmata.krName }, - stigmata.name - ) - if (size === 'sm') { return ( { @@ -49,7 +40,7 @@ const StigmataCard = ({ stigmata, size }: StigmataCardProps) => { textAlign: 'center', }} > - {stigmataName} + {stigmata.name} @@ -68,7 +59,7 @@ const StigmataCard = ({ stigmata, size }: StigmataCardProps) => { { textAlign: 'center', }} > - {stigmataName} + {stigmata.name} {'⭐'.repeat(stigmata.rarity)} diff --git a/src/components/molecules/StigmataSetCard.tsx b/src/components/molecules/StigmataSetCard.tsx index a860b725..e5b6115d 100644 --- a/src/components/molecules/StigmataSetCard.tsx +++ b/src/components/molecules/StigmataSetCard.tsx @@ -1,24 +1,15 @@ /** @jsxImportSource theme-ui */ import { Box, Card, Flex, Text } from '@theme-ui/components' -import { useRouter } from 'next/router' import { assetsBucketBaseUrl } from '../../lib/consts' import { StigmataSet } from '../../lib/honkai3rd/stigmata' -import { translate } from '../../lib/i18n' import PageLink from '../atoms/PageLink' import SquareImageBox from '../atoms/SquareImageBox' interface StigmataSetCardProps { - stigmataSet: Pick + stigmataSet: Pick } const StigmataSetCard = ({ stigmataSet }: StigmataSetCardProps) => { - const { locale } = useRouter() - const stigmataSetName = translate( - locale, - { 'ko-KR': stigmataSet.krName }, - stigmataSet.name - ) - return ( { @@ -54,7 +45,7 @@ const StigmataSetCard = ({ stigmataSet }: StigmataSetCardProps) => { textAlign: 'center', }} > - {stigmataSetName} + {stigmataSet.name} {'⭐'.repeat(stigmataSet.rarity)} diff --git a/src/components/pages/SingleStigmataPage.tsx b/src/components/pages/SingleStigmataPage.tsx index 09c6cc08..022cec2d 100644 --- a/src/components/pages/SingleStigmataPage.tsx +++ b/src/components/pages/SingleStigmataPage.tsx @@ -12,8 +12,7 @@ import PageLink from '../../components/atoms/PageLink' import StigmataCard from '../../components/molecules/StigmataCard' import Breadcrumb from '../../components/organisms/Breadcrumb' import SecondaryLabel from '../atoms/SecondaryLabel' -import { useRouter } from 'next/router' -import { translate, useTranslation } from '../../lib/i18n' +import { useTranslation } from '../../lib/i18n' import Head from '../atoms/Head' import { assetsBucketBaseUrl } from '../../lib/consts' import Honkai3rdLayout from '../layouts/Honkai3rdLayout' @@ -30,27 +29,12 @@ const SingleStigmataPage = ({ stigmataSet, stigmataSetList, }: SingleStigmataPageProps) => { - const { locale } = useRouter() const { t } = useTranslation() - const stigmataName = translate( - locale, - { 'ko-KR': stigmataData.krName }, - stigmataData.name - ) - - const stigmataSkillDescription = translate( - locale, - { - 'ko-KR': stigmataData.skill.krDescription, - }, - stigmataData.skill.description - ) - return ( - {stigmataName} + {stigmataData.name} {stigmataName} - {stigmataSkillDescription} + {stigmataData.skill.description} {stigmataSet != null && ( @@ -111,20 +95,10 @@ const SingleStigmataPage = ({ - {translate( - locale, - { 'ko-KR': stigmataSet.krName }, - stigmataSet.name - )} + {stigmataSet.name} - - {translate( - locale, - { 'ko-KR': stigmataSet.krAltName }, - stigmataSet.altName - )} - + {stigmataSet.altName} - {translate( - locale, - { 'ko-KR': stigmataSet.twoSetSkill.krName }, - stigmataSet.twoSetSkill.name - )} + {stigmataSet.twoSetSkill.name} - {translate( - locale, - { 'ko-KR': stigmataSet.twoSetSkill.krDescription }, - stigmataSet.twoSetSkill.description - )} + {stigmataSet.twoSetSkill.description} - {translate( - locale, - { 'ko-KR': stigmataSet.threeSetSkill.krName }, - stigmataSet.threeSetSkill.name - )} + {stigmataSet.threeSetSkill.name} - {translate( - locale, - { 'ko-KR': stigmataSet.threeSetSkill.krDescription }, - stigmataSet.threeSetSkill.description - )} + {stigmataSet.threeSetSkill.description} )} diff --git a/src/components/pages/StigmataSetPage.tsx b/src/components/pages/StigmataSetPage.tsx index 8356bf64..202086ab 100644 --- a/src/components/pages/StigmataSetPage.tsx +++ b/src/components/pages/StigmataSetPage.tsx @@ -6,8 +6,7 @@ import StigmataCard from '../../components/molecules/StigmataCard' import Breadcrumb from '../../components/organisms/Breadcrumb' import { StigmataData, StigmataSet } from '../../lib/honkai3rd/stigmata' import SecondaryLabel from '../atoms/SecondaryLabel' -import { translate, useTranslation } from '../../lib/i18n' -import { useRouter } from 'next/router' +import { useTranslation } from '../../lib/i18n' import Head from '../atoms/Head' import Honkai3rdLayout from '../layouts/Honkai3rdLayout' @@ -22,28 +21,16 @@ const StigmataSetPage = ({ stigmataSetList, }: StigmataSetProps) => { const { t } = useTranslation() - const { locale } = useRouter() - - const stigmataSetName = translate( - locale, - { 'ko-KR': stigmataSet.krName }, - stigmataSet.name - ) - const stigmataSetAltName = translate( - locale, - { 'ko-KR': stigmataSet.krAltName }, - stigmataSet.altName - ) return ( @@ -59,16 +46,16 @@ const StigmataSetPage = ({ }, { href: `/honkai3rd/stigmata/${stigmataSet.id}-set`, - label: stigmataSetName, + label: stigmataSet.name, }, ]} /> - {stigmataSetName} + {stigmataSet.name} - {stigmataSetAltName} + {stigmataSet.altName} @@ -90,16 +77,6 @@ const StigmataSetPage = ({ })} {stigmataSetList.map((stigmataSetItem) => { - const stigmataName = translate( - locale, - { 'ko-KR': stigmataSetItem.krName }, - stigmataSetItem.name - ) - const stigmataSkillDescrpition = translate( - locale, - { 'ko-KR': stigmataSetItem.skill.krDescription }, - stigmataSetItem.skill.description - ) return ( @@ -107,7 +84,7 @@ const StigmataSetPage = ({ - {stigmataName} + {stigmataSetItem.name} @@ -126,7 +103,7 @@ const StigmataSetPage = ({ '&:last-child': { borderBottom: 'none' }, }} > - {stigmataSkillDescrpition} + {stigmataSetItem.skill.description} ) @@ -135,34 +112,16 @@ const StigmataSetPage = ({ - {translate( - locale, - { 'ko-KR': stigmataSet.twoSetSkill.krName }, - stigmataSet.twoSetSkill.name - )} + {stigmataSet.twoSetSkill.name} - {translate( - locale, - { 'ko-KR': stigmataSet.twoSetSkill.krDescription }, - stigmataSet.twoSetSkill.description - )} + {stigmataSet.twoSetSkill.description} - {translate( - locale, - { - 'ko-KR': stigmataSet.threeSetSkill.krName, - }, - stigmataSet.threeSetSkill.name - )} + {stigmataSet.threeSetSkill.name} - {translate( - locale, - { 'ko-KR': stigmataSet.threeSetSkill.krDescription }, - stigmataSet.threeSetSkill.description - )} + {stigmataSet.threeSetSkill.description} diff --git a/src/lib/honkai3rd/stigmata.ts b/src/lib/honkai3rd/stigmata.ts index e450e939..3d17ea80 100644 --- a/src/lib/honkai3rd/stigmata.ts +++ b/src/lib/honkai3rd/stigmata.ts @@ -1,14 +1,11 @@ export interface StigmataSkill { name: string - krName?: string description: string - krDescription?: string } export interface StigmataData { id: string name: string - krName?: string skill: StigmataSkill atk: number def: number @@ -24,9 +21,7 @@ export interface StigmataData { export interface StigmataSet { id: string name: string - krName?: string altName: string - krAltName?: string twoSetSkill: StigmataSkill threeSetSkill: StigmataSkill rarity: 3 | 4 | 5 diff --git a/src/pages/honkai3rd/battlesuits/[battlesuitId].tsx b/src/pages/honkai3rd/battlesuits/[battlesuitId].tsx index 85dc0a9d..f10780f1 100644 --- a/src/pages/honkai3rd/battlesuits/[battlesuitId].tsx +++ b/src/pages/honkai3rd/battlesuits/[battlesuitId].tsx @@ -295,7 +295,7 @@ export async function getStaticProps({ ) const weaponMap = getWeaponMapByIds(weaponIds, locale) - const stigmataMap = getStigmataMapByIds(stigmataIds) + const stigmataMap = getStigmataMapByIds(stigmataIds, locale) return { props: { diff --git a/src/pages/honkai3rd/prints/battlesuits/[battlesuitId].tsx b/src/pages/honkai3rd/prints/battlesuits/[battlesuitId].tsx index a5ce0d37..0920bf6d 100644 --- a/src/pages/honkai3rd/prints/battlesuits/[battlesuitId].tsx +++ b/src/pages/honkai3rd/prints/battlesuits/[battlesuitId].tsx @@ -229,9 +229,9 @@ export async function getStaticProps({ }, { weaponIds: [], stigmataIds: [] } ) - const weaponMap = getWeaponMapByIds(weaponIds) + const weaponMap = getWeaponMapByIds(weaponIds, locale) - const stigmataMap = getStigmataMapByIds(stigmataIds) + const stigmataMap = getStigmataMapByIds(stigmataIds, locale) return { props: { diff --git a/src/pages/honkai3rd/prints/stigmata/[stigmataId].tsx b/src/pages/honkai3rd/prints/stigmata/[stigmataId].tsx index 21149808..3afece0a 100644 --- a/src/pages/honkai3rd/prints/stigmata/[stigmataId].tsx +++ b/src/pages/honkai3rd/prints/stigmata/[stigmataId].tsx @@ -6,47 +6,35 @@ import { StigmataSetProps } from '../../../../components/pages/StigmataSetPage' import { generateI18NPaths, getI18NProps } from '../../../../server/i18n' import { listStigmata, - getStigmataById, getStigmataListBySetId, getStigmataSetBySetId, listStigmataSet, + getStigmaById, } from '../../../../server/data/honkai3rd/stigmata' import { Box, Card, Flex, Heading, Paragraph } from 'theme-ui' import SecondaryLabel from '../../../../components/atoms/SecondaryLabel' import StigmataCard from '../../../../components/molecules/StigmataCard' -import { translate, useTranslation } from '../../../../lib/i18n' -import { useRouter } from 'next/router' +import { useTranslation } from '../../../../lib/i18n' import { SingleStigmataPageProps } from '../../../../components/pages/SingleStigmataPage' type StigmataShowPageProps = StigmataSetProps | SingleStigmataPageProps const StigmataListPage = (props: StigmataShowPageProps) => { const { t } = useTranslation() - const { locale } = useRouter() if (props.type === 'single') { return null } const { stigmataSet, stigmataSetList } = props - const stigmataSetName = translate( - locale, - { 'ko-KR': stigmataSet.krName }, - stigmataSet.name - ) - const stigmataSetAltName = translate( - locale, - { 'ko-KR': stigmataSet.krAltName }, - stigmataSet.altName - ) return ( - {stigmataSetName} + {stigmataSet.name} - {stigmataSetAltName} + {stigmataSet.altName} @@ -68,22 +56,12 @@ const StigmataListPage = (props: StigmataShowPageProps) => { })} {stigmataSetList.map((stigmataSetItem) => { - const stigmataName = translate( - locale, - { 'ko-KR': stigmataSetItem.krName }, - stigmataSetItem.name - ) - const stigmataSkillDescrpition = translate( - locale, - { 'ko-KR': stigmataSetItem.skill.krDescription }, - stigmataSetItem.skill.description - ) return ( - {stigmataName} + {stigmataSetItem.name} @@ -102,7 +80,7 @@ const StigmataListPage = (props: StigmataShowPageProps) => { '&:last-child': { borderBottom: 'none' }, }} > - {stigmataSkillDescrpition} + {stigmataSetItem.skill.description} ) @@ -111,34 +89,16 @@ const StigmataListPage = (props: StigmataShowPageProps) => { - {translate( - locale, - { 'ko-KR': stigmataSet.twoSetSkill.krName }, - stigmataSet.twoSetSkill.name - )} + {stigmataSet.twoSetSkill.name} - {translate( - locale, - { 'ko-KR': stigmataSet.twoSetSkill.krDescription }, - stigmataSet.twoSetSkill.description - )} + {stigmataSet.twoSetSkill.description} - {translate( - locale, - { - 'ko-KR': stigmataSet.threeSetSkill.krName, - }, - stigmataSet.threeSetSkill.name - )} + {stigmataSet.threeSetSkill.name} - {translate( - locale, - { 'ko-KR': stigmataSet.threeSetSkill.krDescription }, - stigmataSet.threeSetSkill.description - )} + {stigmataSet.threeSetSkill.description} @@ -151,29 +111,29 @@ export async function getStaticProps({ params, locale, }: NextPageContext & { params: { stigmataId: string } }) { - const stigmataData = getStigmataById(params.stigmataId)! + const stigmataData = getStigmaById(params.stigmataId, locale)! if (stigmataData != null) { return { props: { type: 'single', stigmataData: stigmataData, - stigmataSetList: (getStigmataListBySetId(stigmataData.set!) || []).sort( - (a, b) => -a.type.localeCompare(b.type) - ), - stigmataSet: getStigmataSetBySetId(stigmataData.set!) || null, + stigmataSetList: ( + getStigmataListBySetId(stigmataData.set!, locale) || [] + ).sort((a, b) => -a.type.localeCompare(b.type)), + stigmataSet: getStigmataSetBySetId(stigmataData.set!, locale) || null, ...(await getI18NProps(locale)), }, } } const [stigmataSetId] = params.stigmataId.split('-set') - const stigmataSet = getStigmataSetBySetId(stigmataSetId)! + const stigmataSet = getStigmataSetBySetId(stigmataSetId, locale)! return { props: { type: 'set', stigmataSet, - stigmataSetList: (getStigmataListBySetId(stigmataSet.id) || []).sort( - (a, b) => -a.type.localeCompare(b.type) - ), + stigmataSetList: ( + getStigmataListBySetId(stigmataSet.id, locale) || [] + ).sort((a, b) => -a.type.localeCompare(b.type)), ...(await getI18NProps(locale)), }, } diff --git a/src/pages/honkai3rd/stigmata/[stigmataId].tsx b/src/pages/honkai3rd/stigmata/[stigmataId].tsx index cb109e52..3dbc627e 100644 --- a/src/pages/honkai3rd/stigmata/[stigmataId].tsx +++ b/src/pages/honkai3rd/stigmata/[stigmataId].tsx @@ -9,7 +9,7 @@ import StigmataSetPage, { import { generateI18NPaths, getI18NProps } from '../../../server/i18n' import { listStigmata, - getStigmataById, + getStigmaById, getStigmataListBySetId, getStigmataSetBySetId, listStigmataSet, @@ -31,29 +31,29 @@ export async function getStaticProps({ params, locale, }: NextPageContext & { params: { stigmataId: string } }) { - const stigmataData = getStigmataById(params.stigmataId)! + const stigmataData = getStigmaById(params.stigmataId, locale)! if (stigmataData != null) { return { props: { type: 'single', stigmataData: stigmataData, - stigmataSetList: (getStigmataListBySetId(stigmataData.set!) || []).sort( - (a, b) => -a.type.localeCompare(b.type) - ), - stigmataSet: getStigmataSetBySetId(stigmataData.set!) || null, + stigmataSetList: ( + getStigmataListBySetId(stigmataData.set!, locale) || [] + ).sort((a, b) => -a.type.localeCompare(b.type)), + stigmataSet: getStigmataSetBySetId(stigmataData.set!, locale) || null, ...(await getI18NProps(locale)), }, } } const [stigmataSetId] = params.stigmataId.split('-set') - const stigmataSet = getStigmataSetBySetId(stigmataSetId)! + const stigmataSet = getStigmataSetBySetId(stigmataSetId, locale)! return { props: { type: 'set', stigmataSet, - stigmataSetList: (getStigmataListBySetId(stigmataSet.id) || []).sort( - (a, b) => -a.type.localeCompare(b.type) - ), + stigmataSetList: ( + getStigmataListBySetId(stigmataSet.id, locale) || [] + ).sort((a, b) => -a.type.localeCompare(b.type)), ...(await getI18NProps(locale)), }, } diff --git a/src/pages/honkai3rd/stigmata/index.tsx b/src/pages/honkai3rd/stigmata/index.tsx index 4e840b3a..09835433 100644 --- a/src/pages/honkai3rd/stigmata/index.tsx +++ b/src/pages/honkai3rd/stigmata/index.tsx @@ -19,8 +19,8 @@ import Head from '../../../components/atoms/Head' import Honkai3rdLayout from '../../../components/layouts/Honkai3rdLayout' interface StigmataListPageProps { - stigmataDataList: Pick[] - stigmataSetList: Pick[] + stigmataDataList: Pick[] + stigmataSetList: Pick[] } const StigmataListPage = ({ @@ -102,11 +102,11 @@ export default StigmataListPage export async function getStaticProps({ locale }: NextPageContext) { return { props: { - stigmataDataList: listStigmata().map((stigmata) => - pick(['id', 'name', 'rarity', 'krName'], stigmata) + stigmataDataList: listStigmata(locale).map((stigmata) => + pick(['id', 'name', 'rarity'], stigmata) ), - stigmataSetList: listStigmataSet().map((stigmataSet) => - pick(['id', 'name', 'rarity', 'krName'], stigmataSet) + stigmataSetList: listStigmataSet(locale).map((stigmataSet) => + pick(['id', 'name', 'rarity'], stigmataSet) ), ...(await getI18NProps(locale)), }, diff --git a/src/scripts/migrations/stigmata-set.ts b/src/scripts/migrations/stigmata-set.ts deleted file mode 100644 index 8fd1295a..00000000 --- a/src/scripts/migrations/stigmata-set.ts +++ /dev/null @@ -1,39 +0,0 @@ -import { runScript } from '../lib/utils' -import fs from 'fs' -import path from 'path' -import yaml from 'yaml' -import { listStigmataSet } from '../../server/data/honkai3rd/stigmata' -import sortKeys from 'sort-keys' - -const order = [ - 'id', - 'version', - 'name', - 'krName', - 'altName', - 'krAltName', - 'type', - 'rarity', - 'twoSetSkill', - 'threeSetSkill', - 'description', - 'krDescription', -] - -async function migrateStigmataSets() { - const stigmataSetList = listStigmataSet() - const newDataDir = path.join(__dirname, '../../../data/honkai3rd') - - const sortedData = sortKeys(stigmataSetList, { - deep: true, - compare: (a, b) => { - return order.indexOf(a) - order.indexOf(b) - }, - }) - fs.writeFileSync( - path.join(newDataDir, 'stigmata-sets.yaml'), - yaml.stringify(sortedData) - ) -} - -runScript(migrateStigmataSets) diff --git a/src/scripts/migrations/stigmata.ts b/src/scripts/migrations/stigmata.ts deleted file mode 100644 index 38c48931..00000000 --- a/src/scripts/migrations/stigmata.ts +++ /dev/null @@ -1,41 +0,0 @@ -import { runScript } from '../lib/utils' -import fs from 'fs' -import path from 'path' -import yaml from 'yaml' -import { listStigmata } from '../../server/data/honkai3rd/stigmata' -import sortKeys from 'sort-keys' - -const order = [ - 'id', - 'version', - 'name', - 'krName', - 'type', - 'rarity', - 'hp', - 'atk', - 'def', - 'crt', - 'set', - 'skill', - 'description', - 'krDescription', -] - -async function migrateStigmata() { - const stigmataList = listStigmata() - const newDataDir = path.join(__dirname, '../../../data/honkai3rd') - - const sortedData = sortKeys(stigmataList, { - deep: true, - compare: (a, b) => { - return order.indexOf(a) - order.indexOf(b) - }, - }) - fs.writeFileSync( - path.join(newDataDir, 'stigmata.yaml'), - yaml.stringify(sortedData) - ) -} - -runScript(migrateStigmata) diff --git a/src/server/data/honkai3rd/stigmata.ts b/src/server/data/honkai3rd/stigmata.ts index 4cf87894..02781742 100644 --- a/src/server/data/honkai3rd/stigmata.ts +++ b/src/server/data/honkai3rd/stigmata.ts @@ -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()) - -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()) + 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()) - -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, - }, - } -}