Spaces:
Sleeping
Sleeping
Shail Choksi commited on
Commit ·
e911711
1
Parent(s): 76d191c
Added config
Browse files- config.yml +5 -0
- lichess.py +14 -15
- main.py +13 -12
config.yml
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
token: "SGOsQpBWgK9dMcYb"
|
| 2 |
+
url: "https://listage.ovh/"
|
| 3 |
+
engines_dir: "./engines/"
|
| 4 |
+
engine: "lczero"
|
| 5 |
+
weights: "latest.txt"
|
lichess.py
CHANGED
|
@@ -1,27 +1,26 @@
|
|
| 1 |
import json
|
| 2 |
import requests
|
| 3 |
|
| 4 |
-
|
| 5 |
-
BASEURL = "https://listage.ovh{}"
|
| 6 |
-
|
| 7 |
ENDPOINTS = {
|
| 8 |
-
"profile": "
|
| 9 |
-
"stream": "
|
| 10 |
-
"game": "
|
| 11 |
-
"move": "
|
| 12 |
-
"upgrade": "
|
| 13 |
}
|
| 14 |
|
| 15 |
# docs: https://lichess.org/api
|
| 16 |
class Lichess():
|
| 17 |
|
| 18 |
-
def __init__(self, token):
|
| 19 |
self.header = self._get_header(token)
|
|
|
|
|
|
|
| 20 |
self._test()
|
| 21 |
|
| 22 |
|
| 23 |
def _test(self):
|
| 24 |
-
url =
|
| 25 |
r = requests.get(url, headers=self.header)
|
| 26 |
|
| 27 |
if r.status_code != 200:
|
|
@@ -32,7 +31,7 @@ class Lichess():
|
|
| 32 |
|
| 33 |
|
| 34 |
def get_game(self, game_id):
|
| 35 |
-
url =
|
| 36 |
r = requests.get(url, headers=self.header)
|
| 37 |
|
| 38 |
if r.status_code != 200:
|
|
@@ -43,7 +42,7 @@ class Lichess():
|
|
| 43 |
|
| 44 |
|
| 45 |
def upgrade_to_bot_account(self):
|
| 46 |
-
url =
|
| 47 |
r = requests.post(url, headers=self.header)
|
| 48 |
|
| 49 |
if r.status_code != 200:
|
|
@@ -53,7 +52,7 @@ class Lichess():
|
|
| 53 |
return r.json()
|
| 54 |
|
| 55 |
def make_move(self, game_id, move):
|
| 56 |
-
url =
|
| 57 |
r = requests.post(url, headers=self.header)
|
| 58 |
|
| 59 |
if r.status_code != 200:
|
|
@@ -64,13 +63,13 @@ class Lichess():
|
|
| 64 |
|
| 65 |
|
| 66 |
def get_stream(self, game_id):
|
| 67 |
-
url =
|
| 68 |
return requests.get(url, headers=self.header, stream=True)
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
def get_profile(self):
|
| 73 |
-
url =
|
| 74 |
r = requests.get(url, headers=self.header)
|
| 75 |
if r.status_code != 200:
|
| 76 |
print("Something went wrong! status_code: {}, response: {}".format(r.status_code, r.text))
|
|
|
|
| 1 |
import json
|
| 2 |
import requests
|
| 3 |
|
|
|
|
|
|
|
|
|
|
| 4 |
ENDPOINTS = {
|
| 5 |
+
"profile": "account/me",
|
| 6 |
+
"stream": "bot/game/stream/{}",
|
| 7 |
+
"game": "bot/game/{}",
|
| 8 |
+
"move": "bot/game/{}/move/{}",
|
| 9 |
+
"upgrade": "bot/account/upgrade"
|
| 10 |
}
|
| 11 |
|
| 12 |
# docs: https://lichess.org/api
|
| 13 |
class Lichess():
|
| 14 |
|
| 15 |
+
def __init__(self, token, url):
|
| 16 |
self.header = self._get_header(token)
|
| 17 |
+
self.baseUrl = url
|
| 18 |
+
print url
|
| 19 |
self._test()
|
| 20 |
|
| 21 |
|
| 22 |
def _test(self):
|
| 23 |
+
url = self.baseUrl + ENDPOINTS["profile"]
|
| 24 |
r = requests.get(url, headers=self.header)
|
| 25 |
|
| 26 |
if r.status_code != 200:
|
|
|
|
| 31 |
|
| 32 |
|
| 33 |
def get_game(self, game_id):
|
| 34 |
+
url = self.baseUrl + ENDPOINTS["game"].format(game_id)
|
| 35 |
r = requests.get(url, headers=self.header)
|
| 36 |
|
| 37 |
if r.status_code != 200:
|
|
|
|
| 42 |
|
| 43 |
|
| 44 |
def upgrade_to_bot_account(self):
|
| 45 |
+
url = self.baseUrl + ENDPOINTS["upgrade"]
|
| 46 |
r = requests.post(url, headers=self.header)
|
| 47 |
|
| 48 |
if r.status_code != 200:
|
|
|
|
| 52 |
return r.json()
|
| 53 |
|
| 54 |
def make_move(self, game_id, move):
|
| 55 |
+
url = self.baseUrl + ENDPOINTS["move"].format(game_id, move)
|
| 56 |
r = requests.post(url, headers=self.header)
|
| 57 |
|
| 58 |
if r.status_code != 200:
|
|
|
|
| 63 |
|
| 64 |
|
| 65 |
def get_stream(self, game_id):
|
| 66 |
+
url = self.baseUrl + ENDPOINTS["stream"].format(game_id)
|
| 67 |
return requests.get(url, headers=self.header, stream=True)
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
def get_profile(self):
|
| 72 |
+
url = self.baseUrl + ENDPOINTS["profile"]
|
| 73 |
r = requests.get(url, headers=self.header)
|
| 74 |
if r.status_code != 200:
|
| 75 |
print("Something went wrong! status_code: {}, response: {}".format(r.status_code, r.text))
|
main.py
CHANGED
|
@@ -2,8 +2,10 @@ import argparse
|
|
| 2 |
import chess
|
| 3 |
import chess.uci
|
| 4 |
import lichess
|
|
|
|
| 5 |
import json
|
| 6 |
import logging
|
|
|
|
| 7 |
|
| 8 |
|
| 9 |
def is_bot_account(li):
|
|
@@ -41,14 +43,15 @@ def start(li, game_id, engine, weights=None):
|
|
| 41 |
if game_info.get("white").get("name"):
|
| 42 |
is_white = (game_info.get("white")["name"] == username)
|
| 43 |
|
| 44 |
-
print(game_info)
|
|
|
|
| 45 |
board = play_first_move(game_info, game_id, is_white, engine, board, li)
|
| 46 |
|
| 47 |
for update in updates:
|
| 48 |
if update:
|
| 49 |
#board = process_update(board, engine, update, movetime, is_white)
|
| 50 |
upd = json.loads(update.decode('utf-8'))
|
| 51 |
-
print(upd)
|
| 52 |
wtime, btime, winc, binc = get_time_controls(upd)
|
| 53 |
moves = upd.get("moves").split()
|
| 54 |
board = update_board(board, moves[-1])
|
|
@@ -56,7 +59,7 @@ def start(li, game_id, engine, weights=None):
|
|
| 56 |
if is_engine_move(is_white, moves):
|
| 57 |
engine.position(board)
|
| 58 |
best_move, ponder = engine.go(wtime=wtime, btime=btime, winc=winc, binc=binc)
|
| 59 |
-
print(best_move)
|
| 60 |
li.make_move(game_id, best_move)
|
| 61 |
|
| 62 |
print("Game over!")
|
|
@@ -70,7 +73,6 @@ def play_first_move(game_info, game_id, is_white, engine, board, li):
|
|
| 70 |
engine.position(board)
|
| 71 |
# need to hardcode first movetime since Lichess has 30 sec limit.
|
| 72 |
best_move, ponder = engine.go(movetime=2000)
|
| 73 |
-
print(best_move)
|
| 74 |
li.make_move(game_id, best_move)
|
| 75 |
|
| 76 |
return board
|
|
@@ -102,7 +104,6 @@ def is_white_to_move(moves):
|
|
| 102 |
|
| 103 |
|
| 104 |
def update_board(board, move):
|
| 105 |
-
print(move)
|
| 106 |
uci_move = chess.Move.from_uci(move)
|
| 107 |
board.push(uci_move)
|
| 108 |
return board
|
|
@@ -127,14 +128,12 @@ def get_time_controls(data):
|
|
| 127 |
if __name__ == "__main__":
|
| 128 |
logger = logging.basicConfig(level=logging.INFO)
|
| 129 |
parser = argparse.ArgumentParser(description='Play on Lichess with a bot')
|
| 130 |
-
parser.add_argument('token', type=str, help='OAuth token')
|
| 131 |
parser.add_argument('-g', '--gameid', type=str, help='Lichess Game Id')
|
| 132 |
-
parser.add_argument('-e', '--engine', type=str, help='path to engine binary')
|
| 133 |
-
parser.add_argument('-w', '--weights', type=str, help='path to training data (weights) file')
|
| 134 |
parser.add_argument('-u', action='store_true', help='Add this flag to upgrade your account to a bot account.')
|
| 135 |
args = parser.parse_args()
|
| 136 |
|
| 137 |
-
|
|
|
|
| 138 |
|
| 139 |
is_bot = is_bot_account(li)
|
| 140 |
if args.u is True and is_bot is False:
|
|
@@ -142,9 +141,11 @@ if __name__ == "__main__":
|
|
| 142 |
|
| 143 |
|
| 144 |
if is_bot:
|
| 145 |
-
if args.gameid
|
| 146 |
-
|
|
|
|
|
|
|
| 147 |
else:
|
| 148 |
-
print("
|
| 149 |
else:
|
| 150 |
print("This is not a bot account. Please upgrade your Lichess account to a bot account!")
|
|
|
|
| 2 |
import chess
|
| 3 |
import chess.uci
|
| 4 |
import lichess
|
| 5 |
+
import os
|
| 6 |
import json
|
| 7 |
import logging
|
| 8 |
+
import yaml
|
| 9 |
|
| 10 |
|
| 11 |
def is_bot_account(li):
|
|
|
|
| 43 |
if game_info.get("white").get("name"):
|
| 44 |
is_white = (game_info.get("white")["name"] == username)
|
| 45 |
|
| 46 |
+
print("Game Info: {}".format(game_info))
|
| 47 |
+
|
| 48 |
board = play_first_move(game_info, game_id, is_white, engine, board, li)
|
| 49 |
|
| 50 |
for update in updates:
|
| 51 |
if update:
|
| 52 |
#board = process_update(board, engine, update, movetime, is_white)
|
| 53 |
upd = json.loads(update.decode('utf-8'))
|
| 54 |
+
print("Updated moves: {}".format(upd))
|
| 55 |
wtime, btime, winc, binc = get_time_controls(upd)
|
| 56 |
moves = upd.get("moves").split()
|
| 57 |
board = update_board(board, moves[-1])
|
|
|
|
| 59 |
if is_engine_move(is_white, moves):
|
| 60 |
engine.position(board)
|
| 61 |
best_move, ponder = engine.go(wtime=wtime, btime=btime, winc=winc, binc=binc)
|
| 62 |
+
print("Engines best move: {}".format(best_move))
|
| 63 |
li.make_move(game_id, best_move)
|
| 64 |
|
| 65 |
print("Game over!")
|
|
|
|
| 73 |
engine.position(board)
|
| 74 |
# need to hardcode first movetime since Lichess has 30 sec limit.
|
| 75 |
best_move, ponder = engine.go(movetime=2000)
|
|
|
|
| 76 |
li.make_move(game_id, best_move)
|
| 77 |
|
| 78 |
return board
|
|
|
|
| 104 |
|
| 105 |
|
| 106 |
def update_board(board, move):
|
|
|
|
| 107 |
uci_move = chess.Move.from_uci(move)
|
| 108 |
board.push(uci_move)
|
| 109 |
return board
|
|
|
|
| 128 |
if __name__ == "__main__":
|
| 129 |
logger = logging.basicConfig(level=logging.INFO)
|
| 130 |
parser = argparse.ArgumentParser(description='Play on Lichess with a bot')
|
|
|
|
| 131 |
parser.add_argument('-g', '--gameid', type=str, help='Lichess Game Id')
|
|
|
|
|
|
|
| 132 |
parser.add_argument('-u', action='store_true', help='Add this flag to upgrade your account to a bot account.')
|
| 133 |
args = parser.parse_args()
|
| 134 |
|
| 135 |
+
config = yaml.load(open("./config.yml"))
|
| 136 |
+
li = lichess.Lichess(config["token"], config["url"])
|
| 137 |
|
| 138 |
is_bot = is_bot_account(li)
|
| 139 |
if args.u is True and is_bot is False:
|
|
|
|
| 141 |
|
| 142 |
|
| 143 |
if is_bot:
|
| 144 |
+
if args.gameid:
|
| 145 |
+
engine_path = os.path.join(config["engines_dir"], config["engine"])
|
| 146 |
+
weights_path = os.path.join(config["engines_dir"], config["weights"]) if config["weights"] is not None else None
|
| 147 |
+
start(li, args.gameid, engine_path, weights_path)
|
| 148 |
else:
|
| 149 |
+
print("Game id is not specified!")
|
| 150 |
else:
|
| 151 |
print("This is not a bot account. Please upgrade your Lichess account to a bot account!")
|