EnkaNetwork.py/enkanetwork/model/character.py

159 lines
4.8 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-06-22 06:14:31 +00:00
from .equipments import Equipments
from .stats import CharacterStats
2022-07-16 19:36:26 +00:00
from .assets import (
2022-08-14 09:44:47 +00:00
CharacterIconAsset,
2022-07-16 19:36:26 +00:00
)
2022-08-14 09:44:47 +00:00
from .utils import IconAsset
2022-07-16 19:36:26 +00:00
from ..assets import Assets
2022-07-12 19:54:36 +00:00
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
class CharacterSkill(BaseModel):
id: int = 0
name: str = ""
2022-08-14 09:44:47 +00:00
icon: IconAsset = None
level: int = 0
2022-06-22 06:14:31 +00:00
2022-07-29 04:31:21 +00:00
class CharacterConstellations(BaseModel):
id: int = 0
2022-07-29 04:31:21 +00:00
name: str = ""
2022-08-14 09:44:47 +00:00
icon: IconAsset = None
2022-07-29 04:31:21 +00:00
unlocked: bool = False # If character has this constellation.
2022-06-22 06:14:31 +00:00
class CharacterInfo(BaseModel):
"""
API Response data
"""
2022-06-22 06:14:31 +00:00
id: int = Field(0, alias="avatarId")
equipments: List[Equipments] = Field([], alias="equipList")
stats: CharacterStats = Field({}, alias="fightPropMap")
2022-06-22 06:14:31 +00:00
skill_data: List[int] = Field([], alias="inherentProudSkillList")
skill_id: int = Field(0, alias="skillDepotId")
"""
Custom data
"""
2022-07-29 04:31:21 +00:00
name: str = "" # Get from name hash map
friendship_level: int = 1
2022-07-12 19:54:36 +00:00
element: ElementType = ElementType.Unknown
rarity: int = 0
2022-08-14 09:44:47 +00:00
image: CharacterIconAsset = None
skills: List[CharacterSkill] = []
constellations: List[CharacterConstellations] = []
# Prop Maps
2022-07-29 04:31:21 +00:00
xp: int = 0 # AKA. propMap 1001
ascension: int = 0 # AKA. propMap 4001
level: int = 0 # AKA. propMap 1002
# Other
max_level: int = 20
2022-07-29 04:31:21 +00:00
constellations_unlocked: int = 0 # Constellation is unlocked count
def __init__(__pydantic_self__, **data: Any) -> None:
super().__init__(**data)
2022-07-29 04:31:21 +00:00
# Friendship level
__pydantic_self__.friendship_level = data["fetterInfo"]["expLevel"]
2022-07-05 08:46:47 +00:00
# Get prop map
2022-08-14 09:44:47 +00:00
__pydantic_self__.xp = int(data["propMap"]["1001"]["ival"]) if "1001" in data["propMap"] else 0 # noqa: E501
__pydantic_self__.ascension = int(data["propMap"]["1002"]["ival"]) if "1002" in data["propMap"] else 0 # noqa: E501
__pydantic_self__.level = int(data["propMap"]["4001"]["ival"]) if "4001" in data["propMap"] else 0 # noqa: E501
2022-07-05 08:46:47 +00:00
2022-07-18 08:53:09 +00:00
# Constellation unlocked count
2022-08-14 09:44:47 +00:00
__pydantic_self__.constellations_unlocked = len(data["talentIdList"]) if "talentIdList" in data else 0
2022-07-18 08:53:09 +00:00
# Get max character level
2022-08-14 09:44:47 +00:00
__pydantic_self__.max_level = (__pydantic_self__.ascension * 10) + (10 if __pydantic_self__.ascension > 0 else 0) + 20 # noqa: E501
# Get character
2022-07-29 04:31:21 +00:00
LOGGER.debug("=== Character Data ===")
avatarId = str(data["avatarId"])
avatarId += f"-{data['skillDepotId']}" if data["avatarId"] in [10000005, 10000007] else "" # noqa: E501
character = Assets.character(avatarId) # noqa: E501
2022-07-05 08:46:47 +00:00
# Check if character is founded
if not character:
return
2022-07-05 08:46:47 +00:00
# Load icon
if "costumeId" in data:
_data = Assets.character_costume(str(data["costumeId"]))
if _data:
__pydantic_self__.image = _data.images
else:
__pydantic_self__.image = character.images
else:
__pydantic_self__.image = character.images
2022-07-12 19:54:36 +00:00
# Get element
__pydantic_self__.element = ElementType(character.element)
# Check caulate rarity
__pydantic_self__.rarity = character.rarity
2022-07-29 04:31:21 +00:00
# Load constellation
2022-07-29 04:31:21 +00:00
LOGGER.debug("=== Constellation ===")
2022-07-16 19:36:26 +00:00
for constellation in character.constellations:
_constellation = Assets.constellations(constellation)
2022-07-05 08:46:47 +00:00
if not _constellation:
continue
# Get name hash map
2022-07-16 19:36:26 +00:00
_name = Assets.get_hash_map(str(_constellation.hash_id))
2022-07-05 08:46:47 +00:00
if _name is None:
continue
__pydantic_self__.constellations.append(CharacterConstellations(
2022-07-12 19:54:36 +00:00
id=int(constellation),
2022-07-16 19:36:26 +00:00
name=_name,
icon=_constellation.icon,
2022-07-29 04:31:21 +00:00
unlocked=int(
constellation) in data["talentIdList"] if "talentIdList" in data else False # noqa: E501
2022-07-05 08:46:47 +00:00
))
2022-07-29 04:31:21 +00:00
# Load skills
LOGGER.debug("=== Skills ===")
2022-07-16 19:36:26 +00:00
for skill in character.skills:
_skill = Assets.skills(skill)
2022-07-05 08:46:47 +00:00
if not _skill:
continue
# Get name hash map
2022-07-16 19:36:26 +00:00
_name = Assets.get_hash_map(_skill.hash_id)
2022-07-05 08:46:47 +00:00
if _name is None:
continue
__pydantic_self__.skills.append(CharacterSkill(
id=skill,
2022-07-16 19:36:26 +00:00
name=_name,
icon=_skill.icon,
2022-07-05 08:46:47 +00:00
level=data["skillLevelMap"].get(str(skill), 0)
))
2022-07-29 04:31:21 +00:00
LOGGER.debug("=== Character Name ===")
2022-07-16 19:36:26 +00:00
_name = Assets.get_hash_map(str(character.hash_id))
2022-07-05 08:46:47 +00:00
if _name is None:
return
# Get name from hash map
2022-07-16 19:36:26 +00:00
__pydantic_self__.name = _name
2022-07-29 04:31:21 +00:00
2022-07-12 19:54:36 +00:00
class Config:
2022-07-29 04:31:21 +00:00
use_enum_values = True