Skip to content

Commit

Permalink
Allow matchmaking during games (#913)
Browse files Browse the repository at this point in the history
* Allow matchmaking challenges while playing games

* Allow matchmaking after 10 minutes during games

* More detail in docs

* Infinity does not work as a timedelta value

* Fix concurrent game limit
  • Loading branch information
MarkZH authored Feb 17, 2024
1 parent 97bbb1f commit 7b6f359
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
1 change: 1 addition & 0 deletions config.yml.default
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ greeting:

matchmaking:
allow_matchmaking: false # Set it to 'true' to challenge other bots.
allow_during_games: false # Set it to 'true' to create challenges during long games.
challenge_variant: "random" # If set to 'random', the bot will choose one variant from the variants enabled in 'challenge.variants'.
challenge_timeout: 30 # Create a challenge after being idle for 'challenge_timeout' minutes. The minimum is 1 minute.
challenge_initial_time: # Initial time in seconds of the challenge (to be chosen at random).
Expand Down
14 changes: 11 additions & 3 deletions lib/matchmaking.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import datetime
import test_bot.lichess
from lib import model
from lib.timer import Timer, seconds, minutes, days
from lib.timer import Timer, seconds, minutes, days, years
from collections import defaultdict
from collections.abc import Sequence
from lib import lichess
Expand Down Expand Up @@ -55,6 +55,9 @@ def __init__(self, li: LICHESS_TYPE, config: Configuration, user_profile: USER_P
self.last_game_ended_delay = Timer(minutes(self.matchmaking_cfg.challenge_timeout))
self.last_user_profile_update_time = Timer(minutes(5))
self.min_wait_time = seconds(60) # Wait before new challenge to avoid api rate limits.

# Maximum time between challenges, even if there are active games
self.max_wait_time = minutes(10) if self.matchmaking_cfg.allow_during_games else years(10)
self.challenge_id: str = ""
self.daily_challenges: DAILY_TIMERS_TYPE = read_daily_challenges()

Expand Down Expand Up @@ -241,14 +244,19 @@ def get_random_config_value(self, config: Configuration, parameter: str, choices
value: str = config.lookup(parameter)
return value if value != "random" else random.choice(choices)

def challenge(self, active_games: set[str], challenge_queue: MULTIPROCESSING_LIST_TYPE) -> None:
def challenge(self, active_games: set[str], challenge_queue: MULTIPROCESSING_LIST_TYPE, max_games: int) -> None:
"""
Challenge an opponent.
:param active_games: The games that the bot is playing.
:param challenge_queue: The queue containing the challenges.
:param max_games: The maximum allowed number of simultaneous games.
"""
if active_games or challenge_queue or not self.should_create_challenge():
max_games_for_matchmaking = max_games if self.matchmaking_cfg.allow_during_games else 1
game_count = len(active_games) + len(challenge_queue)
if (game_count >= max_games_for_matchmaking
or (game_count > 0 and self.last_challenge_created_delay.time_since_reset() < self.max_wait_time)
or not self.should_create_challenge()):
return

logger.info("Challenging a random bot")
Expand Down
2 changes: 1 addition & 1 deletion lichess-bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ def lichess_bot_main(li: LICHESS_TYPE,
active_games,
max_games)
accept_challenges(li, challenge_queue, active_games, max_games)
matchmaker.challenge(active_games, challenge_queue)
matchmaker.challenge(active_games, challenge_queue, max_games)
check_online_status(li, user_profile, last_check_online_time)

control_queue.task_done()
Expand Down
1 change: 1 addition & 0 deletions wiki/Configure-lichess-bot.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ will precede the `go` command to start thinking with `sd 5`. The other `go_comma
## Challenging other bots
- `matchmaking`: Challenge a random bot.
- `allow_matchmaking`: Whether to challenge other bots.
- `allow_during_games`: Whether to issue new challenges while the bot is already playing games. If true, no more than 10 minutes will pass between matchmaking challenges.
- `challenge_variant`: The variant for the challenges. If set to `random` a variant from the ones enabled in `challenge.variants` will be chosen at random.
- `challenge_timeout`: The time (in minutes) the bot has to be idle before it creates a challenge.
- `challenge_initial_time`: A list of initial times (in seconds and to be chosen at random) for the challenges.
Expand Down

0 comments on commit 7b6f359

Please sign in to comment.