Skip to content

Commit

Permalink
Use game stream to recognize end of game
Browse files Browse the repository at this point in the history
  • Loading branch information
Torom committed May 18, 2023
1 parent 303f587 commit e00e004
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
2 changes: 1 addition & 1 deletion event_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']

Expand Down
6 changes: 4 additions & 2 deletions game.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
26 changes: 13 additions & 13 deletions game_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)

Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down

0 comments on commit e00e004

Please sign in to comment.