Skip to content

Commit

Permalink
update position to graphql
Browse files Browse the repository at this point in the history
  • Loading branch information
RedBeardEth committed Dec 15, 2024
1 parent 5dc1d58 commit 4b4972f
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 21 deletions.
19 changes: 11 additions & 8 deletions landing/src/components/modules/bridge-in.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import { calculateDonkeysNeeded, getSeasonAddresses, getTotalResourceWeight } fr
import { BridgeFees } from "./bridge-fees";

export const BridgeIn = () => {

const { address } = useAccount();
const [realmEntityId, setRealmEntityId] = useState<number>();

Expand All @@ -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<number[]>([ResourcesIds.Lords]);
Expand Down Expand Up @@ -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]);

Expand Down Expand Up @@ -294,9 +298,7 @@ export const BridgeIn = () => {
<div>Time to Transfer</div>
<div>{travelTimeInHoursAndMinutes(travelTime ?? 0)}</div>
</div>
<div
className={"flex justify-between mb-3 " + (donkeysNeeded > donkeyBalance ? "text-destructive" : "")}
>
<div className={"flex justify-between mb-3 " + (donkeysNeeded > donkeyBalance ? "text-destructive" : "")}>
<div>
Donkeys Burnt
<TooltipProvider>
Expand All @@ -312,7 +314,8 @@ export const BridgeIn = () => {
</TooltipProvider>
</div>
<div className="flex items-center gap-2">
{donkeysNeeded} / {isResourcesLoading ? <Loader className="animate-spin pr-2" /> : divideByPrecision(donkeyBalance)}{" "}
{donkeysNeeded} /{" "}
{isResourcesLoading ? <Loader className="animate-spin pr-2" /> : divideByPrecision(donkeyBalance)}{" "}
<ResourceIcon withTooltip={false} resource={"Donkey"} size="md" />
</div>
</div>
Expand Down
5 changes: 5 additions & 0 deletions landing/src/hooks/gql/gql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.
*/
Expand Down
24 changes: 24 additions & 0 deletions landing/src/hooks/gql/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}>;
Expand Down Expand Up @@ -8248,6 +8256,22 @@ export const GetEternumOwnerRealmIdsDocument = new TypedDocumentString(`
}
}
`) as unknown as TypedDocumentString<GetEternumOwnerRealmIdsQuery, GetEternumOwnerRealmIdsQueryVariables>;
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<GetEntityPositionQuery, GetEntityPositionQueryVariables>;
export const GetAccountTokensDocument = new TypedDocumentString(`
query getAccountTokens($accountAddress: String!) {
tokenBalances(accountAddress: $accountAddress, limit: 8000) {
Expand Down
18 changes: 18 additions & 0 deletions landing/src/hooks/query/position.tsx
Original file line number Diff line number Diff line change
@@ -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
}
}
}
}
}
`);
27 changes: 14 additions & 13 deletions landing/src/hooks/useTravel.tsx
Original file line number Diff line number Diff line change
@@ -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);
Expand Down

0 comments on commit 4b4972f

Please sign in to comment.