Skip to content

Commit

Permalink
Accept engines that require an interpreter (#1023)
Browse files Browse the repository at this point in the history
  • Loading branch information
AttackingOrDefending authored Oct 1, 2024
1 parent 42428c3 commit f1b31b8
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 13 deletions.
3 changes: 3 additions & 0 deletions config.yml.default
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ url: "https://lichess.org/" # Lichess base URL.
engine: # Engine settings.
dir: "./engines/" # Directory containing the engine. This can be an absolute path or one relative to lichess-bot/.
name: "engine_name" # Binary name of the engine to use.
# interpreter: "java"
# interpreter_options:
# - "-jar"
working_dir: "" # Directory where the chess engine will read and write files. If blank or missing, the current directory is used.
# NOTE: If working_dir is set, the engine will look for files and directories relative to this directory, not where lichess-bot was launched. Absolute paths are unaffected.
protocol: "uci" # "uci", "xboard" or "homemade"
Expand Down
3 changes: 3 additions & 0 deletions lib/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ def insert_default_values(CONFIG: CONFIG_DICT_TYPE) -> None:
set_config_default(CONFIG, key="pgn_directory", default=None)
set_config_default(CONFIG, key="pgn_file_grouping", default="game", force_empty_values=True)
set_config_default(CONFIG, key="max_takebacks_accepted", default=0, force_empty_values=True)
set_config_default(CONFIG, "engine", key="interpreter", default=None)
set_config_default(CONFIG, "engine", key="interpreter_options", default=[], force_empty_values=True)
change_value_to_list(CONFIG, "engine", key="interpreter_options")
set_config_default(CONFIG, "engine", key="working_dir", default=os.getcwd(), force_empty_values=True)
set_config_default(CONFIG, "engine", key="silence_stderr", default=False)
set_config_default(CONFIG, "engine", "draw_or_resign", key="offer_draw_enabled", default=False)
Expand Down
6 changes: 5 additions & 1 deletion lib/engine_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ def create_engine(engine_config: Configuration, game: Optional[model.Game] = Non
cfg = engine_config.engine
engine_path = os.path.abspath(os.path.join(cfg.dir, cfg.name))
engine_type = cfg.protocol
commands = [engine_path]
commands = []
if cfg.interpreter:
commands.append(cfg.interpreter)
commands.extend(cfg.interpreter_options)
commands.append(engine_path)
if cfg.engine_options:
for k, v in cfg.engine_options.items():
commands.append(f"--{k}={v}" if v is not None else f"--{k}")
Expand Down
24 changes: 13 additions & 11 deletions test_bot/test_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@


def download_sf() -> None:
"""Download Stockfish 15."""
"""Download Stockfish 16."""
if os.path.exists(stockfish_path):
return

Expand Down Expand Up @@ -284,7 +284,7 @@ def test_homemade() -> None:
"bo vs b - zzzzzzzz.pgn"))


@pytest.mark.timeout(30, method="thread")
@pytest.mark.timeout(60, method="thread")
def test_buggy_engine() -> None:
"""Test lichess-bot with an engine that causes a timeout error within python-chess."""
with open("./config.yml.default") as file:
Expand All @@ -294,15 +294,17 @@ def test_buggy_engine() -> None:

def engine_path(CONFIG: CONFIG_DICT_TYPE) -> str:
dir: str = CONFIG["engine"]["dir"]
name: str = CONFIG["engine"]["name"]
return os.path.join(dir, name)

if platform == "win32":
CONFIG["engine"]["name"] = "buggy_engine.bat"
else:
CONFIG["engine"]["name"] = "buggy_engine"
st = os.stat(engine_path(CONFIG))
os.chmod(engine_path(CONFIG), st.st_mode | stat.S_IEXEC)
name: str = CONFIG["engine"]["name"].removesuffix(".py")
path = os.path.join(dir, name)
if platform == "win32":
path += ".bat"
else:
st = os.stat(path)
os.chmod(path, st.st_mode | stat.S_IEXEC)
return path

CONFIG["engine"]["name"] = "buggy_engine.py"
CONFIG["engine"]["interpreter"] = "python" if platform == "win32" else "python3"
CONFIG["engine"]["uci_options"] = {"go_commands": {"movetime": 100}}
CONFIG["pgn_directory"] = "TEMP/bug_game_record"

Expand Down
4 changes: 3 additions & 1 deletion wiki/Configure-lichess-bot.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
There are many possible options within `config.yml` for configuring lichess-bot.

## Engine options
- `protocol`: Specify which protocol your engine uses. Choices are
- `interpreter`: Specify whether your engine requires an interpreter to run (e.g. `java`, `python`).
- `interpreter_options`: A list of options passed to the interpreter (e.g. `-jar` for `java`).
- `protocol`: Specify which protocol your engine uses. Choices are:
1. `"uci"` for the [Universal Chess Interface](https://wbec-ridderkerk.nl/html/UCIProtocol.html)
2. `"xboard"` for the XBoard/WinBoard/[Chess Engine Communication Protocol](https://www.gnu.org/software/xboard/engine-intf.html)
3. `"homemade"` if you want to write your own engine in Python within lichess-bot. See [**Create a homemade engine**](https://github.com/lichess-bot-devs/lichess-bot/wiki/Create-a-homemade-engine).
Expand Down

0 comments on commit f1b31b8

Please sign in to comment.