Skip to content

Commit

Permalink
Actually use set_opening_book_player function, fix rating check, clea…
Browse files Browse the repository at this point in the history
…n up initial openings explorer request
  • Loading branch information
qpwoeirut committed Jul 13, 2024
1 parent f8d947b commit 2518b6c
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions homemade.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from typing import Union, Any, override, cast

import chess
import requests
import yaml
from chess.engine import PlayResult

Expand Down Expand Up @@ -158,30 +159,29 @@ def chat_command(self, game: model.Game, cmd: str) -> str:
return "Invalid format! Use \"!setplayer <username>\" to set the opening explorer player."
else:
username = parts[1]
self.opening_book_player = username
return f"Set opening explorer to \"{username}\". It may take a bit to index all games for this player."
self.set_opening_book_player(game, username)
return (f"Set opening explorer to \"{username}\". "
"It may take a bit to index all games for this player, so some moves may be missing initially.")
elif cmd == "unsetplayer":
self.opening_book_player = None
self.opening_book_player_rating = 0
return "Using general Lichess opening explorer."
elif cmd == "mode":
return f"Currently using {self.mode.value}."
else:
return "Command not recognized!"

def set_opening_book_player(self, game: model.Game, username: str) -> None:
# send requests to the lichess server to start indexing games for this player
params = {"player": username, "fen": chess.STARTING_FEN, "moves": 100, "variant": game.variant_name,
"recentGames": 0, "color": "white"}
self.li.online_book_get("https://explorer.lichess.ovh/player", params)

params["color"] = "black"
self.li.online_book_get("https://explorer.lichess.ovh/player", params)

self.opening_book_player = username
user_data = self.li.get_public_data(username)
if "perfs" in user_data and game.variant_name in user_data["perfs"] and "rating" in user_data["perfs"][
game.variant_name]:
self.opening_book_player_rating = user_data["perfs"][game.variant_name]["rating"]
self.opening_book_player_rating = user_data["perfs"][game.variant_key]["rating"]

# send request to the lichess server to start indexing games for this player
params = {"player": username, "moves": 100, "variant": game.variant_name, "recentGames": 0, "color": "white"}
try:
requests.get("https://explorer.lichess.ovh/player", params=params, timeout=0.5, stream=True).close()
except requests.exceptions.Timeout:
pass

self.opening_book_player = username
# extend abort time so that lichess servers have more time to index
self.game.ping(seconds(60), seconds(120), seconds(120))
game.ping(seconds(60), seconds(120), seconds(120))

0 comments on commit 2518b6c

Please sign in to comment.