Skip to content

Commit

Permalink
add useful methods for fetching any in models
Browse files Browse the repository at this point in the history
  • Loading branch information
staciax committed Dec 29, 2024
1 parent 68ff71f commit af2ce47
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 2 deletions.
17 changes: 15 additions & 2 deletions valorant/models/buddies.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
DEALINGS IN THE SOFTWARE.
"""

from typing import Any
from __future__ import annotations

from typing import TYPE_CHECKING

from pydantic import Field

Expand All @@ -33,6 +35,10 @@
'Level',
)

if TYPE_CHECKING:
from ..client import Client
from .themes import Theme


class Level(BaseModel):
uuid: str
Expand All @@ -50,10 +56,17 @@ class Buddy(BaseModel):
uuid: str
display_name: LocalizedField = Field(alias='displayName')
is_hidden_if_not_owned: bool = Field(alias='isHiddenIfNotOwned')
theme_uuid: Any = Field(alias='themeUuid')
theme_uuid: str | None = Field(alias='themeUuid')
display_icon: str = Field(alias='displayIcon')
asset_path: str = Field(alias='assetPath')
levels: list[Level]

def __repr__(self) -> str:
return f'<Buddy display_name={self.display_name!r}>'

# useful methods

async def fetch_theme(self, *, client: Client) -> Theme | None:
if self.theme_uuid is None:
return None
return await client.fetch_theme(self.theme_uuid)
54 changes: 54 additions & 0 deletions valorant/models/contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
DEALINGS IN THE SOFTWARE.
"""

from __future__ import annotations

import logging
from typing import TYPE_CHECKING, TypeAlias

from pydantic import Field

from ..enums import RelationType, RewardType
Expand All @@ -35,13 +40,47 @@
'Reward',
)

if TYPE_CHECKING:
from ..client import Client
from .agents import Agent
from .buddies import Level as BuddyLevel
from .currencies import Currency
from .events import Event
from .player_cards import PlayerCard
from .player_titles import PlayerTitle
from .seasons import Season
from .sprays import Spray
from .weapons import Level as SkinLevel

RewardItemType: TypeAlias = SkinLevel | BuddyLevel | Currency | PlayerCard | PlayerTitle | Spray

log = logging.getLogger(__name__)


class Reward(BaseModel):
type: RewardType
uuid: str
amount: int
is_highlighted: bool = Field(alias='isHighlighted')

# useful methods

async def fetch_item(self, *, client: Client) -> RewardItemType | None: # noqa: PLR0911
if self.type is RewardType.skin_level:
return await client.fetch_weapon_skin_level(str(self.uuid))
if self.type is RewardType.buddy_level:
return await client.fetch_buddy_level(str(self.uuid))
if self.type is RewardType.player_card:
return await client.fetch_player_card(str(self.uuid))
if self.type is RewardType.player_title:
return await client.fetch_player_title(str(self.uuid))
if self.type is RewardType.spray:
return await client.fetch_spray(str(self.uuid))
if self.type is RewardType.currency:
return await client.fetch_currency(str(self.uuid))
log.warning('Unknown reward type: %s', self.type)
return None


class Level(BaseModel):
reward: Reward
Expand All @@ -52,6 +91,7 @@ class Level(BaseModel):
is_purchasable_with_dough: bool = Field(alias='isPurchasableWithDough')


# TODO: add index chapter
class Chapter(BaseModel):
is_epilogue: bool = Field(alias='isEpilogue')
levels: list[Level]
Expand All @@ -65,6 +105,20 @@ class Content(BaseModel):
premium_reward_schedule_uuid: str | None = Field(alias='premiumRewardScheduleUuid')
premium_vp_cost: int = Field(alias='premiumVPCost')

# useful methods

async def fetch_relationship(self, *, client: Client) -> Agent | Event | Season | None:
if self.relation_type is None or self.relation_uuid is None:
return None
if self.relation_type is RelationType.agent:
return await client.fetch_agent(self.relation_uuid)
if self.relation_type is RelationType.event:
return await client.fetch_event(self.relation_uuid)
if self.relation_type is RelationType.season:
return await client.fetch_season(self.relation_uuid)
log.warning('Unknown relation type: %s, uuid: %s', self.relation_type, self.relation_uuid)
return None


