mirror of
https://github.com/ldericher/fftcgtool
synced 2025-01-15 15:02:59 +00:00
card.py scope refactoring
This commit is contained in:
parent
1e04e9f080
commit
d49d3cbff6
3 changed files with 73 additions and 66 deletions
|
@ -1,7 +1,6 @@
|
||||||
import dataclasses
|
import dataclasses
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
from dataclasses import replace
|
|
||||||
|
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@ from __future__ import annotations
|
||||||
|
|
||||||
import re
|
import re
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
from .code import Code
|
from .code import Code
|
||||||
from .language import Language, API_LANGS
|
from .language import Language, API_LANGS
|
||||||
|
@ -15,20 +16,67 @@ class CardContent:
|
||||||
face: str
|
face: str
|
||||||
|
|
||||||
|
|
||||||
class Card:
|
_ELEMENTS_JAP = [
|
||||||
__ELEMENTS_JAP = [
|
|
||||||
"火", "氷", "風", "土", "雷", "水", "光", "闇"
|
"火", "氷", "風", "土", "雷", "水", "光", "闇"
|
||||||
]
|
]
|
||||||
|
|
||||||
__ELEMENTS_ENG = [
|
_ELEMENTS_ENG = [
|
||||||
"Fire", "Ice", "Wind", "Earth", "Lightning", "Water", "Light", "Darkness"
|
"Fire", "Ice", "Wind", "Earth", "Lightning", "Water", "Light", "Darkness"
|
||||||
]
|
]
|
||||||
|
|
||||||
__ELEMENTS_MAP = {
|
_ELEMENTS_MAP = {
|
||||||
elem_j: elem_e
|
elem_j: elem_e
|
||||||
for elem_j, elem_e in zip(__ELEMENTS_JAP, __ELEMENTS_ENG)
|
for elem_j, elem_e in zip(_ELEMENTS_JAP, _ELEMENTS_ENG)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def _sub_encircle(match: re.Match) -> str:
|
||||||
|
return encircle_symbol(match.group(1), False)
|
||||||
|
|
||||||
|
|
||||||
|
def _sub_elements(match: re.Match) -> str:
|
||||||
|
return encircle_symbol(_ELEMENTS_MAP[match.group(1)], False)
|
||||||
|
|
||||||
|
|
||||||
|
def _load_name(language: Language, data: dict[str, Any]) -> str:
|
||||||
|
return data[f"Name{language.key_suffix}"]
|
||||||
|
|
||||||
|
|
||||||
|
def _load_text(language: Language, data: dict) -> str:
|
||||||
|
# load text
|
||||||
|
text = str(data[f"Text{language.key_suffix}"])
|
||||||
|
# place "S" symbols
|
||||||
|
text = text.replace("《S》", encircle_symbol("S", False))
|
||||||
|
# place elemental cost symbols
|
||||||
|
text = re.sub(rf"《([{''.join(_ELEMENTS_JAP)}])》", _sub_elements, text, flags=re.UNICODE)
|
||||||
|
# place dull symbols
|
||||||
|
text = text.replace("《ダル》", "[⤵]")
|
||||||
|
# relocate misplaced line break markers
|
||||||
|
text = re.sub(r"(\[\[[a-z]+]][^\[]*?)(\[\[br]])([^\[]*?\[\[/]])", r"\2\1\3", 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)
|
||||||
|
# also place unmarked EX-BURST markers
|
||||||
|
text = re.sub(r"([^\[]|^)(EX BURST)\s*([^]]|$)", r"\1[\2] \3", text, flags=re.UNICODE)
|
||||||
|
# replace Damage hints with brackets and en-dash
|
||||||
|
text = re.sub(r"\[\[i]](Schaden|Damage|Daños|Dégâts|Danni)\s*([0-9]+)\s*--\s*\[\[/]]\s*", r"[\1 \2] – ",
|
||||||
|
text, flags=re.IGNORECASE | re.UNICODE)
|
||||||
|
# place other letter and numerical cost symbols
|
||||||
|
text = re.sub(r"《([a-z0-9])》", _sub_encircle, text, flags=re.IGNORECASE | re.UNICODE)
|
||||||
|
# remove empty formatting hints
|
||||||
|
text = re.sub(r"\[\[[a-z]]]\s*\[\[/]]\s*", r" ", text, flags=re.IGNORECASE | re.UNICODE)
|
||||||
|
# replace formatting hints with brackets
|
||||||
|
text = re.sub(r"\[\[[a-z]]]([^\[]*?)\s*\[\[/]]\s*", r"[\1] ", text, flags=re.IGNORECASE | re.UNICODE)
|
||||||
|
# relocate misplaced spaces at start of bracketed string
|
||||||
|
text = re.sub(r"\s*(\[)\s+([^]]*?])", r" \1\2", text, flags=re.IGNORECASE | re.UNICODE)
|
||||||
|
# relocate misplaced spaces at end of bracketed string
|
||||||
|
text = re.sub(r"(\[[^]]*?)\s+(])\s*", r"\1\2 ", text, flags=re.IGNORECASE | re.UNICODE)
|
||||||
|
# place line breaks
|
||||||
|
return re.sub(r"\s*\[\[br]]\s*", "\n\n", text, flags=re.IGNORECASE | re.UNICODE)
|
||||||
|
|
||||||
|
|
||||||
|
class Card:
|
||||||
def __init__(self, code: Code, elements: list[str], content: dict[Language, CardContent], index: int = 0):
|
def __init__(self, code: Code, elements: list[str], content: dict[Language, CardContent], index: int = 0):
|
||||||
self.__code: Code = code
|
self.__code: Code = code
|
||||||
self.__elements: list[str] = elements
|
self.__elements: list[str] = elements
|
||||||
|
@ -36,7 +84,7 @@ class Card:
|
||||||
self.__index = index
|
self.__index = index
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_square_api_data(cls, data: dict[str, any]) -> Card:
|
def from_square_api_data(cls, data: dict[str, Any]) -> Card:
|
||||||
if not data:
|
if not data:
|
||||||
return cls(
|
return cls(
|
||||||
code=Code(""),
|
code=Code(""),
|
||||||
|
@ -45,61 +93,20 @@ class Card:
|
||||||
)
|
)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
def sub_encircle(match: re.Match) -> str:
|
|
||||||
return encircle_symbol(match.group(1), False)
|
|
||||||
|
|
||||||
def sub_elements(match: re.Match) -> str:
|
|
||||||
return encircle_symbol(Card.__ELEMENTS_MAP[match.group(1)], False)
|
|
||||||
|
|
||||||
def load_name(language: Language) -> str:
|
|
||||||
return data[f"Name{language.key_suffix}"]
|
|
||||||
|
|
||||||
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 elemental cost symbols
|
|
||||||
text = re.sub(rf"《([{''.join(Card.__ELEMENTS_JAP)}])》", sub_elements, text, flags=re.UNICODE)
|
|
||||||
# place dull symbols
|
|
||||||
text = text.replace("《ダル》", "[⤵]")
|
|
||||||
# relocate misplaced line break markers
|
|
||||||
text = re.sub(r"(\[\[[a-z]+]][^\[]*?)(\[\[br]])([^\[]*?\[\[/]])", r"\2\1\3", text,
|
|
||||||
flags=re.IGNORECASE | re.UNICODE)
|
|
||||||
# # relocate misplaced spaces
|
|
||||||
# text = re.sub(r"(\[\[[a-z]+]][^\[]*?)\s+(\[\[/]])", r"\1\2 ", text, flags=re.IGNORECASE | re.UNICODE)
|
|
||||||
# text = re.sub(r"(\[\[[a-z]+]])\s+([^\[]*?\[\[/]])", r" \1\2", 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)
|
|
||||||
text = re.sub(r"([^\[]|^)(EX BURST)\s*([^]]|$)", r"\1[\2] \3", text, flags=re.UNICODE)
|
|
||||||
# replace Damage hints with brackets and en-dash
|
|
||||||
text = re.sub(r"\[\[i]](Schaden|Damage|Daños|Dégâts|Danni)\s*([0-9]+)\s*--\s*\[\[/]]\s*", r"[\1 \2] – ",
|
|
||||||
text, flags=re.IGNORECASE | re.UNICODE)
|
|
||||||
# place other letter and numerical cost symbols
|
|
||||||
text = re.sub(r"《([a-z0-9])》", sub_encircle, text, flags=re.IGNORECASE | re.UNICODE)
|
|
||||||
# remove empty formatting hints
|
|
||||||
text = re.sub(r"\[\[[a-z]]]\s*\[\[/]]\s*", r" ", text, flags=re.IGNORECASE | re.UNICODE)
|
|
||||||
# replace formatting hints with brackets
|
|
||||||
text = re.sub(r"\[\[[a-z]]]([^\[]*?)\s*\[\[/]]\s*", r"[\1] ", text, flags=re.IGNORECASE | re.UNICODE)
|
|
||||||
# relocate misplaced spaces
|
|
||||||
text = re.sub(r"(\[[^]]*?)\s+(])\s*", r"\1\2 ", text, flags=re.IGNORECASE | re.UNICODE)
|
|
||||||
text = re.sub(r"\s*(\[)\s+([^]]*?])", r" \1\2", text, flags=re.IGNORECASE | re.UNICODE)
|
|
||||||
# place line breaks
|
|
||||||
return re.sub(r"\s*\[\[br]]\s*", "\n\n", text, flags=re.IGNORECASE | re.UNICODE)
|
|
||||||
|
|
||||||
content = {
|
|
||||||
language: CardContent(load_name(language), load_text(language), "")
|
|
||||||
for language in API_LANGS
|
|
||||||
}
|
|
||||||
|
|
||||||
return cls(
|
return cls(
|
||||||
code=Code(data["Code"]),
|
code=Code(data["Code"]),
|
||||||
elements=[
|
elements=[
|
||||||
Card.__ELEMENTS_MAP[element]
|
_ELEMENTS_MAP[element]
|
||||||
for element in data["Element"].split("/")
|
for element in data["Element"].split("/")
|
||||||
],
|
],
|
||||||
content=content,
|
content={
|
||||||
|
language: CardContent(
|
||||||
|
_load_name(language, data),
|
||||||
|
_load_text(language, data),
|
||||||
|
""
|
||||||
|
)
|
||||||
|
for language in API_LANGS
|
||||||
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
|
|
|
@ -3,6 +3,7 @@ from __future__ import annotations
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
from .carddb import CardDB
|
from .carddb import CardDB
|
||||||
from .cards import Cards
|
from .cards import Cards
|
||||||
|
@ -44,7 +45,7 @@ class TTSDeck(Cards):
|
||||||
def file_name(self) -> str:
|
def file_name(self) -> str:
|
||||||
return f"{super().file_name}.json"
|
return f"{super().file_name}.json"
|
||||||
|
|
||||||
def get_tts_object(self, language: Language) -> dict[str, any]:
|
def get_tts_object(self, language: Language) -> dict[str, Any]:
|
||||||
carddb = CardDB()
|
carddb = CardDB()
|
||||||
|
|
||||||
# unique face urls used
|
# unique face urls used
|
||||||
|
|
Loading…
Reference in a new issue