Skip to content

Commit

Permalink
Merge branch 'next' of https://github.com/BibliothecaDAO/eternum into…
Browse files Browse the repository at this point in the history
… raschel/bugs
  • Loading branch information
aymericdelab committed Jan 23, 2025
2 parents f3ab123 + 44f0db3 commit 58be88f
Show file tree
Hide file tree
Showing 7 changed files with 325 additions and 240 deletions.
45 changes: 23 additions & 22 deletions client/apps/game/src/dojo/debounced-queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { ToriiClient } from "@dojoengine/torii-client";
import debounce from "lodash/debounce";
import {
addDonkeysAndArmiesSubscription,
addHyperstructureSubscription,
addMarketSubscription,
addToSubscription,
addToSubscriptionOneKeyModelbyRealmEntityId,
getEntitiesFromTorii,
getMarketFromTorii,
getOneKeyModelbyRealmEntityIdFromTorii,
getTwoKeyModelbyRealmEntityIdFromTorii,
} from "./queries";

// Queue class to manage requests
Expand Down Expand Up @@ -52,70 +52,71 @@ class RequestQueue {

const subscriptionQueue = new RequestQueue();
const marketQueue = new RequestQueue();
const hyperstructureQueue = new RequestQueue();

export const debouncedAddToSubscriptionOneKey = debounce(
export const debouncedTwoKeyEntitiesFromTorii = debounce(
async <S extends Schema>(
client: ToriiClient,
components: Component<S, Metadata, undefined>[],
entityID: string[],
onComplete?: () => void,
) => {
await subscriptionQueue.add(
() => addToSubscriptionOneKeyModelbyRealmEntityId(client, components, entityID),
onComplete,
);
await subscriptionQueue.add(() => getTwoKeyModelbyRealmEntityIdFromTorii(client, components, entityID), onComplete);
},
250,
{ leading: true },
);

export const debounceAddDonkeysAndArmiesSubscription = debounce(
export const debouncedGetOneKeyEntitiesByRealmEntityIdFromTorii = debounce(
async <S extends Schema>(
client: ToriiClient,
components: Component<S, Metadata, undefined>[],
entityID: number[],
entityID: string[],
onComplete?: () => void,
) => {
await subscriptionQueue.add(() => addDonkeysAndArmiesSubscription(client, components, entityID), onComplete);
await subscriptionQueue.add(() => getOneKeyModelbyRealmEntityIdFromTorii(client, components, entityID), onComplete);
},
250,
{ leading: true },
);

export const debouncedAddToSubscription = debounce(
export const debouncedGetDonkeysAndArmiesFromTorii = debounce(
async <S extends Schema>(
client: ToriiClient,
components: Component<S, Metadata, undefined>[],
entityID: string[],
position?: { x: number; y: number }[],
entityID: number[],
onComplete?: () => void,
) => {
await subscriptionQueue.add(() => addToSubscription(client, components, entityID, position), onComplete);
await subscriptionQueue.add(() => addDonkeysAndArmiesSubscription(client, components, entityID), onComplete);
},
250,
{ leading: true },
);

export const debouncedAddMarketSubscription = debounce(
export const debouncedGetEntitiesFromTorii = debounce(
async <S extends Schema>(
client: ToriiClient,
components: Component<S, Metadata, undefined>[],
entityID: string[],
entityModels: string[],
positions?: { x: number; y: number }[],
onComplete?: () => void,
) => {
await marketQueue.add(() => addMarketSubscription(client, components), onComplete);
await subscriptionQueue.add(
() => getEntitiesFromTorii(client, components, entityID, entityModels, positions),
onComplete,
);
},
500,
250,
{ leading: true },
);

const debouncedAddHyperstructureSubscription = debounce(
export const debouncedGetMarketFromTorii = debounce(
async <S extends Schema>(
client: ToriiClient,
components: Component<S, Metadata, undefined>[],
onComplete?: () => void,
) => {
await hyperstructureQueue.add(() => addHyperstructureSubscription(client, components), onComplete);
await marketQueue.add(() => getMarketFromTorii(client, components), onComplete);
},
500,
{ leading: true },
Expand Down
137 changes: 70 additions & 67 deletions client/apps/game/src/dojo/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
import { Component, Metadata, Schema } from "@dojoengine/recs";
import { getEntities } from "@dojoengine/state";
import { PatternMatching, ToriiClient } from "@dojoengine/torii-client";

import { LogicalOperator } from "@dojoengine/torii-wasm";
// on hexception -> fetch below queries based on entityID

// background sync after load ->
export const addToSubscriptionOneKeyModelbyRealmEntityId = async <S extends Schema>(
export const getTwoKeyModelbyRealmEntityIdFromTorii = async <S extends Schema>(
client: ToriiClient,
components: Component<S, Metadata, undefined>[],
entityID: string[],
Expand All @@ -20,30 +19,26 @@ export const addToSubscriptionOneKeyModelbyRealmEntityId = async <S extends Sche
clauses: [
...entityID.map((id) => ({
Keys: {
keys: [id],
keys: [id, undefined],
pattern_matching: "VariableLen" as PatternMatching,
models: ["s1_eternum-ArrivalTime", "s1_eternum-OwnedResourcesTracker"],
models: ["s1_eternum-BuildingQuantityv2"],
},
})),
],
},
},
components,
[],
[],
5_000,
["s1_eternum-BuildingQuantityv2"],
20_000,
);
};

export const addToSubscription = async <S extends Schema>(
export const getOneKeyModelbyRealmEntityIdFromTorii = async <S extends Schema>(
client: ToriiClient,
components: Component<S, Metadata, undefined>[],
entityID: string[],
position?: { x: number; y: number }[],
) => {
const start = performance.now();

console.log("AddToSubscriptionStart", entityID);
await getEntities(
client,
{
Expand All @@ -57,33 +52,64 @@ export const addToSubscription = async <S extends Schema>(
models: [],
},
})),
...(position
? position.map((position) => ({
Keys: {
keys: [String(position?.x || 0), String(position?.y || 0), undefined, undefined],
pattern_matching: "FixedLen" as PatternMatching,
models: [],
},
}))
: []),
],
},
},
components as any,
[],
components,
[],
5_000,
["s1_eternum-ArrivalTime", "s1_eternum-OwnedResourcesTracker"],
20_000,
);
const end = performance.now();
console.log("AddToSubscriptionEnd", end - start);
};

export const addMarketSubscription = async <S extends Schema>(
export const getEntitiesFromTorii = async <S extends Schema>(
client: ToriiClient,
components: Component<S, Metadata, undefined>[],
entityIDs: string[],
entityModels: string[],
positions?: { x: number; y: number }[],
) => {
const start = performance.now();
await getEntities(
const query =
!positions && entityIDs.length === 1
? {
Keys: {
keys: [entityIDs[0]],
pattern_matching: "VariableLen" as PatternMatching,
models: [],
},
}
: {
Composite: {
operator: "Or" as LogicalOperator,
clauses: [
...entityIDs.map((id) => ({
Keys: {
keys: [id],
pattern_matching: "VariableLen" as PatternMatching,
models: [],
},
})),
...(positions
? positions.map((position) => ({
Keys: {
keys: [String(position?.x || 0), String(position?.y || 0), undefined, undefined],
pattern_matching: "FixedLen" as PatternMatching,
models: [],
},
}))
: []),
],
},
};
await getEntities(client, query, components as any, [], entityModels, 40_000);
};

export const getMarketFromTorii = async <S extends Schema>(
client: ToriiClient,
components: Component<S, Metadata, undefined>[],
) => {
let start = performance.now();
const resourcePromise = await getEntities(
client,
{
Member: {
Expand All @@ -95,59 +121,39 @@ export const addMarketSubscription = async <S extends Schema>(
},
components,
[],
[],
["s1_eternum-DetachedResource"],
30_000,
false,
);
const end = performance.now();
console.log("MarketEnd", end - start);
};
let end = performance.now();
console.log("[keys] detached resource query", end - start);

export const addHyperstructureSubscription = async <S extends Schema>(
client: ToriiClient,
components: Component<S, Metadata, undefined>[],
) => {
const start = performance.now();
await getEntities(
start = performance.now();
const marketPromise = await getEntities(
client,
{
Composite: {
operator: "Or",
clauses: [
{
Keys: {
keys: [undefined, undefined],
pattern_matching: "FixedLen",
models: ["s1_eternum-Epoch", "s1_eternum-Progress"],
},
},
{
Keys: {
keys: [undefined, undefined, undefined],
pattern_matching: "FixedLen",
models: ["s1_eternum-Contribution"],
},
},
],
Keys: {
keys: [undefined],
pattern_matching: "VariableLen",
models: [],
},
},
components as any,
[],
components,
[],
40_000,
["s1_eternum-Market", "s1_eternum-Liquidity"],
30_000,
false,
);
const end = performance.now();
console.log("HyperstructureEnd", end - start);
await Promise.all([resourcePromise, marketPromise]);
end = performance.now();
console.log("[keys] market query", end - start);
};

export const addDonkeysAndArmiesSubscription = async <S extends Schema>(
client: ToriiClient,
components: Component<S, Metadata, undefined>[],
entityIds: number[],
) => {
const start = performance.now();
console.log("ArrivalsEnd: starting resource arrivals");
await getEntities(
client,
{
Expand All @@ -163,7 +169,6 @@ export const addDonkeysAndArmiesSubscription = async <S extends Schema>(
})),
},
},

components,
[],
[
Expand All @@ -181,6 +186,4 @@ export const addDonkeysAndArmiesSubscription = async <S extends Schema>(
1000,
false,
);
const end = performance.now();
console.log("ArrivalsEnd", end - start);
};
Loading

0 comments on commit 58be88f

Please sign in to comment.