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

106 lines
3.1 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 .code import Code
2021-08-17 15:37:28 +00:00
from .utils import encircle_symbol
2021-08-16 01:46:27 +00:00
2021-08-18 14:52:32 +00:00
class Card:
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-18 14:52:32 +00:00
def __init__(self, code: Code, elements: list[str], name: str, text: str, face_url: str = "", index: int = 0):
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-17 15:37:28 +00:00
def from_data(cls, data: dict[str, any], language: str) -> 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=[],
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:
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)
# load text
text = str(data[f"Text_{language}"])
# 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)
# place elemental cost symbols
2021-08-18 11:49:34 +00:00
text = re.sub(rf"《([{''.join(Card.__ELEMENTS_MAP.keys())}])》", sub_elements, text, flags=re.IGNORECASE)
# place dull symbols
text = text.replace("《ダル》", "[⤵]")
# replace formatting hints with brackets
2021-08-18 11:49:34 +00:00
text = re.sub(r"\[\[[a-z]]]([^\[]*?)\s*\[\[/]]\s*", r"[\1] ", text, flags=re.IGNORECASE)
# place EX-BURST markers
2021-08-18 11:49:34 +00:00
text = re.sub(r"\[\[ex]]\s*EX BURST\s*\[\[/]]\s*", r"[EX BURST] ", text, flags=re.IGNORECASE)
# place line breaks
2021-08-18 11:49:34 +00:00
text = re.sub(r"\s*\[\[br]]\s*", "\n\n", text, flags=re.IGNORECASE)
2021-08-03 17:45:35 +00:00
2021-08-16 01:46:27 +00:00
return cls(
code=Code(data["Code"]),
2021-08-16 01:46:27 +00:00
elements=[Card.__ELEMENTS_MAP[element] for element in data["Element"].split("/")],
name=data[f"Name_{language}"],
text=text,
2021-08-16 01:46:27 +00:00
)
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
# 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 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