EnkaNetwork.py/enkanetwork/model/character.py

131 lines
3.9 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
2022-06-22 06:14:31 +00:00
from .combats import CharacterCombat
2022-07-16 19:36:26 +00:00
from .assets import (
CharacterIconAsset
)
from ..assets import Assets
from ..utils import create_ui_path
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-06-22 06:14:31 +00:00
class ChatacterFriendshipLevel(BaseModel):
level: int = Field(0, alias="expLevel")
class CharacterSkill(BaseModel):
id: int = 0
name: str = ""
icon: str = ""
level: int = 0
2022-06-22 06:14:31 +00:00
class CharacterConstellations(BaseModel):
id: int = 0
name: str = ""
icon: str = ""
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")
friendship: ChatacterFriendshipLevel = Field({},alias="fetterInfo")
equipments: List[Equipments] = Field([], alias="equipList")
2022-06-22 06:14:31 +00:00
combat: CharacterCombat = Field({}, alias="fightPropMap")
skill_data: List[int] = Field([], alias="inherentProudSkillList")
skill_id: int = Field(0, alias="skillDepotId")
"""
Custom data
"""
name: str = "" # Get from name hash map
2022-07-12 19:54:36 +00:00
element: ElementType = ElementType.Unknown
2022-07-16 19:36:26 +00:00
image: CharacterIconAsset = CharacterIconAsset()
skills: List[CharacterSkill] = []
constellations: List[CharacterConstellations] = []
# Prop Maps
xp: int = 0 # AKA. propMap 1001
ascension: int = 0 # AKA. propMap 4001
level: int = 0 # AKA. propMap 1002
def __init__(__pydantic_self__, **data: Any) -> None:
super().__init__(**data)
2022-07-05 08:46:47 +00:00
# Get prop map
__pydantic_self__.xp = int(data["propMap"]["1001"]["ival"]) if "1001" in data["propMap"] else 0
__pydantic_self__.ascension = int(data["propMap"]["1002"]["ival"]) if "1002" in data["propMap"] else 0
__pydantic_self__.level = int(data["propMap"]["4001"]["ival"]) if "4001" in data["propMap"] else 0
# Get character
2022-07-05 08:46:47 +00:00
LOGGER.debug(f"=== Character Data ===")
2022-07-16 19:36:26 +00:00
character = Assets.character(str(data["avatarId"]))
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
2022-07-16 19:36:26 +00:00
__pydantic_self__.image = character.images
2022-07-12 19:54:36 +00:00
# Get element
2022-07-16 19:36:26 +00:00
__pydantic_self__.element = character.element
2022-07-12 19:54:36 +00:00
# Load constellation
2022-07-05 08:46:47 +00:00
LOGGER.debug(f"=== 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-12 19:54:36 +00:00
unlocked=int(constellation) in data["talentIdList"] if "talentIdList" in data else False
2022-07-05 08:46:47 +00:00
))
# Load skills
2022-07-05 08:46:47 +00:00
LOGGER.debug(f"=== 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:
LOGGER.error(f"Name hash map not found.")
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)
))
LOGGER.debug(f"=== 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-12 19:54:36 +00:00
class Config:
use_enum_values = True