-
Notifications
You must be signed in to change notification settings - Fork 455
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
More complex challenge filtering and game-dependent option management (…
…#924) * Allow more complex challenge filtering * Change engine options based on game parameters * Less misleading comment * Handle missing initialFen field in Challenges * Rename Game.opponent field * Create default extra_game_handles.py file * Update documentation * Use actual player color if randomly chosen
- Loading branch information
Showing
8 changed files
with
97 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
"""Functions for the user to implement when the config file is not adequate to express bot requirements.""" | ||
from lib import model | ||
from typing import Any | ||
|
||
|
||
def game_specific_options(game: model.Game) -> dict[str, Any]: | ||
""" | ||
Return a dictionary of engine options based on game aspects. | ||
By default, an empty dict is returned so that the options in the configuration file are used. | ||
""" | ||
return {} | ||
|
||
|
||
def is_supported_extra(challenge: model.Challenge) -> bool: | ||
""" | ||
Determine whether to accept a challenge. | ||
By default, True is always returned so that there are no extra restrictions beyond those in the config file. | ||
""" | ||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
## Extra customizations | ||
|
||
If your bot has more complex requirements than can be expressed in the configuration file, edit the file named `extra_game_handlers.py` in the main lichess-bot directory. | ||
Within this file, write whatever code is needed. | ||
|
||
Each section below describes a customization. | ||
Only one function is needed to make that customization work. | ||
However, if writing other functions makes implementing the customization easier, do so. | ||
Only the named function will be used in lichess-bot. | ||
|
||
|
||
### Filtering challenges | ||
|
||
The function `is_supported_extra()` allows for finer control over which challenges from other players are accepted. | ||
It should use the data in the `Challenge` argument (see `lib/model.py`) and return `True` to accept the challenge or `False` to reject it. | ||
As an example, here's a version that will only only accept games where the bot plays black: | ||
``` python | ||
def is_supported_extra(challenge): | ||
return challenge.color == "white" | ||
``` | ||
For another example, this function will reject any board that contains queens: | ||
``` python | ||
def is_supported_extra(challenge): | ||
# https://en.wikipedia.org/wiki/Forsyth%E2%80%93Edwards_Notation | ||
starting_position = challenge.initial_fen | ||
return starting_position != "startpos" and "Q" not in starting_position.upper() | ||
``` | ||
The body of the function can be as complex as needed and combine any conditions to suit the needs of the bot. | ||
Information within the `Challenge` instance is detailed in the [Lichess API documentation](https://lichess.org/api#tag/Bot/operation/apiStreamEvent) (click on 200 under Responses, then select `ChallengeEvent` under Response Schema and expand the `challenge` heading). | ||
|
||
### Tailoring engine options | ||
|
||
The function `game_specific_options()` can modify the engine options for UCI and XBoard engines based on aspects of the game about to be played. | ||
It use the data in the `Game` argument (see `lib/model.py`) and return a dictionary of `str` to values. | ||
This dictionary will add or replace values in the `uci_options` or `xboard_options` section of the bot's configuration file. | ||
For example, this version of the function will changes the move overhead value for longer games: | ||
``` python | ||
from datetime import timedelta | ||
|
||
def game_specific_options(game): | ||
if game.clock_initial >= timedelta(minutes=5): | ||
return {"Move Overhead": 5000} | ||
else: | ||
return {} | ||
``` | ||
Returning an empty dictionary leaves the engine options unchanged. |