Skip to content

Commit

Permalink
Cancel seek request if game search is cancelled
Browse files Browse the repository at this point in the history
  • Loading branch information
trevorbayless committed Jun 25, 2024
1 parent 931348e commit 5832ab8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/cli_chess/core/game/online_game/online_game_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
5 changes: 5 additions & 0 deletions src/cli_chess/core/game/online_game/online_game_presenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit 5832ab8

Please sign in to comment.