Skip to content

Commit

Permalink
fix:
Browse files Browse the repository at this point in the history
  • Loading branch information
ponderingdemocritus committed Dec 19, 2024
1 parent 2adc56d commit 37e8a96
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 90 deletions.
2 changes: 1 addition & 1 deletion client/src/dojo/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export async function setup({ ...config }: DojoConfig) {
},
{
Keys: {
keys: [HYPERSTRUCTURE_CONFIG_ID.toString(), undefined],
keys: [HYPERSTRUCTURE_CONFIG_ID.toString()],
pattern_matching: "VariableLen",
models: [],
},
Expand Down
2 changes: 2 additions & 0 deletions client/src/hooks/helpers/use-resource-arrivals.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ const usePlayerArrivals = () => {
const arrivals = positions.flatMap((position) => {
return Array.from(runQuery([HasValue(Position, { x: position.x, y: position.y }), ...queryFragments]));
});

console.log("arrivals", arrivals);
return arrivals;
}, []);

Expand Down
92 changes: 13 additions & 79 deletions client/src/hooks/helpers/useEntities.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
type ID,
} from "@bibliothecadao/eternum";
import { useEntityQuery } from "@dojoengine/react";
import { Has, getComponentValue, type ComponentValue } from "@dojoengine/recs";
import { Has, HasValue, getComponentValue, type ComponentValue } from "@dojoengine/recs";
import { useMemo } from "react";
import { shortString } from "starknet";
import { useDojo } from "../context/DojoContext";
Expand All @@ -23,7 +23,7 @@ export type PlayerStructure = ComponentValue<ClientComponents["Structure"]["sche
owner: ComponentValue<ClientComponents["Owner"]["schema"]>;
};

