-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2438 from BibliothecaDAO/loaf-opti
Loaf opti
- Loading branch information
Showing
13 changed files
with
393 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }), | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.