Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/next' into bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
RedBeardEth committed Dec 14, 2024
2 parents cd6a13a + c57642b commit ef44514
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 99 deletions.
4 changes: 2 additions & 2 deletions client/.env.preview
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ VITE_PUBLIC_MASTER_PRIVATE_KEY=0x075362a844768f31c8058ce31aec3dd7751686440b4f220
VITE_PUBLIC_WORLD_ADDRESS="0x00fd85ef42eaed3b90d02d2cdc7417d6cae189ff4ba876aa5608551afbf1fb47"
VITE_PUBLIC_ACCOUNT_CLASS_HASH="0x07dc7899aa655b0aae51eadff6d801a58e97dd99cf4666ee59e704249e51adf2"
VITE_PUBLIC_FEE_TOKEN_ADDRESS=0x49d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7
VITE_PUBLIC_TORII=https://api.cartridge.gg/x/sepolia-rc-17/torii
VITE_PUBLIC_TORII=https://api.cartridge.gg/x/sepolia-rc-18/torii
VITE_PUBLIC_NODE_URL=https://api.cartridge.gg/x/starknet/sepolia
VITE_PUBLIC_DEV=false
VITE_PUBLIC_GAME_VERSION="v1.0.0-rc7"
VITE_PUBLIC_SHOW_FPS=false
VITE_PUBLIC_GRAPHICS_DEV=false
VITE_PUBLIC_TORII_RELAY=/dns4/api.cartridge.gg/tcp/443/x-parity-wss/%2Fx%2Fsepolia-rc-17%2Ftorii%2Fwss
VITE_PUBLIC_TORII_RELAY=/dns4/api.cartridge.gg/tcp/443/x-parity-wss/%2Fx%2Fsepolia-rc-18%2Ftorii%2Fwss
VITE_SEASON_PASS_ADDRESS=0x23cc88996a5f9c7bcb559fdcffc257c0f75abe60f2a7e5d5cd343f8a95967f7
VITE_REALMS_ADDRESS=0x3205f47bd6f0b5e9cd5c79fcae19e12523a024709776d0a9e8b375adf63468d
VITE_LORDS_ADDRESS=0x0342ad5cc14002c005a5cedcfce2bd3af98d5e7fb79e9bf949b3a91cf145d72e
Expand Down
6 changes: 3 additions & 3 deletions client/src/dojo/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export const addToSubscription = async <S extends Schema>(
...positionClause,
},
components,
10_000,
30_000,
false,
));

