Skip to content

Commit

Permalink
Properly display exceptions if caught during game creation
Browse files Browse the repository at this point in the history
  • Loading branch information
trevorbayless committed Jun 25, 2024
1 parent 46da2ae commit 3a76d94
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 24 deletions.
54 changes: 30 additions & 24 deletions src/cli_chess/core/game/online_game/online_game_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,30 +38,36 @@ def __init__(self, game_parameters: dict, is_vs_ai: bool):
@threaded
def create_game(self) -> None:
"""Sends a request to lichess to start an AI challenge using the selected game parameters"""
# Note: Only subscribe to IEM events right before creating challenge to lessen chance of grabbing another game
self.api_iem.add_event_listener(self._handle_iem_event)
self._notify_game_model_updated(EventTopics.GAME_SEARCH)
self.searching = True

if self.vs_ai: # Challenge Lichess AI (stockfish)
self.api_client.challenges.create_ai(level=self.game_metadata.players[not self.my_color].ai_level,
clock_limit=self.game_metadata.clocks[WHITE].time * 60, # challenges need time in seconds
clock_increment=self.game_metadata.clocks[WHITE].increment,
color=COLOR_NAMES[self.game_metadata.my_color],
variant=self.game_metadata.variant)
else: # Find a random opponent
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
try:
# Note: Only subscribe to IEM events right before creating challenge to lessen chance of grabbing another game
self.api_iem.add_event_listener(self._handle_iem_event)
self._notify_game_model_updated(EventTopics.GAME_SEARCH)
self.searching = True

if self.vs_ai: # Challenge Lichess AI (stockfish)
self.api_client.challenges.create_ai(level=self.game_metadata.players[not self.my_color].ai_level,
clock_limit=self.game_metadata.clocks[WHITE].time * 60, # challenges need time in seconds
clock_increment=self.game_metadata.clocks[WHITE].increment,
color=COLOR_NAMES[self.game_metadata.my_color],
variant=self.game_metadata.variant)
else: # Find a random opponent
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
except Exception as e:
# Since this exception happened in a thread, notify via the model event instead of raising
self.searching = False
msg = f"Error creating online game: {e}"
log.error(msg)
self._notify_game_model_updated(EventTopics.ERROR, msg=msg)

def _start_game(self, game_id: str) -> None:
"""Called when a game is started. Sets proper class variables
Expand Down
2 changes: 2 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 @@ -29,6 +29,8 @@ def update(self, *args, **kwargs) -> None:
super().update(*args, **kwargs)
if EventTopics.GAME_SEARCH in args:
self.view.alert.show_alert("Searching for opponent...", AlertType.NEUTRAL)
elif EventTopics.ERROR in args:
self.view.alert.show_alert(kwargs.get('msg', "An unspecified error has occurred"), AlertType.ERROR)

def _parse_and_present_game_over(self) -> None:
"""Triages game over status for parsing and sending to the view for display"""
Expand Down

0 comments on commit 3a76d94

Please sign in to comment.