Skip to content

Commit 9fbedf1

Browse files
authored
Limit simultaneous challenges from one user (#1098)
In order to limit the number of games with a single opponent, the number of queued challenges from a single opponent must be limited as well. This change adds the number of active challenges to the number of active games to keep the eventual number of simultaneous games with a single user under the max_simultaneous_games_per_user configuration.
1 parent f99a420 commit 9fbedf1

2 files changed

Lines changed: 5 additions & 4 deletions

File tree

lib/lichess_bot.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -607,9 +607,10 @@ def handle_challenge(event: EventType, li: lichess.Lichess, challenge_queue: MUL
607607
if chlng.from_self:
608608
return
609609

610-
players_with_active_games = Counter(game["opponent"]["username"] for game in li.get_ongoing_games())
610+
opponent_engagements = Counter(game["opponent"]["username"] for game in li.get_ongoing_games())
611+
opponent_engagements.update(challenge.challenger.name for challenge in challenge_queue)
611612

612-
is_supported, decline_reason = chlng.is_supported(challenge_config, recent_bot_challenges, players_with_active_games)
613+
is_supported, decline_reason = chlng.is_supported(challenge_config, recent_bot_challenges, opponent_engagements)
613614
if is_supported:
614615
challenge_queue.append(chlng)
615616
sort_challenges(challenge_queue, challenge_config)

lib/model.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def decline_due_to(self, requirement_met: bool, decline_reason: str) -> str:
9292
return "" if requirement_met else decline_reason
9393

9494
def is_supported(self, config: Configuration, recent_bot_challenges: defaultdict[str, list[Timer]],
95-
players_with_active_games: Counter[str]) -> tuple[bool, str]:
95+
opponent_engagements: Counter[str]) -> tuple[bool, str]:
9696
"""Whether the challenge is supported."""
9797
try:
9898
if self.from_self:
@@ -109,7 +109,7 @@ def is_supported(self, config: Configuration, recent_bot_challenges: defaultdict
109109
or self.decline_due_to(self.challenger.name not in config.block_list, "generic")
110110
or self.decline_due_to(self.challenger.name in allowed_opponents, "generic")
111111
or self.decline_due_to(self.is_supported_recent(config, recent_bot_challenges), "later")
112-
or self.decline_due_to(players_with_active_games[self.challenger.name]
112+
or self.decline_due_to(opponent_engagements[self.challenger.name]
113113
< config.max_simultaneous_games_per_user, "later")
114114
or self.decline_due_to(is_supported_extra(self), "generic"))
115115

0 commit comments

Comments
 (0)