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

45 lines
1 KiB
Python
Raw Normal View History

2021-08-17 15:37:28 +00:00
from __future__ import annotations
2021-08-23 11:43:12 +00:00
import bz2
import pickle
2021-08-16 12:45:15 +00:00
2021-08-18 14:52:32 +00:00
from fftcg import Card
2021-08-17 15:37:28 +00:00
from fftcg.code import Code
2021-08-23 14:00:17 +00:00
from fftcg.utils import CARDDB_FILE_NAME
2021-08-17 15:37:28 +00:00
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):
2021-08-18 14:52:32 +00:00
self.__content: dict[Code, Card] = {}
2021-08-17 15:37:28 +00:00
2021-08-18 14:52:32 +00:00
def __getitem__(self, code: Code) -> Card:
return self.__content[code]
2021-08-17 15:37:28 +00:00
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-23 14:00:17 +00:00
with bz2.BZ2File(CARDDB_FILE_NAME, "r") as file:
book = pickle.load(file)
2021-08-16 12:45:15 +00:00
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-18 14:52:32 +00:00
for file_name, cards in book.items():
2021-08-17 15:37:28 +00:00
self.__content |= {
2021-08-18 14:52:32 +00:00
card.code: card
for card in cards
2021-08-16 12:45:15 +00:00
}