From 4ffefcfef20fa6d1e6cec0d8a31a85b97f4c3b62 Mon Sep 17 00:00:00 2001 From: Starknet Dev Date: Mon, 27 Jan 2025 09:25:47 +0000 Subject: [PATCH] - add check that blobert has patricipated in launch tournament - fix rerender issues --- ui/src/app/components/start/Spawn.tsx | 70 +++++++++++++++++++++------ ui/src/app/hooks/graphql/queries.ts | 22 +++++++++ 2 files changed, 77 insertions(+), 15 deletions(-) diff --git a/ui/src/app/components/start/Spawn.tsx b/ui/src/app/components/start/Spawn.tsx index 50887427d..fda7a5aad 100644 --- a/ui/src/app/components/start/Spawn.tsx +++ b/ui/src/app/components/start/Spawn.tsx @@ -10,16 +10,21 @@ import { import { TxActivity } from "@/app/components/navigation/TxActivity"; import PaymentDetails from "@/app/components/start/PaymentDetails"; import SeasonDetails from "@/app/components/start/SeasonDetails"; -import { getDeadAdventurersByXPPaginated } from "@/app/hooks/graphql/queries"; +import { + getBlobertlaimedFreeGames, + getDeadAdventurersByXPPaginated, +} from "@/app/hooks/graphql/queries"; import useCustomQuery from "@/app/hooks/useCustomQuery"; import useLoadingStore from "@/app/hooks/useLoadingStore"; import useNetworkAccount from "@/app/hooks/useNetworkAccount"; import { soundSelector, useUiSounds } from "@/app/hooks/useUiSound"; import useUIStore from "@/app/hooks/useUIStore"; +import { gameClient } from "@/app/lib/clients"; import { battle } from "@/app/lib/constants"; import { networkConfig } from "@/app/lib/networkConfig"; import { formatLords } from "@/app/lib/utils"; import { Adventurer, FormData } from "@/app/types"; +import { useQuery } from "@apollo/client"; import Image from "next/image"; import Logo from "public/icons/logo.svg"; import Lords from "public/icons/lords.svg"; @@ -112,20 +117,43 @@ export const Spawn = ({ }; const blobertTokens = blobertsData?.tokens; - const blobertTokenIds: number[] = blobertTokens?.map( - (token: any) => token.tokenId + const blobertTokenIds: number[] = blobertTokens?.map((token: any) => + Number(token.tokenId) ); + const client = useMemo(() => { + if (!network) return null; + return gameClient(networkConfig[network].lsGQLURL); + }, [network]); + + const claimedFreeGameVariables = useMemo(() => { + return { + tokenIds: blobertTokenIds, + }; + }, [blobertTokenIds]); + + const { data: claimedFreeGamesData } = useQuery(getBlobertlaimedFreeGames, { + client, + variables: claimedFreeGameVariables, + skip: !client, + }); + const getUsableBlobertToken = async (tokenIds: number[]) => { - // Loop through contract calls to see if the token is usable, if none then return 0 for (let tokenId of tokenIds) { - const canPlay = await gameContract.call( - "free_game_available", - CallData.compile(["1", tokenId.toString()]) - ); - if (canPlay) { - setUsableBlobertToken(tokenId.toString()); - break; + const hasParticipatedInLaunch = + claimedFreeGamesData?.claimedFreeGames?.some( + (freeGame: any) => freeGame.tokenId === tokenId + ); + + if (hasParticipatedInLaunch) { + const canPlay = await gameContract.call( + "free_game_available", + CallData.compile(["1", tokenId.toString()]) + ); + if (canPlay) { + setUsableBlobertToken(tokenId.toString()); + break; + } } } }; @@ -137,10 +165,22 @@ export const Spawn = ({ const seasonActive = process.env.NEXT_PUBLIC_SEASON_ACTIVE === "true"; useEffect(() => { - getUsableGoldenToken(goldenTokens ?? []); - if (tournamentEnded) { - getUsableBlobertToken(blobertTokenIds ?? []); - } + const checkTokens = async () => { + // Only check if we have new tokens to check + if (goldenTokens?.length && usableGoldenToken === "0") { + await getUsableGoldenToken(goldenTokens); + } + + if ( + tournamentEnded && + blobertTokenIds?.length && + usableBlobertToken === "0" + ) { + await getUsableBlobertToken(blobertTokenIds); + } + }; + + checkTokens(); }, [goldenTokens, blobertTokenIds]); const handlePayment = async (goldenToken: boolean, blobertToken: boolean) => { diff --git a/ui/src/app/hooks/graphql/queries.ts b/ui/src/app/hooks/graphql/queries.ts index d04ab54ac..69e980e9c 100644 --- a/ui/src/app/hooks/graphql/queries.ts +++ b/ui/src/app/hooks/graphql/queries.ts @@ -558,6 +558,27 @@ const getTournament = gql` } `; +const getBlobertlaimedFreeGames = gql` + query getBlobertlaimedFreeGames($tokenIds: [FeltValue!]) { + claimedFreeGames( + where: { + token: { + eq: "0x539f522b29ae9251dbf7443c7a950cf260372e69efab3710a11bf17a9599f1" + } + tokenId: { In: $tokenIds } + } + limit: 1000 + ) { + token + tokenId + adventurerId + gameOwnerAddress + revealed + hash + } + } +`; + export { getAdventurerById, getAdventurerCounts, @@ -570,6 +591,7 @@ export { getAliveAdventurersCount, getBattlesByBeast, getBeast, + getBlobertlaimedFreeGames, getCollectionsTotals, getDeadAdventurersByXPPaginated, getDiscoveriesAndBattlesByAdventurerPaginated,