EnkaNetwork.py/enkanetwork/model/players.py

150 lines
3.7 KiB
Python
Raw Normal View History

2022-07-05 08:46:47 +00:00
import logging
from pydantic import BaseModel, Field
from typing import List, Any, Union
2022-08-14 09:44:47 +00:00
from .utils import IconAsset
2022-07-16 19:36:26 +00:00
from ..assets import Assets
from ..enum import ElementType
2022-06-22 06:14:31 +00:00
2022-07-05 08:46:47 +00:00
LOGGER = logging.getLogger(__name__)
2022-07-29 04:31:21 +00:00
__all__ = (
'ProfilePicture',
'showAvatar',
'Namecard',
'PlayerInfo'
)
2022-07-29 04:31:21 +00:00
2022-06-22 06:14:31 +00:00
class ProfilePicture(BaseModel):
"""
API Response data
"""
id: int = Field(0, alias="avatarId")
"""
Custom add data
"""
icon: IconAsset = None
def __init__(self, **data: Any) -> None:
super().__init__(**data)
# Get character
2022-07-29 04:31:21 +00:00
LOGGER.debug("=== Avatar ===")
if "avatarId" in data:
if "costumeId" in data:
_data = Assets.character_costume(str(data["costumeId"]))
icon = _data.images if _data is not None else _data
else:
icon = Assets.character_icon(str(data["avatarId"]))
2022-07-16 19:36:26 +00:00
if not icon:
return
2022-07-29 04:31:21 +00:00
self.icon = icon.icon
2022-06-22 06:14:31 +00:00
2022-07-29 04:31:21 +00:00
2022-06-22 06:14:31 +00:00
class showAvatar(BaseModel):
"""
API Response data
"""
id: str = Field(0, alias="avatarId")
level: int = 1
"""
Custom data
"""
name: str = ""
2022-08-15 20:17:52 +00:00
icon: IconAsset = None
element: ElementType = ElementType.Unknown
def __init__(self, **data: Any) -> None:
super().__init__(**data)
# Get character
2022-07-29 04:31:21 +00:00
LOGGER.debug("=== Character preview ===")
# Find traveler
2022-07-16 19:36:26 +00:00
character_preview = Assets.character(str(data["avatarId"]))
2022-07-05 08:46:47 +00:00
if not character_preview:
return
self.element = character_preview.element
if "costumeId" in data:
_data = Assets.character_costume(str(data["costumeId"]))
if _data:
self.icon = _data.images.icon
else:
self.icon = character_preview.images.icon
2022-07-05 08:46:47 +00:00
# Get name hash map
2022-07-16 19:36:26 +00:00
_name = Assets.get_hash_map(str(character_preview.hash_id))
2022-07-05 08:46:47 +00:00
if _name is None:
2022-07-29 04:31:21 +00:00
return
2022-07-05 08:46:47 +00:00
self.name = _name
2022-07-29 04:31:21 +00:00
class Namecard(BaseModel):
id: int = 0
name: str = ""
2022-08-14 09:44:47 +00:00
icon: IconAsset = None
banner: IconAsset = None
navbar: IconAsset = None
def __init__(self, **data: Any) -> None:
super().__init__(**data)
if self.id > 0:
2022-07-29 04:31:21 +00:00
LOGGER.debug("=== Namecard ===")
# Get name card
namecard = Assets.namecards(str(self.id))
2022-07-05 08:46:47 +00:00
if not namecard:
return
self.icon = namecard.icon
self.banner = namecard.banner
self.navbar = namecard.navbar
2022-07-05 08:46:47 +00:00
2022-07-16 19:36:26 +00:00
_name = Assets.get_hash_map(str(namecard.hash_id))
2022-07-05 08:46:47 +00:00
if _name is None:
2022-07-29 04:31:21 +00:00
return
2022-07-05 08:46:47 +00:00
self.name = _name
2022-06-22 06:14:31 +00:00
2022-07-29 04:31:21 +00:00
2022-06-22 06:14:31 +00:00
class PlayerInfo(BaseModel):
"""
API Response data
"""
2022-06-22 06:14:31 +00:00
# Profile info
achievement: int = Field(0, alias="finishAchievementNum")
level: int = 0
nickname: str = ""
signature: str = ""
world_level: int = Field(1, alias="worldLevel")
avatar: ProfilePicture = Field(None, alias="profilePicture")
2022-06-22 06:14:31 +00:00
# Avatars
2022-08-15 20:17:52 +00:00
characters_preview: List[showAvatar] = Field([], alias="showAvatarInfoList")
2022-06-22 06:14:31 +00:00
# Abyss floor
abyss_floor: int = Field(0, alias="towerFloorIndex")
abyss_room: int = Field(0, alias="towerLevelIndex")
"""
Custom data
"""
namecard: Namecard = Namecard() # Profile name-card
namecards: List[Namecard] = [] # List name-card preview in profile
def __init__(self, **data: Any) -> None:
super().__init__(**data)
2022-07-29 04:31:21 +00:00
self.namecard = Namecard(id=data["nameCardId"])
self.namecards = [Namecard(id=namecard) for namecard in (data["showNameCardIdList"])] if "showNameCardIdList" in data else [] # noqa: E501