From 26831e8e46b9a2e0eb6d0881ebbf8662a0b7b56e Mon Sep 17 00:00:00 2001 From: Friedrich ten Hagen Date: Wed, 11 Dec 2024 16:43:33 +0100 Subject: [PATCH] Add bots endpoints. --- CHANGELOG.rst | 3 ++- README.rst | 2 ++ berserk/clients/bots.py | 46 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index b5b2936..9ad72b0 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -8,8 +8,9 @@ To be released * Added ``sheet`` optional parameter to ``Tournaments.stream_results``, and fix returned typed dict. * Added ``studies.import_pgn`` to import PGN to study * Added ``tv.stream_current_game_of_channel`` to stream the current TV game of a channel +# Added ``bots.handle_draw_offer`` and ``bots.handle_takeback_offer`` to handle draw and takeback offers -Thanks to @nicvagn, @tors42, @fitztrev and @trevorbayless for their contributions to this release. +Thanks to @nicvagn, @tors42, @fitztrev, @trevorbayless, @friedrichtenhagen for their contributions to this release. v0.13.2 (2023-12-04) -------------------- diff --git a/README.rst b/README.rst index e3f8fbc..66b5b63 100644 --- a/README.rst +++ b/README.rst @@ -94,6 +94,8 @@ Most of the API is available: client.bots.post_message client.bots.abort_game client.bots.resign_game + client.bots.handle_draw_offer + client.bots.handle_takeback_offer client.bots.accept_challenge client.bots.decline_challenge diff --git a/berserk/clients/bots.py b/berserk/clients/bots.py index 9a6827d..7eaf24e 100644 --- a/berserk/clients/bots.py +++ b/berserk/clients/bots.py @@ -1,6 +1,6 @@ from __future__ import annotations -from typing import Iterator, Any, Dict +from typing import Iterator, Any, Dict, Union, Literal from .. import models from ..formats import NDJSON @@ -77,6 +77,50 @@ def resign_game(self, game_id: str) -> None: path = f"/api/bot/game/{game_id}/resign" self._r.post(path) + def handle_draw_offer( + self, game_id: str, accept: Union[bool, Literal["yes", "no"]] + ) -> None: + """Create/accept/decline draw offers + + :param game_id: ID of a game + :param accept: boolean or "yes"/"no" + """ + if isinstance(accept, bool): + accept_str = "true" if accept else "false" + elif accept == "yes": + accept_str = "yes" + elif accept == "no": + accept_str = "no" + else: + raise ValueError( + f"Invalid value for 'accept': {accept!r}. Must be True, False, 'no' or 'yes'." + ) + + path = f"/api/bot/game/{game_id}/draw/{accept_str}" + self._r.post(path) + + def handle_takeback_offer( + self, game_id: str, accept: Union[bool, Literal["yes", "no"]] + ) -> None: + """Create/accept/decline takeback offers + + :param game_id: ID of a game + :param accept: boolean or "yes"/"no" + """ + if isinstance(accept, bool): + accept_str = "true" if accept else "false" + elif accept == "yes": + accept_str = "yes" + elif accept == "no": + accept_str = "no" + else: + raise ValueError( + f"Invalid value for 'accept': {accept!r}. Must be True, False, 'no' or 'yes'." + ) + + path = f"/api/bot/game/{game_id}/takeback/{accept_str}" + self._r.post(path) + def accept_challenge(self, challenge_id: str) -> None: """Accept an incoming challenge.