1
0
Fork 0
mirror of https://github.com/ldericher/fftcgtool synced 2025-01-15 15:02:59 +00:00
fftcgtool/fftcg/card.py
2021-08-16 03:46:27 +02:00

106 lines
2.7 KiB
Python

import json
import re
import yaml
class Card(yaml.YAMLObject):
yaml_tag = u'!Card'
__ELEMENTS_MAP = {
"": "Fire",
"": "Ice",
"": "Wind",
"": "Earth",
"": "Lightning",
"": "Water",
"": "Light",
"": "Darkness"
}
def __init__(self, opus, serial, rarity, elements, name, text):
self.__opus = opus
self.__serial = serial
self.__rarity = rarity
self.__elements = elements
self.__name = name
self.__text = text
@classmethod
def from_data(cls, data: dict[str, any], language: str):
if not data:
return cls(
opus="0",
serial="000",
rarity="X",
elements=[],
name=None,
text=None,
)
else:
if str(data["Code"])[0].isnumeric():
# card code starts with a number
opus, serial, rarity = \
re.match(r"([0-9]+)-([0-9]+)([CRHLS])", data["Code"]).groups()
elif str(data["Code"]).startswith("PR"):
# card code starts with "PR"
opus, serial = \
re.match(r"(PR)-([0-9]+)", data["Code"]).groups()
rarity = ""
elif str(data["Code"]).startswith("B"):
# card code starts with "B"
opus, serial = \
re.match(r"(B)-([0-9]+)", data["Code"]).groups()
rarity = ""
else:
# card code not recognized
opus, serial, rarity = \
"?", "???", "?"
return cls(
opus=opus,
serial=serial,
rarity=rarity,
elements=[Card.__ELEMENTS_MAP[element] for element in data["Element"].split("/")],
name=data[f"Name_{language}"],
text=data[f"Text_{language}"],
)
def __str__(self) -> str:
return f"'{self.__name}' ({'/'.join(self.__elements)}, {self.code})"
def to_json(self) -> str:
return json.dumps(self, default=lambda o: o.__dict__)
# 6-048C
@property
def code(self) -> str:
return f"{self.__opus}-{self.__serial}{self.__rarity}"
@property
def opus(self) -> str:
return self.__opus
@property
def serial(self) -> int:
return int(self.__serial)
@property
def rarity(self) -> str:
return self.__rarity
@property
def name(self) -> str:
return self.__name
@property
def text(self) -> str:
return self.__text
@property
def elements(self) -> list[str]:
return self.__elements