Skip to content

Commit

Permalink
feat: add support zenless
Browse files Browse the repository at this point in the history
  • Loading branch information
lgou2w committed Jul 4, 2024
1 parent 9f81fc7 commit fa5bac2
Show file tree
Hide file tree
Showing 13 changed files with 88 additions and 92 deletions.
8 changes: 7 additions & 1 deletion src-tauri/src/gacha/impl_genshin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use super::{
GameDataDirectoryFinder,
};
use crate::error::Result;
use crate::storage::entity_account::AccountFacet;
use async_trait::async_trait;
use reqwest::Client as Reqwest;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -114,7 +115,12 @@ impl GachaRecordFetcher for GenshinGacha {
end_id: Option<&str>,
) -> Result<Option<Vec<Self::Target>>> {
let response = fetch_gacha_records::<GenshinGachaRecordPagination>(
reqwest, ENDPOINT, gacha_url, gacha_type, end_id,
reqwest,
&AccountFacet::Genshin,
ENDPOINT,
gacha_url,
gacha_type,
end_id,
)
.await?;

Expand Down
8 changes: 7 additions & 1 deletion src-tauri/src/gacha/impl_starrail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use super::{
GameDataDirectoryFinder,
};
use crate::error::Result;
use crate::storage::entity_account::AccountFacet;
use async_trait::async_trait;
use reqwest::Client as Reqwest;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -116,7 +117,12 @@ impl GachaRecordFetcher for StarRailGacha {
end_id: Option<&str>,
) -> Result<Option<Vec<Self::Target>>> {
let response = fetch_gacha_records::<StarRailGachaRecordPagination>(
reqwest, ENDPOINT, gacha_url, gacha_type, end_id,
reqwest,
&AccountFacet::StarRail,
ENDPOINT,
gacha_url,
gacha_type,
end_id,
)
.await?;

Expand Down
8 changes: 7 additions & 1 deletion src-tauri/src/gacha/impl_zzz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use super::{
GameDataDirectoryFinder,
};
use crate::error::Result;
use crate::storage::entity_account::AccountFacet;
use async_trait::async_trait;
use reqwest::Client as Reqwest;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -116,7 +117,12 @@ impl GachaRecordFetcher for ZenlessZoneZeroGacha {
end_id: Option<&str>,
) -> Result<Option<Vec<Self::Target>>> {
let response = fetch_gacha_records::<ZenlessZoneZeroGachaRecordPagination>(
reqwest, ENDPOINT, gacha_url, gacha_type, end_id,
reqwest,
&AccountFacet::ZenlessZoneZero,
ENDPOINT,
gacha_url,
gacha_type,
end_id,
)
.await?;

Expand Down
14 changes: 11 additions & 3 deletions src-tauri/src/gacha/utilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ pub(super) struct GachaResponse<T> {

pub(super) async fn fetch_gacha_records<T: Sized + DeserializeOwned + Send>(
reqwest: &Reqwest,
facet: &AccountFacet,
endpoint: &str,
gacha_url: &str,
gacha_type: Option<&str>,
Expand All @@ -266,14 +267,21 @@ pub(super) async fn fetch_gacha_records<T: Sized + DeserializeOwned + Send>(
.into_owned()
.collect();

let gacha_type_field: &'static str = if facet == &AccountFacet::ZenlessZoneZero {
"real_gacha_type"
} else {
"gacha_type"
};

let origin_gacha_type = queries
.get("gacha_type")
.get(gacha_type_field)
.cloned()
.ok_or(Error::IllegalGachaUrl)?;

let origin_end_id = queries.get("end_id").cloned();
let gacha_type = gacha_type.unwrap_or(&origin_gacha_type);

queries.remove("gacha_type");
queries.remove(gacha_type_field);
queries.remove("page");
queries.remove("size");
queries.remove("begin_id");
Expand All @@ -285,7 +293,7 @@ pub(super) async fn fetch_gacha_records<T: Sized + DeserializeOwned + Send>(
.query_pairs_mut()
.append_pair("page", "1")
.append_pair("size", "20")
.append_pair("gacha_type", gacha_type);
.append_pair(gacha_type_field, gacha_type);

if let Some(end_id) = end_id.or(origin_end_id.as_deref()) {
url.query_pairs_mut().append_pair("end_id", end_id);
Expand Down
17 changes: 13 additions & 4 deletions src/components/account/AccountMenuDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,9 @@ function AccountMenuDialogForm (props: AccountMenuDialogFormProps) {

const onSubmit = React.useCallback<SubmitHandler<IFormInput>>(async (data) => {
const uid = Number(data.uid)
if (uid < 1_0000_0000) {

const isZZZ = facet === AccountFacet.ZenlessZoneZero
if ((isZZZ && uid < 10_000_000) || (!isZZZ && uid < 100_000_000)) {
setError('uid', { message: '请输入正确的 UID 值!' })
return
}
Expand All @@ -227,7 +229,7 @@ function AccountMenuDialogForm (props: AccountMenuDialogFormProps) {
} finally {
setBusy(false)
}
}, [setBusy, onSuccess, setError, isEdit, handleCreateAccount, handleUpdateAccount])
}, [facet, setBusy, onSuccess, setError, isEdit, handleCreateAccount, handleUpdateAccount])

return (
<form id={id} onSubmit={handleSubmit(onSubmit)} autoComplete="off" noValidate>
Expand All @@ -241,7 +243,13 @@ function AccountMenuDialogForm (props: AccountMenuDialogFormProps) {
InputProps={{
...register('uid', {
required: '请填写账号 UID 字段!',
validate: value => /^[1-9][0-9]{8}$/.test(value) || '请输入正确的 UID 值!'
validate: value => {
const isZZZ = facet === AccountFacet.ZenlessZoneZero
return (isZZZ
? +value >= 10_000_000
: /^[1-9][0-9]{8}$/.test(value)
) || '请输入正确的 UID 值!'
}
}),
onKeyPress: numericOnly,
startAdornment: (
Expand Down Expand Up @@ -311,7 +319,8 @@ function AccountMenuDialogForm (props: AccountMenuDialogFormProps) {

const FacetGameDataDirExamples: Record<AccountFacet, string> = {
[AccountFacet.Genshin]: 'D:/Genshin Impact/Genshin Impact Game/YuanShen_Data',
[AccountFacet.StarRail]: 'D:/StarRail/Game/StarRail_Data'
[AccountFacet.StarRail]: 'D:/StarRail/Game/StarRail_Data',
[AccountFacet.ZenlessZoneZero]: 'D:/ZenlessZoneZero Game/ZenlessZoneZero_Data'
}

function numericOnly (evt: React.KeyboardEvent<HTMLElement>) {
Expand Down
2 changes: 1 addition & 1 deletion src/components/gacha/analysis/GachaAnalysisHistory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function GachaAnalysisHistoryList ({ facet, value }: {
key={item.id}
name={item.name}
id={item.item_id || item.name}
isWeapon={item.item_type === '武器' || item.item_type === '光锥'}
isWeapon={item.item_type === '武器' || item.item_type === '光锥' || item.item_type === '音擎'}
rank={5}
size={GachaAnalysisHistoryItemViewSize}
usedPity={item.usedPity}
Expand Down
3 changes: 2 additions & 1 deletion src/components/gacha/icons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,8 @@ const StarRail = {

const Assets = {
genshin: Genshin,
starrail: StarRail
starrail: StarRail,
zzz: {} // TODO: wait a minute
}

export function lookupAssetIcon (
Expand Down
2 changes: 1 addition & 1 deletion src/components/gacha/overview/GachaOverviewGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ function GachaOverviewGridCard ({ facet, value, newbie }: {
key={lastGolden.id}
name={lastGolden.name}
id={lastGolden.item_id || lastGolden.name}
isWeapon={lastGolden.item_type === '武器' || lastGolden.item_type === '光锥'}
isWeapon={lastGolden.item_type === '武器' || lastGolden.item_type === '光锥' || lastGolden.item_type === '音擎'}
rank={5}
size={72}
usedPity={lastGolden.usedPity}
Expand Down
2 changes: 1 addition & 1 deletion src/components/gacha/overview/GachaOverviewTags.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default function GachaOverviewTags () {

return {
luck: luck ? { name: luck.name, total: luck.usedPity } : null,
unluck: unluck ? { name: unluck.name, total: unluck.usedPity } : null,
unluck: aggregatedValues.metadata.golden.sum > 1 && unluck ? { name: unluck.name, total: unluck.usedPity } : null,
related: related && related[1].length > 1 ? { name: related[0], total: related[1].length } : null,
crazy: crazy ? { name: crazy.day, total: crazy.value } : null
}
Expand Down
6 changes: 5 additions & 1 deletion src/components/gacha/toolbar/GachaActionFetch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default function GachaActionFetch () {

const { facet, uid, gachaUrl } = selectedAccount
try {
const { namedValues: { character, weapon, permanent, newbie, anthology } } = gachaRecords
const { namedValues: { character, weapon, permanent, newbie, anthology, bangboo } } = gachaRecords
const pullNewbie = shouldPullNewbie(facet, newbie)
const fragments = await pull(facet, uid, {
gachaUrl,
Expand All @@ -44,6 +44,7 @@ export default function GachaActionFetch () {
[weapon.gachaType]: weapon.lastEndId ?? null,
[permanent.gachaType]: permanent.lastEndId ?? null,
...(anthology ? { [anthology.gachaType]: anthology.lastEndId ?? null } : {}),
...(bangboo ? { [bangboo.gachaType]: bangboo.lastEndId ?? null } : {}),
...(pullNewbie || {})
},
eventChannel: 'gachaRecords-fetcher-event-channel',
Expand Down Expand Up @@ -150,10 +151,13 @@ function shouldPullNewbie (
// HACK:
// Genshin Impact : Newbie Gacha Pool = 20 times
// Honkai: Star Rail : = 50 times
// Zenless Zone Zero : Useless
if (facet === AccountFacet.Genshin && newbie.total >= 20) {
return null
} else if (facet === AccountFacet.StarRail && newbie.total >= 50) {
return null
} else if (facet === AccountFacet.ZenlessZoneZero) {
return null
} else {
return {
[newbie.gachaType]: newbie.lastEndId ?? null
Expand Down
12 changes: 7 additions & 5 deletions src/components/gacha/toolbar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,19 @@ export interface GachaToolbarProps {
}

export default function GachaToolbar (props: GachaToolbarProps) {
const { ActionTabsProps } = props
const { facet, ActionTabsProps } = props
return (
<Stack direction="row" gap={2}>
<GachaActionUrl />
<GachaActionFetch />
<Stack direction="row" gap={3} marginLeft="auto">
<GachaActionTabs {...ActionTabsProps} />
<Stack direction="row" gap={1}>
<GachaActionImport />
<GachaActionExport />
</Stack>
{facet !== AccountFacet.ZenlessZoneZero && (
<Stack direction="row" gap={1}>
<GachaActionImport />
<GachaActionExport />
</Stack>
)}
</Stack>
</Stack>
)
Expand Down
92 changes: 23 additions & 69 deletions src/hooks/useGachaRecordsQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,60 +64,6 @@ const gachaRecordsQueryFn: FetchQueryOptions<GachaRecords | null>['queryFn'] = a
}

const rawGachaRecords: GachaRecord[] = await PluginStorage.findGachaRecords(facet, { uid })

// FIXME: FAKE TEST
if (facet === AccountFacet.ZenlessZoneZero) {
const fake = []
for (let i = 0; i < 35; i++) {
fake.push({
id: i.toString(),
uid: '100_000_000',
gacha_id: 'idk',
gacha_type: '2001',
item_id: 'idk',
count: '1',
time: '2024-07-02 12:00:00',
name: i.toString(),
lang: 'zh-cn',
item_type: i > 0 && i % 10 === 0 ? '代理人' : '音擎',
rank_type: i > 0 && i % 10 === 0 ? '4' : '3'
})
}

rawGachaRecords.push(...[
...fake,
{
id: (fake.length + 1).toString(),
uid: '100_000_000',
gacha_id: 'idk',
gacha_type: '2001',
item_id: 'idk',
count: '1',
time: '2024-07-02 12:00:00',
name: '艾莲',
lang: 'zh-cn',
item_type: '代理人',
rank_type: '5'
}
])
rawGachaRecords.push(...[
{
id: (rawGachaRecords.length + 1).toString(),
uid: '100_000_000',
gacha_id: 'idk',
gacha_type: '5001',
item_id: 'idk',
count: '1',
time: '2024-07-02 12:00:00',
name: '邦布',
lang: 'zh-cn',
item_type: '邦布',
rank_type: '5'
}
])
}
//

return computeGachaRecords(facet, uid, rawGachaRecords)
}

Expand Down Expand Up @@ -213,11 +159,11 @@ const KnownStarRailGachaTypes: Record<StarRailGachaRecord['gacha_type'], NamedGa

const KnownZenlessZoneZeroGachaTypes: Record<ZenlessZoneZeroGachaRecord['gacha_type'], NamedGachaRecords['category']> = {
0: 'newbie', // Avoid undefined metadata
1001: 'permanent',
2001: 'character',
3001: 'weapon',
4001: 'bangboo', // TODO: deprecated?
5001: 'bangboo'
1: 'permanent',
2: 'character',
3: 'weapon',
4: 'bangboo', // TODO: deprecated?
5: 'bangboo'
}

const KnownCategoryTitles: Record<AccountFacet, Record<NamedGachaRecords['category'], string>> = {
Expand Down Expand Up @@ -247,9 +193,13 @@ const KnownCategoryTitles: Record<AccountFacet, Record<NamedGachaRecords['catego
}
}

const isRankTypeOfBlue = (record: GachaRecord) => record.rank_type === '3'
const isRankTypeOfPurple = (record: GachaRecord) => record.rank_type === '4'
const isRankTypeOfGolden = (record: GachaRecord) => record.rank_type === '5'
const isRankTypeOfBlue = (facet: AccountFacet, record: GachaRecord) =>
record.rank_type === (facet === AccountFacet.ZenlessZoneZero ? '2' : '3')
const isRankTypeOfPurple = (facet: AccountFacet, record: GachaRecord) =>
record.rank_type === (facet === AccountFacet.ZenlessZoneZero ? '3' : '4')
const isRankTypeOfGolden = (facet: AccountFacet, record: GachaRecord) =>
record.rank_type === (facet === AccountFacet.ZenlessZoneZero ? '4' : '5')

const sortGachaRecordById = (a: GachaRecord, b: GachaRecord) => a.id.localeCompare(b.id)

function concatNamedGachaRecordsValues (
Expand Down Expand Up @@ -300,8 +250,8 @@ function computeNamedGachaRecords (
const firstTime = data[0]?.time
const lastTime = data[total - 1]?.time
const metadata: NamedGachaRecords['metadata'] = {
blue: computeGachaRecordsMetadata(total, data.filter(isRankTypeOfBlue)),
purple: computeGachaRecordsMetadata(total, data.filter(isRankTypeOfPurple)),
blue: computeGachaRecordsMetadata(total, data.filter((v) => isRankTypeOfBlue(facet, v))),
purple: computeGachaRecordsMetadata(total, data.filter((v) => isRankTypeOfPurple(facet, v))),
golden: computeGoldenGachaRecordsMetadata(facet, data)
}

Expand Down Expand Up @@ -338,7 +288,7 @@ function computeAggregatedGachaRecords (
(anthology ? anthology.metadata.blue.sum : 0)

const blueSumPercentage = blueSum > 0 ? Math.round(blueSum / total * 10000) / 100 : 0
const blueValues = data.filter(isRankTypeOfBlue)
const blueValues = data.filter((v) => isRankTypeOfBlue(facet, v))

const purpleSum =
newbie.metadata.purple.sum +
Expand All @@ -348,7 +298,7 @@ function computeAggregatedGachaRecords (
(anthology ? anthology.metadata.purple.sum : 0)

const purpleSumPercentage = purpleSum > 0 ? Math.round(purpleSum / total * 10000) / 100 : 0
const purpleValues = data.filter(isRankTypeOfPurple)
const purpleValues = data.filter((v) => isRankTypeOfPurple(facet, v))

const goldenSum =
newbie.metadata.golden.sum +
Expand Down Expand Up @@ -431,7 +381,7 @@ function computeGoldenGachaRecordsMetadata (
let sumRestricted = 0

for (const record of values) {
const isGolden = isRankTypeOfGolden(record)
const isGolden = isRankTypeOfGolden(facet, record)
pity += 1

if (isGolden) {
Expand Down Expand Up @@ -472,8 +422,7 @@ function isRestrictedGolden (
case AccountFacet.StarRail:
return !KnownStarRailPermanentGoldenItemIds.includes(record.item_id)
case AccountFacet.ZenlessZoneZero:
// FIXME: TODO zzz isRestrictedGolden
return false
return !KnownZenlessZoneZeroPermanentGoldenItemIds.includes(record.item_id)
default:
throw new Error(`Unknown facet: ${facet}`)
}
Expand All @@ -492,3 +441,8 @@ const KnownStarRailPermanentGoldenItemIds: string[] = [
'1003', '1004', '1101', '1104', '1107', '1209', '1211',
'23000', '23002', '23003', '23004', '23005', '23012', '23013'
]

// TODO: wait a minute
const KnownZenlessZoneZeroPermanentGoldenItemIds: string[] = [
'1211'
]
Loading

0 comments on commit fa5bac2

Please sign in to comment.