diff --git a/web/plugins/com.msgbyte.genshin/src/GenshinPanel/GachaPool.tsx b/web/plugins/com.msgbyte.genshin/src/GenshinPanel/GachaPool.tsx index 43c09e07..8bc50223 100644 --- a/web/plugins/com.msgbyte.genshin/src/GenshinPanel/GachaPool.tsx +++ b/web/plugins/com.msgbyte.genshin/src/GenshinPanel/GachaPool.tsx @@ -1,8 +1,16 @@ -import { useAsync } from '@capital/common'; -import { Divider } from '@capital/component'; -import React from 'react'; -import { OfficialGachaPoolItem, util } from 'genshin-gacha-kit'; +import { useAsync, showToasts } from '@capital/common'; +import { Divider, Button } from '@capital/component'; +import React, { useCallback, useMemo, useState } from 'react'; +import { + AppGachaItem, + AppWishResult, + GenshinGachaKit, + OfficialGachaPool, + OfficialGachaPoolItem, + util, +} from 'genshin-gacha-kit'; import { GenshinRichtext } from '../components/GenshinRichtext'; +import { getAppGachaItemText } from './utils'; const GachaPoolItem: React.FC<{ items: OfficialGachaPoolItem[]; @@ -21,12 +29,51 @@ const GachaPoolItem: React.FC<{ }); GachaPoolItem.displayName = 'GachaPoolItem'; +const WishResult: React.FC<{ items: AppGachaItem[] }> = React.memo( + ({ items }) => { + return {items.map(getAppGachaItemText).join(',')}; + } +); +WishResult.displayName = 'WishResult'; + +function useWish(poolData: OfficialGachaPool) { + const [gachaResult, setGachaResult] = useState({ + ssr: [], + sr: [], + r: [], + }); + const [gachaCount, setGachaCount] = useState(0); + + const gachaKit = useMemo(() => { + return poolData + ? new GenshinGachaKit(util.poolStructureConverter(poolData)) + : null; + }, [poolData]); + const handleGacha = useCallback( + (num) => { + if (!gachaKit) { + return; + } + + const res = gachaKit.multiWish(num); + showToasts('抽卡结果: ' + res.map((item) => item.name).join(',')); + + setGachaCount(gachaKit.getCounter('total') as number); + setGachaResult(JSON.parse(JSON.stringify(gachaKit.getResult()))); + }, + [gachaKit] + ); + + return { handleGacha, gachaResult, gachaCount }; +} + export const GachaPool: React.FC<{ gachaId: string; }> = React.memo((props) => { const { value: poolData } = useAsync(() => { return util.getGachaData(props.gachaId); }, [props.gachaId]); + const { handleGacha, gachaResult, gachaCount } = useWish(poolData); if (!poolData) { return
Loading...
; @@ -42,6 +89,31 @@ export const GachaPool: React.FC<{ +
+ + +
+ + {gachaCount > 0 && ( +
+
已抽: {gachaCount} 次
+ +
+ 5星: +
+
+ 4星: +
+
+ 3星: +
+
+ )} + diff --git a/web/plugins/com.msgbyte.genshin/src/GenshinPanel/utils.ts b/web/plugins/com.msgbyte.genshin/src/GenshinPanel/utils.ts new file mode 100644 index 00000000..d6fe3801 --- /dev/null +++ b/web/plugins/com.msgbyte.genshin/src/GenshinPanel/utils.ts @@ -0,0 +1,9 @@ +import type { AppGachaItem } from 'genshin-gacha-kit'; + +export function getAppGachaItemText(item: AppGachaItem) { + if (item.count >= 2) { + return `${item.name}(${item.count})`; + } else { + return item.name; + } +}