type RealmWithPosition = ComponentValue<ClientComponents["Realm"]["schema"]> & {
export type RealmWithPosition = ComponentValue<ClientComponents["Realm"]["schema"]> & {
position: ComponentValue<ClientComponents["Position"]["schema"]>;
name: string;
owner: ComponentValue<ClientComponents["Owner"]["schema"]>;
Expand All @@ -43,53 +43,18 @@ export const useEntities = () => {
const { getEntityName } = useEntitiesUtils();

// Get all realms
const allRealms = useEntityQuery([Has(Realm)]);

const filterPlayerRealms = useMemo(() => {
return allRealms.filter((id) => {
const owner = getComponentValue(Owner, id);
return owner && ContractAddress(owner.address) === ContractAddress(address);
});
}, [allRealms, address]);

const filterOtherRealms = useMemo(() => {
return allRealms.filter((id) => {
const owner = getComponentValue(Owner, id);
return owner && ContractAddress(owner.address) !== ContractAddress(address);
});
}, [allRealms, address]);
const playerRealmsQuery = useEntityQuery([Has(Realm), HasValue(Owner, { address: address })]);

// Get all structures
const allStructures = useEntityQuery([Has(Structure), Has(Position), Has(Owner)]);

const filterPlayerStructures = useMemo(() => {
return allStructures.filter((id) => {
const owner = getComponentValue(Owner, id);
return owner && ContractAddress(owner.address) === ContractAddress(address);
});
}, [allStructures, address]);

const filterOtherStructures = useMemo(() => {
return allStructures.filter((id) => {
const owner = getComponentValue(Owner, id);
return owner && ContractAddress(owner.address) !== ContractAddress(address);
});
}, [allStructures, address]);
const playerStructuresQuery = useEntityQuery([
Has(Structure),
Has(Position),
Has(Owner),
HasValue(Owner, { address: address }),
]);

const playerRealms = useMemo(() => {
return filterPlayerRealms.map((id) => {
const realm = getComponentValue(Realm, id);
return {
...realm,
position: getComponentValue(Position, id),
name: getRealmNameById(realm!.realm_id),
owner: getComponentValue(Owner, id),
} as RealmWithPosition;
});
}, [filterPlayerRealms]);

const otherRealms = useMemo(() => {
return filterOtherRealms.map((id) => {
return playerRealmsQuery.map((id) => {
const realm = getComponentValue(Realm, id);
return {
...realm,
Expand All @@ -98,10 +63,10 @@ export const useEntities = () => {
owner: getComponentValue(Owner, id),
} as RealmWithPosition;
});
}, [filterOtherRealms]);
}, [playerRealmsQuery]);

const playerStructures = useMemo(() => {
return filterPlayerStructures
return playerStructuresQuery
.map((id) => {
const structure = getComponentValue(Structure, id);
if (!structure) return;
Expand All @@ -121,24 +86,7 @@ export const useEntities = () => {
if (b.category === StructureType[StructureType.Realm]) return 1;
return a.category.localeCompare(b.category);
});
}, [filterPlayerStructures]);

const otherStructures = useMemo(() => {
return filterOtherStructures
.map((id) => {
const structure = getComponentValue(Structure, id);
if (!structure || structure.category === StructureType[StructureType.Realm]) return;

const position = getComponentValue(Position, id);

const structureName = getEntityName(structure.entity_id);

const name = structureName ? `${structure?.category} ${structureName}` : structure.category || "";
return { ...structure, position: position!, name, owner: getComponentValue(Owner, id) };
})
.filter((structure): structure is PlayerStructure => structure !== undefined)
.sort((a, b) => a.category.localeCompare(b.category));
}, [filterOtherStructures]);
}, [playerStructuresQuery]);

const getPlayerRealms = (filterFn?: (realm: RealmWithPosition) => boolean) => {
return useMemo(() => {
Expand All @@ -147,30 +95,16 @@ export const useEntities = () => {
}, [playerRealms, filterFn]);
};

const getOtherRealms = (filterFn?: (realm: RealmWithPosition) => boolean) => {
return useMemo(() => {
return filterFn ? otherRealms.filter(filterFn) : otherRealms;
}, [otherRealms, filterFn]);
};

const getPlayerStructures = (filterFn?: (structure: PlayerStructure) => boolean) => {
return useMemo(() => {
const structures = filterFn ? playerStructures.filter(filterFn) : playerStructures;
return structures.sort((a, b) => a.name.localeCompare(b.name));
}, [playerStructures, filterFn]);
};

const getOtherStructures = (filterFn?: (structure: PlayerStructure) => boolean) => {
return useMemo(() => {
return filterFn ? otherStructures.filter(filterFn) : otherStructures;
}, [otherStructures, filterFn]);
};

return {
playerRealms: getPlayerRealms,
otherRealms: getOtherRealms,
playerStructures: getPlayerStructures,
otherStructures: getOtherStructures,
};
};

Expand Down
12 changes: 6 additions & 6 deletions client/src/hooks/helpers/useRealm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { type ClientComponents } from "@/dojo/createClientComponents";
import { configManager } from "@/dojo/setup";
import {
ContractAddress,
type ID,
getOrderName,
getQuestResources as getStartingResources,
type ID,
} from "@bibliothecadao/eternum";
import { useEntityQuery } from "@dojoengine/react";
import { type ComponentValue, type Entity, Has, HasValue, getComponentValue, runQuery } from "@dojoengine/recs";
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";
Expand Down Expand Up @@ -265,7 +265,7 @@ export function getRealms(): RealmInfo[] {
const position = getComponentValue(Position, entity);
const population = getComponentValue(Population, entity);

if (!realm || !owner || !position) return;
if (!realm || !owner || !position) return null;

const { realm_id, entity_id, produced_resources, order } = realm;

Expand All @@ -291,7 +291,7 @@ export function getRealms(): RealmInfo[] {
hasWonder: realm.has_wonder,
};
})
.filter((realm) => realm !== undefined);
.filter((realm): realm is RealmInfo => realm !== null);
}

export function usePlayerRealms(): RealmInfo[] {
Expand All @@ -312,7 +312,7 @@ export function usePlayerRealms(): RealmInfo[] {
const position = getComponentValue(Position, entity);
const population = getComponentValue(Population, entity);

if (!realm || !owner || !position) return;
if (!realm || !owner || !position) return null;

const { realm_id, entity_id, produced_resources, order } = realm;

Expand All @@ -338,7 +338,7 @@ export function usePlayerRealms(): RealmInfo[] {
hasWonder: realm.has_wonder,
};
})
.filter((realm) => realm !== undefined);
.filter((realm): realm is RealmInfo => realm !== null);
}, [realmEntities]);
return realms;
}
55 changes: 51 additions & 4 deletions client/src/ui/components/trading/TransferView.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import { useDojo } from "@/hooks/context/DojoContext";
import { useEntities } from "@/hooks/helpers/useEntities";
import { PlayerStructure, RealmWithPosition, useEntities, useEntitiesUtils } from "@/hooks/helpers/useEntities";
import { useGuilds } from "@/hooks/helpers/useGuilds";
import { getRealmNameById } from "@/ui/utils/realms";
import { ContractAddress, StructureType } from "@bibliothecadao/eternum";
import { useEntityQuery } from "@dojoengine/react";
import { Has, NotValue, getComponentValue } from "@dojoengine/recs";
import { useMemo, useState } from "react";
import { TransferBetweenEntities } from "./TransferBetweenEntities";

