2021-08-16 01:46:27 +00:00
|
|
|
import json
|
2021-08-02 23:37:22 +00:00
|
|
|
import re
|
|
|
|
|
2021-08-16 01:46:27 +00:00
|
|
|
import yaml
|
|
|
|
|
|
|
|
|
|
|
|
class Card(yaml.YAMLObject):
|
|
|
|
yaml_tag = u'!Card'
|
2021-08-02 23:37:22 +00:00
|
|
|
|
2021-08-04 01:39:19 +00:00
|
|
|
__ELEMENTS_MAP = {
|
2021-08-09 04:18:56 +00:00
|
|
|
"火": "Fire",
|
|
|
|
"氷": "Ice",
|
|
|
|
"風": "Wind",
|
|
|
|
"土": "Earth",
|
|
|
|
"雷": "Lightning",
|
|
|
|
"水": "Water",
|
|
|
|
"光": "Light",
|
|
|
|
"闇": "Darkness"
|
2021-08-04 01:39:19 +00:00
|
|
|
}
|
|
|
|
|
2021-08-16 01:46:27 +00:00
|
|
|
def __init__(self, opus, serial, rarity, elements, name, text):
|
|
|
|
self.__opus = opus
|
|
|
|
self.__serial = serial
|
|
|
|
self.__rarity = rarity
|
|
|
|
self.__elements = elements
|
|
|
|
self.__name = name
|
|
|
|
self.__text = text
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_data(cls, data: dict[str, any], language: str):
|
2021-08-03 17:45:35 +00:00
|
|
|
if not data:
|
2021-08-16 01:46:27 +00:00
|
|
|
return cls(
|
|
|
|
opus="0",
|
|
|
|
serial="000",
|
|
|
|
rarity="X",
|
|
|
|
elements=[],
|
|
|
|
name=None,
|
|
|
|
text=None,
|
|
|
|
)
|
2021-08-02 23:37:22 +00:00
|
|
|
|
|
|
|
else:
|
2021-08-03 17:45:35 +00:00
|
|
|
if str(data["Code"])[0].isnumeric():
|
2021-08-04 01:39:19 +00:00
|
|
|
# card code starts with a number
|
2021-08-16 01:46:27 +00:00
|
|
|
opus, serial, rarity = \
|
2021-08-09 04:18:56 +00:00
|
|
|
re.match(r"([0-9]+)-([0-9]+)([CRHLS])", data["Code"]).groups()
|
2021-08-03 17:45:35 +00:00
|
|
|
|
|
|
|
elif str(data["Code"]).startswith("PR"):
|
2021-08-04 01:39:19 +00:00
|
|
|
# card code starts with "PR"
|
2021-08-16 01:46:27 +00:00
|
|
|
opus, serial = \
|
2021-08-09 04:18:56 +00:00
|
|
|
re.match(r"(PR)-([0-9]+)", data["Code"]).groups()
|
2021-08-16 01:46:27 +00:00
|
|
|
rarity = ""
|
2021-08-03 17:45:35 +00:00
|
|
|
|
|
|
|
elif str(data["Code"]).startswith("B"):
|
2021-08-04 01:39:19 +00:00
|
|
|
# card code starts with "B"
|
2021-08-16 01:46:27 +00:00
|
|
|
opus, serial = \
|
2021-08-09 04:18:56 +00:00
|
|
|
re.match(r"(B)-([0-9]+)", data["Code"]).groups()
|
2021-08-16 01:46:27 +00:00
|
|
|
rarity = ""
|
2021-08-02 23:37:22 +00:00
|
|
|
|
2021-08-02 23:55:12 +00:00
|
|
|
else:
|
2021-08-04 01:39:19 +00:00
|
|
|
# card code not recognized
|
2021-08-16 01:46:27 +00:00
|
|
|
opus, serial, rarity = \
|
2021-08-03 17:45:35 +00:00
|
|
|
"?", "???", "?"
|
|
|
|
|
2021-08-16 01:46:27 +00:00
|
|
|
return cls(
|
|
|
|
opus=opus,
|
|
|
|
serial=serial,
|
|
|
|
rarity=rarity,
|
|
|
|
elements=[Card.__ELEMENTS_MAP[element] for element in data["Element"].split("/")],
|
|
|
|
name=data[f"Name_{language}"],
|
|
|
|
text=data[f"Text_{language}"],
|
|
|
|
)
|
2021-08-02 23:37:22 +00:00
|
|
|
|
2021-08-09 02:03:24 +00:00
|
|
|
def __str__(self) -> str:
|
2021-08-04 16:36:23 +00:00
|
|
|
return f"'{self.__name}' ({'/'.join(self.__elements)}, {self.code})"
|
2021-08-02 23:37:22 +00:00
|
|
|
|
2021-08-16 01:46:27 +00:00
|
|
|
def to_json(self) -> str:
|
|
|
|
return json.dumps(self, default=lambda o: o.__dict__)
|
|
|
|
|
2021-08-02 23:37:22 +00:00
|
|
|
# 6-048C
|
2021-08-04 16:36:23 +00:00
|
|
|
@property
|
2021-08-09 02:03:24 +00:00
|
|
|
def code(self) -> str:
|
2021-08-02 23:55:12 +00:00
|
|
|
return f"{self.__opus}-{self.__serial}{self.__rarity}"
|
2021-08-04 01:39:19 +00:00
|
|
|
|
2021-08-04 22:33:18 +00:00
|
|
|
@property
|
2021-08-09 02:03:24 +00:00
|
|
|
def opus(self) -> str:
|
2021-08-04 22:33:18 +00:00
|
|
|
return self.__opus
|
|
|
|
|
|
|
|
@property
|
2021-08-09 02:03:24 +00:00
|
|
|
def serial(self) -> int:
|
2021-08-04 22:33:18 +00:00
|
|
|
return int(self.__serial)
|
|
|
|
|
|
|
|
@property
|
2021-08-09 02:03:24 +00:00
|
|
|
def rarity(self) -> str:
|
2021-08-04 22:33:18 +00:00
|
|
|
return self.__rarity
|
|
|
|
|
2021-08-04 16:36:23 +00:00
|
|
|
@property
|
2021-08-09 02:03:24 +00:00
|
|
|
def name(self) -> str:
|
2021-08-04 01:39:19 +00:00
|
|
|
return self.__name
|
|
|
|
|
2021-08-04 16:36:23 +00:00
|
|
|
@property
|
2021-08-09 02:03:24 +00:00
|
|
|
def text(self) -> str:
|
2021-08-04 01:39:19 +00:00
|
|
|
return self.__text
|
|
|
|
|
2021-08-04 16:36:23 +00:00
|
|
|
@property
|
2021-08-09 03:32:05 +00:00
|
|
|
def elements(self) -> list[str]:
|
2021-08-04 01:39:19 +00:00
|
|
|
return self.__elements
|