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

118 lines
3.4 KiB
Python
Raw Normal View History

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
from .code import Code
def encircle_symbol(symbol: str, negative: bool):
symbol = symbol[0].upper()
base_symbols: tuple[str, str] = "", ""
if symbol.isalpha():
if negative:
base_symbols = "A", "🅐"
else:
base_symbols = "A", ""
elif symbol == "0":
if negative:
base_symbols = "0", "🄌"
else:
base_symbols = "0", ""
elif symbol.isnumeric():
if negative:
base_symbols = "1", ""
else:
base_symbols = "1", ""
symbol_num = ord(symbol) - ord(base_symbols[0])
return chr(ord(base_symbols[1]) + symbol_num)
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
}
__ELEMENTS = "".join(__ELEMENTS_MAP.keys())
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
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(
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):
return encircle_symbol(match.group(1), True)
def sub_elements(match: re.Match):
return encircle_symbol(Card.__ELEMENTS_MAP[match.group(1)], True)
# 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
text = re.sub(rf"《([{Card.__ELEMENTS}])》", sub_elements, text, flags=re.IGNORECASE)
# 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)
# place EX-BURST markers
text = re.sub(r"\[\[ex\]\]\s*EX BURST\s*\[\[/\]\]\s*", r"[EX BURST] ", text, flags=re.IGNORECASE)
# place line breaks
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
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
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