Skip to content

Commit

Permalink
hs cost (#2443)
Browse files Browse the repository at this point in the history
* hs cost

* fix deposit arrival

* update lock
  • Loading branch information
aymericdelab authored Dec 12, 2024
1 parent e7c3dee commit 9fc09d6
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 283 deletions.
12 changes: 9 additions & 3 deletions client/src/dojo/setup.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
BUILDING_CATEGORY_POPULATION_CONFIG_ID,
WORLD_CONFIG_ID
HYPERSTRUCTURE_CONFIG_ID,
WORLD_CONFIG_ID,
} from "@bibliothecadao/eternum";
import { DojoConfig } from "@dojoengine/core";
import { getSyncEntities, getSyncEvents, syncEntities } from "@dojoengine/state";
Expand Down Expand Up @@ -48,8 +49,13 @@ export async function setup({ ...config }: DojoConfig) {
models: [],
},
},


{
Keys: {
keys: [HYPERSTRUCTURE_CONFIG_ID.toString(), undefined],
pattern_matching: "VariableLen",
models: [],
},
},
];

// fetch all existing entities from torii
Expand Down
31 changes: 29 additions & 2 deletions client/src/ui/components/entities/Entity.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { addToSubscription } from "@/dojo/queries";
import { useDojo } from "@/hooks/context/DojoContext";
import { ArrivalInfo } from "@/hooks/helpers/use-resource-arrivals";
import { getArmyByEntityId } from "@/hooks/helpers/useArmies";
Expand All @@ -10,7 +11,7 @@ import { divideByPrecision, formatTime, getEntityIdFromKeys } from "@/ui/utils/u
import { EntityType } from "@bibliothecadao/eternum";
import { useComponentValue } from "@dojoengine/react";
import clsx from "clsx";
import React, { useMemo } from "react";
import React, { useEffect, useMemo, useState } from "react";
import { DepositResources } from "../resources/DepositResources";

const entityIcon: Record<EntityType, string> = {
Expand All @@ -32,6 +33,8 @@ type EntityProps = {
export const EntityArrival = ({ arrival, ...props }: EntityProps) => {
const dojo = useDojo();

const [isSyncing, setIsSyncing] = useState(false);

const { getEntityInfo, getEntityName } = useEntitiesUtils();
const { getResourcesFromBalance } = useResourcesUtils();
const nextBlockTimestamp = useUIStore.getState().nextBlockTimestamp;
Expand All @@ -45,6 +48,26 @@ export const EntityArrival = ({ arrival, ...props }: EntityProps) => {
return getResourcesFromBalance(arrival.entityId);
}, [weight]);

useEffect(() => {
if (entityResources.length === 0) {
setIsSyncing(true);
const fetch = async () => {
try {
await addToSubscription(
dojo.network.toriiClient,
dojo.network.contractComponents as any,
arrival.entityId.toString(),
);
} catch (error) {
console.error("Fetch failed", error);
} finally {
setIsSyncing(false);
}
};
fetch();
}
}, [arrival.entityId, dojo.network.toriiClient, dojo.network.contractComponents, entityResources.length]);

const army = useMemo(() => getArmy(arrival.entityId), [arrival.entityId, entity.resources]);

const renderEntityStatus = useMemo(() => {
Expand All @@ -62,6 +85,10 @@ export const EntityArrival = ({ arrival, ...props }: EntityProps) => {
}, [nextBlockTimestamp, arrival.recipientEntityId, arrival.hasResources, entity.arrivalTime]);

const renderedResources = useMemo(() => {
if (isSyncing) {
return <div className="text-gold/50 italic">Syncing resources...</div>;
}

return entityResources
.filter(Boolean)
.map((resource) => (
Expand All @@ -74,7 +101,7 @@ export const EntityArrival = ({ arrival, ...props }: EntityProps) => {
amount={divideByPrecision(resource.amount)}
/>
));
}, [entityResources]);
}, [entityResources, isSyncing]);

const name = entity.entityType === EntityType.TROOP ? army?.name : entityName[entity.entityType];

Expand Down
Loading

0 comments on commit 9fc09d6

Please sign in to comment.