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

61 lines
1.6 KiB
Python
Raw Normal View History

2021-08-17 15:37:28 +00:00
from __future__ import annotations
2021-08-16 12:45:15 +00:00
import yaml
2021-08-17 15:37:28 +00:00
from fftcg.code import Code
from fftcg.utils import BOOK_YML_NAME
class CardDB:
__instance: CardDB = None
2021-08-16 12:45:15 +00:00
2021-08-17 15:37:28 +00:00
@classmethod
def get(cls) -> CardDB:
if not CardDB.__instance:
CardDB.__instance = CardDB()
2021-08-16 12:45:15 +00:00
2021-08-17 15:37:28 +00:00
return CardDB.__instance
2021-08-16 12:45:15 +00:00
2021-08-17 15:37:28 +00:00
def __init__(self):
self.__content: dict[Code, dict[str, any]] = {}
def __getitem__(self, code: Code) -> dict[str, any]:
return self.__content[str(code)]
def load(self):
2021-08-16 12:45:15 +00:00
# load book.yml file
2021-08-17 15:37:28 +00:00
book: dict
2021-08-16 12:45:15 +00:00
try:
2021-08-17 15:37:28 +00:00
with open(BOOK_YML_NAME, "r") as file:
2021-08-16 12:45:15 +00:00
book = yaml.load(file, Loader=yaml.Loader)
except FileNotFoundError:
book = {}
# "invert" book into card database:
# every card is indexable by its code
2021-08-17 15:37:28 +00:00
self.__content.clear()
2021-08-16 12:45:15 +00:00
2021-08-17 15:37:28 +00:00
for file_name, content in book.items():
self.__content |= {
2021-08-16 12:45:15 +00:00
str(card.code): {
"card": card,
2021-08-17 15:37:28 +00:00
"file": file_name,
2021-08-16 12:45:15 +00:00
"index": i,
} for i, card in enumerate(content["cards"])
}
# write carddb.yml file
with open("carddb.yml", "w") as file:
2021-08-17 15:37:28 +00:00
yaml.dump(self.__content, file, Dumper=yaml.Dumper)
# def make_deck(self, filters):
# # filter codes by card criteria
# codes = [
# content["card"].code
# for content in self.__content.values()
# if all([f(content["card"]) for f in filters])
# ]
#
# from .ttsdeck import TTSDeck
# return TTSDeck(codes)