diff --git a/fftcg/carddb.py b/fftcg/carddb.py index 30d7ab4..72c4103 100644 --- a/fftcg/carddb.py +++ b/fftcg/carddb.py @@ -1,7 +1,8 @@ from __future__ import annotations -import bz2 +import json import pickle +import zipfile from .card import Card from .cards import Cards @@ -15,6 +16,9 @@ class CardDB: __cards: dict[Code, Card] __face_to_url: dict[str, str] + __DB_FILE_NAME = "cards.pickle" + __MAPPING_FILE_NAME = "face_to_url.json" + def __new__(cls) -> CardDB: if CardDB.__instance is None: CardDB.__instance = object.__new__(cls) @@ -29,24 +33,48 @@ class CardDB: def __getitem__(self, code: Code) -> Card: return self.__cards[code] - def __pickle(self): - # pickle db file - with bz2.BZ2File(CARDDB_FILE_NAME, "w") as file: - pickle.dump(self.__cards, file) - pickle.dump(self.__face_to_url, file) - - def update(self, cards: Cards): - for card in cards: - self.__cards[card.code] = card - - self.__pickle() - def get_face_url(self, face: str) -> str: if face in self.__face_to_url: return self.__face_to_url[face] else: return face + def __pickle(self) -> None: + with zipfile.ZipFile(CARDDB_FILE_NAME, "w", compression=zipfile.ZIP_LZMA) as zip_file: + # cards db + with zip_file.open(CardDB.__DB_FILE_NAME, "w") as file: + pickle.dump(self.__cards, file) + + # face_to_url mapping + with zip_file.open(CardDB.__MAPPING_FILE_NAME, "w") as file: + file.write(json.dumps(self.__face_to_url, indent=2).encode("utf-8")) + + def __unpickle(self) -> None: + # unpickle db file + self.__cards.clear() + self.__face_to_url.clear() + try: + with zipfile.ZipFile(CARDDB_FILE_NAME, "r") as zip_file: + # cards db + with zip_file.open(CardDB.__DB_FILE_NAME, "r") as file: + self.__cards |= pickle.load(file) + + # face_to_url mapping + with zip_file.open(CardDB.__MAPPING_FILE_NAME, "r") as file: + self.__face_to_url |= json.load(file) + + except FileNotFoundError: + pass + + def load(self) -> None: + self.__unpickle() + + def update(self, cards: Cards) -> None: + for card in cards: + self.__cards[card.code] = card + + self.__pickle() + def upload_prompt(self) -> None: faces = list(set([ card[lang].face @@ -63,17 +91,3 @@ class CardDB: self.__face_to_url[face] = face_url self.__pickle() - - def __unpickle(self): - # unpickle db file - self.__cards.clear() - self.__face_to_url.clear() - try: - with bz2.BZ2File(CARDDB_FILE_NAME, "r") as file: - self.__cards |= pickle.load(file) - self.__face_to_url |= pickle.load(file) - except FileNotFoundError: - pass - - def load(self) -> None: - self.__unpickle() diff --git a/fftcg/utils.py b/fftcg/utils.py index c0a2ef6..bda6858 100644 --- a/fftcg/utils.py +++ b/fftcg/utils.py @@ -7,7 +7,7 @@ GRID = Grid((10, 7)) # default in TTsim: 10 columns, 7 rows RESOLUTION = Grid((429, 600)) # default in TTsim: 480x670 pixels per card DECKS_DIR_NAME = "decks" # name of decks directory IMAGES_DIR_NAME = "images" # name of images directory -CARDDB_FILE_NAME = "carddb.pickle.bz2" # name of card book file +CARDDB_FILE_NAME = "carddb.zip" # name of card db file # card back URL (image by Aurik) CARD_BACK_URL = "http://cloud-3.steamusercontent.com/ugc/948455238665576576/85063172B8C340602E8D6C783A457122F53F7843/"