🎨 Refactor RecordCard and Fix CI Check

This commit is contained in:
luoshuijs 2023-09-12 10:58:46 +08:00 committed by GitHub
parent d13e2e28c9
commit f789d40bcc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 17 deletions

View File

@ -124,4 +124,4 @@ class BaseChronicleClient(BaseClient):
if not data["list"]: if not data["list"]:
raise DataNotPublic({"retcode": 10102}) raise DataNotPublic({"retcode": 10102})
return [RecordCard(**card) for card in data["list"]] return [RecordCard.creat(**card) for card in data["list"]]

View File

@ -1,6 +1,4 @@
"""Genshin calculator models.""" """Genshin calculator models."""
import collections import collections
from typing import Dict, Any, Literal, Optional, List 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 List[CalculatorConsumable]: A list of CalculatorConsumable objects representing the total
consumables used across all categories. 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 combined = self.character + self.weapon + self.talents + artifacts
grouped: Dict[int, List[CalculatorConsumable]] = collections.defaultdict(list) grouped: Dict[int, List[CalculatorConsumable]] = collections.defaultdict(list)

View File

@ -1,6 +1,6 @@
import enum import enum
import re import re
from typing import Optional, Any, Dict, List, Union from typing import Optional, Any, Dict, List, Union, Type
from pydantic import Field, validator from pydantic import Field, validator
@ -22,6 +22,9 @@ __all__ = (
) )
RECORD_CARD_MAP: Dict[int, Type["RecordCard"]] = {}
class Account(APIModel): class Account(APIModel):
"""A representation of an account. """A representation of an account.
@ -253,24 +256,19 @@ class RecordCard(BaseRecordCard):
url (str): The URL of the record card. url (str): The URL of the record card.
""" """
def __new__(cls, **kwargs: Any) -> "RecordCard": @classmethod
"""Creates an appropriate record card instance based on the provided game ID. def creat(cls, **kwargs: Any):
"""Creates a record card.
Args: Args:
**kwargs: The arguments passed in to create the record card. **kwargs: Keyword arguments.
Returns: 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) game_id = kwargs.get("game_id", 0)
if game_id == 1: new_cls = RECORD_CARD_MAP.get(game_id, cls)
cls = HonkaiRecordCard # skipcq: PYL-W0642 return new_cls(**kwargs)
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
class GenshinRecordCard(RecordCard): class GenshinRecordCard(RecordCard):
@ -418,3 +416,8 @@ class StarRailRecodeCard(RecordCard):
int: The number of chests the user has found. int: The number of chests the user has found.
""" """
return int(self.data[3].value) return int(self.data[3].value)
RECORD_CARD_MAP.setdefault(1, HonkaiRecordCard)
RECORD_CARD_MAP.setdefault(2, GenshinRecordCard)
RECORD_CARD_MAP.setdefault(6, StarRailRecodeCard)