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

feat: allow looking up player's titles #100

Merged
merged 1 commit into from
Nov 2, 2024
Merged
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: 3 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ and are not meant to be created by the user of the library.
.. autoclass:: rlapi.Playlist
:members:

.. autoclass:: rlapi.PlayerTitle
:members:

.. autoclass:: rlapi.SkillLeaderboard
:members:

Expand Down
3 changes: 3 additions & 0 deletions rlapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
Playlist as Playlist,
SeasonRewards as SeasonRewards,
)
from .player_titles import PlayerTitle
from .population import ( # noqa
KNOWN_POPULATION_PLAYLISTS as KNOWN_POPULATION_PLAYLISTS,
PlatformPopulation as PlatformPopulation,
Expand Down Expand Up @@ -95,6 +96,8 @@
"PlayerStats",
"Playlist",
"SeasonRewards",
# player_titles
"PlayerTitle",
# population
"KNOWN_POPULATION_PLAYLISTS",
"PlatformPopulation",
Expand Down
35 changes: 35 additions & 0 deletions rlapi/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
from .enums import Platform, PlaylistKey, Stat
from .leaderboard import SkillLeaderboard, StatLeaderboard
from .player import Player
from .player_titles import PlayerTitle
from .population import Population
from .typedefs import TierBreakdownType

Expand Down Expand Up @@ -374,6 +375,7 @@ async def _iter_get_profiles(

for player_data in raw_players:
yield Player(
client=self,
platform=platform,
tier_breakdown=self.tier_breakdown,
data=player_data,
Expand Down Expand Up @@ -600,6 +602,39 @@ async def _find_steam_ids(self, match: Match[str]) -> List[str]:

return ids

async def get_player_titles(
self, platform: Platform, player_id: str
) -> List[PlayerTitle]:
"""
Get player's titles.

.. note::

Some titles that the player has may not be included in the response.

Parameters
----------
platform: Platform
Platform to lookup the player on.
player_id: str
Identifier to lookup the player by.
This needs to be a user ID for the Steam and Epic platforms
and a name for the rest of the platforms.

Returns
-------
`list` of `PlayerTitle`
List of player's titles.

Raises
------
HTTPException
HTTP request to Rocket League failed.
"""
endpoint = f"/player/titles/{platform.value}/{player_id}"
data = await self._rlapi_request(endpoint)
return [PlayerTitle(title_id) for title_id in data["titles"]]

async def get_population(self) -> Population:
"""
Get population across different platforms and playlists.
Expand Down
34 changes: 33 additions & 1 deletion rlapi/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,19 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import annotations

import contextlib
from typing import Any, Dict, Final, List, Optional, Union
from typing import TYPE_CHECKING, Any, Dict, Final, List, Optional, Union

from .enums import Platform, PlaylistKey, Stat
from .player_titles import PlayerTitle
from .tier_estimates import TierEstimates
from .typedefs import PlaylistBreakdownType, TierBreakdownType

if TYPE_CHECKING:
from .client import Client

# The documentation of below constants needs to be manually repeated in docs/api.rst
# due to: https://github.com/sphinx-doc/sphinx/issues/6495

Expand Down Expand Up @@ -336,6 +342,7 @@ class Player:
"""

__slots__ = (
"_client",
"platform",
"user_id",
"user_name",
Expand All @@ -349,10 +356,12 @@ class Player:
def __init__(
self,
*,
client: Client,
tier_breakdown: Optional[TierBreakdownType] = None,
platform: Platform,
data: Dict[str, Any],
) -> None:
self._client = client
self.platform = platform
self.user_id: Optional[str] = data.get("player_id")
self.user_name: str = data["player_name"]
Expand Down Expand Up @@ -409,6 +418,29 @@ def __hash__(self) -> int:
return hash((self.platform, "by_user_id", self.user_id))
return hash((self.platform, "by_user_name", self.user_name))

async def titles(self) -> List[PlayerTitle]:
"""
Get player's titles.

.. note::

Some titles that the player has may not be included in the response.

Returns
-------
`list` of `PlayerTitle`
List of player's titles.

Raises
------
HTTPException
HTTP request to Rocket League failed.
"""
if self.platform in (Platform.steam, Platform.epic):
assert self.user_id is not None
return await self._client.get_player_titles(self.platform, self.user_id)
return await self._client.get_player_titles(self.platform, self.user_name)

def get_playlist(self, playlist_key: PlaylistKey) -> Optional[Playlist]:
"""
Get playlist for the player.
Expand Down
Loading
Loading