Skip to content

Commit

Permalink
fix model manager in landing (#2632)
Browse files Browse the repository at this point in the history
* move model managers to sdk

* fix army manager, config and resource manager

* fix build

* fix model manager in landing
  • Loading branch information
aymericdelab authored Jan 9, 2025
1 parent a623b7f commit 4cfbabb
Show file tree
Hide file tree
Showing 108 changed files with 3,061 additions and 35,543 deletions.
1,588 changes: 0 additions & 1,588 deletions client/apps/game/src/data/geodata/flags_heights.json

This file was deleted.

32,002 changes: 0 additions & 32,002 deletions client/apps/game/src/data/geodata/realms_raw.json

This file was deleted.

3 changes: 1 addition & 2 deletions client/apps/game/src/dojo/createSystemCalls.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type * as SystemProps from "@bibliothecadao/eternum";
import { type SetupNetworkResult } from "./setupNetwork";

export function createSystemCalls({ provider }: SetupNetworkResult) {
export function createSystemCalls({ provider }: { provider: SystemProps.EternumProvider }) {
const uuid = async () => {
return await provider.uuid();
};
Expand Down
31 changes: 0 additions & 31 deletions client/apps/game/src/dojo/modelManager/utils/ArmyMovementUtils.ts

This file was deleted.

2 changes: 1 addition & 1 deletion client/apps/game/src/dojo/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { AppStore } from "@/hooks/store/useUIStore";
import { LoadingStateKey } from "@/hooks/store/useWorldLoading";
import {
BUILDING_CATEGORY_POPULATION_CONFIG_ID,
ClientConfigManager,
HYPERSTRUCTURE_CONFIG_ID,
WORLD_CONFIG_ID,
} from "@bibliothecadao/eternum";
Expand All @@ -12,7 +13,6 @@ import { Clause, EntityKeysClause, ToriiClient } from "@dojoengine/torii-client"
import { debounce } from "lodash";
import { createClientComponents } from "./createClientComponents";
import { createSystemCalls } from "./createSystemCalls";
import { ClientConfigManager } from "./modelManager/ConfigManager";
import { setupNetwork } from "./setupNetwork";

export type SetupResult = Awaited<ReturnType<typeof setup>>;
Expand Down
5 changes: 2 additions & 3 deletions client/apps/game/src/hooks/helpers/battles/useBattles.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ClientComponents } from "@/dojo/createClientComponents";
import { BattleManager } from "@/dojo/modelManager/BattleManager";
import { EternumGlobalConfig, ID, Position } from "@bibliothecadao/eternum";
import { BattleManager, EternumGlobalConfig, ID, Position } from "@bibliothecadao/eternum";
import { useComponentValue, useEntityQuery } from "@dojoengine/react";
import {
Component,
Expand Down Expand Up @@ -62,7 +61,7 @@ export const useBattleManager = (battleEntityId: ID) => {
const battle = useComponentValue(dojo.setup.components.Battle, getEntityIdFromKeys([BigInt(battleEntityId)]));

const battleManager = useMemo(() => {
return new BattleManager(battleEntityId, dojo);
return new BattleManager(dojo.setup.components, dojo.network.provider, battleEntityId);
}, [battleEntityId, battle]);

return battleManager;
Expand Down
25 changes: 2 additions & 23 deletions client/apps/game/src/hooks/helpers/useArmies.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { type ClientComponents } from "@/dojo/createClientComponents";
import { getArmyTotalCapacity } from "@/dojo/modelManager/utils/ArmyMovementUtils";
import {
ArmyInfo,
CapacityConfigCategory,
ContractAddress,
EternumGlobalConfig,
getArmyTotalCapacity,
type ID,
type Position,
} from "@bibliothecadao/eternum";
Expand All @@ -16,7 +17,6 @@ import {
getComponentValue,
runQuery,
type Component,
type ComponentValue,
type Entity,
} from "@dojoengine/recs";
import { getEntityIdFromKeys } from "@dojoengine/utils";
Expand All @@ -25,27 +25,6 @@ import { shortString } from "starknet";
import { useDojo } from "../context/DojoContext";
import { type PlayerStructure } from "./useEntities";

export type ArmyInfo = ComponentValue<ClientComponents["Army"]["schema"]> & {
name: string;
isMine: boolean;
isMercenary: boolean;
isHome: boolean;
offset: Position;
health: ComponentValue<ClientComponents["Health"]["schema"]>;
position: ComponentValue<ClientComponents["Position"]["schema"]>;
quantity: ComponentValue<ClientComponents["Quantity"]["schema"]>;
owner: ComponentValue<ClientComponents["Owner"]["schema"]>;
entityOwner: ComponentValue<ClientComponents["EntityOwner"]["schema"]>;
protectee: ComponentValue<ClientComponents["Protectee"]["schema"]> | undefined;
movable: ComponentValue<ClientComponents["Movable"]["schema"]> | undefined;
totalCapacity: bigint;
weight: bigint;
arrivalTime: ComponentValue<ClientComponents["ArrivalTime"]["schema"]> | undefined;
stamina: ComponentValue<ClientComponents["Stamina"]["schema"]> | undefined;
realm: ComponentValue<ClientComponents["Realm"]["schema"]> | undefined;
homePosition: ComponentValue<ClientComponents["Position"]["schema"]> | undefined;
};

const formatArmies = (
armies: Entity[],
playerAddress: string,
Expand Down
3 changes: 1 addition & 2 deletions client/apps/game/src/hooks/helpers/useHyperstructures.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { ClientComponents } from "@/dojo/createClientComponents";
import { configManager } from "@/dojo/setup";
import { DUMMY_HYPERSTRUCTURE_ENTITY_ID } from "@/three/scenes/constants";
import { divideByPrecision, toHexString, toInteger } from "@/ui/utils/utils";
import { ContractAddress, ID, ResourcesIds } from "@bibliothecadao/eternum";
import { ContractAddress, DUMMY_HYPERSTRUCTURE_ENTITY_ID, ID, ResourcesIds } from "@bibliothecadao/eternum";
import { useEntityQuery } from "@dojoengine/react";
import { Component, ComponentValue, Entity, Has, HasValue, getComponentValue, runQuery } from "@dojoengine/recs";
import { useCallback, useMemo } from "react";
Expand Down
7 changes: 3 additions & 4 deletions client/apps/game/src/hooks/helpers/useQuests.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { TileManager } from "@/dojo/modelManager/TileManager";
import { questDetails } from "@/ui/components/quest/questDetails";
import { BuildingType, ContractAddress, ID, QuestType } from "@bibliothecadao/eternum";
import { ArmyInfo, BuildingType, ContractAddress, ID, QuestType, TileManager } from "@bibliothecadao/eternum";
import { useComponentValue, useEntityQuery } from "@dojoengine/react";
import { HasValue, getComponentValue } from "@dojoengine/recs";
import { getEntityIdFromKeys } from "@dojoengine/utils";
import { useMemo } from "react";
import { useDojo } from "../context/DojoContext";
import useUIStore from "../store/useUIStore";
import { ArmyInfo, useArmiesByEntityOwnerWithPositionAndQuantity } from "./useArmies";
import { useArmiesByEntityOwnerWithPositionAndQuantity } from "./useArmies";
import { useEntitiesUtils } from "./useEntities";
import { useGetMyOffers } from "./useTrade";

Expand Down Expand Up @@ -82,7 +81,7 @@ const useQuestDependencies = () => {
[structureEntityId, getEntityInfo],
);

const tileManager = new TileManager(setup, {
const tileManager = new TileManager(setup.components, setup.network.provider, {
col: structurePosition.x,
row: structurePosition.y,
});
Expand Down
2 changes: 1 addition & 1 deletion client/apps/game/src/hooks/helpers/useRealm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { useEntityQuery } from "@dojoengine/react";
import { Has, HasValue, getComponentValue, runQuery, type ComponentValue, type Entity } from "@dojoengine/recs";
import { useMemo } from "react";
import { shortString } from "starknet";
import realmIdsByOrder from "../../data/realmids_by_order.json";
import realmIdsByOrder from "../../../../../common/data/realmids_by_order.json";
import { unpackResources } from "../../ui/utils/packedData";
import { getRealmNameById } from "../../ui/utils/realms";
import { getEntityIdFromKeys } from "../../ui/utils/utils";
Expand Down
32 changes: 21 additions & 11 deletions client/apps/game/src/hooks/helpers/useResources.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { configManager } from "@/dojo/setup";
import { CapacityConfigCategory, ID, ResourcesIds, resources, type Resource } from "@bibliothecadao/eternum";
import {
CapacityConfigCategory,
ID,
ResourceManager,
ResourcesIds,
resources,
type Resource,
} from "@bibliothecadao/eternum";
import { useComponentValue } from "@dojoengine/react";
import { Has, HasValue, getComponentValue, runQuery, type Entity } from "@dojoengine/recs";
import { useEffect, useMemo, useState } from "react";
import { ResourceManager } from "../../dojo/modelManager/ResourceManager";
import { getEntityIdFromKeys } from "../../ui/utils/utils";
import { useDojo } from "../context/DojoContext";
import useUIStore from "../store/useUIStore";
Expand All @@ -30,7 +36,7 @@ export function useResourcesUtils() {

return resources
.map(({ id }) => {
const resourceManager = new ResourceManager(setup, entityId, id);
const resourceManager = new ResourceManager(setup.components, entityId, id);
const balance = resourceManager.balance(currentDefaultTick);
return { resourceId: id, amount: balance };
})
Expand All @@ -50,7 +56,7 @@ export function useResourcesUtils() {
const resourceIds = resources.map((r) => r.id);
return resourceIds
.map((id) => {
const resourceManager = new ResourceManager(setup, entityId, id);
const resourceManager = new ResourceManager(setup.components, entityId, id);
const balance = resourceManager.balance(currentDefaultTick);
return { resourceId: id, amount: balance };
})
Expand All @@ -77,7 +83,7 @@ export function useResourcesUtils() {
const realmsWithResource = allRealms
.map((id: Entity) => {
const realm = getComponentValue(Realm, id);
const resourceManager = realm ? new ResourceManager(setup, realm.entity_id, resourceId) : undefined;
const resourceManager = realm ? new ResourceManager(setup.components, realm.entity_id, resourceId) : undefined;
const resource = resourceManager
? {
balance: resourceManager.balance(currentDefaultTick),
Expand Down Expand Up @@ -110,8 +116,12 @@ export function useResourceBalance() {

const getFoodResources = (entityId: ID): Resource[] => {
const currentDefaultTick = useUIStore.getState().currentDefaultTick;
const wheatBalance = new ResourceManager(dojo.setup, entityId, ResourcesIds.Wheat).balance(currentDefaultTick);
const fishBalance = new ResourceManager(dojo.setup, entityId, ResourcesIds.Fish).balance(currentDefaultTick);
const wheatBalance = new ResourceManager(dojo.setup.components, entityId, ResourcesIds.Wheat).balance(
currentDefaultTick,
);
const fishBalance = new ResourceManager(dojo.setup.components, entityId, ResourcesIds.Fish).balance(
currentDefaultTick,
);

return [
{ resourceId: ResourcesIds.Wheat, amount: wheatBalance },
Expand All @@ -120,13 +130,13 @@ export function useResourceBalance() {
};

const getResourceProductionInfo = (entityId: ID, resourceId: ResourcesIds) => {
const resourceManager = new ResourceManager(dojo.setup, entityId, resourceId);
const resourceManager = new ResourceManager(dojo.setup.components, entityId, resourceId);
return resourceManager.getProduction();
};

const getBalance = (entityId: ID, resourceId: ResourcesIds) => {
const currentDefaultTick = useUIStore.getState().currentDefaultTick;
const resourceManager = new ResourceManager(dojo.setup, entityId, resourceId);
const resourceManager = new ResourceManager(dojo.setup.components, entityId, resourceId);
return { balance: resourceManager.balance(currentDefaultTick), resourceId };
};

Expand All @@ -145,7 +155,7 @@ export function useResourceBalance() {
const [resourceBalance, setResourceBalance] = useState<Resource>({ amount: 0, resourceId });

useEffect(() => {
const resourceManager = new ResourceManager(dojo.setup, entityId, resourceId);
const resourceManager = new ResourceManager(dojo.setup.components, entityId, resourceId);
setResourceBalance({ amount: resourceManager.balance(currentDefaultTick), resourceId });
}, []);

Expand All @@ -169,7 +179,7 @@ export const useResourceManager = (entityId: ID, resourceId: ResourcesIds) => {
);

const resourceManager = useMemo(() => {
return new ResourceManager(dojo.setup, entityId, resourceId);
return new ResourceManager(dojo.setup.components, entityId, resourceId);
}, [dojo.setup, entityId, resourceId, production]);

return resourceManager;
Expand Down
5 changes: 2 additions & 3 deletions client/apps/game/src/hooks/helpers/useStamina.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { StaminaManager } from "@/dojo/modelManager/StaminaManager";
import { ID } from "@bibliothecadao/eternum";
import { ID, StaminaManager } from "@bibliothecadao/eternum";
import { useComponentValue } from "@dojoengine/react";
import { getEntityIdFromKeys } from "@dojoengine/utils";
import { useMemo } from "react";
Expand All @@ -11,7 +10,7 @@ export const useStaminaManager = (entityId: ID) => {
const stamina = useComponentValue(setup.components.Stamina, getEntityIdFromKeys([BigInt(entityId)]));

const manager = useMemo(() => {
return new StaminaManager(setup, entityId);
return new StaminaManager(setup.components, entityId);
}, [entityId, stamina?.amount, stamina?.last_refill_tick]);

return manager;
Expand Down
32 changes: 16 additions & 16 deletions client/apps/game/src/hooks/helpers/useStructures.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
import { ClientComponents } from "@/dojo/createClientComponents";
import { BattleManager } from "@/dojo/modelManager/BattleManager";
import { configManager } from "@/dojo/setup";
import { currentTickCount } from "@/ui/utils/utils";
import { ContractAddress, ID, Position, StructureType, TickIds } from "@bibliothecadao/eternum";
import { ComponentValue, Has, HasValue, getComponentValue, runQuery } from "@dojoengine/recs";
import {
BattleManager,
ContractAddress,
ID,
Position,
Structure,
StructureType,
TickIds,
} from "@bibliothecadao/eternum";
import { Has, HasValue, getComponentValue, runQuery } from "@dojoengine/recs";
import { getEntityIdFromKeys } from "@dojoengine/utils";
import { useMemo } from "react";
import { shortString } from "starknet";
import { useDojo } from "../context/DojoContext";
import useNextBlockTimestamp from "../useNextBlockTimestamp";
import { ArmyInfo, getArmyByEntityId } from "./useArmies";
import { getArmyByEntityId } from "./useArmies";
import { useEntitiesUtils } from "./useEntities";

export type Structure = ComponentValue<ClientComponents["Structure"]["schema"]> & {
isMine: boolean;
isMercenary: boolean;
name: string;
ownerName?: string;
protector: ArmyInfo | undefined;
owner: ComponentValue<ClientComponents["Owner"]["schema"]>;
entityOwner: ComponentValue<ClientComponents["EntityOwner"]["schema"]>;
};

export const useStructureAtPosition = ({ x, y }: Position): Structure | undefined => {
const {
account: { account },
Expand Down Expand Up @@ -251,7 +247,11 @@ export const useIsResourcesLocked = (structureEntityId: ID) => {
const structure = getStructureByEntityId(structureEntityId);

return useMemo(() => {
const battleManager = new BattleManager(structure?.protector?.battle_id || 0, dojo);
const battleManager = new BattleManager(
dojo.setup.components,
dojo.network.provider,
structure?.protector?.battle_id || 0,
);
return battleManager.isResourcesLocked(nextBlockTimestamp!);
}, [structure, nextBlockTimestamp]);
};
3 changes: 1 addition & 2 deletions client/apps/game/src/hooks/store/_buildModeStore.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { BuildingType, ResourcesIds, StructureType } from "@bibliothecadao/eternum";
import { BUILDINGS_CENTER, BuildingType, ResourcesIds, StructureType } from "@bibliothecadao/eternum";
import { Entity } from "@dojoengine/recs";
import { BUILDINGS_CENTER } from "@/three/scenes/constants";

export interface BuildModeStore {
previewBuilding: { type: BuildingType | StructureType; resource?: ResourcesIds } | null;
Expand Down
4 changes: 2 additions & 2 deletions client/apps/game/src/hooks/store/_threeStore.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { HexPosition, StructureInfo } from "@/types";
import { BuildingType, ID, Position } from "@bibliothecadao/eternum";
import { StructureInfo } from "@/types";
import { BuildingType, HexPosition, ID, Position } from "@bibliothecadao/eternum";

export interface ThreeStore {
navigationTarget: HexPosition | null;
Expand Down
5 changes: 2 additions & 3 deletions client/apps/game/src/hooks/store/useLeaderBoardStore.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { LeaderboardManager } from "@/dojo/modelManager/LeaderboardManager";
import { ContractAddress, ID } from "@bibliothecadao/eternum";
import { ContractAddress, ID, LeaderboardManager } from "@bibliothecadao/eternum";
import { useCallback } from "react";
import { create } from "zustand";
import { useDojo } from "../context/DojoContext";
Expand Down Expand Up @@ -33,7 +32,7 @@ export const useHyperstructureData = () => {
const setGuildsByRank = useLeaderBoardStore((state) => state.setGuildsByRank);

const updateLeaderboard = useCallback(() => {
const leaderboardManager = LeaderboardManager.instance(dojo);
const leaderboardManager = LeaderboardManager.instance(dojo.setup.components);
const playersByRank = leaderboardManager.getPlayersByRank(nextBlockTimestamp || 0);
const guildsByRank = leaderboardManager.getGuildsByRank(nextBlockTimestamp || 0, getGuildFromPlayerAddress);
setPlayersByRank(playersByRank);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { highlightHexMaterial } from "@/three/shaders/highlightHexMaterial";
import { HexPosition } from "@/types";
import { getWorldPositionForHex } from "@/ui/utils/utils";
import { HexPosition } from "@bibliothecadao/eternum";
import * as THREE from "three";
import { createHexagonShape } from "../geometry/HexagonGeometry";
import { HEX_SIZE } from "../scenes/constants";
Expand Down
2 changes: 1 addition & 1 deletion client/apps/game/src/three/components/Navigator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type HexPosition } from "@/types";
import { calculateDistanceInHexes, getWorldPositionForHex } from "@/ui/utils/utils";
import { type HexPosition } from "@bibliothecadao/eternum";
import throttle from "lodash/throttle";
import * as THREE from "three";
import { CSS2DObject } from "three-stdlib";
Expand Down
3 changes: 1 addition & 2 deletions client/apps/game/src/three/helpers/pathfinding.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { HexPosition } from "@/types";
import { Position } from "@/types/Position";
import { getNeighborOffsets } from "@bibliothecadao/eternum";
import { getNeighborOffsets, HexPosition } from "@bibliothecadao/eternum";

interface Node {
col: number;
Expand Down
3 changes: 2 additions & 1 deletion client/apps/game/src/three/scenes/HexagonScene.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { type SetupResult } from "@/dojo/setup";
import useUIStore, { type AppStore } from "@/hooks/store/useUIStore";
import { type HexPosition, type SceneName } from "@/types";
import { type SceneName } from "@/types";
import { GRAPHICS_SETTING, GraphicsSettings, IS_FLAT_MODE } from "@/ui/config";
import { LeftView } from "@/ui/modules/navigation/LeftNavigationModule";
import { RightView } from "@/ui/modules/navigation/RightNavigationModule";
import { getWorldPositionForHex } from "@/ui/utils/utils";
import { type HexPosition } from "@bibliothecadao/eternum";
import gsap from "gsap";
import throttle from "lodash/throttle";
import * as THREE from "three";
Expand Down
Loading

0 comments on commit 4cfbabb

Please sign in to comment.