Expand All @@ -87,7 +87,7 @@ export const addToSubscription = async <S extends Schema>(
},
},
components,
20_000,
30_000,
false,
);
};
Expand All @@ -106,7 +106,7 @@ export const addMarketSubscription = async <S extends Schema>(
},
},
components,
50_000,
30_000,
false,
);
};
115 changes: 70 additions & 45 deletions client/src/three/scenes/Worldmap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ export default class WorldmapScene extends HexagonScene {

dojo: SetupResult;

private fetchedChunks: Set<string> = new Set();

constructor(
dojoContext: SetupResult,
raycaster: Raycaster,
Expand Down Expand Up @@ -379,6 +381,7 @@ export default class WorldmapScene extends HexagonScene {
useUIStore.getState().setLeftNavigationView(LeftView.None);

this.armyManager.addLabelsToScene();
this.clearTileEntityCache();
}

onSwitchOff() {
Expand Down Expand Up @@ -670,53 +673,71 @@ export default class WorldmapScene extends HexagonScene {
const { width } = this.renderChunkSize;
const range = width / 2;

const sub = await getEntities(
this.dojo.network.toriiClient,
{
Composite: {
operator: "And",
clauses: [
{
Member: {
model: "s0_eternum-Tile",
member: "col",
operator: "Gte",
value: { Primitive: { U32: startCol - range } },
},
},
{
Member: {
model: "s0_eternum-Tile",
member: "col",
operator: "Lte",
value: { Primitive: { U32: startCol + range } },
},
},
{
Member: {
model: "s0_eternum-Tile",
member: "row",
operator: "Gte",
value: { Primitive: { U32: startRow - range } },
},
},
// Create a unique key for this chunk range
const chunkKey = `${startCol - range},${startCol + range},${startRow - range},${startRow + range}`;

console.log(chunkKey);

// Skip if we've already fetched this chunk
if (this.fetchedChunks.has(chunkKey)) {
console.log("Already fetched");
return;
}

// Add to fetched chunks before the query to prevent concurrent duplicate requests
this.fetchedChunks.add(chunkKey);

try {
await getEntities(
this.dojo.network.toriiClient,
{
Member: {
model: "s0_eternum-Tile",
member: "row",
operator: "Lte",
value: { Primitive: { U32: startRow + range } },
},
Composite: {
operator: "And",
clauses: [
{
Member: {
model: "s0_eternum-Tile",
member: "col",
operator: "Gte",
value: { Primitive: { U32: startCol - range } },
},
},
{
Member: {
model: "s0_eternum-Tile",
member: "col",
operator: "Lte",
value: { Primitive: { U32: startCol + range } },
},
},
{
Member: {
model: "s0_eternum-Tile",
member: "row",
operator: "Gte",
value: { Primitive: { U32: startRow - range } },
},
},
{
Member: {
model: "s0_eternum-Tile",
member: "row",
operator: "Lte",
value: { Primitive: { U32: startRow + range } },
},
},
],
},
},
],
},
},
this.dojo.network.contractComponents as any,
1000,
false,
);

console.log(sub);
this.dojo.network.contractComponents as any,
1000,
false,
);
} catch (error) {
// If there's an error, remove the chunk from cached set so it can be retried
this.fetchedChunks.delete(chunkKey);
console.error('Error fetching tile entities:', error);
}
}

private getExploredHexesForCurrentChunk() {
Expand Down Expand Up @@ -823,4 +844,8 @@ export default class WorldmapScene extends HexagonScene {
this.minimap.update();
}
}

public clearTileEntityCache() {
this.fetchedChunks.clear();
}
}
40 changes: 26 additions & 14 deletions client/src/ui/components/entities/Entity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ type EntityProps = {
arrival: ArrivalInfo;
} & React.HTMLAttributes<HTMLDivElement>;

const CACHE_KEY = "inventory-resources-sync";
const CACHE_DURATION = 2 * 60 * 1000; // 2 minutes in milliseconds

export const EntityArrival = ({ arrival, ...props }: EntityProps) => {
const dojo = useDojo();

Expand All @@ -50,23 +53,32 @@ export const EntityArrival = ({ arrival, ...props }: EntityProps) => {

useEffect(() => {
if (entityResources.length === 0) {
const cacheKey = `${CACHE_KEY}-${arrival.entityId}`;
const cachedTime = localStorage.getItem(cacheKey);
const now = Date.now();

if (cachedTime && now - parseInt(cachedTime) < CACHE_DURATION) {
return;
}

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);
try {
await addToSubscription(
dojo.network.toriiClient,
dojo.network.contractComponents as any,
arrival.entityId.toString(),
);
localStorage.setItem(cacheKey, now.toString());
} catch (error) {
console.error("Fetch failed", error);
} finally {
setIsSyncing(false);
}
};
fetch();
}
};
fetch();
}
}, [arrival.entityId, dojo.network.toriiClient, dojo.network.contractComponents, entityResources.length]);
}, [arrival.entityId, dojo.network.toriiClient, dojo.network.contractComponents, entityResources.length]);

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

Expand Down
43 changes: 26 additions & 17 deletions client/src/ui/components/resources/InventoryResources.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { useResourceBalance, useResourcesUtils } from "@/hooks/helpers/useResour
import { ResourceCost } from "@/ui/elements/ResourceCost";
import { divideByPrecision } from "@/ui/utils/utils";
import { ID, Resource, ResourcesIds } from "@bibliothecadao/eternum";
import { useEffect, useMemo, useState } from "react";
import { useMemo, useState } from "react";

const CACHE_KEY = "inventory-resources-sync";
const CACHE_DURATION = 2 * 60 * 1000; // 2 minutes in milliseconds

