-
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.
- Loading branch information
1 parent
a6663dd
commit a8422ff
Showing
8 changed files
with
257 additions
and
50 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
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,104 @@ | ||
// onload -> fetch single key entities | ||
|
||
import { Component, Metadata, Schema } from "@dojoengine/recs"; | ||
import { setEntities } from "@dojoengine/state"; | ||
import { Clause, EntityKeysClause, PatternMatching, 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, | ||
order_by: [], | ||
}); | ||
|
||
setEntities(entities, components); | ||
|
||
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 = false, | ||
) => { | ||
// 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); | ||
}); | ||
}; | ||
|
||
export const addToSubscription = async <S extends Schema>( | ||
client: ToriiClient, | ||
components: Component<S, Metadata, undefined>[], | ||
entityID: string, | ||
position?: { x: number; y: number }, | ||
) => { | ||
const positionClause: EntityKeysClause = { | ||
Keys: { | ||
keys: [String(position?.x || 0), String(position?.y || 0), undefined, undefined], | ||
pattern_matching: "FixedLen" as PatternMatching, | ||
models: [], | ||
}, | ||
}; | ||
|
||
await getEntities( | ||
client, | ||
{ | ||
Composite: { | ||
operator: "Or", | ||
clauses: [ | ||
positionClause, | ||
{ | ||
Keys: { | ||
keys: [entityID], | ||
pattern_matching: "FixedLen", | ||
models: [], | ||
}, | ||
}, | ||
{ | ||
Keys: { | ||
keys: [entityID, undefined], | ||
pattern_matching: "FixedLen", | ||
models: [], | ||
}, | ||
}, | ||
{ | ||
Keys: { | ||
keys: [entityID, undefined, undefined], | ||
pattern_matching: "FixedLen", | ||
models: [], | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
components, | ||
10_000, | ||
false, | ||
); | ||
}; |
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,39 @@ | ||
import { addToSubscription } from "@/dojo/queries"; | ||
import { useEffect, useMemo, useState } from "react"; | ||
import { useDojo } from "../context/DojoContext"; | ||
import { useEntities } from "./useEntities"; | ||
|
||
export const useSyncEntity = (entityIds: number | number[]) => { | ||
const dojo = useDojo(); | ||
const [isSyncing, setIsSyncing] = useState(false); | ||
|
||
useEffect(() => { | ||
setIsSyncing(true); | ||
const fetch = async () => { | ||
try { | ||
const ids = Array.isArray(entityIds) ? entityIds : [entityIds]; | ||
await Promise.all( | ||
ids.map((id) => | ||
addToSubscription(dojo.network.toriiClient, dojo.network.contractComponents as any, id.toString()), | ||
), | ||
); | ||
} catch (error) { | ||
console.error("Fetch failed", error); | ||
} finally { | ||
setIsSyncing(false); | ||
} | ||
}; | ||
fetch(); | ||
}, [entityIds]); | ||
|
||
return isSyncing; | ||
}; | ||
|
||
export const useSyncPlayerRealms = () => { | ||
const { playerRealms } = useEntities(); | ||
const realmEntityIds = useMemo(() => { | ||
return playerRealms.map((realm) => realm!.entity_id); | ||
}, [playerRealms]); | ||
|
||
return useSyncEntity(realmEntityIds); | ||
}; |
Oops, something went wrong.