From af2ce473ec06b63930ebe2e2ed272ecc7dfdd54a Mon Sep 17 00:00:00 2001 From: STACiA <69411527+staciax@users.noreply.github.com> Date: Sun, 29 Dec 2024 20:07:29 +0700 Subject: [PATCH] add useful methods for fetching any in models --- valorant/models/buddies.py | 17 +++++++++-- valorant/models/contracts.py | 54 +++++++++++++++++++++++++++++++++ valorant/models/gamemodes.py | 13 ++++++++ valorant/models/player_cards.py | 15 +++++++++ valorant/models/seasons.py | 16 ++++++++++ valorant/models/sprays.py | 13 ++++++++ 6 files changed, 126 insertions(+), 2 deletions(-) diff --git a/valorant/models/buddies.py b/valorant/models/buddies.py index b38042a..2203387 100644 --- a/valorant/models/buddies.py +++ b/valorant/models/buddies.py @@ -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 @@ -33,6 +35,10 @@ 'Level', ) +if TYPE_CHECKING: + from ..client import Client + from .themes import Theme + class Level(BaseModel): uuid: str @@ -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'' + + # 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) diff --git a/valorant/models/contracts.py b/valorant/models/contracts.py index 31663bb..87580a7 100644 --- a/valorant/models/contracts.py +++ b/valorant/models/contracts.py @@ -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 @@ -35,6 +40,22 @@ '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 @@ -42,6 +63,24 @@ class Reward(BaseModel): 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 @@ -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] @@ -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 diff --git a/valorant/models/gamemodes.py b/valorant/models/gamemodes.py index 3d1308f..a190aec 100644 --- a/valorant/models/gamemodes.py +++ b/valorant/models/gamemodes.py @@ -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 @@ -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') @@ -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) diff --git a/valorant/models/player_cards.py b/valorant/models/player_cards.py index dcd61fc..46d9503 100644 --- a/valorant/models/player_cards.py +++ b/valorant/models/player_cards.py @@ -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 @@ -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) diff --git a/valorant/models/seasons.py b/valorant/models/seasons.py index ee937f8..45b152d 100644 --- a/valorant/models/seasons.py +++ b/valorant/models/seasons.py @@ -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 @@ -34,6 +37,9 @@ 'Season', ) +if TYPE_CHECKING: + from ..client import Client + class Season(BaseModel): uuid: str @@ -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 @@ -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) diff --git a/valorant/models/sprays.py b/valorant/models/sprays.py index 6f80cc4..6426690 100644 --- a/valorant/models/sprays.py +++ b/valorant/models/sprays.py @@ -24,6 +24,8 @@ from __future__ import annotations +from typing import TYPE_CHECKING + from pydantic import Field from .base import BaseModel, LocalizedField @@ -33,6 +35,10 @@ 'Spray', ) +if TYPE_CHECKING: + from ..client import Client + from .themes import Theme + class Level(BaseModel): uuid: str @@ -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)