Skip to content

Commit

Permalink
add a test and use a type
Browse files Browse the repository at this point in the history
  • Loading branch information
Anupya committed Nov 1, 2023
1 parent 5da28d1 commit 986effa
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 26 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ To be released
client.tournaments.schedule_swiss_next_round
client.tournaments.terminate_swiss
client.tournaments.withdraw_swiss

* Add ``client.puzzles.create_race``
* Add ``client.users.get_by_autocomplete``

Expand All @@ -31,7 +31,7 @@ Thanks to @handsamtw and @Anupya for their contributions to this release.
v0.13 (2023-09-29)
--------------------

* Corretly forward that the library is typed (now following PEP-0561)
* Correctly forward that the library is typed (now following PEP-0561)
* Added `broadcast.stream_round` endpoint
* Improve type safety, remove `enum.py` and use typed dicts instead, this is a breaking change if you relied on these enums

Expand Down
8 changes: 4 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -186,25 +186,25 @@ Most of the API is available:
client.tournaments.get
client.tournaments.get_tournament
client.tournaments.get_swiss
client.tournaments.get_tournament_team_standings
client.tournaments.get_team_standings
client.tournaments.update_team_battle
client.tournaments.create_arena
client.tournaments.create_swiss
client.tournaments.export_arena_games
client.tournaments.export_swiss_games
client.tournaments.join_arena
client.tournaments.arena_by_team
client.tournaments.swiss_by_team
client.tournaments.join_arena
client.tournaments.join_swiss
client.tournaments.terminate_arena
client.tournaments.terminate_swiss
client.tournaments.tournaments_by_user
client.tournaments.stream_results
client.tournaments.stream_swiss_results
client.tournaments.stream_by_creator
client.tournaments.withdraw_arena
client.tournaments.withdraw_swiss
client.tournaments.schedule_swiss_next_round
client.tournaments.terminate_arena
client.tournaments.withdraw_arena
client.tv.get_current_games
client.tv.stream_current_game
Expand Down
40 changes: 21 additions & 19 deletions berserk/clients/tournaments.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from ..formats import NDJSON, NDJSON_LIST, PGN
from .base import FmtClient
from ..types import ArenaResult, CurrentTournaments, SwissInfo, SwissResult
from ..types.tournaments import TeamBattleResult


class Tournaments(FmtClient):
Expand Down Expand Up @@ -58,7 +59,7 @@ def join_arena(
}
self._r.post(path=path, params=params, converter=models.Tournament.convert)

def get_team_standings(self, tournament_id: str) -> Dict[str, Any]:
def get_team_standings(self, tournament_id: str) -> TeamBattleResult:
"""Get team standing of a team battle tournament, with their respective top players.
:param tournament_id: tournament ID
Expand Down Expand Up @@ -424,7 +425,7 @@ def edit_swiss(
nbRatedGame: int | None = None,
allowList: str | None = None,
) -> Dict[str, SwissInfo]:
"""Updata a swiss tournament.
"""Update a swiss tournament.
:param tournamentId : The unique identifier of the tournament to be updated.
:param clockLimit : The time limit for each player's clock.
Expand Down Expand Up @@ -473,11 +474,20 @@ def join_swiss(self, tournament_id: str, password: str | None = None) -> None:
"""Join a Swiss tournament, possibly with a password.
:param tournament_id: the Swiss tournament ID.
:param password: the Swiss tournament password, if one is required.
"""
path = f"/api/swiss/{tournament_id}/join"
payload = {"password": password}
self._r.post(path, json=payload)

def terminate_arena(self, tournament_id: str) -> None:
"""Terminate an Arena tournament.
:param tournament_id: tournament ID
"""
path = f"/api/tournament/{tournament_id}/terminate"
self._r.post(path)

def terminate_swiss(self, tournament_id: str) -> None:
"""Terminate a Swiss tournament.
Expand All @@ -486,6 +496,14 @@ def terminate_swiss(self, tournament_id: str) -> None:
path = f"/api/swiss/{tournament_id}/terminate"
self._r.post(path)

def withdraw_arena(self, tournament_id: str) -> None:
"""Leave an upcoming Arena tournament, or take a break on an ongoing Arena tournament.
:param tournament_id: tournament ID
"""
path = f"/api/tournament/{tournament_id}/withdraw"
self._r.post(path)

def withdraw_swiss(self, tournament_id: str) -> None:
"""Withdraw a Swiss tournament.
Expand All @@ -498,24 +516,8 @@ def schedule_swiss_next_round(self, tournament_id: str, schedule_time: int) -> N
"""Manually schedule the next round date and time of a Swiss tournament.
:param tournament_id: the Swiss tournament ID.
:schedule_time: Timestamp in milliseconds to start the next round at a given date and time.
:param schedule_time: Timestamp in milliseconds to start the next round at a given date and time.
"""
path = f"/api/swiss/{tournament_id}/schedule-next-round"
payload = {"date": schedule_time}
self._r.post(path, json=payload)

def terminate_arena(self, tournament_id: str) -> None:
"""Terminate an Arena tournament.
:param tournament_id: tournament ID
"""
path = f"/api/tournament/{tournament_id}/terminate"
self._r.post(path)

def withdraw_arena(self, tournament_id: str) -> None:
"""Leave an upcoming Arena tournament, or take a break on an ongoing Arena tournament.
:param tournament_id: tournament ID
"""
path = f"/api/tournament/{tournament_id}/withdraw"
self._r.post(path)
22 changes: 22 additions & 0 deletions berserk/types/tournaments.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,25 @@ class ArenaResult(TournamentResult):
class SwissResult(TournamentResult):
points: float # can be .5 in case of draw
tieBreak: float


class PlayerInfo(TypedDict):
name: str
id: str


class PlayerResult(TypedDict):
user: PlayerInfo
score: int


class TeamResult(TypedDict):
rank: int
id: str
score: int
players: List[PlayerResult]


class TeamBattleResult(TypedDict):
id: str
teams: List[TeamResult]
10 changes: 9 additions & 1 deletion tests/clients/test_tournaments.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import pytest

from berserk import ArenaResult, Client, SwissInfo, SwissResult
from berserk import ArenaResult, Client, SwissResult
from typing import List

from berserk.types.tournaments import TeamBattleResult
from utils import validate, skip_if_older_3_dot_10


Expand All @@ -17,3 +19,9 @@ def test_swiss_result(self):
def test_arenas_result(self):
res = list(Client().tournaments.stream_results("hallow23", limit=3))
validate(List[ArenaResult], res)

@skip_if_older_3_dot_10
@pytest.mark.vcr
def test_team_standings(self):
res = list(Client().tournaments.get_team_standings("hallow23"))
validate(TeamBattleResult, res)

0 comments on commit 986effa

Please sign in to comment.