Skip to content

Commit

Permalink
Merge pull request #2543 from BibliothecaDAO/enh/setup-entity-queries
Browse files Browse the repository at this point in the history
Enh/setup entity queries
  • Loading branch information
ponderingdemocritus authored Dec 16, 2024
2 parents ccad004 + 0e6fa4b commit 7e18f86
Show file tree
Hide file tree
Showing 8 changed files with 402 additions and 69 deletions.
14 changes: 7 additions & 7 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
"@bibliothecadao/eternum": "workspace:^",
"@cartridge/connector": "0.5.5",
"@cartridge/controller": "0.5.5",
"@dojoengine/core": "1.0.3-alpha.2",
"@dojoengine/create-burner": "1.0.3-alpha.2",
"@dojoengine/react": "1.0.3-alpha.2",
"@dojoengine/core": "1.0.4-alpha.3.1.0",
"@dojoengine/create-burner": "1.0.4-alpha.3.1.0",
"@dojoengine/react": "1.0.4-alpha.3.1.0",
"@dojoengine/recs": "^2.0.13",
"@dojoengine/state": "1.0.3-alpha.2",
"@dojoengine/torii-client": "1.0.3-alpha.2",
"@dojoengine/torii-wasm": "1.0.3-alpha.2",
"@dojoengine/utils": "1.0.3-alpha.2",
"@dojoengine/state": "1.0.4-alpha.3.1.0",
"@dojoengine/torii-client": "1.0.4-alpha.3.1.0",
"@dojoengine/torii-wasm": "1.0.4-alpha.3.1.0",
"@dojoengine/utils": "1.0.4-alpha.3.1.0",
"@headlessui/react": "^1.7.18",
"@latticexyz/utils": "^2.0.0-next.12",
"@radix-ui/react-collapsible": "^1.1.1",
Expand Down
107 changes: 79 additions & 28 deletions client/src/dojo/queries.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,83 @@
// onload -> fetch single key entities

import { Component, Metadata, Schema } from "@dojoengine/recs";
import { setEntities } from "@dojoengine/state";
import { Clause, PatternMatching, ToriiClient } from "@dojoengine/torii-client";
import { getEntities } from "@dojoengine/state";
import { PatternMatching, ToriiClient } from "@dojoengine/torii-client";

// on hexception -> fetch below queries based on entityID

// background sync after load ->

