Skip to content

Commit

Permalink
fix: icons for other languages can now be loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
lgou2w committed Jul 13, 2024
1 parent aa84ea5 commit 4ce46bf
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 29 deletions.
51 changes: 26 additions & 25 deletions src/components/gacha/GachaItemView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import Typography from '@mui/material/Typography'
import GenshinUIRarity3Background from '@/assets/images/genshin/UI_Rarity_3_Background.png'
import GenshinUIRarity4Background from '@/assets/images/genshin/UI_Rarity_4_Background.png'
import GenshinUIRarity5Background from '@/assets/images/genshin/UI_Rarity_5_Background.png'
import { ItemTypeCategoryMappings } from './ItemTypeCategoryMappings'
import { lookupAssetIcon } from './icons'
import dayjs from '@/utilities/dayjs'

export interface GachaItemViewProps {
facet: AccountFacet
name: string
id: string
lang: string
itemName: string
itemId: undefined | null | string
itemType: string
rank: 3 | 4 | 5 | '3' | '4' | '5'
size: number
Expand All @@ -22,47 +24,46 @@ export interface GachaItemViewProps {
}

export default function GachaItemView (props: GachaItemViewProps) {
const { facet, name, id, itemType, rank, size, usedPity, restricted, time } = props

const category = ItemTypeCategoryMappings[itemType]
const icon = lookupAssetIcon(facet, category, id)

let src = icon?.[1]
if (!src) {
src = getRemoteResourceSrc(facet, category, id)
}
const { facet, lang, itemName, itemId, itemType, rank, size, usedPity, restricted, time } = props
const iconSrc = resolveItemIconSrc(facet, lang, itemName, itemId, itemType)

const title = !time
? name
: name + '\n' + dayjs(time).format('LLLL')
? itemName
: itemName + '\n' + dayjs(time).format('LLLL')

return (
<Box className={GachaItemViewCls} sx={GachaItemViewSx}
width={size} height={size}
data-facet={facet}
data-rank={rank}
data-restricted={restricted}
data-category={category}
title={title}
>
<img src={src} alt={name} />
<img src={iconSrc} alt={itemName} />
{usedPity && <Typography className={`${GachaItemViewCls}-used-pity`}>{usedPity}</Typography>}
{restricted && <Typography className={`${GachaItemViewCls}-restricted`}>限定</Typography>}
</Box>
)
}

const ItemTypeCategoryMappings: Record<string, 'character' | 'weapon' | 'bangboo'> = {
角色: 'character',
武器: 'weapon',
光锥: 'weapon',
代理人: 'character',
音擎: 'weapon',
邦布: 'bangboo'
}
function resolveItemIconSrc (facet: AccountFacet, lang: string, itemName: string, itemId: string | undefined | null, itemType: string): string | undefined {
const category = ItemTypeCategoryMappings[facet][lang]?.[itemType]
if (!category) {
console.error('Unable to resolve item icon category:', facet, lang, itemType, itemName, itemId)
return undefined
}

// HACK: GenshinImpact embedded icons are only available for zh-cn language, go to Remote Loading
if (facet === AccountFacet.Genshin && lang !== 'zh-cn') {
return `https://hoyo-gacha.lgou2w.com/static/${facet}/${lang}/${category}/cutted/${itemName}.png`
}

function getRemoteResourceSrc (facet: AccountFacet, category: string, itemIdOrName: string) {
return `https://hoyo-gacha.lgou2w.com/static/${facet}/${category}/cutted/${itemIdOrName}.png`
const embedded = lookupAssetIcon(facet, category, itemId || itemName)
if (embedded) {
return embedded[1]
} else {
return `https://hoyo-gacha.lgou2w.com/static/${facet}/${category}/cutted/${itemId}.png`
}
}

const GachaItemViewCls = 'gacha-item-view'
Expand Down
60 changes: 60 additions & 0 deletions src/components/gacha/ItemTypeCategoryMappings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* eslint-disable object-shorthand */
/* eslint-disable quote-props */

import { AccountFacet } from '@/interfaces/account'

export const Character = 'character'
export const Weapon = 'weapon'
export const Bangboo = 'bangboo'

export type Category = typeof Character | typeof Weapon | typeof Bangboo

export const ItemTypeCategoryMappings: Record<AccountFacet, Record<string, Record<string, Category>>> = {
[AccountFacet.Genshin]: {
'de-de': { 'Figur': Character, 'Waffe': Weapon },
'en-us': { 'Character': Character, 'Weapon': Weapon },
'es-es': { 'Personaje': Character, 'Arma': Weapon },
'fr-fr': { 'Personnages': Character, 'Arme': Weapon },
'id-id': { 'Karakter': Character, 'Senjata': Weapon },
'it-it': { 'Personaggio': Character, 'Armi': Weapon },
'ja-jp': { 'キャラクター': Character, '武器': Weapon },
'ko-kr': { '캐릭터': Character, '무기': Weapon },
'pt-pt': { 'Personagem': Character, 'Arma': Weapon },
'ru-ru': { 'Персонаж': Character, 'Оружие': Weapon },
'th-th': { 'ตัวละคร': Character, 'อาวุธ': Weapon },
'tr-tr': { 'Karakter': Character, 'Silahlar': Weapon },
'vi-vn': { 'Nhân vật': Character, 'Vũ Khí': Weapon },
'zh-cn': { '角色': Character, '武器': Weapon },
'zh-tw': { '角色': Character, '武器': Weapon }
},
[AccountFacet.StarRail]: {
'de-de': { 'Figuren': Character, 'Lichtkegel': Weapon },
'en-us': { 'Character': Character, 'Light Cone': Weapon },
'es-es': { 'Personajes': Character, 'Conos de luz': Weapon },
'fr-fr': { 'Personnages': Character, 'Cônes de lumière': Weapon },
'id-id': { 'Karakter': Character, 'Light Cone': Weapon },
'ja-jp': { 'キャラクター': Character, '光円錐': Weapon },
'ko-kr': { '캐릭터': Character, '광추': Weapon },
'pt-pt': { 'Personagens': Character, 'Cones de Luz': Weapon },
'ru-ru': { 'Персонажи': Character, 'Световые конусы': Weapon },
'th-th': { 'ตัวละคร': Character, 'Light Cone': Weapon },
'vi-vn': { 'Nhân Vật': Character, 'Nón Ánh Sáng': Weapon },
'zh-cn': { '角色': Character, '光锥': Weapon },
'zh-tw': { '角色': Character, '光錐': Weapon }
},
[AccountFacet.ZenlessZoneZero]: {
'de-de': { 'Agenten': Character, 'W-Motoren': Weapon, 'Bangboos': Bangboo },
'en-us': { 'Agents': Character, 'W-Engines': Weapon, 'Bangboo': Bangboo },
'es-es': { 'Agentes': Character, 'Amplificadores': Weapon, 'Bangbús': Bangboo },
'fr-fr': { 'Agents': Character, 'Moteurs-amplis': Weapon, 'Bangbous': Bangboo },
'id-id': { 'Agen': Character, 'W-Engine': Weapon, 'Bangboo': Bangboo },
'ja-jp': { 'エージェント': Character, '音動機': Weapon, 'ボンプ': Bangboo },
'ko-kr': { '에이전트': Character, 'W-엔진': Weapon, '「Bangboo」': Bangboo },
'pt-pt': { 'Agentes': Character, 'Motores-W': Weapon, 'Bangboos': Bangboo },
'ru-ru': { 'Агенты': Character, 'Амплификаторы': Weapon, 'Банбу': Bangboo },
'th-th': { 'Agent': Character, 'W-Engine': Weapon, 'Bangboo': Bangboo },
'vi-vn': { 'Người Đại Diện': Character, 'W-Engine': Weapon, 'Bangboo': Bangboo },
'zh-cn': { '代理人': Character, '音擎': Weapon, '邦布': Bangboo },
'zh-tw': { '代理人': Character, '音擎': Weapon, '邦布': Bangboo }
}
}
5 changes: 3 additions & 2 deletions src/components/gacha/analysis/GachaAnalysisHistory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ function GachaAnalysisHistoryList ({ facet, value }: {
<GachaItemView
facet={facet}
key={item.id}
name={item.name}
id={item.item_id || item.name}
lang={item.lang}
itemName={item.name}
itemId={item.item_id}
itemType={item.item_type}
rank={5}
size={GachaAnalysisHistoryItemViewSize}
Expand Down
5 changes: 3 additions & 2 deletions src/components/gacha/overview/GachaOverviewGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,9 @@ function GachaOverviewGridCard ({ facet, value, newbie }: {
<GachaItemView
facet={facet}
key={lastGolden.id}
name={lastGolden.name}
id={lastGolden.item_id || lastGolden.name}
lang={lastGolden.lang}
itemName={lastGolden.name}
itemId={lastGolden.item_id}
itemType={lastGolden.item_type}
rank={5}
size={72}
Expand Down

0 comments on commit 4ce46bf

Please sign in to comment.