1
0
Fork 0
mirror of https://github.com/ldericher/fftcgtool synced 2025-01-15 23:03:00 +00:00
fftcgtool/fftcg/card.py

125 lines
4 KiB
Python
Raw Normal View History

2021-08-17 15:37:28 +00:00
from __future__ import annotations
2021-08-02 23:37:22 +00:00
import re
from dataclasses import dataclass
2021-08-02 23:37:22 +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 01:46:27 +00:00
@dataclass(frozen=True)
class CardContent:
name: str
text: str
face: str
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
}
def __init__(self, code: Code, elements: list[str], content: dict[Language, CardContent], index: int = 0):
self.__code: Code = code
self.__elements: list[str] = elements
self.__content: dict[Language, CardContent] = content
2021-08-18 14:52:32 +00:00
self.__index = index
2021-08-16 01:46:27 +00:00
@classmethod
def from_square_api_data(cls, data: dict[str, any]) -> Card:
2021-08-03 17:45:35 +00:00
if not data:
2021-08-16 01:46:27 +00:00
return cls(
code=Code(""),
2021-08-16 01:46:27 +00:00
elements=[],
content={},
2021-08-16 01:46:27 +00:00
)
2021-08-02 23:37:22 +00:00
else:
def sub_encircle(match: re.Match):
2021-08-16 23:35:30 +00:00
return encircle_symbol(match.group(1), False)
def sub_elements(match: re.Match):
2021-08-16 23:35:30 +00:00
return encircle_symbol(Card.__ELEMENTS_MAP[match.group(1)], False)
def load_name(language: Language) -> str:
return data[f"Name{language.key_suffix}"]
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"《([a-z0-9])》", sub_encircle, text, flags=re.IGNORECASE | re.UNICODE)
2021-08-23 16:16:56 +00:00
# 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
content = {
language: CardContent(load_name(language), load_text(language), "")
for language in Language.all_api_langs()
}
2021-08-16 01:46:27 +00:00
return cls(
code=Code(data["Code"]),
2021-08-20 22:16:38 +00:00
elements=[
Card.__ELEMENTS_MAP[element]
for element in data["Element"].split("/")
],
content=content,
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}, content={self.__content!r})"
2021-08-23 14:47:11 +00:00
2021-08-09 02:03:24 +00:00
def __str__(self) -> str:
if self.__content:
return f"'{self[''].name}' ({'/'.join(self.__elements)}, {self.code})"
def __getitem__(self, item: Language | str) -> CardContent:
if isinstance(item, Language):
return self.__content[item]
else:
return self.__content[Language(item)]
def __setitem__(self, key: Language | str, value: CardContent) -> None:
if isinstance(key, Language):
self.__content[key] = value
else:
self.__content[Language(key)] = value
2021-08-02 23:37:22 +00:00
# 6-048C
2021-08-04 16:36:23 +00:00
@property
def code(self) -> Code:
return self.__code
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 index(self) -> int:
return self.__index
@index.setter
def index(self, index: int) -> None:
self.__index = index