mirror of https://github.com/tiyn/yeschess
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
2.2 KiB
67 lines
2.2 KiB
4 years ago
|
import nimpy
|
||
|
import asyncnet, asyncdispatch
|
||
|
|
||
|
import ./secret.nim
|
||
|
import ./chess.nim
|
||
|
import ./engine.nim
|
||
|
|
||
|
let berserk = pyImport("berserk")
|
||
|
|
||
|
var session = berserk.TokenSession(secret.api_token)
|
||
|
var client = berserk.Client(session=session)
|
||
|
|
||
|
let engineID = "tiyn-ychess"
|
||
4 years ago
|
let engineDifficulty = 2
|
||
4 years ago
|
let toAccept = ["tiynger"]
|
||
|
|
||
|
|
||
4 years ago
|
proc playLichessGame(id: string) {.async.} =
|
||
4 years ago
|
## Plays a lichess game with `id` asynchronously.
|
||
|
var color: Color
|
||
4 years ago
|
var chess = initChess()
|
||
4 years ago
|
for event in client.bots.stream_game_state(id):
|
||
|
echo(event)
|
||
|
if $event["type"] in ["gameState", "gameFull"]:
|
||
|
var movesString: PyObject
|
||
|
if $event["type"] == "gameFull":
|
||
|
echo("gameFull received")
|
||
|
movesString = event["state"]["moves"]
|
||
|
if $event["white"]["id"] == engineID:
|
||
|
color = Color.White
|
||
|
if $event["black"]["id"] == engineID:
|
||
|
color = Color.Black
|
||
|
else:
|
||
|
echo("gameState received")
|
||
|
movesString = event["moves"]
|
||
|
if $movesString != "":
|
||
|
var moves = movesString.split(" ")
|
||
4 years ago
|
chess.checkedMove(notationToMove($moves[-1], chess.toMove))
|
||
|
chess.echoBoard(chess.toMove)
|
||
|
if chess.toMove == color:
|
||
4 years ago
|
echo("engine has to make a move")
|
||
4 years ago
|
var bestMove = moveToNotation(chess.bestMove(engineDifficulty))
|
||
4 years ago
|
echo(bestMove)
|
||
|
discard client.bots.make_move(id, bestMove)
|
||
|
|
||
|
proc acceptChallenge(whiteList: openArray[string]): void =
|
||
|
## Accepts a challenge of users in a `whiteList` and starts the
|
||
|
## game process for each.
|
||
|
var events = client.bots.stream_incoming_events()
|
||
|
for event in events:
|
||
|
echo(event)
|
||
|
if $event["type"] == "challenge":
|
||
|
var challenger = $event["challenge"]["challenger"]["id"]
|
||
|
var id = $event["challenge"]["id"]
|
||
|
var speed = $event["challenge"]["speed"]
|
||
|
if challenger in whiteList and speed == "correspondence":
|
||
|
echo("challenge of ", challenger, " whiteList: ", id)
|
||
|
discard client.bots.accept_challenge(id)
|
||
4 years ago
|
discard playLichessGame($event["challenge"]["id"])
|
||
4 years ago
|
else:
|
||
|
discard client.bots.decline_challenge(id)
|
||
|
echo("challenge of ", challenger, " whiteList: ", id)
|
||
|
|
||
|
|
||
4 years ago
|
when isMainModule:
|
||
|
acceptChallenge(toAccept)
|