From 75373ae48b2f4cca9409e32f022891b7a82f7b9a Mon Sep 17 00:00:00 2001 From: LDericher <40151420+ldericher@users.noreply.github.com> Date: Mon, 16 Aug 2021 14:45:15 +0200 Subject: [PATCH] Create card lists using filters --- fftcg/__init__.py | 4 +++- fftcg/book.py | 24 ++++-------------------- fftcg/carddb.py | 43 +++++++++++++++++++++++++++++++++++++++++++ main.py | 30 +++++++++++++++++++++++++++++- 4 files changed, 79 insertions(+), 22 deletions(-) create mode 100644 fftcg/carddb.py diff --git a/fftcg/__init__.py b/fftcg/__init__.py index 96f71c1..336f26e 100644 --- a/fftcg/__init__.py +++ b/fftcg/__init__.py @@ -1,4 +1,6 @@ from .book import Book from .opus import Opus +from .card import Card +from .carddb import CardDB -__all__ = ["Book", "Opus"] +__all__ = ["Book", "Opus", "Card", "CardDB"] diff --git a/fftcg/book.py b/fftcg/book.py index 85c736f..d6f7805 100644 --- a/fftcg/book.py +++ b/fftcg/book.py @@ -55,40 +55,24 @@ class Book: def __getitem__(self, index: int) -> Image.Image: return self.__pages[index]["image"] - def save(self, filename: str) -> None: + def save(self, file_name: str, book_yml_name: str) -> None: book: dict[str, dict[str, any]] # load book.yml file try: - with open("book.yml", "r") as file: + with open(book_yml_name, "r") as file: book = yaml.load(file, Loader=yaml.Loader) except FileNotFoundError: book = {} # save book for i, page in enumerate(self.__pages): - fn = f"{filename}_{i}.jpg" + fn = f"{file_name}_{i}.jpg" # save page image page["image"].save(fn) # add contents of that image book[fn] = {"cards": page["cards"]} # update book.yml file - with open("book.yml", "w") as file: + with open(book_yml_name, "w") as file: yaml.dump(book, file, Dumper=yaml.Dumper) - - # invert book - inverse_book: dict[Code, dict[str, any]] = {} - - for fn, content in book.items(): - inverse_book |= { - str(card.code): { - "card": card, - "file": fn, - "index": i - } for i, card in enumerate(content["cards"]) - } - - # write inverse_book.yml file - with open("inverse_book.yml", "w") as file: - yaml.dump(inverse_book, file, Dumper=yaml.Dumper) diff --git a/fftcg/carddb.py b/fftcg/carddb.py new file mode 100644 index 0000000..e333f31 --- /dev/null +++ b/fftcg/carddb.py @@ -0,0 +1,43 @@ +import yaml + +_DictOfDicts = dict[str, dict[str, any]] + + +class CardDB(_DictOfDicts): + def __init__(self, book_yml_name: str): + book: _DictOfDicts + + # load book.yml file + try: + with open(book_yml_name, "r") as file: + book = yaml.load(file, Loader=yaml.Loader) + except FileNotFoundError: + book = {} + + # "invert" book into card database: + # every card is indexable by its code + carddb: _DictOfDicts = {} + + for fn, content in book.items(): + carddb |= { + str(card.code): { + "card": card, + "file": fn, + "index": i, + } for i, card in enumerate(content["cards"]) + } + + super().__init__(carddb) + + # write carddb.yml file + with open("carddb.yml", "w") as file: + yaml.dump(self, file, Dumper=yaml.Dumper) + + def make_deck(self, filters): + # filter codes by card criteria + codes = [ + code + for code, content in self.items() + if all([f(content["card"]) for f in filters]) + ] + return codes diff --git a/main.py b/main.py index 33c258c..4a5535a 100755 --- a/main.py +++ b/main.py @@ -8,6 +8,7 @@ import fftcg # constants GRID = 10, 7 # default in TTsim: 10 columns, 7 rows RESOLUTION = 429, 600 # default in TTsim: 480x670 pixels per card +BOOK_YML_NAME = "book.yml" def main() -> None: @@ -45,7 +46,34 @@ def main() -> None: # main program opus = fftcg.Opus(args.opus_id) book = fftcg.Book(opus, GRID, RESOLUTION, "eg", args.num_threads) - book.save(opus.filename) + book.save(opus.filename, BOOK_YML_NAME) + + # create elemental decks for opus + carddb = fftcg.CardDB(BOOK_YML_NAME) + + def opus_filter(card: fftcg.Card): + return card.code.opus == opus.number + + def element_filter(element: str): + return lambda card: card.elements == [element] + + # simple cases: create lambdas for base elemental decks + base_elements = ["Fire", "Ice", "Wind", "Earth", "Lightning", "Water"] + element_filters = [element_filter(elem) for elem in base_elements] + + element_filters += [ + # light/darkness elemental deck + lambda card: card.elements == ["Light"] or card.elements == ["Darkness"], + # multi element deck + lambda card: len(card.elements) > 1, + ] + + # add in the opus_filter for all elemental decks + filters = list(zip([opus_filter]*len(element_filters), element_filters)) + + # make the decks + for f in filters: + print(carddb.make_deck(f)) # bye logging.info("Done. Put the generated JSON files in your 'Saved Objects' Folder.")