Skip to content

Commit

Permalink
Merge pull request #2438 from BibliothecaDAO/loaf-opti
Browse files Browse the repository at this point in the history
Loaf opti
  • Loading branch information
ponderingdemocritus authored Dec 12, 2024
2 parents 32dd7ab + cb4ca20 commit 32ee747
Show file tree
Hide file tree
Showing 13 changed files with 393 additions and 12 deletions.
4 changes: 2 additions & 2 deletions client/.env.production
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
VITE_PUBLIC_MASTER_ADDRESS=
VITE_PUBLIC_MASTER_PRIVATE_KEY=
VITE_PUBLIC_MASTER_ADDRESS=0x5013b17c43a2b664ec2a38aa45f6d891db1188622ec7cf320411321c3248fb5
VITE_PUBLIC_MASTER_PRIVATE_KEY=0x5013b17c43a2b664ec2a38aa45f6d891db1188622ec7cf320411321c3248fb5
VITE_PUBLIC_WORLD_ADDRESS="0x5013b17c43a2b664ec2a38aa45f6d891db1188622ec7cf320411321c3248fb5"
VITE_PUBLIC_ACCOUNT_CLASS_HASH="0x07dc7899aa655b0aae51eadff6d801a58e97dd99cf4666ee59e704249e51adf2"
VITE_PUBLIC_FEE_TOKEN_ADDRESS=0x49d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7
Expand Down
3 changes: 2 additions & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@cartridge/connector": "0.5.5",
"@cartridge/controller": "0.5.5",
"@dojoengine/core": "1.0.1",
"@dojoengine/torii-wasm": "1.0.1",
"@dojoengine/create-burner": "1.0.1",
"@dojoengine/react": "1.0.1",
"@dojoengine/recs": "^2.0.13",
Expand Down Expand Up @@ -104,4 +105,4 @@
"vitest": "^2.0.5",
"workbox-window": "^7.3.0"
}
}
}
150 changes: 150 additions & 0 deletions client/src/dojo/queries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
// onload -> fetch single key entities

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

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

// background sync after load ->

export const getEntities = async <S extends Schema>(
client: ToriiClient,
clause: Clause | undefined,
components: Component<S, Metadata, undefined>[],
limit: number = 100,
logging: boolean = false,
) => {
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,
});

console.log("entities", entities);

if (logging) console.log(`Fetched ${entities} entities`);

setEntities(entities, components, logging);

if (Object.keys(entities).length < limit) {
continueFetching = false;
} else {
offset += limit;
}
}
};

export const syncEntitiesEternum = async <S extends Schema>(
client: ToriiClient,
components: Component<S, Metadata, undefined>[],
entityKeyClause: EntityKeysClause[],
logging: boolean = true,
) => {
if (logging) console.log("Starting syncEntities");
return await client.onEntityUpdated(entityKeyClause, (fetchedEntities: any, data: any) => {
if (logging) console.log("Entity updated", fetchedEntities);

setEntities({ [fetchedEntities]: data }, components, logging);
});
};

export const addToSubscription = async <S extends Schema>(
client: ToriiClient,
syncObject: { sync: Subscription; clauses: EntityKeysClause[] },
components: Component<S, Metadata, undefined>[],
entityID: string,
position?: { x: number; y: number },
) => {
console.log("position", syncObject);

await getEntities(client, { ...(entityQueryOneKey(entityID) as Clause) }, components, 1000, false);

await getEntities(client, { ...(entityQueryTwoKey(entityID) as Clause) }, components, 1000, false);

await getEntities(client, { ...(entityQueryThreeKey(entityID) as Clause) }, components, 1000, false);

await getEntities(
client,
{
Keys: {
keys: [String(position?.x || 0), String(position?.y || 0), undefined, undefined],
pattern_matching: "FixedLen",
models: [],
},
},
components,
1000,
false,
);
const positionClause: EntityKeysClause = {
Keys: {
keys: [String(position?.x || 0), String(position?.y || 0), undefined, undefined],
pattern_matching: "FixedLen" as PatternMatching,
models: [],
},
};

const newSubscriptions = [
{ ...entityQueryOneKey(entityID) },
{ ...entityQueryTwoKey(entityID) },
{ ...entityQueryThreeKey(entityID) },
{ ...entityQueryFourKey(position?.x || 0, position?.y || 0) },
positionClause,
...syncObject.clauses,
];

try {
await client.updateEntitySubscription(syncObject.sync, newSubscriptions);
} catch (error) {
console.log("error", error);
}

syncObject.clauses = newSubscriptions;
};

const entityQueryOneKey = (entityID: string) => {
return {
Keys: {
keys: [entityID],
pattern_matching: "FixedLen",
models: [],
},
} as EntityKeysClause;
};

const entityQueryTwoKey = (entityID: string) => {
return {
Keys: {
keys: [entityID, undefined],
pattern_matching: "FixedLen",
models: [],
},
} as EntityKeysClause;
};

const entityQueryThreeKey = (entityID: string) => {
return {
Keys: {
keys: [entityID, undefined, undefined],
pattern_matching: "FixedLen",
models: [],
},
} as EntityKeysClause;
};

