-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayLichessGame.nim
399 lines (317 loc) · 15.9 KB
/
playLichessGame.nim
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
import std/[
os,
posix,
strutils,
json,
strformat,
options,
times,
random
]
import
lichessNetUtils,
log,
lichessGame,
anarchyParameters,
historyOfGameResults,
positionUtils,
position,
timeManagedSearch,
types,
hashTable,
utils,
move,
anarchyConditions,
anarchyComments,
search,
evaluation
randomize()
doAssert commandLineParams().len == 6, "Need following arguments: <parent PID> <lichess gameId> <lichess token> <historyOfGameResultsFileName> <anarchyCommentsFileName> <hashSizeMegaByte>"
let
parentPID = commandLineParams()[0].parseInt.Pid
gameId = commandLineParams()[1]
token = commandLineParams()[2]
historyOfGameResultsFileName = commandLineParams()[3]
anarchyCommentsFileName = commandLineParams()[4]
hashSizeMegaByte = commandLineParams()[5].parseInt
loadAnarchyComments anarchyCommentsFileName
logInfo &"Arguments:\n{parentPID = }\n{gameId = }\n{token = }\n{historyOfGameResultsFileName = }\n{hashSizeMegaByte = }"
proc lastEnemyMoveWasKingCloudMove(gameState: LichessGameState, botColor: Color): bool =
doAssert gameState.currentPosition.us == botColor, "Can only be called if next move is a bot move"
if gameState.positionMoveHistory.len == 0:
return false
return gameState.positionMoveHistory[^1].position.isCloudKingMove(gameState.positionMoveHistory[^1].move)
func getCloudKingMove(position: Position): Move =
for move in position.legalMoves:
if position.isCloudKingMove(move):
return move
noMove
var sentGreetingMessage = false
const maxWaitTimeBeforeStarted = initDuration(seconds = 60)
let startTime = now()
proc abortGame(bgs: var BotGameState) =
let query = fmt"https://lichess.org/api/bot/game/{gameId}/abort"
discard bgs.requestsSession.jsonResponse(httpPost, query, token)
logInfo "Aborted game"
proc main() =
var bgs = BotGameState(
requestsSession: getRequestsSession(),
gameId: gameId,
token: token
)
if sentGreetingMessage:
bgs.sendMessage "Sorry, I had some stupid technical difficulties."
let (botUserId, botUserName) = block:
let b = bgs.requestsSession.jsonResponse(httpGet, "https://lichess.org/api/account", token)
if b{"id"}.getStr == "":
logError b.pretty
quit QuitFailure
(b{"id"}.getStr, b{"username"}.getStr)
logInfo "Bot user ID: ", botUserId
logInfo "Bot username: ", botUserName
doAssert getCurrentDir().lastPathPart == "game_directory_" & gameId, "This executable must be run in a directory named \"game_directory_{gameId}\""
doAssert historyOfGameResultsFileName.len > 0 and historyOfGameResultsFileName[0] == '/', "historyOfGameResultsFileName must be an absolute path"
bgs.difficultyLevel = 1.DifficultyLevel
let lichessGame = block:
var
lichessGame = none LichessGame
difficultyLevelOpt = none DifficultyLevel
for gameFullNode in streamEvents(fmt"https://lichess.org/api/bot/game/stream/{gameId}", token):
if gameFullNode.isNone:
continue
let gameFullNode = gameFullNode.get
if gameFullNode{"type"}.getStr != "gameFull":
logWarn &"Didn't receive expected \"gameFull\" JSON type: {gameFullNode}"
continue
lichessGame = some newLichessGame(gameFullNode, botUserId)
difficultyLevelOpt = some historyOfGameResultsFileName.getDifficultyLevel(lichessGame.get.opponentElo)
break # only the first event (initial gamestate) is important
if lichessGame.isNone or difficultyLevelOpt.isNone:
let msg = fmt"Didn't manage to initialize lichessGame and/or difficultyLevel (maybe event stream ended before first json result?)"
logError msg
raise newException(IOError, msg)
bgs.difficultyLevel = difficultyLevelOpt.get
let lg = lichessGame.get
lg # this is a hack, because weirdly when I return lichessGame.get it doesn't work out
# lichessGame.get
doAssert lichessGame.gameId == gameId
doAssert bgs.gameId == gameId
logInfo "Difficulty: ", bgs.difficultyLevel
var
hashTable = newHashTable()
weOfferedDrawAlready = false
weDeclinedDrawAlready = false
consideredRageQuitting = false
canChangeDifficulty = true
hashTable.setSize(sizeInBytes = hashSizeMegaByte * megaByteToByte)
for jsonNode in streamEvents(fmt"https://lichess.org/api/bot/game/stream/{gameId}", token):
if parentPID != getppid(): # parent process stopped
logError "Parent process (", parentPID, ") doesn't exist anymore"
quit QuitFailure
if jsonNode.isNone:
if bgs.lastLichessGameState.positionMoveHistory.len <= 1 and now() - startTime >= maxWaitTimeBeforeStarted:
bgs.abortGame()
continue
let jsonNode = jsonNode.get
logInfo "Received event: ", jsonNode.pretty
let nodeType = jsonNode{"type"}.getStr
let gameStateNode = if nodeType != "gameState":
if nodeType == "chatLine":
if jsonNode{"room"}.getStr == "spectator" and
jsonNode{"username"}.getStr == "lichess":
if (jsonNode{"text"}.getStr == "White offers draw" and lichessGame.botColor == black) or
(jsonNode{"text"}.getStr == "Black offers draw" and lichessGame.botColor == white):
if (not weDeclinedDrawAlready) or rand(1.0) < 0.2:
sleep 300
discard bgs.sendComment enemyOffersDraw
weDeclinedDrawAlready = true
if jsonNode{"room"}.getStr == "player" and jsonNode{"username"}.getStr != botUserName:
func normalize(s: string): string =
s.toLowerAscii.multiReplace(
(",", ""),
(".", ""),
("!", ""),
("?", ""),
("-", ""),
("_", ""),
("\"", ""),
("'", "")
)
let text = jsonNode{"text"}.getStr.normalize
if commandHigherDiffculty.normalize in text:
if not canChangeDifficulty:
bgs.sendMessage messageCanOnlyChangeDifficultyAtBeginOfGame
else:
if bgs.difficultyLevel == DifficultyLevel.high:
bgs.sendMessage messageAlreadyAtHighestDifficulty
else:
inc bgs.difficultyLevel
bgs.sendMessage messageNewDifficultyLevel(bgs.difficultyLevel)
logInfo "Difficulty: ", bgs.difficultyLevel
elif commandLowerDiffculty.normalize in text:
if not canChangeDifficulty:
bgs.sendMessage messageCanOnlyChangeDifficultyAtBeginOfGame
else:
if bgs.difficultyLevel == DifficultyLevel.low:
bgs.sendMessage messageAlreadyAtLowestDifficulty
else:
inc bgs.difficultyLevel, -1
bgs.sendMessage messageNewDifficultyLevel(bgs.difficultyLevel)
logInfo "Difficulty: ", bgs.difficultyLevel
elif text.len > commandSetDiffculty.len and text[0..<commandSetDiffculty.len] == commandSetDiffculty.normalize:
let words = text[commandSetDiffculty.len..^1].splitWhitespace
if words.len >= 1:
let dl = words[0].toDifficultyLevel
if dl.isSome:
bgs.difficultyLevel = dl.get
bgs.sendMessage messageDirectlySetDifficultyLevel(bgs.difficultyLevel)
else:
bgs.sendMessage "Don't know what you mean with " & words[0]
else:
if text.toDifficultyLevel.isSome:
bgs.difficultyLevel = text.toDifficultyLevel.get
bgs.sendMessage messageDirectlySetDifficultyLevel(bgs.difficultyLevel)
continue
if nodeType == "opponentGone":
# if jsonNode{"gone"}.getBool:
# TODO leave game
# ignore for now
continue
if nodeType != "gameFull":
logWarn &"Didn't receive expected \"gameState\" JSON type: {jsonNode}"
continue
jsonNode{"state"}
else:
jsonNode
let gameState = lichessGame.getCurrentState(gameStateNode)
bgs.lastLichessGameState = gameState
if not sentGreetingMessage:
if gameState.positionMoveHistory.len <= 1:
sleep 500
doAssert bgs.sendComment greeting
bgs.sendMessage messageStartingDifficultyLevel(bgs.difficultyLevel)
else:
bgs.sendMessage "Sorry, I crashed."
sentGreetingMessage = true
if gameState.currentPosition.fen in bgs.sentMovesForPositions:
logWarn "Skipping. Already sent move for position: ", gameState.currentPosition.fen
continue
logInfo "Current position: ", gameState.currentPosition.fen
let gameStateStatus = gameStateNode{"status"}.getStr
if gameStateStatus notin ["started", "created"]:
logInfo "Game finished (", gameStateStatus, ")"
if gameStateStatus in ["mate", "resign", "stalemate", "timeout", "draw", "outoftime"]:
if gameStateStatus in ["draw", "stalemate"]:
logInfo "Draw"
else:
logInfo "Winner is ", gameStateNode{"winner"}.getStr
var outcome = 0.5
if gameStateNode{"winner"}.getStr == "white":
outcome = 1.0
if gameStateNode{"winner"}.getStr == "black":
outcome = 0.0
if outcome != 0.5 and lichessGame.botColor == black:
outcome = 1.0 - outcome
sleep 800
if outcome == 1.0:
case gameStateStatus:
of "resign":
doAssert bgs.sendComment(enemyResigns)
of "mate":
doAssert bgs.sendComment(enemyGetsMated)
of "outoftime":
doAssert bgs.sendComment(enemyRunsOutOfTime)
else:
logError "Unsupported game state status: ", gameStateStatus
elif outcome == 0.0:
case gameStateStatus:
of "resign":
doAssert bgs.sendComment(weResign)
of "mate":
doAssert bgs.sendComment(weGetMated)
of "outoftime":
doAssert bgs.sendComment(weRunOutOfTime)
else:
logError "Unsupported game state status: ", gameStateStatus
elif outcome == 0.5:
case gameStateStatus:
of "stalemate":
doAssert bgs.sendComment(stalemate)
of "draw":
doAssert bgs.sendComment(draw)
else:
logError "Unsupported game state status: ", gameStateStatus
else:
doAssert false, fmt"{outcome = }"
historyOfGameResultsFileName.updateHistoryOfGameResults(
difficultyLevel = bgs.difficultyLevel,
opponentElo = lichessGame.opponentElo,
score = outcome
)
return
doAssert gameState.currentPosition.legalMoves.len >= 1
if gameState.positionMoveHistory.len >= 4:
canChangeDifficulty = false
if gameState.currentPosition.us == lichessGame.botColor:
bgs.beforeBotMove(gameState)
if gameState.currentPosition.us == lichessGame.botColor:
# we have to do a move
logInfo "Game ", gameId, ": Starting search on ", gameState.currentPosition.fen
let # we want to do the very first move a bit quicker
moveTimeDivider = if gameState.positionMoveHistory.len <= 1: 5 else: 1
wtime = gameState.wtime div moveTimeDivider
btime = gameState.btime div moveTimeDivider
# if enemy does king cloud move we will also make a king cloud move if possible
let suggestedMove = if gameState.lastEnemyMoveWasKingCloudMove(gameState.lichessGame.botColor):
gameState.currentPosition.getCloudKingMove
else:
noMove
let (value, pv) = if suggestedMove != noMove:
doAssert suggestedMove in gameState.currentPosition.legalMoves
logInfo "Using suggested move instead of search: ", suggestedMove
(0.Value, @[suggestedMove])
else:
logInfo "Using difficulty level ", bgs.difficultyLevel
timeManagedSearch(
position = gameState.currentPosition,
hashTable = hashTable,
positionHistory = gameState.getPositionHistory,
increment = [white: gameState.winc, black: gameState.binc],
timeLeft = [white: wtime, black: btime],
difficultyLevel = bgs.difficultyLevel
)
logInfo "Game ", gameId, ": Finished search"
# rage quit
if value < -600.cp and gameState.timeLeft(gameState.lichessGame.botColor) <= initDuration(seconds = 80):
if rand(1.0) < 0.1 and not consideredRageQuitting:
if bgs.sendComment(rageQuit):
logWarn "Rage quitting"
quit QuitSuccess
consideredRageQuitting = true
let move = if pv.len >= 1: pv[0] else: gameState.currentPosition.legalMoves()[0]
# offer a draw if opponent has forced checkmate, lol
let offerDraw = value <= -valueCheckmate and not weOfferedDrawAlready
if offerDraw:
weOfferedDrawAlready = true
let query = fmt"https://lichess.org/api/bot/game/{gameId}/move/{move}?offeringDraw={offerDraw}"
try:
discard bgs.requestsSession.jsonResponse(httpPost, query, token)
except CatchableError:
if "Not your turn, or game already over" in getCurrentExceptionMsg():
logInfo "Tried to play move when it's not our turn, or game already over"
continue
logError &"Failed to send move {move} for game {gameId} to lichess\nQuery: {query}\nException: ", getCurrentExceptionMsg()
raise
bgs.sentMovesForPositions.add gameState.currentPosition.fen
logInfo "Played move ", move
bgs.afterBotMove(gameState, if pv.len > 0 and pv[0] == move: pv else: @[move], value)
while true:
try:
main()
break
except CatchableError:
logError "Encountered exception while playing game ", gameId, ": ", getCurrentExceptionMsg(), "\n", getCurrentException().getStackTrace()
let cooldownMilliseconds = 1_000
logInfo "Trying to play game again in ", cooldownMilliseconds, " ms"
sleep cooldownMilliseconds
logInfo "Exiting process for game ", gameId