Skip to content

Commit

Permalink
Add bots endpoints.
Browse files Browse the repository at this point in the history
  • Loading branch information
FriedrichtenHagen committed Dec 11, 2024
1 parent 3416a96 commit 26831e8
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
--------------------
Expand Down
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 45 additions & 1 deletion berserk/clients/bots.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 26831e8

Please sign in to comment.