Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
ponderingdemocritus committed Dec 12, 2024
2 parents 90afac3 + 5989d8e commit d69e713
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 169 deletions.
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"
}
}
}
251 changes: 121 additions & 130 deletions client/src/dojo/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,158 +2,149 @@

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

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

// background sync after load ->
// 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
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;
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,
});
while (continueFetching) {
const entities = await client.getEntities({
limit,
offset,
clause,
dont_include_hashed_keys: false,
});

console.log("entities", entities);
console.log("entities", entities);

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

setEntities(entities, components, logging);
setEntities(entities, components, logging);

if (Object.keys(entities).length < limit) {
continueFetching = false;
} else {
offset += limit;
}
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
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);
}
);
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, subscription: Subscription, components: Component<S, Metadata, undefined>[], entityID: string, position?: {x: number, y: number}) => {

console.log("position", subscription);

await getEntities(client, { ...entityQueryOneKey(entityID), Keys: { ...entityQueryOneKey(entityID).Keys, pattern_matching: "FixedLen" } }, components, 1000, false);

await getEntities(client, { ...entityQueryTwoKey(entityID), Keys: { ...entityQueryTwoKey(entityID).Keys, pattern_matching: "FixedLen" } }, components, 1000, false);

await getEntities(client, { ...entityQueryThreeKey(entityID), Keys: { ...entityQueryThreeKey(entityID).Keys, pattern_matching: "FixedLen" } }, 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);

// await client.updateEntitySubscription(subscription, [
// { ...entityQueryOneKey(entityID), Keys: { ...entityQueryOneKey(entityID).Keys, pattern_matching: "FixedLen" } },
// ]);

// await client.updateEntitySubscription(subscription, [
// { ...entityQueryTwoKey(entityID), Keys: { ...entityQueryTwoKey(entityID).Keys, pattern_matching: "FixedLen" } },
// ]);

// await client.updateEntitySubscription(subscription, [
// { ...entityQueryThreeKey(entityID), Keys: { ...entityQueryThreeKey(entityID).Keys, pattern_matching: "FixedLen" } },
// ]);

try {
await client.updateEntitySubscription(subscription, [
{ ...entityQueryOneKey(entityID), Keys: { ...entityQueryOneKey(entityID).Keys, pattern_matching: "FixedLen" } },
{ ...entityQueryTwoKey(entityID), Keys: { ...entityQueryTwoKey(entityID).Keys, pattern_matching: "FixedLen" } },
{ ...entityQueryThreeKey(entityID), Keys: { ...entityQueryThreeKey(entityID).Keys, pattern_matching: "FixedLen" } },
{
Keys: {
keys: [String(position?.x || 0), String(position?.y || 0), undefined, undefined],
pattern_matching: 'FixedLen',
models: [
],
},
},
]);
} catch (error) {
console.error("Error updating entity subscription:", error);
}
}
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: [],
},
}
}
return {
Keys: {
keys: [entityID],
pattern_matching: "FixedLen",
models: [],
},
} as EntityKeysClause;
};

const entityQueryTwoKey = (entityID: string) => {
return {
Keys: {
keys: [entityID, undefined],
pattern_matching: 'FixedLen',
models: [
],
},
}
}
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: [
],
},
}
}

const entityQueryFourKey = ( x: number, y: number) => {
return {
Keys: {
keys: [String(x), String(y), undefined, undefined],
pattern_matching: 'FixedLen',
models: [
],
},
}
}



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;
};
Loading

0 comments on commit d69e713

Please sign in to comment.