EnkaNetwork.py/enkanetwork/model/equipments.py

123 lines
4.2 KiB
Python
Raw Normal View History

2022-07-05 08:46:47 +00:00
import logging
2022-07-05 06:41:29 +00:00
from pydantic import BaseModel, Field
from typing import Any, List
from ..enum import EquipmentsType, DigitType, EquipType
2022-07-16 19:36:26 +00:00
from ..assets import Assets
2022-07-05 06:41:29 +00:00
from ..utils import create_ui_path
2022-07-05 08:46:47 +00:00
LOGGER = logging.getLogger(__name__)
2022-07-29 04:31:21 +00:00
2022-07-05 06:41:29 +00:00
class EquipmentsStats(BaseModel):
prop_id: str = ""
type: DigitType = DigitType.NUMBER
name: str = ""
value: int = 0
def __init__(__pydantic_self__, **data: Any) -> None:
super().__init__(**data)
2022-07-05 08:46:47 +00:00
if isinstance(data["statValue"], float):
__pydantic_self__.type = DigitType.PERCENT
__pydantic_self__.value = data["statValue"]
2022-07-29 04:31:21 +00:00
LOGGER.debug("=== Fight prop ===")
2022-07-05 08:46:47 +00:00
2022-07-05 06:41:29 +00:00
if "mainPropId" in data:
__pydantic_self__.prop_id = str(data["mainPropId"])
2022-07-16 19:36:26 +00:00
fight_prop = Assets.get_hash_map(str(data["mainPropId"]))
2022-07-05 06:41:29 +00:00
else:
__pydantic_self__.prop_id = str(data["appendPropId"])
2022-07-16 19:36:26 +00:00
fight_prop = Assets.get_hash_map(str(data["appendPropId"]))
2022-07-05 06:41:29 +00:00
2022-07-05 08:46:47 +00:00
if not fight_prop:
return
2022-07-05 06:41:29 +00:00
2022-07-16 19:36:26 +00:00
__pydantic_self__.name = fight_prop
2022-07-05 06:41:29 +00:00
2022-07-29 04:31:21 +00:00
2022-07-05 06:41:29 +00:00
class EquipmentsDetail(BaseModel):
"""
Custom data
"""
2022-07-29 04:31:21 +00:00
name: str = "" # Get from name hash map
artifact_name_set: str = "" # Name set artifacts
artifact_type: EquipType = EquipType.Unknown # Type of artifact
icon: str = ""
2022-07-05 06:41:29 +00:00
rarity: int = Field(0, alias="rankLevel")
mainstats: EquipmentsStats = Field(None, alias="reliquaryMainstat")
substats: List[EquipmentsStats] = []
def __init__(__pydantic_self__, **data: Any) -> None:
super().__init__(**data)
2022-07-29 04:31:21 +00:00
if data["itemType"] == "ITEM_RELIQUARY": # AKA. Artifact
LOGGER.debug("=== Artifact ===")
2022-07-16 19:36:26 +00:00
__pydantic_self__.icon = Assets.create_icon_path(data["icon"])
__pydantic_self__.artifact_type = EquipType(data["equipType"]).name
2022-07-05 08:46:47 +00:00
# Sub Stats
for stats in data["reliquarySubstats"] if "reliquarySubstats" in data else []:
2022-07-29 04:31:21 +00:00
__pydantic_self__.substats.append(
EquipmentsStats.parse_obj(stats))
2022-07-05 06:41:29 +00:00
2022-07-29 04:31:21 +00:00
if data["itemType"] == "ITEM_WEAPON": # AKA. Weapon
LOGGER.debug("=== Weapon ===")
2022-07-05 08:46:47 +00:00
__pydantic_self__.icon = create_ui_path(data["icon"])
# Main and Sub Stats
2022-07-29 04:31:21 +00:00
__pydantic_self__.mainstats = EquipmentsStats.parse_obj(
data["weaponStats"][0])
2022-07-05 06:41:29 +00:00
for stats in data["weaponStats"][1:]:
2022-07-29 04:31:21 +00:00
__pydantic_self__.substats.append(
EquipmentsStats.parse_obj(stats))
2022-07-05 08:46:47 +00:00
2022-07-16 19:36:26 +00:00
_name = Assets.get_hash_map(str(data["nameTextMapHash"]))
if "setNameTextMapHash" in data:
2022-07-29 04:31:21 +00:00
_artifact_name_set = Assets.get_hash_map(
str(data["setNameTextMapHash"]))
__pydantic_self__.artifact_name_set = _artifact_name_set or ""
2022-07-29 04:31:21 +00:00
__pydantic_self__.name = _name if _name is not None else ""
2022-07-05 06:41:29 +00:00
class Config:
use_enum_values = True
2022-07-29 04:31:21 +00:00
2022-07-05 06:41:29 +00:00
class Equipments(BaseModel):
"""
API Response data
"""
id: int = Field(0, alias="itemId")
detail: EquipmentsDetail = Field({}, alias="flat")
"""
Custom data
"""
2022-07-29 04:31:21 +00:00
level: int = 0 # Get form key "reliquary" and "weapon"
# Type of equipments (Ex. Artifact, Weapon)
type: EquipmentsType = EquipmentsType.UNKNOWN
refinement: int = 0 # Refinement of equipments (Weapon only)
ascension: int = 0 # Ascension (Weapon only)
2022-07-05 06:41:29 +00:00
class Config:
use_enum_values = True
def __init__(__pydantic_self__, **data: Any) -> None:
super().__init__(**data)
2022-07-29 04:31:21 +00:00
if data["flat"]["itemType"] == "ITEM_RELIQUARY": # AKA. Artifact
2022-07-05 06:41:29 +00:00
__pydantic_self__.type = EquipmentsType.ARTIFACT
__pydantic_self__.level = data["reliquary"]["level"] - 1
2022-07-29 04:31:21 +00:00
if data["flat"]["itemType"] == "ITEM_WEAPON": # AKA. Weapon
2022-07-05 06:41:29 +00:00
__pydantic_self__.type = EquipmentsType.WEAPON
__pydantic_self__.level = data["weapon"]["level"]
if "affixMap" in data["weapon"]:
2022-07-29 04:31:21 +00:00
__pydantic_self__.refinement = data["weapon"]["affixMap"][list(
data["weapon"]["affixMap"].keys())[0]] + 1
if "promoteLevel" in data["weapon"]:
__pydantic_self__.ascension = data["weapon"]["promoteLevel"]