export const TransferView = () => {
const {
account: { account },
setup: {
components: { Structure, Position, Owner, Realm },
},
} = useDojo();

const { playerRealms, playerStructures, otherRealms, otherStructures } = useEntities();
const { playerRealms, playerStructures } = useEntities();

const [guildOnly, setGuildOnly] = useState(false);

Expand All @@ -19,6 +26,46 @@ export const TransferView = () => {
return getPlayersInPlayersGuild(BigInt(account.address)).map((a) => BigInt(a.address));
}, [account.address]);

const { getEntityName } = useEntitiesUtils();

const otherStructuresQuery = useEntityQuery([
Has(Structure),
Has(Position),
Has(Owner),
NotValue(Owner, { address: ContractAddress(account.address) }),
]);

const otherStructures = useMemo(() => {
return otherStructuresQuery
.map((id) => {
const structure = getComponentValue(Structure, id);
if (!structure || structure.category === StructureType[StructureType.Realm]) return;

const position = getComponentValue(Position, id);

const structureName = getEntityName(structure.entity_id);

const name = structureName ? `${structure?.category} ${structureName}` : structure.category || "";
return { ...structure, position: position!, name, owner: getComponentValue(Owner, id) };
})
.filter((structure): structure is PlayerStructure => structure !== undefined)
.sort((a, b) => a.category.localeCompare(b.category));
}, [otherStructuresQuery]);

const otherRealmsQuery = useEntityQuery([Has(Realm), NotValue(Owner, { address: ContractAddress(account.address) })]);

const otherRealms = useMemo(() => {
return otherRealmsQuery.map((id) => {
const realm = getComponentValue(Realm, id);
return {
...realm,
position: getComponentValue(Position, id),
name: getRealmNameById(realm!.realm_id),
owner: getComponentValue(Owner, id),
} as RealmWithPosition;
});
}, [otherRealmsQuery]);

return (
<TransferBetweenEntities
filterBy={setGuildOnly}
Expand All @@ -38,15 +85,15 @@ export const TransferView = () => {
name: "Your Banks",
},
{
entities: otherRealms((a) =>
entities: otherRealms.filter((a) =>
guildOnly
? playersInPlayersGuildAddress.includes(a.owner.address)
: !playersInPlayersGuildAddress.includes(a.owner.address),
),
name: "Other Realms",
},
{
entities: otherStructures((a) =>
entities: otherStructures.filter((a) =>
guildOnly
? playersInPlayersGuildAddress.includes(a.owner.address)
: !playersInPlayersGuildAddress.includes(a.owner.address),
Expand Down

0 comments on commit 37e8a96

Please sign in to comment.