Skip to content

Commit

Permalink
update entropy fetch mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
starknetdev committed Jun 26, 2024
1 parent 2494c76 commit 594e99d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 22 deletions.
22 changes: 0 additions & 22 deletions ui/src/app/containers/UpgradeScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ export default function UpgradeScreen({
const equipItems = useUIStore((state) => state.equipItems);
const dropItems = useUIStore((state) => state.dropItems);
const entropyReady = useUIStore((state) => state.entropyReady);
const setEntropyReady = useUIStore((state) => state.setEntropyReady);
const onKatana = useUIStore((state) => state.onKatana);
const setVitBoostRemoved = useUIStore((state) => state.setVitBoostRemoved);
const pendingMessage = useLoadingStore((state) => state.pendingMessage);
Expand All @@ -99,27 +98,6 @@ export default function UpgradeScreen({

const { play: clickPlay } = useUiSounds(soundSelector.click);

useEffect(() => {
if (onKatana) return;
const fetchEntropy = async () => {
const entropy = await gameContract!.call("get_adventurer_entropy", [
adventurer?.id!,
]);
if (entropy !== BigInt(0)) {
setEntropyReady(true);
clearInterval(interval);
}
};

// Call the function immediately
fetchEntropy();

// Set up the interval to call the function every 5 seconds
const interval = setInterval(fetchEntropy, 5000);

return () => clearInterval(interval); // Cleanup on component unmount
}, []);

const setData = useQueriesStore((state) => state.setData);

useEffect(() => {
Expand Down
4 changes: 4 additions & 0 deletions ui/src/app/hooks/useUIStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ type State = {
setIsMintingLords: (value: boolean) => void;
averageBlockTime: number;
setAverageBlockTime: (value: number) => void;
adventurerEntropy: bigint;
setAdventurerEntropy: (value: bigint) => void;
entropyReady: boolean;
setEntropyReady: (value: boolean) => void;
loginScreen: boolean;
Expand Down Expand Up @@ -176,6 +178,8 @@ const useUIStore = create<State>((set) => ({
setIsMintingLords: (value) => set({ isMintingLords: value }),
averageBlockTime: 0,
setAverageBlockTime: (value) => set({ averageBlockTime: value }),
adventurerEntropy: BigInt(0),
setAdventurerEntropy: (value) => set({ adventurerEntropy: value }),
entropyReady: false,
setEntropyReady: (value) => set({ entropyReady: value }),
loginScreen: false,
Expand Down
26 changes: 26 additions & 0 deletions ui/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ function Home() {
const showDeathDialog = useUIStore((state) => state.showDeathDialog);
const setStartOption = useUIStore((state) => state.setStartOption);
const setEntropyReady = useUIStore((state) => state.setEntropyReady);
const setAdventurerEntropy = useUIStore(
(state) => state.setAdventurerEntropy
);
const [accountChainId, setAccountChainId] = useState<
constants.StarknetChainId | undefined
>();
Expand Down Expand Up @@ -521,6 +524,29 @@ function Home() {
}
}, [onboarded]);

useEffect(() => {
const fetchEntropy = async () => {
if (adventurer?.id) {
const entropy = await gameContract!.call("get_adventurer_entropy", [
adventurer?.id!,
]);
if (entropy !== BigInt(0)) {
setAdventurerEntropy(BigInt(entropy.toString()));
setEntropyReady(true);
clearInterval(interval);
}
}
};

// Call the function immediately
fetchEntropy();

// Set up the interval to call the function every 5 seconds
const interval = setInterval(fetchEntropy, 5000);

return () => clearInterval(interval); // Cleanup on component unmount
}, [adventurer?.level]);

if (!isConnected && disconnected) {
return <WalletSelect />;
}
Expand Down

0 comments on commit 594e99d

Please sign in to comment.