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

97 lines
2.7 KiB
Python
Raw Normal View History

2021-08-16 23:35:57 +00:00
import json
from .carddb import CardDB
2021-08-18 14:52:32 +00:00
from .cards import Cards
2021-08-16 23:35:57 +00:00
from .code import Code
2021-08-18 11:49:34 +00:00
from .utils import CARD_BACK_URL
2021-08-16 23:35:57 +00:00
2021-08-18 14:52:32 +00:00
class TTSDeck(Cards):
2021-08-18 11:49:34 +00:00
def __init__(self, codes: list[Code], name: str, description: str):
2021-08-18 14:52:32 +00:00
super().__init__(name)
2021-08-18 11:49:34 +00:00
self.__description = description
2021-08-18 15:38:58 +00:00
# get cards from carddb
2021-08-18 14:52:32 +00:00
carddb = CardDB.get()
self.extend([carddb[code] for code in codes])
2021-08-18 15:38:58 +00:00
# unique face urls used
unique_face_urls = set([
2021-08-18 14:52:32 +00:00
card.face_url
for card in self
2021-08-18 15:38:58 +00:00
])
2021-08-16 23:35:57 +00:00
2021-08-18 15:38:58 +00:00
# lookup for indices of urls
self.__url_indices = {
url: i + 1
for i, url in enumerate(unique_face_urls)
}
@property
def tts_object(self) -> dict[str, any]:
# build the "CustomDeck" dictionary
2021-08-16 23:35:57 +00:00
custom_deck = {
2021-08-18 15:38:58 +00:00
str(i): {
2021-08-16 23:35:57 +00:00
"NumWidth": "10",
"NumHeight": "7",
"FaceURL": url,
2021-08-18 11:49:34 +00:00
"BackURL": CARD_BACK_URL,
2021-08-18 15:38:58 +00:00
} for url, i in self.__url_indices.items()
2021-08-16 23:35:57 +00:00
}
2021-08-18 15:38:58 +00:00
# values both in main deck and each contained card
2021-08-16 23:35:57 +00:00
common_dict = {
"Transform": {
"scaleX": 2.17822933,
"scaleY": 1.0,
"scaleZ": 2.17822933
},
"Locked": False,
"Grid": True,
"Snap": True,
"Autoraise": True,
"Sticky": True,
"Tooltip": True,
"GridProjection": False,
}
2021-08-18 15:38:58 +00:00
# cards contained in deck
2021-08-16 23:35:57 +00:00
contained_objects = [
{
2021-08-18 14:52:32 +00:00
"Nickname": card.name,
"Description": card.text,
2021-08-18 15:38:58 +00:00
"CardID": 100 * self.__url_indices[card.face_url] + card.index,
2021-08-16 23:35:57 +00:00
2021-08-18 15:38:58 +00:00
"Name": "Card",
2021-08-16 23:35:57 +00:00
"Hands": True,
"SidewaysCard": False,
2021-08-18 14:52:32 +00:00
} | common_dict for card in self
2021-08-16 23:35:57 +00:00
]
2021-08-18 15:38:58 +00:00
# extract the card ids
2021-08-16 23:35:57 +00:00
deck_ids = [
contained_object["CardID"]
for contained_object in contained_objects
]
2021-08-18 15:38:58 +00:00
# create the deck dictionary
return {"ObjectStates": [
2021-08-16 23:35:57 +00:00
{
2021-08-18 14:52:32 +00:00
"Nickname": self.name,
2021-08-18 11:49:34 +00:00
"Description": self.__description,
2021-08-16 23:35:57 +00:00
"DeckIDs": deck_ids,
"CustomDeck": custom_deck,
"ContainedObjects": contained_objects,
2021-08-18 15:38:58 +00:00
"Name": "Deck",
"Hands": False,
"SidewaysCard": False,
2021-08-16 23:35:57 +00:00
} | common_dict
]}
2021-08-18 15:38:58 +00:00
def save(self) -> None:
# only save if the deck contains cards
if self:
with open(f"{self.file_name}.json", "w") as file:
json.dump(self.tts_object, file, indent=2)