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

91 lines
2.7 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
2021-08-16 01:46:27 +00:00
import yaml
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
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
}
def __init__(self, code, elements, name, text):
self.__code = code
2021-08-16 01:46:27 +00:00
self.__elements = elements
self.__name = name
self.__text = text
@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=[],
name=None,
text=None,
)
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