-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathuseVsMaiaController.ts
More file actions
172 lines (149 loc) · 4.61 KB
/
useVsMaiaController.ts
File metadata and controls
172 lines (149 loc) · 4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import { useEffect } from 'react'
import { Chess } from 'chess.ts'
import { PlayGameConfig } from 'src/types'
import { backOff } from 'exponential-backoff'
import { useStats } from 'src/hooks/useStats'
import { usePlayController } from 'src/hooks/usePlayController'
import { fetchGameMove, logGameMove, fetchPlayPlayerStats } from 'src/api'
import { useSound } from 'src/hooks/useSound'
import { safeUpdateRating } from 'src/lib/ratingUtils'
const playStatsLoader = async () => {
const stats = await fetchPlayPlayerStats()
return {
gamesPlayed: stats.playGamesPlayed,
gamesWon: stats.playWon,
rating: stats.playElo,
}
}
export const useVsMaiaPlayController = (
id: string,
playGameConfig: PlayGameConfig,
simulateMaiaTime: boolean,
) => {
const controller = usePlayController(id, playGameConfig)
const [stats, incrementStats, updateRating] = useStats(playStatsLoader)
const { playMoveSound } = useSound()
const makePlayerMove = async (moveUci: string) => {
const moveTime = controller.updateClock()
controller.addMoveWithTime(moveUci, moveTime)
}
useEffect(() => {
let canceled = false
let moveTimeout: ReturnType<typeof setTimeout> | undefined
const makeMaiaMove = async () => {
if (
controller.game.id &&
!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 backOff(
() =>
fetchGameMove(
controller.moveList,
playGameConfig.maiaVersion,
playGameConfig.startFen,
null,
simulateMaiaTime ? initialClock : 0,
simulateMaiaTime ? maiaClock : 0,
),
{
jitter: 'full',
},
)
const nextMove = maiaMoves['top_move']
const moveDelay = maiaMoves['move_delay']
if (canceled) {
return
}
if (simulateMaiaTime) {
const minimumDelayMs = 200 + Math.random() * 100
const delayMs = Math.max(moveDelay * 1000, minimumDelayMs)
moveTimeout = setTimeout(() => {
if (canceled) {
return
}
const moveTime = controller.updateClock()
const chess = new Chess(controller.currentNode.fen)
const destinationSquare = nextMove.slice(2, 4)
const isCapture = !!chess.get(destinationSquare)
controller.addMoveWithTime(nextMove, moveTime)
playMoveSound(isCapture)
}, delayMs)
} else {
const moveTime = controller.updateClock()
const chess = new Chess(controller.currentNode.fen)
const destinationSquare = nextMove.slice(2, 4)
const isCapture = !!chess.get(destinationSquare)
controller.addMoveWithTime(nextMove, moveTime)
playMoveSound(isCapture)
}
}
}
makeMaiaMove()
return () => {
canceled = true
if (moveTimeout) {
clearTimeout(moveTimeout)
}
}
}, [
controller.game.id,
controller.playerActive,
controller.game.termination,
controller.moveList.length,
playGameConfig.maiaVersion,
playGameConfig.startFen,
simulateMaiaTime,
])
useEffect(() => {
const gameOverState = controller.game.termination?.type || 'not_over'
if (controller.moveList.length == 0 && gameOverState == 'not_over') {
return
}
const winner = controller.game.termination?.winner
const submitFn = async () => {
const response = await backOff(
() =>
logGameMove(
controller.game.id,
controller.moveList,
controller.moveTimes,
gameOverState,
'play',
playGameConfig.startFen || undefined,
winner,
),
{
jitter: 'full',
},
)
if (controller.game.termination) {
const winner = controller.game.termination?.winner
safeUpdateRating(response.player_elo, updateRating)
incrementStats(1, winner == playGameConfig.player ? 1 : 0)
}
}
submitFn()
}, [
controller.game.id,
controller.moveList,
controller.game.termination,
controller.moveTimes,
playGameConfig.startFen,
incrementStats,
updateRating,
playGameConfig.player,
])
return {
...controller,
makePlayerMove,
stats,
}
}