export const InventoryResources = ({
entityId,
Expand Down Expand Up @@ -36,25 +39,31 @@ export const InventoryResources = ({
[dynamic, entityId, getBalance],
);

useEffect(() => {
useMemo(async () => {
if (inventoriesResources.length === 0) {
const cacheKey = `${CACHE_KEY}-${entityId}`;
const cachedTime = localStorage.getItem(cacheKey);
const now = Date.now();

if (cachedTime && now - parseInt(cachedTime) < CACHE_DURATION) {
return;
}

setIsSyncing(true);
const fetch = async () => {
try {
await addToSubscription(
dojo.network.toriiClient,
dojo.network.contractComponents as any,
entityId.toString(),
);
} catch (error) {
console.error("Fetch failed", error);
} finally {
setIsSyncing(false);
}
};
fetch();
try {
await addToSubscription(
dojo.network.toriiClient,
dojo.network.contractComponents as any,
entityId.toString(),
);
localStorage.setItem(cacheKey, now.toString());
} catch (error) {
console.error("Fetch failed", error);
} finally {
setIsSyncing(false);
}
}
}, [inventoriesResources.length, entityId]);
}, [inventoriesResources.length, entityId, dojo.network.toriiClient, dojo.network.contractComponents]);

const allResources = [...inventoriesResources, ...dynamicResources];

Expand Down
10 changes: 7 additions & 3 deletions client/src/ui/components/resources/TravelInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import {
getTotalResourceWeight,
multiplyByPrecision,
} from "@/ui/utils/utils";
import { CapacityConfigCategory, ResourcesIds, type ID, type Resource } from "@bibliothecadao/eternum";
import { useEffect, useState } from "react";
import { ResourcesIds, type ID, type Resource } from "@bibliothecadao/eternum";
import { useEffect, useMemo, useState } from "react";

export const TravelInfo = ({
entityId,
Expand All @@ -27,18 +27,22 @@ export const TravelInfo = ({
}) => {
const [resourceWeight, setResourceWeight] = useState(0);
const [donkeyBalance, setDonkeyBalance] = useState(0);
const neededDonkeys = calculateDonkeysNeeded(resourceWeight);
const neededDonkeys = useMemo(() => calculateDonkeysNeeded(resourceWeight), [resourceWeight]);

const { getBalance } = useResourceBalance();

useEffect(() => {
const totalWeight = getTotalResourceWeight(resources);

const multipliedWeight = multiplyByPrecision(totalWeight);
setResourceWeight(multipliedWeight);

const { balance } = getBalance(entityId, ResourcesIds.Donkey);

const currentDonkeyAmount = isAmm ? 0 : resources.find((r) => r.resourceId === ResourcesIds.Donkey)?.amount || 0;

const calculatedDonkeyBalance = divideByPrecision(balance) - currentDonkeyAmount;

setDonkeyBalance(calculatedDonkeyBalance);

if (setCanCarry) {
Expand Down
14 changes: 7 additions & 7 deletions client/src/ui/layouts/World.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,13 @@ export const World = ({ backgroundImage }: { backgroundImage: string }) => {

console.log("world loading", worldLoading);

try {
await addMarketSubscription(dojo.network.toriiClient, dojo.network.contractComponents as any);
} catch (error) {
console.error("Fetch failed", error);
} finally {
setMarketLoading(false);
}
try {
await addMarketSubscription(dojo.network.toriiClient, dojo.network.contractComponents as any);
} catch (error) {
console.error("Fetch failed", error);
} finally {
setMarketLoading(false);
}
};

fetch();
Expand Down
6 changes: 3 additions & 3 deletions client/src/ui/modules/navigation/LeftNavigationModule.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { motion } from "framer-motion";
import { Suspense, lazy, memo, useEffect, useMemo } from "react";
import { construction, military, trade, worldStructures } from "../../components/navigation/Config";
import CircleButton from "../../elements/CircleButton";
import { EventStream } from "../stream/EventStream";
import { Chat } from "../chat/Chat";

const EntityDetails = lazy(() =>
import("../entity-details/EntityDetails").then((module) => ({ default: module.EntityDetails })),
Expand Down Expand Up @@ -259,8 +259,8 @@ export const LeftNavigationModule = memo(() => {
</div>
{!IS_MOBILE && (
<div className="flex">
{/* <Chat /> */}
<EventStream />
<Chat />
{/* <EventStream /> */}
</div>
)}
</div>
Expand Down
5 changes: 3 additions & 2 deletions client/src/ui/utils/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@ export const adjustWonderLordsCost = (cost: ResourceCost[]): ResourceCost[] => {

export const calculateDonkeysNeeded = (orderWeight: number): number => {
const configManager = ClientConfigManager.instance();
const donkeyCapacityGrams = configManager.getCapacityConfig(CapacityConfigCategory.Donkey);

return Math.ceil(orderWeight / configManager.getCapacityConfig(CapacityConfigCategory.Donkey));
};
return Math.ceil((orderWeight / 1000) / donkeyCapacityGrams);
};
Loading

0 comments on commit ef44514

Please sign in to comment.