export const getEntities = async <S extends Schema>(
export const syncPosition = async <S extends Schema>(
client: ToriiClient,
clause: Clause | undefined,
components: Component<S, Metadata, undefined>[],
limit: number = 100,
logging: boolean = false,
entityID: string,
) => {
if (logging) console.log("Starting getEntities");
let offset = 0;
let continueFetching = true;

while (continueFetching) {
const entities = await client.getEntities({
limit,
offset,
clause,
dont_include_hashed_keys: false,
order_by: [],
});

setEntities(entities, components);
await getEntities(
client,
{
Keys: {
keys: [entityID],
pattern_matching: "FixedLen" as PatternMatching,
models: ["s0_eternum-Position"],
},
},
components,
[],
[],
30_000,
);
};
export const syncBuildingQty = async <S extends Schema>(
client: ToriiClient,
components: Component<S, Metadata, undefined>[],
entityID: string,
) => {
await getEntities(
client,
{
Keys: {
keys: [entityID, undefined],
pattern_matching: "FixedLen" as PatternMatching,
models: ["s0_eternum-BuildingQuantityv2"],
},
},
components,
[],
[],
30_000,
);
};

if (Object.keys(entities).length < limit) {
continueFetching = false;
} else {
offset += limit;
}
}
export const addToSubscriptionBuildingQty = async <S extends Schema>(
client: ToriiClient,
components: Component<S, Metadata, undefined>[],
entityID: string[],
) => {
const start = performance.now();
await getEntities(
client,
{
Composite: {
operator: "Or",
clauses: [
...entityID.map((id) => ({
Keys: {
keys: [id, undefined],
pattern_matching: "VariableLen" as PatternMatching,
models: ["s0_eternum-BuildingQuantityv2"],
},
})),
],
},
},
components,
[],
[],
30_000,
);
const end = performance.now();
console.log("AddToSubscription Building qty", end - start);
};

export const addToSubscription = async <S extends Schema>(
Expand All @@ -44,6 +86,7 @@ export const addToSubscription = async <S extends Schema>(
entityID: string[],
position?: { x: number; y: number }[],
) => {
const start = performance.now();
await getEntities(
client,
{
Expand All @@ -69,16 +112,20 @@ export const addToSubscription = async <S extends Schema>(
],
},
},
components,
components as any,
[],
[],
30_000,
false,
);
const end = performance.now();
console.log("AddToSubscriptionEnd", end - start);
};

export const addMarketSubscription = async <S extends Schema>(
client: ToriiClient,
components: Component<S, Metadata, undefined>[],
) => {
const start = performance.now();
await getEntities(
client,
{
Expand All @@ -89,7 +136,11 @@ export const addMarketSubscription = async <S extends Schema>(
},
},
components,
[],
[],
30_000,
false,
);
const end = performance.now();
console.log("MarketEnd", end - start);
};
59 changes: 55 additions & 4 deletions client/src/dojo/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const syncEntitiesDebounced = async <S extends Schema>(

const debouncedSetEntities = debounce(() => {
if (Object.keys(entityBatch).length > 0) {
console.log("Applying batch update", entityBatch);
// console.log("Applying batch update", entityBatch);
setEntities(entityBatch, components, logging);
entityBatch = {}; // Clear the batch after applying
}
Expand Down Expand Up @@ -79,7 +79,7 @@ export async function setup({ ...config }: DojoConfig) {
const configClauses: Clause[] = [
{
Keys: {
keys: [WORLD_CONFIG_ID.toString(), undefined, undefined],
keys: [WORLD_CONFIG_ID.toString()],
pattern_matching: "FixedLen",
models: [],
},
Expand All @@ -91,6 +91,13 @@ export async function setup({ ...config }: DojoConfig) {
models: [],
},
},
{
Keys: {
keys: [WORLD_CONFIG_ID.toString(), undefined, undefined],
pattern_matching: "FixedLen",
models: [],
},
},
{
Keys: {
keys: [BUILDING_CATEGORY_POPULATION_CONFIG_ID.toString(), undefined],
Expand All @@ -107,34 +114,74 @@ export async function setup({ ...config }: DojoConfig) {
},
];

const CompositeStart = performance.now();
await getEntities(
network.toriiClient,
{ Composite: { operator: "Or", clauses: configClauses } },
network.contractComponents as any,
);
const CompositeEnd = performance.now();

// fetch all existing entities from torii
const SingleKeyStart = performance.now();
await getEntities(
network.toriiClient,
{
Keys: {
keys: [undefined],
pattern_matching: "FixedLen",
models: [],
models: [
"s0_eternum-AddressName",
"s0_eternum-Realm",
"s0_eternum-PopulationConfig",
"s0_eternum-CapacityConfig",
"s0_eternum-ProductionConfig",
"s0_eternum-RealmLevelConfig",
"s0_eternum-BankConfig",
"s0_eternum-Bank",
"s0_eternum-Trade",
"s0_eternum-Army",
// Probably load this w/ market
],
},
},
network.contractComponents as any,
[],
[],
40_000,
false,
);
const SingleKeyEnd = performance.now();

const DoubleKeyStart = performance.now();
await getEntities(
network.toriiClient,
{
Keys: {
keys: [undefined, undefined],
pattern_matching: "FixedLen",
models: ["s0_eternum-CapacityConfigCategory", "s0_eternum-ResourceCost"],
},
},
network.contractComponents as any,
[],
[],
40_000,
false,
);
const DoubleKeyEnd = performance.now();

const SyncStart = performance.now();
const sync = await syncEntitiesDebounced(network.toriiClient, network.contractComponents as any, [], false);
const SyncEnd = performance.now();

configManager.setDojo(components);

const eventSync = getEvents(
network.toriiClient,
network.contractComponents.events as any,
[],
[],
20000,
{
Keys: {
Expand All @@ -151,14 +198,18 @@ export async function setup({ ...config }: DojoConfig) {
"s0_eternum-SwapEvent",
"s0_eternum-LiquidityEvent",
"s0_eternum-HyperstructureContribution",
"s0_eternum-MapExplored",
],
},
},
false,
false,
);

console.log("CompositeEnd", CompositeEnd - CompositeStart);
console.log("SyncEnd", SyncEnd - SyncStart);
console.log("SingleKeyEnd", SingleKeyEnd - SingleKeyStart);
console.log("DoubleKeyEnd", DoubleKeyEnd - DoubleKeyStart);

return {
network,
components,
Expand Down
3 changes: 3 additions & 0 deletions client/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ async function init() {

root.render(<LoadingScreen backgroundImage={backgroundImage} />);

const setupStart = performance.now();
const setupResult = await setup(dojoConfig);
const setupEnd = performance.now();
console.log("SetupEnd", setupEnd - setupStart);

const graphic = new GameRenderer(setupResult);

Expand Down
Loading

0 comments on commit 7e18f86

Please sign in to comment.