1
0
Fork 0
mirror of https://github.com/ldericher/fftcgtool synced 2025-01-15 15:02:59 +00:00

Create card lists using filters

This commit is contained in:
Jörn-Michael Miehe 2021-08-16 14:45:15 +02:00
parent 2946bc81e4
commit 75373ae48b
4 changed files with 79 additions and 22 deletions

View file

@ -1,4 +1,6 @@
from .book import Book from .book import Book
from .opus import Opus from .opus import Opus
from .card import Card
from .carddb import CardDB
__all__ = ["Book", "Opus"] __all__ = ["Book", "Opus", "Card", "CardDB"]

View file

@ -55,40 +55,24 @@ class Book:
def __getitem__(self, index: int) -> Image.Image: def __getitem__(self, index: int) -> Image.Image:
return self.__pages[index]["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]] book: dict[str, dict[str, any]]
# load book.yml file # load book.yml file
try: try:
with open("book.yml", "r") as file: with open(book_yml_name, "r") as file:
book = yaml.load(file, Loader=yaml.Loader) book = yaml.load(file, Loader=yaml.Loader)
except FileNotFoundError: except FileNotFoundError:
book = {} book = {}
# save book # save book
for i, page in enumerate(self.__pages): for i, page in enumerate(self.__pages):
fn = f"{filename}_{i}.jpg" fn = f"{file_name}_{i}.jpg"
# save page image # save page image
page["image"].save(fn) page["image"].save(fn)
# add contents of that image # add contents of that image
book[fn] = {"cards": page["cards"]} book[fn] = {"cards": page["cards"]}
# update book.yml file # 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) 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)

43
fftcg/carddb.py Normal file
View file

@ -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

30
main.py
View file

@ -8,6 +8,7 @@ import fftcg
# constants # constants
GRID = 10, 7 # default in TTsim: 10 columns, 7 rows GRID = 10, 7 # default in TTsim: 10 columns, 7 rows
RESOLUTION = 429, 600 # default in TTsim: 480x670 pixels per card RESOLUTION = 429, 600 # default in TTsim: 480x670 pixels per card
BOOK_YML_NAME = "book.yml"
def main() -> None: def main() -> None:
@ -45,7 +46,34 @@ def main() -> None:
# main program # main program
opus = fftcg.Opus(args.opus_id) opus = fftcg.Opus(args.opus_id)
book = fftcg.Book(opus, GRID, RESOLUTION, "eg", args.num_threads) 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 # bye
logging.info("Done. Put the generated JSON files in your 'Saved Objects' Folder.") logging.info("Done. Put the generated JSON files in your 'Saved Objects' Folder.")