Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bots.handle_draw_offers and bots.handle_takeback_offers #86

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
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
Loading