Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/next' into bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
RedBeardEth committed Dec 21, 2024
2 parents 216c3bf + b4b9145 commit 775e9cc
Show file tree
Hide file tree
Showing 44 changed files with 1,095 additions and 580 deletions.
2 changes: 2 additions & 0 deletions client/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import "./index.css";
import { Toaster } from "./ui/components/Toaster";
import { TransactionNotification } from "./ui/components/TxEmit";
import { WorldLoading } from "./ui/components/WorldLoading";
import { World } from "./ui/layouts/World";

function App({ backgroundImage }: { backgroundImage: string }) {
Expand All @@ -9,6 +10,7 @@ function App({ backgroundImage }: { backgroundImage: string }) {
<Toaster />
<TransactionNotification />
<World backgroundImage={backgroundImage} />
<WorldLoading />
</>
);
}
Expand Down
163 changes: 163 additions & 0 deletions client/src/dojo/debouncedQueries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
import { Component, Metadata, Schema } from "@dojoengine/recs";
import { ToriiClient } from "@dojoengine/torii-client";
import debounce from "lodash/debounce";
import {
addArrivalsSubscription,
addHyperstructureSubscription,
addMarketSubscription,
addToSubscription,
addToSubscriptionOneKeyModelbyRealmEntityId,
addToSubscriptionTwoKeyModelbyRealmEntityId,
syncPosition,
} from "./queries";

// Queue class to manage requests
class RequestQueue {
private queue: Array<() => Promise<void>> = [];
private processing = false;
private batchSize = 3; // Number of concurrent requests
private batchDelayMs = 100; // Delay between batches

async add(request: () => Promise<void>, onComplete?: () => void) {
this.queue.push(async () => {
await request();
onComplete?.(); // Call onComplete after the request is processed
});
if (!this.processing) {
this.processing = true;
this.processQueue();
}
}

private async processQueue() {
while (this.queue.length > 0) {
const batch = this.queue.splice(0, this.batchSize);

try {
await Promise.all(batch.map((request) => request()));
} catch (error) {
console.error("Error processing request batch:", error);
}

if (this.queue.length > 0) {
// Add delay between batches to prevent overwhelming the server
await new Promise((resolve) => setTimeout(resolve, this.batchDelayMs));
}
}
this.processing = false;
}

clear() {
this.queue = [];
}
}

// Create separate queues for different types of requests
const positionQueue = new RequestQueue();
const subscriptionQueue = new RequestQueue();
const marketQueue = new RequestQueue();
const hyperstructureQueue = new RequestQueue();
// Debounced functions that add to queues
export const debouncedSyncPosition = debounce(
async <S extends Schema>(
client: ToriiClient,
components: Component<S, Metadata, undefined>[],
entityID: string,
onComplete?: () => void,
) => {
await positionQueue.add(() => syncPosition(client, components, entityID), onComplete);
},
100,
{ leading: true }, // Add leading: true to execute immediately on first call
);

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

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

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

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

export const debouncedAddMarketSubscription = debounce(
async <S extends Schema>(
client: ToriiClient,
components: Component<S, Metadata, undefined>[],
onComplete?: () => void,
) => {
await marketQueue.add(() => addMarketSubscription(client, components), onComplete);
},
500,
{ leading: true },
);

export const debouncedAddHyperstructureSubscription = debounce(
async <S extends Schema>(
client: ToriiClient,
components: Component<S, Metadata, undefined>[],
onComplete?: () => void,
) => {
await hyperstructureQueue.add(() => addHyperstructureSubscription(client, components), onComplete);
},
500,
{ leading: true },
);

// Utility function to clear all queues if needed
export const clearAllQueues = () => {
positionQueue.clear();
subscriptionQueue.clear();
marketQueue.clear();
hyperstructureQueue.clear();
};
124 changes: 116 additions & 8 deletions client/src/dojo/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const syncPosition = async <S extends Schema>(
components,
[],
[],
30_000,
5_000,
);
};

Expand Down Expand Up @@ -53,7 +53,7 @@ export const addToSubscriptionTwoKeyModelbyRealmEntityId = async <S extends Sche
components,
[],
[],
30_000,
5_000,
);
};

Expand Down Expand Up @@ -81,7 +81,7 @@ export const addToSubscriptionOneKeyModelbyRealmEntityId = async <S extends Sche
components,
[],
[],
30_000,
5_000,
);
};

Expand Down Expand Up @@ -122,7 +122,7 @@ export const addToSubscription = async <S extends Schema>(
components as any,
[],
[],
30_000,
5_000,
);
const end = performance.now();
console.log("AddToSubscriptionEnd", end - start);
Expand All @@ -136,10 +136,11 @@ export const addMarketSubscription = async <S extends Schema>(
await getEntities(
client,
{
Keys: {
keys: [undefined, undefined],
pattern_matching: "FixedLen",
models: ["s0_eternum-DetachedResource"],
Member: {
model: "s0_eternum-DetachedResource",
member: "resource_amount",
operator: "Gt",
value: { Primitive: { U128: "0" } },
},
},
components,
Expand All @@ -151,3 +152,110 @@ export const addMarketSubscription = async <S extends Schema>(
const end = performance.now();
console.log("MarketEnd", end - start);
};

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

export const addArrivalsSubscription = 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,
// todo: waiting on ghlim to check issue with this query
// {
// Composite: {
// operator: "And",
// clauses: [
// {
// Composite: {
// operator: "Or",
// clauses: entityIds.map((id) => ({
// Member: {
// model: "s0_eternum-EntityOwner",
// member: "entity_owner_id",
// operator: "Eq",
// value: { Primitive: { U32: id } },
// },
// })),
// },
// },
// {
// Member: {
// model: "s0_eternum-OwnedResourcesTracker",
// member: "resource_types",
// operator: "Neq",
// value: { Primitive: { U256: "0" } },
// },
// },
// ],
// },
// },
{
Composite: {
operator: "Or",
clauses: entityIds.map((id) => ({
Member: {
model: "s0_eternum-EntityOwner",
member: "entity_owner_id",
operator: "Eq",
value: { Primitive: { U32: id } },
},
})),
},
},

components,
[],
[
"s0_eternum-Army",
"s0_eternum-Position",
"s0_eternum-EntityOwner",
"s0_eternum-Weight",
"s0_eternum-OwnedResourcesTracker",
"s0_eternum-ArrivalTime",
],
1000,
false,
);
const end = performance.now();
console.log("ArrivalsEnd", end - start);
};
Loading

0 comments on commit 775e9cc

Please sign in to comment.