Skip to content

Commit

Permalink
Wait the matchmaking delay when all bots are busy
Browse files Browse the repository at this point in the history
  • Loading branch information
Torom committed Nov 14, 2023
1 parent 0663e9a commit 52acf15
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 20 deletions.
1 change: 1 addition & 0 deletions aliases.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Game_ID = str
Has_Reached_Rate_Limit = bool
Is_Misconfigured = bool
No_Opponent = bool
Offer_Draw = bool
Outcome = str
Performance = int
Expand Down
11 changes: 6 additions & 5 deletions botli_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import chess
from chess.polyglot import MemoryMappedReader

from aliases import Challenge_ID
from aliases import Challenge_ID, Has_Reached_Rate_Limit, Is_Misconfigured, No_Opponent, Success
from enums import Challenge_Color, Perf_Type, Variant


Expand Down Expand Up @@ -56,12 +56,13 @@ def __eq__(self, __o: object) -> bool:
return NotImplemented


@dataclass
@dataclass(kw_only=True)
class Challenge_Response:
challenge_id: Challenge_ID | None = None
success: bool = False
has_reached_rate_limit: bool = False
is_misconfigured: bool = False
success: Success = False
no_opponent: No_Opponent = False
has_reached_rate_limit: Has_Reached_Rate_Limit = False
is_misconfigured: Is_Misconfigured = False


@dataclass
Expand Down
7 changes: 4 additions & 3 deletions game_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,17 +166,18 @@ def _check_matchmaking(self) -> None:
pending_challenge = Pending_Challenge()
Thread(target=self.matchmaking.create_challenge, args=(pending_challenge,), daemon=True).start()

challenge_id = pending_challenge.get_challenge_id()
self.current_matchmaking_game_id = challenge_id
self.current_matchmaking_game_id = pending_challenge.get_challenge_id()

success, has_reached_rate_limit, is_misconfigured = pending_challenge.get_final_state()
success, no_opponent, has_reached_rate_limit, is_misconfigured = pending_challenge.get_final_state()
self.is_rate_limited = False

if success:
# Reserve a spot for this game
self.reserved_game_spots += 1
else:
self.current_matchmaking_game_id = None
if no_opponent:
self._delay_matchmaking(self.matchmaking_delay)
if has_reached_rate_limit:
self._delay_matchmaking(timedelta(hours=1.0))
next_matchmaking_str = self.next_matchmaking.isoformat(sep=' ', timespec='seconds')
Expand Down
12 changes: 6 additions & 6 deletions matchmaking.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,21 @@ def create_challenge(self, pending_challenge: Pending_Challenge) -> None:
pending_challenge.set_final_state(Challenge_Response(is_misconfigured=True))
return

pending_challenge.return_early()
pending_challenge.set_final_state(Challenge_Response(no_opponent=True))
return

if next_opponent:
opponent, color = next_opponent
else:
print(f'No opponent available for matchmaking type {self.current_type.name}.')
self.current_type = None
pending_challenge.return_early()
pending_challenge.set_final_state(Challenge_Response(no_opponent=True))
return

if busy_reason := self._get_busy_reason(opponent):
if busy_reason == Busy_Reason.PLAYING:
print(f'Skipping {opponent.username} ({opponent.rating_diffs[self.current_type.perf_type]:+}) '
f'as {color.value} because it is playing ...')
rating_diff = opponent.rating_diffs[self.current_type.perf_type]
print(f'Skipping {opponent.username} ({rating_diff:+}) as {color.value} ...')
self.opponents.skip_bot()
elif busy_reason == Busy_Reason.OFFLINE:
print(f'Removing {opponent.username} from online bots because it is offline ...')
Expand All @@ -69,8 +69,8 @@ def create_challenge(self, pending_challenge: Pending_Challenge) -> None:
pending_challenge.return_early()
return

print(f'Challenging {opponent.username} ({opponent.rating_diffs[self.current_type.perf_type]:+}) '
f'as {color.value} to {self.current_type.name} ...')
rating_diff = opponent.rating_diffs[self.current_type.perf_type]
print(f'Challenging {opponent.username} ({rating_diff:+}) as {color.value} to {self.current_type.name} ...')
challenge_request = Challenge_Request(opponent.username, self.current_type.initial_time,
self.current_type.increment, self.current_type.rated, color,
self.current_type.variant, self.timeout)
Expand Down
14 changes: 8 additions & 6 deletions pending_challenge.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from threading import Event

from aliases import Challenge_ID, Has_Reached_Rate_Limit, Is_Misconfigured, Success
from aliases import Challenge_ID, Has_Reached_Rate_Limit, Is_Misconfigured, No_Opponent, Success
from botli_dataclasses import Challenge_Response


Expand All @@ -9,26 +9,28 @@ def __init__(self) -> None:
self._challenge_id_event = Event()
self._challenge_id: Challenge_ID | None = None
self._finished_event = Event()
self._success: Success | None = None
self._has_reached_rate_limit: Has_Reached_Rate_Limit | None = None
self._is_misconfigured: Is_Misconfigured | None = None
self._success: Success = False
self._no_opponent: No_Opponent = False
self._has_reached_rate_limit: Has_Reached_Rate_Limit = False
self._is_misconfigured: Is_Misconfigured = False

def get_challenge_id(self) -> Challenge_ID | None:
''' This is blocking '''
self._challenge_id_event.wait()
return self._challenge_id

def get_final_state(self) -> tuple[Success, Has_Reached_Rate_Limit, Is_Misconfigured]:
def get_final_state(self) -> tuple[Success, No_Opponent, Has_Reached_Rate_Limit, Is_Misconfigured]:
''' This is blocking '''
self._finished_event.wait()
return bool(self._success), bool(self._has_reached_rate_limit), bool(self._is_misconfigured)
return self._success, self._no_opponent, self._has_reached_rate_limit, self._is_misconfigured

def set_challenge_id(self, challenge_id: Challenge_ID) -> None:
self._challenge_id = challenge_id
self._challenge_id_event.set()

def set_final_state(self, challenge_response: Challenge_Response) -> None:
self._success = challenge_response.success
self._no_opponent = challenge_response.no_opponent
self._has_reached_rate_limit = challenge_response.has_reached_rate_limit
self._is_misconfigured = challenge_response.is_misconfigured
self._finished_event.set()
Expand Down

0 comments on commit 52acf15

Please sign in to comment.