From 5832ab8d26b79c9ad68303b8c01c1e06492326dd Mon Sep 17 00:00:00 2001 From: Trevor Bayless <3620552+trevorbayless@users.noreply.github.com> Date: Tue, 25 Jun 2024 12:39:57 -0500 Subject: [PATCH] Cancel seek request if game search is cancelled --- .../game/online_game/online_game_model.py | 25 ++++++++++++++----- .../game/online_game/online_game_presenter.py | 5 ++++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/cli_chess/core/game/online_game/online_game_model.py b/src/cli_chess/core/game/online_game/online_game_model.py index 0c8becb..95d1acd 100644 --- a/src/cli_chess/core/game/online_game/online_game_model.py +++ b/src/cli_chess/core/game/online_game/online_game_model.py @@ -3,6 +3,7 @@ from cli_chess.core.api import GameStateDispatcher from cli_chess.utils import log, threaded, RequestSuccessfullySent, EventTopics from chess import COLORS, COLOR_NAMES, WHITE, BLACK, Color +from berserk.formats import TEXT from enum import Enum, auto from typing import Optional, Dict @@ -49,12 +50,18 @@ def create_game(self) -> None: color=COLOR_NAMES[self.game_metadata.my_color], variant=self.game_metadata.variant) else: # Find a random opponent - self.api_client.board.seek(time=self.game_metadata.clocks[WHITE].time, - increment=self.game_metadata.clocks[WHITE].increment, - color=COLOR_NAMES[self.game_metadata.my_color], - variant=self.game_metadata.variant, - rated=self.game_metadata.rated, - rating_range=None) + payload = { + "rated": str(self.game_metadata.rated).lower(), + "time": self.game_metadata.clocks[WHITE].time, + "increment": self.game_metadata.clocks[WHITE].increment, + "variant": self.game_metadata.variant, + "color": COLOR_NAMES[self.game_metadata.my_color], + "ratingRange": "", + } + + for _ in self.api_client.board._r.post("/api/board/seek", data=payload, fmt=TEXT, stream=True): + if not self.searching: + break def _start_game(self, game_id: str) -> None: """Called when a game is started. Sets proper class variables @@ -309,3 +316,9 @@ def cleanup(self) -> None: if self.game_in_progress: self.game_state_dispatcher.unsubscribe_from_events(self._handle_gsd_event) log.debug(f"Cleared subscription from {type(self.game_state_dispatcher).__name__} (id={id(self.game_state_dispatcher)})") + + def exit(self): + """Gracefully exit the online game model. Ensure subscriptions are + cleaned up and any active game searches are closed + """ + self._game_end() diff --git a/src/cli_chess/core/game/online_game/online_game_presenter.py b/src/cli_chess/core/game/online_game/online_game_presenter.py index 46410a4..31d9111 100644 --- a/src/cli_chess/core/game/online_game/online_game_presenter.py +++ b/src/cli_chess/core/game/online_game/online_game_presenter.py @@ -106,3 +106,8 @@ def _display_no_winner_output(self, status: str) -> None: def is_vs_ai(self) -> bool: """Returns true if the game being played is versus lichess AI""" return self.model.vs_ai + + def exit(self) -> None: + """Exits the game and returns to the main menu""" + self.model.exit() + super().exit()