EnkaNetwork.py/enkanetwork/model/players.py

128 lines
3.1 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
2022-07-16 19:36:26 +00:00
from ..assets import Assets
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
2022-06-22 06:14:31 +00:00
class ProfilePicture(BaseModel):
"""
API Response data
"""
id: int = Field(0, alias="avatarId")
"""
Custom add data
"""
2022-07-12 19:54:36 +00:00
url: str = ""
def __init__(__pydantic_self__, **data: Any) -> None:
super().__init__(**data)
# Get character
2022-07-29 04:31:21 +00:00
LOGGER.debug("=== Avatar ===")
2022-07-16 19:36:26 +00:00
icon = Assets.character_icon(str(data["avatarId"]))
if not icon:
2022-07-05 08:46:47 +00:00
return
2022-07-29 04:31:21 +00:00
2022-07-16 19:36:26 +00:00
__pydantic_self__.url = 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 = ""
icon: str = ""
def __init__(__pydantic_self__, **data: Any) -> None:
super().__init__(**data)
# Get character
2022-07-29 04:31:21 +00:00
LOGGER.debug("=== Character preview ===")
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
2022-07-16 19:36:26 +00:00
__pydantic_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
2022-07-16 19:36:26 +00:00
__pydantic_self__.name = _name
2022-07-29 04:31:21 +00:00
class Namecard(BaseModel):
id: int = 0
icon: str = ""
banner: str = ""
navbar: str = ""
name: str = ""
def __init__(__pydantic_self__, **data: Any) -> None:
super().__init__(**data)
if __pydantic_self__.id > 0:
2022-07-29 04:31:21 +00:00
LOGGER.debug("=== Namecard ===")
# Get name card
2022-07-16 19:36:26 +00:00
namecard = Assets.namecards(str(__pydantic_self__.id))
2022-07-05 08:46:47 +00:00
if not namecard:
return
2022-07-16 19:36:26 +00:00
__pydantic_self__.icon = namecard.icon
__pydantic_self__.banner = namecard.banner
__pydantic_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
2022-07-16 19:36:26 +00:00
__pydantic_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")
2022-07-12 19:54:36 +00:00
icon: ProfilePicture = Field(None, alias="profilePicture")
2022-06-22 06:14:31 +00:00
# Avatars
2022-07-29 04:31:21 +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
"""
2022-07-29 04:31:21 +00:00
namecard: Namecard = Namecard() # Profile namecard
namecards: List[Namecard] = [] # List namecard preview in profile
def __init__(__pydantic_self__, **data: Any) -> None:
super().__init__(**data)
2022-07-29 04:31:21 +00:00
__pydantic_self__.namecard = Namecard(id=data["nameCardId"])
2022-07-29 04:31:21 +00:00
__pydantic_self__.namecards = [Namecard(id=namecard) for namecard in (
data["showNameCardIdList"])] if "showNameCardIdList" in data else [] # noqa: E501