class Contract(BaseModel):
uuid: str
Expand Down
13 changes: 13 additions & 0 deletions valorant/models/gamemodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
DEALINGS IN THE SOFTWARE.
"""

from __future__ import annotations

from typing import TYPE_CHECKING

from pydantic import Field

from ..enums import GameRule
Expand All @@ -34,6 +38,10 @@
'GameRuleBoolOverride',
)

if TYPE_CHECKING:
from ..client import Client
from .weapons import Weapon


class GameFeatureOverride(BaseModel):
feature_name: str = Field(alias='featureName')
Expand Down Expand Up @@ -71,3 +79,8 @@ class Equippable(BaseModel):
display_icon: str = Field(alias='displayIcon')
kill_stream_icon: str = Field(alias='killStreamIcon')
asset_path: str = Field(alias='assetPath')

# useful methods

async def fetch_weapon(self, *, client: Client) -> Weapon | None:
return await client.fetch_weapon(self.uuid)
15 changes: 15 additions & 0 deletions valorant/models/player_cards.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,20 @@
DEALINGS IN THE SOFTWARE.
"""

from __future__ import annotations

from typing import TYPE_CHECKING

from pydantic import Field

from .base import BaseModel, LocalizedField

__all__ = ('PlayerCard',)

if TYPE_CHECKING:
from ..client import Client
from .themes import Theme


class PlayerCard(BaseModel):
uuid: str
Expand All @@ -39,3 +47,10 @@ class PlayerCard(BaseModel):
wide_art: str = Field(alias='wideArt')
large_art: str = Field(alias='largeArt')
asset_path: str = Field(alias='assetPath')

# useful methods

async def fetch_theme(self, *, client: Client) -> Theme | None:
if self.theme_uuid is None:
return None
return await client.fetch_theme(self.theme_uuid)
16 changes: 16 additions & 0 deletions valorant/models/seasons.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
DEALINGS IN THE SOFTWARE.
"""

from __future__ import annotations

from datetime import datetime
from typing import TYPE_CHECKING

from pydantic import Field

Expand All @@ -34,6 +37,9 @@
'Season',
)

if TYPE_CHECKING:
from ..client import Client


class Season(BaseModel):
uuid: str
Expand All @@ -45,6 +51,13 @@ class Season(BaseModel):
parent_uuid: str | None = Field(alias='parentUuid')
asset_path: str = Field(alias='assetPath')

# useful methods

async def fetch_parent(self, *, client: Client) -> Season | None:
if self.parent_uuid is None:
return None
return await client.fetch_season(self.parent_uuid)


class Border(BaseModel):
uuid: str
Expand All @@ -63,3 +76,6 @@ class Competitive(BaseModel):
competitive_tiers_uuid: str = Field(alias='competitiveTiersUuid')
borders: list[Border] | None
asset_path: str = Field(alias='assetPath')

async def fetch_season(self, *, client: Client) -> Season | None:
return await client.fetch_season(self.season_uuid)
13 changes: 13 additions & 0 deletions valorant/models/sprays.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

from __future__ import annotations

from typing import TYPE_CHECKING

from pydantic import Field

from .base import BaseModel, LocalizedField
Expand All @@ -33,6 +35,10 @@
'Spray',
)

if TYPE_CHECKING:
from ..client import Client
from .themes import Theme


class Level(BaseModel):
uuid: str
Expand All @@ -56,3 +62,10 @@ class Spray(BaseModel):
animation_gif: str | None = Field(alias='animationGif')
asset_path: str = Field(alias='assetPath')
levels: list[Level]

# useful methods

async def fetch_theme(self, *, client: Client) -> Theme | None:
if self.theme_uuid is None:
return None
return await client.fetch_theme(self.theme_uuid)

0 comments on commit af2ce47

Please sign in to comment.