diff --git a/event_handler.py b/event_handler.py index dca0888..b3eabf6 100755 --- a/event_handler.py +++ b/event_handler.py @@ -53,7 +53,7 @@ def run(self) -> None: elif event['type'] == 'gameStart': self.game_manager.on_game_started(event['game']['id']) elif event['type'] == 'gameFinish': - self.game_manager.on_game_finished(event['game']['id']) + continue elif event['type'] == 'challengeDeclined': opponent_name = event['challenge']['destUser']['name'] diff --git a/game.py b/game.py index 86a3c31..a99634b 100755 --- a/game.py +++ b/game.py @@ -1,5 +1,5 @@ from queue import Queue -from threading import Thread +from threading import Event, Thread from api import API from botli_dataclasses import Game_Information @@ -9,11 +9,12 @@ class Game(Thread): - def __init__(self, config: dict, api: API, game_id: str) -> None: + def __init__(self, config: dict, api: API, game_id: str, game_finished_event: Event) -> None: Thread.__init__(self) self.config = config self.api = api self.game_id = game_id + self.game_finished_event = game_finished_event self.ping_counter = 0 self.abortion_counter = 0 self.lichess_game: Lichess_Game | None = None @@ -89,6 +90,7 @@ def run(self) -> None: print(event) self.lichess_game.end_game() + self.game_finished_event.set() def _make_move(self) -> None: assert self.lichess_game diff --git a/game_manager.py b/game_manager.py index 00ac7b8..90b0267 100644 --- a/game_manager.py +++ b/game_manager.py @@ -21,7 +21,6 @@ def __init__(self, config: dict, api: API) -> None: self.games: dict[Game_ID, Game] = {} self.open_challenge_ids: deque[Challenge_ID] = deque() self.reserved_game_ids: list[Game_ID] = [] - self.finished_game_ids: deque[Game_ID] = deque() self.started_game_ids: deque[Game_ID] = deque() self.challenge_requests: deque[Challenge_Request] = deque() self.changed_event = Event() @@ -47,12 +46,11 @@ def run(self) -> None: self.changed_event.clear() + self._check_for_finished_games() + while self.started_game_ids: self._start_game(self.started_game_ids.popleft()) - while self.finished_game_ids: - self._finish_game(self.finished_game_ids.popleft()) - while challenge_request := self._get_next_challenge_request(): self._create_challenge(challenge_request) @@ -83,15 +81,17 @@ def on_game_started(self, game_id: Game_ID) -> None: self.matchmaking.on_game_started() self.changed_event.set() - def on_game_finished(self, game_id: Game_ID) -> None: - if game_id not in self.games: - return + def _check_for_finished_games(self) -> None: + for game_id, game in list(self.games.items()): + if game.is_alive(): + continue - self.finished_game_ids.append(game_id) - if game_id == self.current_matchmaking_game_id: - self.matchmaking.on_game_finished(self.games[game_id]) - self.current_matchmaking_game_id = None - self.changed_event.set() + if game_id == self.current_matchmaking_game_id: + self.matchmaking.on_game_finished(game) + self.current_matchmaking_game_id = None + + del self.games[game_id] + self.game_counter.decrement() def _start_game(self, game_id: Game_ID) -> None: if game_id in self.reserved_game_ids: @@ -103,7 +103,7 @@ def _start_game(self, game_id: Game_ID) -> None: self.api.abort_game(game_id) return - self.games[game_id] = Game(self.config, self.api, game_id) + self.games[game_id] = Game(self.config, self.api, game_id, self.changed_event) self.games[game_id].start() def _finish_game(self, game_id: Game_ID) -> None: