Skip to content

Commit

Permalink
Collect repeated code into method
Browse files Browse the repository at this point in the history
This method names the purpose of setting challenge_id
to the empty string.
  • Loading branch information
MarkZH committed Jan 31, 2024
1 parent 208162b commit 082210c
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions lib/matchmaking.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def should_create_challenge(self) -> bool:
if challenge_expired:
self.li.cancel(self.challenge_id)
logger.info(f"Challenge id {self.challenge_id} cancelled.")
self.challenge_id = ""
self.discard_challenge(self.challenge_id)
self.show_earliest_challenge_time()
return bool(matchmaking_enabled and (time_has_passed or challenge_expired) and min_wait_time_passed)

Expand Down Expand Up @@ -257,6 +257,15 @@ def challenge(self, active_games: set[str], challenge_queue: MULTIPROCESSING_LIS
logger.info(f"Challenge id is {challenge_id if challenge_id else 'None'}.")
self.challenge_id = challenge_id

def discard_challenge(self, challenge_id: str) -> None:
"""
Clear the ID of the most recent challenge if it is no longer needed.
:param challenge_id: The ID of the challenge that is expired, accepted, or declined.
"""
if self.challenge_id == challenge_id:
self.challenge_id = ""

def game_done(self) -> None:
"""Reset the timer for when the last game ended, and prints the earliest that the next challenge will be created."""
self.last_game_ended_delay.reset()
Expand Down Expand Up @@ -308,8 +317,7 @@ def accepted_challenge(self, event: EVENT_TYPE) -> None:
Otherwise, we would attempt to cancel the challenge later.
"""
if self.challenge_id == event["game"]["id"]:
self.challenge_id = ""
self.discard_challenge(event["game"]["id"])

def declined_challenge(self, event: EVENT_TYPE) -> None:
"""
Expand All @@ -321,8 +329,7 @@ def declined_challenge(self, event: EVENT_TYPE) -> None:
opponent = challenge.opponent
reason = event["challenge"]["declineReason"]
logger.info(f"{opponent} declined {challenge}: {reason}")
if self.challenge_id == challenge.id:
self.challenge_id = ""
self.discard_challenge(challenge.id)
if not challenge.from_self or self.challenge_filter == FilterType.NONE:
return

Expand Down

0 comments on commit 082210c

Please sign in to comment.