Skip to content

Commit 8eace05

Browse files
authored
Assume game is active until lichess says otherwise (#1164)
Previously, if a communication error prevented lichess from sending ongoing games, an empty list would be returned from the Lichess function. This could cause game_is_active() to return False during a temporary communication error during a game. This would cause a game to be aborted locally even if the game is actually ongoing. Now, when checking if a game is ongoing, don't assume a game is over until a list is successfully received from lichess.
1 parent 1b5287e commit 8eace05

2 files changed

Lines changed: 15 additions & 8 deletions

File tree

lib/lichess.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -401,13 +401,16 @@ def get_profile(self) -> UserProfileType:
401401
self.set_user_agent(profile["username"])
402402
return profile
403403

404-
def get_ongoing_games(self) -> list[GameType]:
405-
"""Get the bot's ongoing games."""
406-
ongoing_games: list[GameType] = []
404+
def get_ongoing_games(self) -> list[GameType] | None:
405+
"""
406+
Get the bot's ongoing games.
407+
408+
If an error occurs when retreiving the games, None is returned.
409+
"""
407410
with contextlib.suppress(Exception):
408411
response = cast(dict[str, list[GameType]], self.api_get_json("playing"))
409-
ongoing_games = response["nowPlaying"]
410-
return ongoing_games
412+
return response["nowPlaying"]
413+
return None
411414

412415
def resign(self, game_id: str) -> None:
413416
"""Resign a game."""

lib/lichess_bot.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ def lichess_bot_main(li: lichess.Lichess,
343343

344344
one_game_completed = False
345345

346-
all_games = li.get_ongoing_games()
346+
all_games = li.get_ongoing_games() or []
347347
prune_takeback_records(all_games)
348348
startup_correspondence_games = [game["gameId"]
349349
for game in all_games
@@ -540,7 +540,10 @@ def sort_challenges(challenge_queue: MULTIPROCESSING_LIST_TYPE, challenge_config
540540

541541
def game_is_active(li: lichess.Lichess, game_id: str) -> bool:
542542
"""Determine if a game is still being played."""
543-
return game_id in (ongoing_game["gameId"] for ongoing_game in li.get_ongoing_games())
543+
active_games = li.get_ongoing_games()
544+
if active_games is None:
545+
return True
546+
return game_id in (ongoing_game["gameId"] for ongoing_game in active_games)
544547

545548

546549
def start_game_thread(active_games: set[str], game_id: str, play_game_args: PlayGameArgsType, pool: POOL_TYPE) -> None:
@@ -613,7 +616,8 @@ def handle_challenge(event: EventType, li: lichess.Lichess, challenge_queue: MUL
613616
if chlng.from_self:
614617
return
615618

616-
opponent_engagements = Counter(game["opponent"]["username"] for game in li.get_ongoing_games())
619+
active_games = li.get_ongoing_games() or []
620+
opponent_engagements = Counter(game["opponent"]["username"] for game in active_games)
617621
opponent_engagements.update(challenge.challenger.name for challenge in challenge_queue)
618622

619623
online_block_list.refresh()

0 commit comments

Comments
 (0)