const entityQueryFourKey = (x: number, y: number) => {
return {
Keys: {
keys: [String(x), String(y), undefined, undefined],
pattern_matching: "FixedLen",
models: [],
},
} as EntityKeysClause;
};
146 changes: 144 additions & 2 deletions client/src/dojo/setup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { WORLD_CONFIG_ID } from "@bibliothecadao/eternum";
import { DojoConfig } from "@dojoengine/core";
import { getSyncEntities, getSyncEvents } from "@dojoengine/state";
import { Clause } from "@dojoengine/torii-client";
import { createClientComponents } from "./createClientComponents";
import { createSystemCalls } from "./createSystemCalls";
import { ClientConfigManager } from "./modelManager/ConfigManager";
Expand All @@ -14,8 +16,148 @@ export async function setup({ ...config }: DojoConfig) {
const components = createClientComponents(network);
const systemCalls = createSystemCalls(network);

const singleKeyQuery = {
Keys: {
keys: [undefined],
pattern_matching: "FixedLen",
models: [
"s0_eternum-AddressName",
"s0_eternum-Army",
"s0_eternum-ArrivalTime",
"s0_eternum-Bank",
"s0_eternum-BankConfig",
"s0_eternum-Battle",
"s0_eternum-BattleConfig",
"s0_eternum-Building",
"s0_eternum-BuildingCategoryPopConfig",
"s0_eternum-BuildingConfig",
"s0_eternum-BuildingGeneralConfig",
"s0_eternum-BuildingQuantityv2",
"s0_eternum-CapacityCategory",
"s0_eternum-CapacityConfig",
"s0_eternum-Contribution",
"s0_eternum-DetachedResource",
"s0_eternum-EntityName",
"s0_eternum-EntityOwner",
"s0_eternum-Epoch",
"s0_eternum-Guild",
"s0_eternum-GuildMember",
"s0_eternum-GuildWhitelist",
"s0_eternum-Health",
"s0_eternum-Hyperstructure",
"s0_eternum-HyperstructureConfig",
"s0_eternum-HyperstructureResourceConfig",
"s0_eternum-Leaderboard",
"s0_eternum-LeaderboardEntry",
"s0_eternum-LeaderboardRegistered",
"s0_eternum-LeaderboardRewardClaimed",
"s0_eternum-LevelingConfig",
"s0_eternum-Liquidity",
"s0_eternum-MapConfig",

"s0_eternum-MercenariesConfig",
"s0_eternum-Message",
"s0_eternum-Movable",
"s0_eternum-Orders",
"s0_eternum-OwnedResourcesTracker",
"s0_eternum-Owner",
"s0_eternum-Population",
"s0_eternum-PopulationConfig",
"s0_eternum-Position",
"s0_eternum-ProductionConfig",
"s0_eternum-ProductionDeadline",
"s0_eternum-ProductionInput",
"s0_eternum-ProductionOutput",

"s0_eternum-Protectee",
"s0_eternum-Protector",
"s0_eternum-Quantity",
"s0_eternum-QuantityTracker",

"s0_eternum-QuestConfig",
"s0_eternum-QuestRewardConfig",
// "s0_eternum-Realm",
],
},
};

const twoKeyQuery = {
Keys: {
keys: ["763", undefined],
pattern_matching: "FixedLen",
models: [
// 's0_eternum-Tile', - done
// 's0_eternum-BuildingQuantityv2', - done with Realm
// // 's0_eternum-Resource', - done with Realm
// // 's0_eternum-Production', - done with Realm
// 's0_eternum-GuildWhitelist', - add to subscription from somewhere
// 's0_eternum-Progress', - done
// 's0_eternum-HyperstructureContribution', - done
// 's0_eternum-Epoch', - done
// // 's0_eternum-QuestBonus', - done
// // "s0_eternum-Progress", - done
// // "s0_eternum-Market",
// 's0_eternum-Quest',
// 's0_eternum-Position',
],
},
};

const threeKeyQuery = {
Keys: {
keys: [undefined, undefined, undefined],
pattern_matching: "FixedLen",
models: ["s0_eternum-BuildingConfig", "s0_eternum-Liquidity"],
},
};

const fourKeyQuery = {
Keys: {
keys: [undefined, undefined, undefined, undefined],
pattern_matching: "FixedLen",
models: ["s0_eternum-Building"],
},
};

const clauses: Clause[] = [
{
Keys: {
keys: [undefined],
pattern_matching: "FixedLen",
models: [],
},
},
{
Keys: {
keys: [WORLD_CONFIG_ID.toString(), undefined],
pattern_matching: "VariableLen",
models: [],
},
},
{
Keys: {
keys: [WORLD_CONFIG_ID.toString()],
pattern_matching: "VariableLen",
models: [],
},
},
];

// fetch all existing entities from torii
const sync = await getSyncEntities(network.toriiClient, network.contractComponents as any, undefined, [], 20_000);
const sync = await getSyncEntities(
network.toriiClient,
network.contractComponents as any,
{ Composite: { operator: "Or", clauses: [...clauses] } },
[],
5000,
true,
);

const syncObject = {
sync,
clauses: [...clauses],
};

const eventSync = getSyncEvents(
network.toriiClient,
network.contractComponents.events as any,
Expand All @@ -32,7 +174,7 @@ export async function setup({ ...config }: DojoConfig) {
network,
components,
systemCalls,
sync,
syncObject,
eventSync,
};
}
12 changes: 12 additions & 0 deletions client/src/hooks/store/useWorldLoading.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// World loading state
import { create } from "zustand";

interface WorldState {
isWorldLoading: boolean;
setWorldLoading: (loading: boolean) => void;
}

export const useWorldStore = create<WorldState>((set) => ({
isWorldLoading: true,
setWorldLoading: (loading: boolean) => set({ isWorldLoading: loading }),
}));
2 changes: 2 additions & 0 deletions client/src/three/scenes/HexagonScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ export abstract class HexagonScene {

const { row, col } = this.getHexFromWorldPosition(position);

console.log("row", row, col);

return { row, col, x: position.x, z: position.z };
}

Expand Down
Loading

0 comments on commit 32ee747

Please sign in to comment.