Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,18 @@
print("Engine could not be terminated cleanly.")

self.transport.close()

async def send_gameover(self, result: str) -> None:
"""Send a custom 'gameover <result>' UCI extension command to the engine.

Check failure on line 127 in engine.py

View workflow job for this annotation

GitHub Actions / build

ruff (W293)

engine.py:127:1: W293 Blank line contains whitespace help: Remove whitespace from blank line
result should be one of: '1-0', '0-1', '1/2-1/2'
This is a non-standard extension — engines that don't handle it will
simply ignore the unknown command.
"""
try:
# python-chess exposes the asyncio SubprocessTransport on the protocol.
# Pipe 0 is stdin of the engine process.
stdin_transport = self.transport.get_pipe_transport(0)
stdin_transport.write(f"gameover {result}\n".encode())
except Exception:
pass # Never crash BotLi because of a custom engine extension
19 changes: 19 additions & 0 deletions game.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ async def run(self) -> None:
if info.state["status"] != "started":
self._print_result_message(info.state, lichess_game, info)
await chatter.send_goodbyes()
if uci_result := self._get_uci_result(info.state):
await lichess_game.engine.send_gameover(uci_result)
await lichess_game.close()
return

Expand Down Expand Up @@ -85,6 +87,8 @@ async def run(self) -> None:

self._print_result_message(event, lichess_game, info)
await chatter.send_goodbyes()
if uci_result := self._get_uci_result(event):
await lichess_game.engine.send_gameover(uci_result)
break

if has_updated:
Expand Down Expand Up @@ -186,3 +190,18 @@ def _print_result_message(
message = " • ".join([info.id_str, opponents_str, message])

print(f"{message}\n{123 * '‾'}")

@staticmethod
def _get_uci_result(game_state: dict[str, Any]) -> str | None:
"""Convert a Lichess game_state dict to a UCI result string.
Returns None for aborted games (no meaningful result to report).
"""
winner = game_state.get("winner")
status = game_state.get("status", "")
if status in {"aborted", "noStart", "unknownFinish"}:
return None
if winner == "white":
return "1-0"
if winner == "black":
return "0-1"
return "1/2-1/2"
Loading