Skip to content

Commit

Permalink
Add sheet optional parameter to Tournaments.stream_results, and f…
Browse files Browse the repository at this point in the history
…ix returned typed dict.

close #76
  • Loading branch information
kraktus committed Apr 1, 2024
1 parent e62b65b commit 7acdd4b
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Changelog
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

v0.13.2 (2023-12-04)
Expand Down
5 changes: 3 additions & 2 deletions berserk/clients/tournaments.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ def swiss_by_team(
)

def stream_results(
self, id: str, limit: int | None = None
self, id: str, limit: int | None = None, sheet: bool = False
) -> Iterator[Dict[str, ArenaResult]]:
"""Stream the results of a tournament.
Expand All @@ -373,10 +373,11 @@ def stream_results(
:param id: tournament ID
:param limit: maximum number of results to stream
:param sheet: add a `sheet` field to player containing their concatenated results. Expensive server computation that slows the stream.
:return: iterator over the results
"""
path = f"/api/tournament/{id}/results"
params = {"nb": limit}
params = {"nb": limit, "sheet": sheet}
yield from self._r.get(path, params=params, stream=True)

def stream_by_creator(self, username: str) -> Iterator[Dict[str, Any]]:
Expand Down
9 changes: 8 additions & 1 deletion berserk/types/tournaments.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,19 @@ class TournamentResult(TypedDict):
rank: int
rating: int
username: str
title: NotRequired[Title]
performance: int
title: NotRequired[Title]
flair: NotRequired[str]


class ArenaSheet(TypedDict):
scores: str


class ArenaResult(TournamentResult):
score: int
# only when requested, expensive and slowing down the stream
sheet: NotRequired[ArenaSheet]


class SwissResult(TournamentResult):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ interactions:
User-Agent:
- python-requests/2.31.0
method: GET
uri: https://lichess.org/api/tournament/hallow23/results?nb=3
uri: https://lichess.org/api/tournament/hallow23/results?nb=3&sheet=False
response:
body:
string: '{"rank":1,"score":149,"rating":2394,"username":"GGbers","performance":2449}
string: '{"rank":1,"score":149,"rating":2394,"username":"GGbers","flair":"activity.lichess-variant-king-of-the-hill","performance":2449}
{"rank":2,"score":124,"rating":2346,"username":"Kondor75","title":"IM","performance":2447}
{"rank":3,"score":123,"rating":2241,"username":"sadisticTushi","performance":2314}
{"rank":3,"score":123,"rating":2241,"username":"sadisticTushi","flair":"food-drink.french-fries","performance":2314}
'
headers:
Expand All @@ -35,7 +35,7 @@ interactions:
Content-Type:
- application/x-ndjson
Date:
- Tue, 31 Oct 2023 21:10:24 GMT
- Mon, 01 Apr 2024 12:26:53 GMT
Permissions-Policy:
- interest-cohort=()
Server:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
interactions:
- request:
body: null
headers:
Accept:
- application/json
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
User-Agent:
- python-requests/2.31.0
method: GET
uri: https://lichess.org/api/tournament/hallow23/results?nb=3&sheet=True
response:
body:
string: '{"rank":1,"score":149,"rating":2394,"username":"GGbers","flair":"activity.lichess-variant-king-of-the-hill","performance":2449,"sheet":{"scores":"55330000533054533003305555555445555555330330330"}}
{"rank":2,"score":124,"rating":2346,"username":"Kondor75","title":"IM","performance":2447,"sheet":{"scores":"44332544522100445230554555445555330202"}}
{"rank":3,"score":123,"rating":2241,"username":"sadisticTushi","flair":"food-drink.french-fries","performance":2314,"sheet":{"scores":"22042202022044444220200444444442204422044220422020042210"}}
'
headers:
Access-Control-Allow-Headers:
- Origin, Authorization, If-Modified-Since, Cache-Control, Content-Type
Access-Control-Allow-Methods:
- OPTIONS, GET, POST, PUT, DELETE
Access-Control-Allow-Origin:
- '*'
Connection:
- keep-alive
Content-Disposition:
- attachment; filename=lichess_tournament_2023.10.31_hallow23_halloween-arena-2023.ndjson
Content-Type:
- application/x-ndjson
Date:
- Mon, 01 Apr 2024 12:30:31 GMT
Permissions-Policy:
- interest-cohort=()
Server:
- nginx
Strict-Transport-Security:
- max-age=63072000; includeSubDomains; preload
Transfer-Encoding:
- chunked
Vary:
- Origin
X-Frame-Options:
- DENY
status:
code: 200
message: OK
version: 1
6 changes: 6 additions & 0 deletions tests/clients/test_tournaments.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ 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_arenas_result_with_sheet(self):
res = list(Client().tournaments.stream_results("hallow23", sheet=True, limit=3))
validate(List[ArenaResult], res)

@skip_if_older_3_dot_10
@pytest.mark.vcr
def test_team_standings(self):
Expand Down

0 comments on commit 7acdd4b

Please sign in to comment.