Skip to content

Commit

Permalink
feat: added human-like move time simulation config
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinjosethomas committed Nov 10, 2024
1 parent ec16a8a commit 88068f3
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 12 deletions.
26 changes: 21 additions & 5 deletions src/api/play/play.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ export const getGameMove = async (
maiaVersion = 'maia_kdd_1900',
fen: string | null = null,
piece: string | null = null,
initial_clock = 0,
current_clock = 0,
) => {
//return new Chess(fen).moves({ verbose: true }).map((m) => m.from + m.to)

const res = await fetch(
buildUrl(
'play/get_move?' +
Expand All @@ -66,11 +66,27 @@ export const getGameMove = async (
fen: fen || '',
maia_name: maiaVersion,
piece: piece || '',
initial_clock: initial_clock.toString(),
current_clock: current_clock.toString(),
}
: {
maia_name: maiaVersion,
piece: piece || '',
initial_clock: initial_clock.toString(),
current_clock: current_clock.toString(),
}
: { maia_name: maiaVersion, piece: piece || '' }
: fen
? { fen: fen || '', maia_name: maiaVersion }
: { maia_name: maiaVersion },
? {
fen: fen || '',
maia_name: maiaVersion,
initial_clock: initial_clock.toString(),
current_clock: current_clock.toString(),
}
: {
maia_name: maiaVersion,
initial_clock: initial_clock.toString(),
current_clock: current_clock.toString(),
},
),
),
{
Expand Down
9 changes: 9 additions & 0 deletions src/components/modals/PlaySetupModal/PlaySetupModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,15 @@ export const PlaySetupModal: React.FC<Props> = (props: Props) => {
onChange={setTimeControl}
/>
</div>
<div className="flex flex-row items-center justify-start gap-2">
Maia time simulation:{' '}
<OptionSelect
options={[false, true]}
labels={['Play Instantly', 'Play with delay']}
selected={simulateMaiaTime}
onChange={setSimulateMaiaTime}
/>
</div>
{openMoreOptions ? (
<div className="flex cursor-pointer flex-row items-center justify-start gap-1 text-sm">
<input
Expand Down
1 change: 0 additions & 1 deletion src/hooks/useAnalysisController/useAnalysisController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ export const useAnalysisController = (
message: StockfishEvaluation,
moveIndex: number,
) => {
console.log(message)
setStockfishEvaluations((prev) => {
const newEvaluations = [...prev]
newEvaluations[moveIndex] = message
Expand Down
26 changes: 23 additions & 3 deletions src/pages/play/hb.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,17 +126,37 @@ const useHandBrainPlayController = (
let canceled = false

const makeMaiaMove = async () => {
const maiaClock =
(controller.player == 'white'
? controller.blackClock
: controller.whiteClock) / 1000
const initialClock = controller.timeControl.includes('+')
? parseInt(controller.timeControl.split('+')[0]) * 60
: 0

const maiaMoves = await getGameMove(
controller.moves,
playGameConfig.maiaVersion,
playGameConfig.startFen,
null,
playGameConfig.simulateMaiaTime ? initialClock : 0,
playGameConfig.simulateMaiaTime ? maiaClock : 0,
)
const nextMove = maiaMoves['top_move']
const moveDelay = maiaMoves['move_delay']

if (!canceled) {
controller.updateClock()
controller.setMoves([...controller.moves, nextMove])
if (canceled) {
return
}

setTimeout(
() => {
const moveTime = controller.updateClock()
controller.setMoves([...controller.moves, nextMove])
controller.setMoveTimes([...controller.moveTimes, moveTime])
},
playGameConfig.simulateMaiaTime ? moveDelay * 1000 : 0,
)
}

if (
Expand Down
25 changes: 22 additions & 3 deletions src/pages/play/maia.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,21 @@ const useVsMaiaPlayController = (
!controller.playerActive &&
!controller.game.termination
) {
const maiaClock =
(controller.player == 'white'
? controller.blackClock
: controller.whiteClock) / 1000
const initialClock = controller.timeControl.includes('+')
? parseInt(controller.timeControl.split('+')[0]) * 60
: 0

const maiaMoves = await getGameMove(
controller.moves,
playGameConfig.maiaVersion,
playGameConfig.startFen,
null,
playGameConfig.simulateMaiaTime ? initialClock : 0,
playGameConfig.simulateMaiaTime ? maiaClock : 0,
)
const nextMove = maiaMoves['top_move']
const moveDelay = maiaMoves['move_delay']
Expand All @@ -63,9 +74,17 @@ const useVsMaiaPlayController = (
return
}

const moveTime = controller.updateClock(moveDelay)
controller.setMoves([...controller.moves, nextMove])
controller.setMoveTimes([...controller.moveTimes, moveTime])
if (playGameConfig.simulateMaiaTime) {
setTimeout(() => {
const moveTime = controller.updateClock()
controller.setMoves([...controller.moves, nextMove])
controller.setMoveTimes([...controller.moveTimes, moveTime])
}, moveDelay * 1000)
} else {
const moveTime = controller.updateClock()
controller.setMoves([...controller.moves, nextMove])
controller.setMoveTimes([...controller.moveTimes, moveTime])
}
}
}

Expand Down

0 comments on commit 88068f3

Please sign in to comment.