2021-08-17 15:37:28 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-08-02 23:37:22 +00:00
|
|
|
import re
|
|
|
|
|
2021-08-16 11:17:24 +00:00
|
|
|
from .code import Code
|
2021-08-23 16:16:56 +00:00
|
|
|
from .language import Language
|
2021-08-17 15:37:28 +00:00
|
|
|
from .utils import encircle_symbol
|
2021-08-16 11:17:24 +00:00
|
|
|
|
2021-08-16 01:46:27 +00:00
|
|
|
|
2021-08-18 14:52:32 +00:00
|
|
|
class Card:
|
2021-08-23 12:56:09 +00:00
|
|
|
__ELEMENTS_JAP = [
|
|
|
|
"火", "氷", "風", "土", "雷", "水", "光", "闇"
|
|
|
|
]
|
|
|
|
|
|
|
|
__ELEMENTS_ENG = [
|
|
|
|
"Fire", "Ice", "Wind", "Earth", "Lightning", "Water", "Light", "Darkness"
|
|
|
|
]
|
|
|
|
|
2021-08-04 01:39:19 +00:00
|
|
|
__ELEMENTS_MAP = {
|
2021-08-23 12:56:09 +00:00
|
|
|
elem_j: elem_e
|
|
|
|
for elem_j, elem_e in zip(__ELEMENTS_JAP, __ELEMENTS_ENG)
|
2021-08-04 01:39:19 +00:00
|
|
|
}
|
|
|
|
|
2021-08-18 14:52:32 +00:00
|
|
|
def __init__(self, code: Code, elements: list[str], name: str, text: str, face_url: str = "", index: int = 0):
|
2021-08-16 11:17:24 +00:00
|
|
|
self.__code = code
|
2021-08-16 01:46:27 +00:00
|
|
|
self.__elements = elements
|
|
|
|
self.__name = name
|
|
|
|
self.__text = text
|
|
|
|
|
2021-08-18 14:52:32 +00:00
|
|
|
self.__face_url = face_url
|
|
|
|
self.__index = index
|
|
|
|
|
2021-08-16 01:46:27 +00:00
|
|
|
@classmethod
|
2021-08-23 16:16:56 +00:00
|
|
|
def from_square_api_data(cls, data: dict[str, any], language: Language) -> Card:
|
2021-08-03 17:45:35 +00:00
|
|
|
if not data:
|
2021-08-16 01:46:27 +00:00
|
|
|
return cls(
|
2021-08-16 11:17:24 +00:00
|
|
|
code=Code(""),
|
2021-08-16 01:46:27 +00:00
|
|
|
elements=[],
|
2021-08-18 14:52:32 +00:00
|
|
|
name="",
|
|
|
|
text="",
|
2021-08-16 01:46:27 +00:00
|
|
|
)
|
2021-08-02 23:37:22 +00:00
|
|
|
|
|
|
|
else:
|
2021-08-16 11:17:24 +00:00
|
|
|
def sub_encircle(match: re.Match):
|
2021-08-16 23:35:30 +00:00
|
|
|
return encircle_symbol(match.group(1), False)
|
2021-08-16 11:17:24 +00:00
|
|
|
|
|
|
|
def sub_elements(match: re.Match):
|
2021-08-16 23:35:30 +00:00
|
|
|
return encircle_symbol(Card.__ELEMENTS_MAP[match.group(1)], False)
|
2021-08-16 11:17:24 +00:00
|
|
|
|
2021-08-23 16:16:56 +00:00
|
|
|
def load_text(language: Language) -> str:
|
|
|
|
# load text
|
|
|
|
text = str(data[f"Text{language.key_suffix}"])
|
|
|
|
# place "S" symbols
|
|
|
|
text = text.replace("《S》", encircle_symbol("S", False))
|
|
|
|
# place other letter and numerical cost symbols
|
|
|
|
text = re.sub(r"《([\w])》", sub_encircle, text, flags=re.UNICODE)
|
|
|
|
# place elemental cost symbols
|
|
|
|
text = re.sub(rf"《([{''.join(Card.__ELEMENTS_JAP)}])》", sub_elements, text, flags=re.UNICODE)
|
|
|
|
# place dull symbols
|
|
|
|
text = text.replace("《ダル》", "[⤵]")
|
|
|
|
# replace formatting hints with brackets
|
|
|
|
text = re.sub(r"\[\[[a-z]]]([^\[]*?)\s*\[\[/]]\s*", r"[\1] ", text, flags=re.IGNORECASE | re.UNICODE)
|
|
|
|
# place EX-BURST markers
|
|
|
|
text = re.sub(r"\[\[ex]]\s*EX BURST\s*\[\[/]]\s*", r"[EX BURST] ", text,
|
|
|
|
flags=re.IGNORECASE | re.UNICODE)
|
|
|
|
# place line breaks
|
|
|
|
return re.sub(r"\s*\[\[br]]\s*", "\n\n", text, flags=re.IGNORECASE | re.UNICODE)
|
2021-08-03 17:45:35 +00:00
|
|
|
|
2021-08-16 01:46:27 +00:00
|
|
|
return cls(
|
2021-08-16 11:17:24 +00:00
|
|
|
code=Code(data["Code"]),
|
2021-08-20 22:16:38 +00:00
|
|
|
elements=[
|
|
|
|
Card.__ELEMENTS_MAP[element]
|
|
|
|
for element in data["Element"].split("/")
|
|
|
|
],
|
2021-08-23 16:16:56 +00:00
|
|
|
name=data[f"Name{language.key_suffix}"],
|
|
|
|
text=load_text(language),
|
2021-08-16 01:46:27 +00:00
|
|
|
)
|
2021-08-02 23:37:22 +00:00
|
|
|
|
2021-08-23 14:47:11 +00:00
|
|
|
def __repr__(self) -> str:
|
|
|
|
return f"Card(code={self.code!r}, name={self.name!r}, face_url={self.face_url!r})"
|
|
|
|
|
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
|
|
|
|
|
|
|
# 6-048C
|
2021-08-04 16:36:23 +00:00
|
|
|
@property
|
2021-08-16 11:17:24 +00:00
|
|
|
def code(self) -> Code:
|
|
|
|
return self.__code
|
2021-08-04 22:33:18 +00:00
|
|
|
|
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
|
2021-08-18 14:52:32 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def face_url(self) -> str:
|
|
|
|
return self.__face_url
|
|
|
|
|
|
|
|
@face_url.setter
|
|
|
|
def face_url(self, face_url: str) -> None:
|
|
|
|
self.__face_url = face_url
|
|
|
|
|
|
|
|
@property
|
|
|
|
def index(self) -> int:
|
|
|
|
return self.__index
|
|
|
|
|
|
|
|
@index.setter
|
|
|
|
def index(self, index: int) -> None:
|
|
|
|
self.__index = index
|