Skip to content

Commit

Permalink
Merge pull request WandererXII#249 from hyperchessbot/declinereason
Browse files Browse the repository at this point in the history
Submit challenge decline reason
  • Loading branch information
ShailChoksi committed Jan 24, 2021
2 parents b8d039b + fdb57d1 commit ee43e13
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
1 change: 1 addition & 0 deletions config.yml.default
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ challenge: # incoming challenges
concurrency: 1 # number of games to play simultaneously
sort_by: "best" # possible values: "best", "first"
accept_bot: false # accepts challenges coming from other bots
only_bot: false # accept challenges by bots only
max_increment: 180 # maximum amount of increment to accaept a challenge. the max is 180. set to 0 for no increment
min_increment: 0 # minimum amount of increment to accept a challenge
variants: # chess variants to accept (http://lichess.org/variant)
Expand Down
16 changes: 14 additions & 2 deletions lichess-bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,20 @@ def start(li, user_profile, engine_factory, config):
challenge_queue = list_c
else:
try:
li.decline_challenge(chlng.id)
logger.info(" Decline {}".format(chlng))
reason = "generic"
challenge = config["challenge"]
if not chlng.is_supported_variant(challenge["variants"]):
reason = "variant"
if not chlng.is_supported_time_control(challenge["time_controls"], challenge.get("max_increment", 180), challenge.get("min_increment", 0)):
reason = "timeControl"
if not chlng.is_supported_mode(challenge["modes"]):
reason = "casual" if chlng.rated else "rated"
if ( not challenge.get("accept_bot", False) ) and chlng.challenger_is_bot:
reason = "noBot"
if challenge.get("only_bot", False) and ( not chlng.challenger_is_bot ):
reason = "onlyBot"
li.decline_challenge(chlng.id, reason=reason)
logger.info(" Decline {} for reason '{}'".format(chlng, reason))
except Exception:
pass
elif event["type"] == "gameStart":
Expand Down
8 changes: 4 additions & 4 deletions lichess.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ def api_get(self, path):
max_time=60,
interval=0.1,
giveup=is_final)
def api_post(self, path, data=None):
def api_post(self, path, data=None, headers=None):
url = urljoin(self.baseUrl, path)
response = self.session.post(url, data=data, timeout=2)
response = self.session.post(url, data=data, headers=headers, timeout=2)
response.raise_for_status()
return response.json()

Expand Down Expand Up @@ -91,8 +91,8 @@ def get_game_stream(self, game_id):
def accept_challenge(self, challenge_id):
return self.api_post(ENDPOINTS["accept"].format(challenge_id))

def decline_challenge(self, challenge_id):
return self.api_post(ENDPOINTS["decline"].format(challenge_id))
def decline_challenge(self, challenge_id, reason = "generic"):
return self.api_post(ENDPOINTS["decline"].format(challenge_id), data=f"reason={reason}", headers={"Content-Type":"application/x-www-form-urlencoded"})

def get_profile(self):
profile = self.api_get(ENDPOINTS["profile"])
Expand Down
4 changes: 3 additions & 1 deletion model.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ def is_supported_mode(self, supported):
return "rated" in supported if self.rated else "casual" in supported

def is_supported(self, config):
if not config.get("accept_bot", False) and self.challenger_is_bot:
if ( not config.get("accept_bot", False) ) and self.challenger_is_bot:
return False
if config.get("only_bot", False) and ( not self.challenger_is_bot ):
return False
variants = config["variants"]
tc = config["time_controls"]
Expand Down

0 comments on commit ee43e13

Please sign in to comment.