From f789d40bcc8be49fb3dba88140e8e5021904c90f Mon Sep 17 00:00:00 2001 From: luoshuijs Date: Tue, 12 Sep 2023 10:58:46 +0800 Subject: [PATCH] :art: Refactor `RecordCard` and Fix CI Check --- simnet/client/components/chronicle/base.py | 2 +- simnet/models/genshin/calculator.py | 4 +-- simnet/models/lab/record.py | 29 ++++++++++++---------- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/simnet/client/components/chronicle/base.py b/simnet/client/components/chronicle/base.py index 6e0de7e..bb575ab 100644 --- a/simnet/client/components/chronicle/base.py +++ b/simnet/client/components/chronicle/base.py @@ -124,4 +124,4 @@ class BaseChronicleClient(BaseClient): if not data["list"]: raise DataNotPublic({"retcode": 10102}) - return [RecordCard(**card) for card in data["list"]] + return [RecordCard.creat(**card) for card in data["list"]] diff --git a/simnet/models/genshin/calculator.py b/simnet/models/genshin/calculator.py index ae1d390..01ec501 100644 --- a/simnet/models/genshin/calculator.py +++ b/simnet/models/genshin/calculator.py @@ -1,6 +1,4 @@ """Genshin calculator models.""" - - import collections from typing import Dict, Any, Literal, Optional, List @@ -347,7 +345,7 @@ class CalculatorResult(APIModel): List[CalculatorConsumable]: A list of CalculatorConsumable objects representing the total consumables used across all categories. """ - artifacts = [i for a in self.artifacts for i in a.consumable_list] + artifacts = [i for a in self.artifacts for i in a.consumable_list] # skipcq: PYL-E1133 combined = self.character + self.weapon + self.talents + artifacts grouped: Dict[int, List[CalculatorConsumable]] = collections.defaultdict(list) diff --git a/simnet/models/lab/record.py b/simnet/models/lab/record.py index 308b1ed..4a554e2 100644 --- a/simnet/models/lab/record.py +++ b/simnet/models/lab/record.py @@ -1,6 +1,6 @@ import enum import re -from typing import Optional, Any, Dict, List, Union +from typing import Optional, Any, Dict, List, Union, Type from pydantic import Field, validator @@ -22,6 +22,9 @@ __all__ = ( ) +RECORD_CARD_MAP: Dict[int, Type["RecordCard"]] = {} + + class Account(APIModel): """A representation of an account. @@ -253,24 +256,19 @@ class RecordCard(BaseRecordCard): url (str): The URL of the record card. """ - def __new__(cls, **kwargs: Any) -> "RecordCard": - """Creates an appropriate record card instance based on the provided game ID. + @classmethod + def creat(cls, **kwargs: Any): + """Creates a record card. Args: - **kwargs: The arguments passed in to create the record card. + **kwargs: Keyword arguments. Returns: - RecordCard: An instance of a subclass of `BaseRecordCard` based on the provided game ID. + RecordCard: The record card. """ game_id = kwargs.get("game_id", 0) - if game_id == 1: - cls = HonkaiRecordCard # skipcq: PYL-W0642 - if game_id == 2: - cls = GenshinRecordCard # skipcq: PYL-W0642 - if game_id == 6: - cls = StarRailRecodeCard # skipcq: PYL-W0642 - - return super().__new__(cls) # skipcq: PYL-E1120 + new_cls = RECORD_CARD_MAP.get(game_id, cls) + return new_cls(**kwargs) class GenshinRecordCard(RecordCard): @@ -418,3 +416,8 @@ class StarRailRecodeCard(RecordCard): int: The number of chests the user has found. """ return int(self.data[3].value) + + +RECORD_CARD_MAP.setdefault(1, HonkaiRecordCard) +RECORD_CARD_MAP.setdefault(2, GenshinRecordCard) +RECORD_CARD_MAP.setdefault(6, StarRailRecodeCard)