EnkaNetwork.py/enkanetwork/model/character.py

174 lines
5.0 KiB
Python
Raw Permalink Normal View History

2022-07-05 08:46:47 +00:00
import logging
from pydantic import BaseModel, Field
2022-12-09 05:51:21 +00:00
from typing import List, Any, Dict
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__)
__all__ = (
'CharacterSkill',
'CharacterConstellations',
'CharacterInfo'
)
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
2022-12-09 05:51:21 +00:00
is_boosted: bool = False
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__(self, **data: Any) -> None:
super().__init__(**data)
2022-07-29 04:31:21 +00:00
# Friendship level
self.friendship_level = data["fetterInfo"]["expLevel"]
2022-07-05 08:46:47 +00:00
# Get prop map
2023-02-06 15:03:15 +00:00
self.xp = int(data["propMap"]["1001"].get("ival", 0)) if "1001" in data["propMap"] else 0
self.ascension = int(data["propMap"]["1002"].get("ival", 0)) if "1002" in data["propMap"] else 0
self.level = int(data["propMap"]["4001"].get("ival", 0)) if "4001" in data["propMap"] else 0
2022-07-05 08:46:47 +00:00
2022-07-18 08:53:09 +00:00
# Constellation unlocked count
self.constellations_unlocked = len(data["talentIdList"]) if "talentIdList" in data else 0
2022-07-18 08:53:09 +00:00
# Get max character level
self.max_level = (self.ascension * 10) + (10 if self.ascension > 0 else 0) + 20
# 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 ""
character = Assets.character(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
if "costumeId" in data:
_data = Assets.character_costume(str(data["costumeId"]))
if _data:
self.image = _data.images
else:
self.image = character.images
else:
self.image = character.images
2022-07-12 19:54:36 +00:00
# Get element
self.element = ElementType(character.element)
# Check calculate rarity
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
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
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:
2022-12-09 05:51:21 +00:00
_lvl = data["skillLevelMap"].get(str(skill), 0)
_is_boosted = False
2022-07-16 19:36:26 +00:00
_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
2022-12-09 05:51:21 +00:00
if "proudSkillExtraLevelMap" in data:
boost_level = data["proudSkillExtraLevelMap"].get(str(_skill.pround_map), None)
if not boost_level is None:
_is_boosted = True
_lvl += boost_level
self.skills.append(CharacterSkill(
2022-07-05 08:46:47 +00:00
id=skill,
2022-07-16 19:36:26 +00:00
name=_name,
icon=_skill.icon,
2022-12-09 05:51:21 +00:00
is_boosted=_is_boosted,
level=_lvl
2022-07-05 08:46:47 +00:00
))
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
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