-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: front end tick rate * fix: await tick tx
- Loading branch information
Showing
7 changed files
with
111 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import dayjs from "dayjs"; | ||
import { useEffect, useState } from "react"; | ||
import { useAutoBattleFn } from "./useAutoBattleFn"; | ||
|
||
export function useAutoBattle() { | ||
const { autoBattleFn } = useAutoBattleFn(); | ||
const [shouldRun, setShouldRun] = useState<boolean>(false); | ||
const [isRunning, setIsRunning] = useState<boolean>(false); | ||
const [runningStart, setRunningStart] = useState<number>(0); | ||
|
||
useEffect(() => { | ||
if (shouldRun) { | ||
if (!isRunning) { | ||
// set to now | ||
setRunningStart(dayjs().unix()); | ||
setIsRunning(true); | ||
|
||
// delay 0.5s to avoid on-chain fail and minimal tick interval | ||
setTimeout(() => { | ||
// run auto battle | ||
autoBattleFn() | ||
.then(() => { | ||
setIsRunning(false); | ||
}) | ||
.catch((e) => { | ||
setIsRunning(false); | ||
console.error(e); | ||
}); | ||
}, 500); | ||
} | ||
} | ||
}, [shouldRun, isRunning, setIsRunning, autoBattleFn, setRunningStart]); | ||
|
||
// check timeout | ||
useEffect(() => { | ||
if (shouldRun) { | ||
const interval = setInterval(() => { | ||
// running timeout 10s | ||
if (dayjs().unix() - runningStart > 10) { | ||
setIsRunning(false); | ||
console.log("running tick timeout"); | ||
} | ||
// check every seconds: | ||
}, 1000); | ||
return () => { | ||
clearInterval(interval); | ||
}; | ||
} | ||
}, [runningStart, shouldRun]); | ||
|
||
return { setShouldRun, shouldRun, isRunning }; | ||
} |
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,24 @@ | ||
import { useMUD } from "@/MUDContext"; | ||
import { useComponentValue } from "@latticexyz/react"; | ||
import { useCallback } from "react"; | ||
|
||
export function useAutoBattleFn() { | ||
const { | ||
components: { PlayerGlobal }, | ||
systemCalls: { autoBattle }, | ||
network: { localAccount, playerEntity }, | ||
} = useMUD(); | ||
|
||
const _playerGlobal = useComponentValue(PlayerGlobal, playerEntity); | ||
|
||
const autoBattleFn = useCallback(async () => { | ||
if (!_playerGlobal) { | ||
console.log("unknown player"); | ||
return false; | ||
} | ||
await autoBattle(_playerGlobal.gameId, localAccount); | ||
return true; | ||
}, [_playerGlobal, autoBattle, localAccount]); | ||
|
||
return { autoBattleFn }; | ||
} |
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,19 @@ | ||
import { useMUD } from "@/MUDContext"; | ||
import { useComponentValue } from "@latticexyz/react"; | ||
import { useEffect, useState } from "react"; | ||
|
||
export function useBoardStatus() { | ||
const { | ||
components: { Board }, | ||
network: { playerEntity }, | ||
} = useMUD(); | ||
|
||
const BoardList = useComponentValue(Board, playerEntity); | ||
|
||
const [currentBoardStatus, setCurrentBoardStatus] = useState(0); | ||
useEffect(() => { | ||
setCurrentBoardStatus(BoardList?.status || 0); | ||
}, [BoardList, BoardList?.status]); | ||
|
||
return { currentBoardStatus }; | ||
} |
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