-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlichessGame.nim
94 lines (77 loc) · 3.22 KB
/
lichessGame.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
import
types,
position,
positionUtils,
move
import std/[
json,
strformat,
strutils,
times
]
type
LichessGame* = object
botUserId: string
gameId*: string
initialFEN*: string
userId: array[white..black, string]
opponentElo*: float
LichessGameState* = object
lichessGame*: LichessGame
positionMoveHistory*: seq[tuple[position: Position, move: Move]]
currentPosition*: Position
wtime*, btime*, winc*, binc*: Duration
proc lichessFenToPosition(fen: string): Position =
if fen == "startpos":
result = startpos
else:
result = fen.toPosition
func botIsWhite(lg: LichessGame): bool =
lg.botUserId == lg.userId[white]
func botColor*(lg: LichessGame): Color =
if lg.botIsWhite: white else: black
func timeLeft*(lgs: LichessGameState, color: Color): Duration =
if color == white:
lgs.wtime
else:
lgs.btime
func newLichessGame*(gameFullNode: JsonNode, botUserId: string): LichessGame =
if gameFullNode{"type"}.getStr != "gameFull":
raise newException(ValueError, &"Can only create LichessGame from JSON responses of type \"gameFull\". {gameFullNode = }")
result = LichessGame(
botUserId: botUserId,
gameId: gameFullNode{"id"}.getStr,
initialFEN: gameFullNode{"initialFen"}.getStr,
userId: [white: gameFullNode{"white"}{"id"}.getStr, black: gameFullNode{"black"}{"id"}.getStr]
)
result.opponentElo = if result.botIsWhite:
gameFullNode{"black"}{"rating"}.getFloat
else:
gameFullNode{"white"}{"rating"}.getFloat
proc getCurrentState*(lichessGame: LichessGame, gameStateNode: JsonNode): LichessGameState =
if gameStateNode{"type"}.getStr != "gameState":
raise newException(ValueError, &"Can only create LichessGameState from JSON responses of type \"gameState\". {gameStateNode = }")
result.lichessGame = lichessGame
var position: Position
try:
position = lichessGame.initialFEN.lichessFenToPosition
except ValueError:
raise newException(ValueError, &"Wrongly formatted initial FEN in lichess game: {lichessGame = }\n{getCurrentExceptionMsg() = }")
for moveString in gameStateNode{"moves"}.getStr.splitWhitespace:
try:
let move = moveString.toMove(position)
result.positionMoveHistory.add (position, move)
position = position.doMove move
except ValueError:
raise newException(ValueError, &"Failed to apply move string \"{moveString}\" in position \"{position.fen}\". \n{getCurrentExceptionMsg()}")
result.wtime = initDuration(milliseconds = gameStateNode{"wtime"}.getInt)
result.btime = initDuration(milliseconds = gameStateNode{"btime"}.getInt)
result.winc = initDuration(milliseconds = gameStateNode{"winc"}.getInt)
result.binc = initDuration(milliseconds = gameStateNode{"binc"}.getInt)
result.currentPosition = position
proc getPositionHistory*(lgs: LichessGameState): seq[Position] =
for (position, move) in lgs.positionMoveHistory:
result.add position
result.add lgs.currentPosition
doAssert result.len == lgs.positionMoveHistory.len + 1
doAssert lgs.currentPosition == result[^1]