From 4b4972f5e26f1a3bf722eae1022a55863477fdb1 Mon Sep 17 00:00:00 2001 From: RedBeardEth <90423049+RedBeardEth@users.noreply.github.com> Date: Sun, 15 Dec 2024 15:00:58 +1100 Subject: [PATCH] update position to graphql --- landing/src/components/modules/bridge-in.tsx | 19 ++++++++------ landing/src/hooks/gql/gql.ts | 5 ++++ landing/src/hooks/gql/graphql.ts | 24 +++++++++++++++++ landing/src/hooks/query/position.tsx | 18 +++++++++++++ landing/src/hooks/useTravel.tsx | 27 ++++++++++---------- 5 files changed, 72 insertions(+), 21 deletions(-) create mode 100644 landing/src/hooks/query/position.tsx diff --git a/landing/src/components/modules/bridge-in.tsx b/landing/src/components/modules/bridge-in.tsx index 7e55a492c..baffd197e 100644 --- a/landing/src/components/modules/bridge-in.tsx +++ b/landing/src/components/modules/bridge-in.tsx @@ -26,7 +26,6 @@ import { calculateDonkeysNeeded, getSeasonAddresses, getTotalResourceWeight } fr import { BridgeFees } from "./bridge-fees"; export const BridgeIn = () => { - const { address } = useAccount(); const [realmEntityId, setRealmEntityId] = useState(); @@ -46,7 +45,12 @@ export const BridgeIn = () => { totalFee?: string; }[] >([]); - const { computeTravelTime } = useTravel(); + const { computeTravelTime } = useTravel( + Number(ADMIN_BANK_ENTITY_ID), + Number(realmEntityId!), + configManager.getSpeedConfig(DONKEY_ENTITY_TYPE), + true, + ); const { getRealmNameById } = useRealm(); const [isLoading, setIsLoading] = useState(false); const [selectedResourceIds, setSelectedResourceIds] = useState([ResourcesIds.Lords]); @@ -120,10 +124,10 @@ export const BridgeIn = () => { const donkeyBalance = useMemo(() => { if (realmEntityId) { return resourceBalances?.s0EternumResourceModels?.edges?.find( - (edge) => edge?.node?.resource_type === ResourcesIds.Donkey + (edge) => edge?.node?.resource_type === ResourcesIds.Donkey, )?.node?.balance; } else { - return 0 ; + return 0; } }, [resourceBalances, realmEntityId]); @@ -294,9 +298,7 @@ export const BridgeIn = () => {
Time to Transfer
{travelTimeInHoursAndMinutes(travelTime ?? 0)}
-
donkeyBalance ? "text-destructive" : "")} - > +
donkeyBalance ? "text-destructive" : "")}>
Donkeys Burnt @@ -312,7 +314,8 @@ export const BridgeIn = () => {
- {donkeysNeeded} / {isResourcesLoading ? : divideByPrecision(donkeyBalance)}{" "} + {donkeysNeeded} /{" "} + {isResourcesLoading ? : divideByPrecision(donkeyBalance)}{" "}
diff --git a/landing/src/hooks/gql/gql.ts b/landing/src/hooks/gql/gql.ts index 358a0b08d..35c14db28 100644 --- a/landing/src/hooks/gql/gql.ts +++ b/landing/src/hooks/gql/gql.ts @@ -16,6 +16,7 @@ import * as types from './graphql'; */ const documents = { "\n query getEternumOwnerRealmIds($accountAddress: ContractAddress!) {\n s0EternumOwnerModels(where: { address: $accountAddress }) {\n edges {\n node {\n address\n entity_id\n entity {\n models {\n __typename\n ... on s0_eternum_Realm {\n realm_id\n }\n }\n }\n }\n }\n }\n }\n": types.GetEternumOwnerRealmIdsDocument, + "\n query getEntityPosition($fromEntityId: u32!, $toEntityId: u32!) {\n s0EternumPositionModels(where: { entity_idIN: [$fromEntityId, $toEntityId] }) {\n edges {\n node {\n x\n y\n entity_id\n entity {\n __typename\n }\n }\n }\n }\n }\n": types.GetEntityPositionDocument, "\n query getAccountTokens($accountAddress: String!) {\n tokenBalances(accountAddress: $accountAddress, limit: 8000) {\n edges {\n node {\n tokenMetadata {\n __typename\n ... on ERC721__Token {\n tokenId\n metadataDescription\n imagePath\n contractAddress\n metadata\n }\n }\n }\n }\n }\n }\n": types.GetAccountTokensDocument, "\n query getERC721Mints {\n tokenTransfers(accountAddress: \"0x0\", limit: 8000) {\n edges {\n node {\n tokenMetadata {\n __typename\n ... on ERC721__Token {\n tokenId\n metadataDescription\n imagePath\n contractAddress\n metadata\n }\n }\n }\n }\n }\n }\n": types.GetErc721MintsDocument, "\n query getEntityResources($entityId: u32!) {\n s0EternumResourceModels(where: { entity_id: $entityId }) {\n edges {\n node {\n resource_type\n balance\n entity {\n __typename\n }\n }\n }\n }\n }\n": types.GetEntityResourcesDocument, @@ -25,6 +26,10 @@ const documents = { * The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients. */ export function graphql(source: "\n query getEternumOwnerRealmIds($accountAddress: ContractAddress!) {\n s0EternumOwnerModels(where: { address: $accountAddress }) {\n edges {\n node {\n address\n entity_id\n entity {\n models {\n __typename\n ... on s0_eternum_Realm {\n realm_id\n }\n }\n }\n }\n }\n }\n }\n"): typeof import('./graphql').GetEternumOwnerRealmIdsDocument; +/** + * The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients. + */ +export function graphql(source: "\n query getEntityPosition($fromEntityId: u32!, $toEntityId: u32!) {\n s0EternumPositionModels(where: { entity_idIN: [$fromEntityId, $toEntityId] }) {\n edges {\n node {\n x\n y\n entity_id\n entity {\n __typename\n }\n }\n }\n }\n }\n"): typeof import('./graphql').GetEntityPositionDocument; /** * The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients. */ diff --git a/landing/src/hooks/gql/graphql.ts b/landing/src/hooks/gql/graphql.ts index 6c5f731a1..dffa0c6a4 100644 --- a/landing/src/hooks/gql/graphql.ts +++ b/landing/src/hooks/gql/graphql.ts @@ -8194,6 +8194,14 @@ export type GetEternumOwnerRealmIdsQueryVariables = Exact<{ export type GetEternumOwnerRealmIdsQuery = { __typename?: 'World__Query', s0EternumOwnerModels?: { __typename?: 's0_eternum_OwnerConnection', edges?: Array<{ __typename?: 's0_eternum_OwnerEdge', node?: { __typename?: 's0_eternum_Owner', address?: any | null, entity_id?: any | null, entity?: { __typename?: 'World__Entity', models?: Array<{ __typename: 'darkshuffle_Battle' } | { __typename: 'darkshuffle_BattleEffects' } | { __typename: 'darkshuffle_Board' } | { __typename: 'darkshuffle_Donation' } | { __typename: 'darkshuffle_Draft' } | { __typename: 'darkshuffle_Game' } | { __typename: 'darkshuffle_GameEffects' } | { __typename: 'darkshuffle_Leaderboard' } | { __typename: 'darkshuffle_Map' } | { __typename: 'darkshuffle_PlayerReward' } | { __typename: 'darkshuffle_Season' } | { __typename: 's0_eternum_AcceptOrder' } | { __typename: 's0_eternum_AcceptPartialOrder' } | { __typename: 's0_eternum_AddressName' } | { __typename: 's0_eternum_Army' } | { __typename: 's0_eternum_ArrivalTime' } | { __typename: 's0_eternum_Bank' } | { __typename: 's0_eternum_BankConfig' } | { __typename: 's0_eternum_Battle' } | { __typename: 's0_eternum_BattleClaimData' } | { __typename: 's0_eternum_BattleConfig' } | { __typename: 's0_eternum_BattleJoinData' } | { __typename: 's0_eternum_BattleLeaveData' } | { __typename: 's0_eternum_BattlePillageData' } | { __typename: 's0_eternum_BattleStartData' } | { __typename: 's0_eternum_Building' } | { __typename: 's0_eternum_BuildingCategoryPopConfig' } | { __typename: 's0_eternum_BuildingConfig' } | { __typename: 's0_eternum_BuildingGeneralConfig' } | { __typename: 's0_eternum_BuildingQuantityv2' } | { __typename: 's0_eternum_BurnDonkey' } | { __typename: 's0_eternum_CancelOrder' } | { __typename: 's0_eternum_CapacityCategory' } | { __typename: 's0_eternum_CapacityConfig' } | { __typename: 's0_eternum_Contribution' } | { __typename: 's0_eternum_CreateGuild' } | { __typename: 's0_eternum_CreateOrder' } | { __typename: 's0_eternum_DetachedResource' } | { __typename: 's0_eternum_EntityName' } | { __typename: 's0_eternum_EntityOwner' } | { __typename: 's0_eternum_Epoch' } | { __typename: 's0_eternum_FragmentMineDiscovered' } | { __typename: 's0_eternum_GameEnded' } | { __typename: 's0_eternum_Guild' } | { __typename: 's0_eternum_GuildMember' } | { __typename: 's0_eternum_GuildWhitelist' } | { __typename: 's0_eternum_Health' } | { __typename: 's0_eternum_Hyperstructure' } | { __typename: 's0_eternum_HyperstructureCoOwnersChange' } | { __typename: 's0_eternum_HyperstructureConfig' } | { __typename: 's0_eternum_HyperstructureContribution' } | { __typename: 's0_eternum_HyperstructureFinished' } | { __typename: 's0_eternum_HyperstructureResourceConfig' } | { __typename: 's0_eternum_HyperstructureStarted' } | { __typename: 's0_eternum_JoinGuild' } | { __typename: 's0_eternum_Leaderboard' } | { __typename: 's0_eternum_LeaderboardEntry' } | { __typename: 's0_eternum_LeaderboardRegisterContribution' } | { __typename: 's0_eternum_LeaderboardRegisterShare' } | { __typename: 's0_eternum_LeaderboardRegistered' } | { __typename: 's0_eternum_LeaderboardRewardClaimed' } | { __typename: 's0_eternum_LevelingConfig' } | { __typename: 's0_eternum_Liquidity' } | { __typename: 's0_eternum_LiquidityEvent' } | { __typename: 's0_eternum_MapConfig' } | { __typename: 's0_eternum_MapExplored' } | { __typename: 's0_eternum_Market' } | { __typename: 's0_eternum_MercenariesConfig' } | { __typename: 's0_eternum_Message' } | { __typename: 's0_eternum_Movable' } | { __typename: 's0_eternum_Orders' } | { __typename: 's0_eternum_OwnedResourcesTracker' } | { __typename: 's0_eternum_Owner' } | { __typename: 's0_eternum_Population' } | { __typename: 's0_eternum_PopulationConfig' } | { __typename: 's0_eternum_Position' } | { __typename: 's0_eternum_Production' } | { __typename: 's0_eternum_ProductionConfig' } | { __typename: 's0_eternum_ProductionDeadline' } | { __typename: 's0_eternum_ProductionInput' } | { __typename: 's0_eternum_ProductionOutput' } | { __typename: 's0_eternum_Progress' } | { __typename: 's0_eternum_Protectee' } | { __typename: 's0_eternum_Protector' } | { __typename: 's0_eternum_Quantity' } | { __typename: 's0_eternum_QuantityTracker' } | { __typename: 's0_eternum_Quest' } | { __typename: 's0_eternum_QuestBonus' } | { __typename: 's0_eternum_QuestConfig' } | { __typename: 's0_eternum_QuestRewardConfig' } | { __typename: 's0_eternum_Realm', realm_id?: any | null } | { __typename: 's0_eternum_RealmLevelConfig' } | { __typename: 's0_eternum_RealmMaxLevelConfig' } | { __typename: 's0_eternum_Resource' } | { __typename: 's0_eternum_ResourceAllowance' } | { __typename: 's0_eternum_ResourceBridgeConfig' } | { __typename: 's0_eternum_ResourceBridgeFeeSplitConfig' } | { __typename: 's0_eternum_ResourceBridgeWhitelistConfig' } | { __typename: 's0_eternum_ResourceCost' } | { __typename: 's0_eternum_ResourceTransferLock' } | { __typename: 's0_eternum_Season' } | { __typename: 's0_eternum_SeasonAddressesConfig' } | { __typename: 's0_eternum_SeasonBridgeConfig' } | { __typename: 's0_eternum_SettleRealmData' } | { __typename: 's0_eternum_SettlementConfig' } | { __typename: 's0_eternum_SpeedConfig' } | { __typename: 's0_eternum_Stamina' } | { __typename: 's0_eternum_StaminaConfig' } | { __typename: 's0_eternum_StaminaRefillConfig' } | { __typename: 's0_eternum_Status' } | { __typename: 's0_eternum_Structure' } | { __typename: 's0_eternum_StructureCount' } | { __typename: 's0_eternum_SwapEvent' } | { __typename: 's0_eternum_TickConfig' } | { __typename: 's0_eternum_Tile' } | { __typename: 's0_eternum_Trade' } | { __typename: 's0_eternum_Transfer' } | { __typename: 's0_eternum_Travel' } | { __typename: 's0_eternum_TravelFoodCostConfig' } | { __typename: 's0_eternum_TravelStaminaCostConfig' } | { __typename: 's0_eternum_TroopConfig' } | { __typename: 's0_eternum_TrophyCreation' } | { __typename: 's0_eternum_TrophyProgression' } | { __typename: 's0_eternum_VRFConfig' } | { __typename: 's0_eternum_Weight' } | { __typename: 's0_eternum_WeightConfig' } | { __typename: 's0_eternum_WorldConfig' } | null> | null } | null } | null } | null> | null } | null }; +export type GetEntityPositionQueryVariables = Exact<{ + fromEntityId: Scalars['u32']['input']; + toEntityId: Scalars['u32']['input']; +}>; + + +export type GetEntityPositionQuery = { __typename?: 'World__Query', s0EternumPositionModels?: { __typename?: 's0_eternum_PositionConnection', edges?: Array<{ __typename?: 's0_eternum_PositionEdge', node?: { __typename?: 's0_eternum_Position', x?: any | null, y?: any | null, entity_id?: any | null, entity?: { __typename: 'World__Entity' } | null } | null } | null> | null } | null }; + export type GetAccountTokensQueryVariables = Exact<{ accountAddress: Scalars['String']['input']; }>; @@ -8248,6 +8256,22 @@ export const GetEternumOwnerRealmIdsDocument = new TypedDocumentString(` } } `) as unknown as TypedDocumentString; +export const GetEntityPositionDocument = new TypedDocumentString(` + query getEntityPosition($fromEntityId: u32!, $toEntityId: u32!) { + s0EternumPositionModels(where: {entity_idIN: [$fromEntityId, $toEntityId]}) { + edges { + node { + x + y + entity_id + entity { + __typename + } + } + } + } +} + `) as unknown as TypedDocumentString; export const GetAccountTokensDocument = new TypedDocumentString(` query getAccountTokens($accountAddress: String!) { tokenBalances(accountAddress: $accountAddress, limit: 8000) { diff --git a/landing/src/hooks/query/position.tsx b/landing/src/hooks/query/position.tsx new file mode 100644 index 000000000..ec47e0311 --- /dev/null +++ b/landing/src/hooks/query/position.tsx @@ -0,0 +1,18 @@ +import { graphql } from "../gql"; + +export const GET_ENTITY_DISTANCE = graphql(` + query getEntityPosition($fromEntityId: u32!, $toEntityId: u32!) { + s0EternumPositionModels(where: { entity_idIN: [$fromEntityId, $toEntityId] }) { + edges { + node { + x + y + entity_id + entity { + __typename + } + } + } + } + } +`); diff --git a/landing/src/hooks/useTravel.tsx b/landing/src/hooks/useTravel.tsx index 4249efda1..8a2077f7e 100644 --- a/landing/src/hooks/useTravel.tsx +++ b/landing/src/hooks/useTravel.tsx @@ -1,24 +1,25 @@ import { ID } from "@bibliothecadao/eternum"; -import { getComponentValue } from "@dojoengine/recs"; -import { getEntityIdFromKeys } from "@dojoengine/utils"; +import { useQuery } from "@tanstack/react-query"; import { calculateDistance } from "../components/ui/utils/utils"; -import { useDojo } from "./context/DojoContext"; +import { execute } from "./gql/execute"; +import { GET_ENTITY_DISTANCE } from "./query/position"; -export function useTravel() { - const { - setup: { - components: { Position }, - }, - } = useDojo(); +export function useTravel(fromId: ID, toId: ID, secPerKm: number, pickup?: boolean) { + const { data: entityPositions, isLoading } = useQuery({ + queryKey: ["entityResources", fromId, toId], + queryFn: () => execute(GET_ENTITY_DISTANCE, { fromEntityId: fromId, toEntityId: toId }), + refetchInterval: 10_000, + }); + const computeTravelTime = (fromId: ID, toId: ID, secPerKm: number, pickup?: boolean) => { - const fromPosition = getComponentValue(Position, getEntityIdFromKeys([BigInt(fromId)])); - const toPosition = getComponentValue(Position, getEntityIdFromKeys([BigInt(toId)])); + const fromPosition = entityPositions?.s0EternumPositionModels?.edges?.find((entity)=> entity?.node?.entity_id == fromId); + const toPosition = entityPositions?.s0EternumPositionModels?.edges?.find((entity)=> entity?.node?.entity_id == toId); if (!fromPosition || !toPosition) return; const distanceFromPosition = calculateDistance( - { x: Number(fromPosition.x), y: Number(fromPosition.y) }, - { x: Number(toPosition.x), y: Number(toPosition.y) }, + { x: Number(fromPosition?.node?.x), y: Number(fromPosition?.node?.y) }, + { x: Number(toPosition?.node?.x), y: Number(toPosition?.node?.y) }, ) ?? 0; const onewayTime = Math.floor((distanceFromPosition * secPerKm) / 60);