-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
173 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { Flex, Text } from '@chakra-ui/react'; | ||
import React from 'react'; | ||
import { ItemData, ItemMMEData } from '../../types'; | ||
import CardBase from '../Card/CardBase'; | ||
import ItemCard from './ItemCard'; | ||
import { useTranslations } from 'next-intl'; | ||
|
||
type Props = { | ||
item: ItemData; | ||
mmeData: ItemMMEData; | ||
}; | ||
|
||
const MMECard = (props: Props) => { | ||
const t = useTranslations(); | ||
const { item, mmeData } = props; | ||
const color = item.color.rgb; | ||
|
||
return ( | ||
<CardBase title={`${mmeData.name} Info`} color={color}> | ||
<Flex gap={3} wrap="wrap" alignItems="center" flexFlow={'column'}> | ||
<Text fontSize={'sm'} textAlign={'center'}> | ||
{t.rich('ItemPage.mme-text', { | ||
b: (children) => <b>{children}</b>, | ||
name: mmeData.name, | ||
isMini: mmeData.isMini, | ||
})} | ||
</Text> | ||
<Flex wrap="wrap" gap={2} alignItems="center" justifyContent={'center'}> | ||
{Object.keys(mmeData.trails).map((trail) => ( | ||
<Flex | ||
key={trail} | ||
direction="column" | ||
gap={2} | ||
bg="blackAlpha.500" | ||
p={3} | ||
borderRadius={'md'} | ||
> | ||
<Text> | ||
<b> | ||
{t('ItemPage.mme-trail', { | ||
x: trail.toUpperCase(), | ||
})} | ||
</b> | ||
</Text> | ||
<Flex wrap="wrap" gap={2} justifyContent={'center'}> | ||
<ItemCard item={mmeData.initial} small /> | ||
{mmeData.trails[trail].map((i) => ( | ||
<ItemCard key={i.internal_id} item={i} small /> | ||
))} | ||
</Flex> | ||
</Flex> | ||
))} | ||
</Flex> | ||
<Text fontSize={'sm'} textAlign={'center'}> | ||
{t.rich('ItemPage.mme-chance', { | ||
b: (children) => <b>{children}</b>, | ||
})} | ||
</Text> | ||
<ItemCard item={mmeData.bonus} small isLE /> | ||
</Flex> | ||
</CardBase> | ||
); | ||
}; | ||
|
||
export default MMECard; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { NextApiRequest, NextApiResponse } from 'next'; | ||
import { getItem } from '.'; | ||
import { doSearch } from '../../search'; | ||
import { ItemData, ItemMMEData } from '../../../../../types'; | ||
|
||
const mmeRegex = /^(mini)?(MME)(\d+)/i; | ||
|
||
export default async function handle(req: NextApiRequest, res: NextApiResponse) { | ||
if (req.method === 'GET') return GET(req, res); | ||
|
||
if (req.method == 'OPTIONS') { | ||
res.setHeader('Access-Control-Allow-Methods', 'GET'); | ||
return res.status(200).json({}); | ||
} | ||
|
||
return res.status(405).json({ error: 'Method not allowed' }); | ||
} | ||
|
||
async function GET(req: NextApiRequest, res: NextApiResponse) { | ||
const result = await getMMEData(req.query.id_name as string); | ||
|
||
return res.status(200).json(result); | ||
} | ||
|
||
export const getMMEData = async (id_name: string | number): Promise<ItemMMEData | null> => { | ||
const internal_id = Number(id_name); | ||
const name = isNaN(internal_id) ? (id_name as string) : undefined; | ||
|
||
const item = await getItem(name ?? internal_id); | ||
if (!item) return null; | ||
|
||
if (!isMME(item.name)) return null; | ||
const mmeName = item.name.match(mmeRegex)![0]; | ||
console.log('MME Name:', mmeName); | ||
|
||
const search = await doSearch(mmeName); | ||
|
||
if (!search) return null; | ||
const mmeItems = search.content.filter((i) => i.name.match(mmeRegex)?.[0] === mmeName); | ||
const firstOne = mmeItems.find((i) => i.name.toLowerCase().includes('s1')); | ||
const bonus = mmeItems.find((i) => i.name.toLowerCase().includes(mmeName.toLowerCase() + '-b')); | ||
|
||
if (!firstOne || !bonus) return null; | ||
|
||
const trails: { [name: string]: ItemData[] } = {}; | ||
const allTrails: ItemData[] = []; | ||
for (const mmeItem of mmeItems) { | ||
if (mmeItem.name === firstOne.name || mmeItem.name === bonus.name) continue; | ||
|
||
const trailName = mmeItem.name.match(/(?<=S\d)[a-z]/gim)?.[0]; | ||
if (!trailName) { | ||
allTrails.push(mmeItem); | ||
console.error('No trail name found for', mmeItem.name); | ||
continue; | ||
} | ||
|
||
if (!trails[trailName]) trails[trailName] = []; | ||
trails[trailName].push(mmeItem); | ||
} | ||
|
||
Object.keys(trails).forEach((key) => { | ||
trails[key].push(...allTrails); | ||
trails[key] = trails[key].sort((a, b) => a.name.localeCompare(b.name)); | ||
}); | ||
|
||
if (Object.keys(trails).length === 0) { | ||
trails['A'] = allTrails; | ||
} | ||
|
||
return { | ||
name: mmeName, | ||
isMini: mmeName.toLowerCase().includes('mini'), | ||
initial: firstOne, | ||
bonus, | ||
trails, | ||
}; | ||
}; | ||
|
||
export const isMME = (name: string) => !!name.match(mmeRegex); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters