mirror of
https://github.com/ldericher/fftcgtool
synced 2025-01-15 15:02:59 +00:00
carddb file format
This commit is contained in:
parent
1294cc3a38
commit
c270959ed2
2 changed files with 42 additions and 28 deletions
|
@ -1,7 +1,8 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import bz2
|
import json
|
||||||
import pickle
|
import pickle
|
||||||
|
import zipfile
|
||||||
|
|
||||||
from .card import Card
|
from .card import Card
|
||||||
from .cards import Cards
|
from .cards import Cards
|
||||||
|
@ -15,6 +16,9 @@ class CardDB:
|
||||||
__cards: dict[Code, Card]
|
__cards: dict[Code, Card]
|
||||||
__face_to_url: dict[str, str]
|
__face_to_url: dict[str, str]
|
||||||
|
|
||||||
|
__DB_FILE_NAME = "cards.pickle"
|
||||||
|
__MAPPING_FILE_NAME = "face_to_url.json"
|
||||||
|
|
||||||
def __new__(cls) -> CardDB:
|
def __new__(cls) -> CardDB:
|
||||||
if CardDB.__instance is None:
|
if CardDB.__instance is None:
|
||||||
CardDB.__instance = object.__new__(cls)
|
CardDB.__instance = object.__new__(cls)
|
||||||
|
@ -29,24 +33,48 @@ class CardDB:
|
||||||
def __getitem__(self, code: Code) -> Card:
|
def __getitem__(self, code: Code) -> Card:
|
||||||
return self.__cards[code]
|
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:
|
def get_face_url(self, face: str) -> str:
|
||||||
if face in self.__face_to_url:
|
if face in self.__face_to_url:
|
||||||
return self.__face_to_url[face]
|
return self.__face_to_url[face]
|
||||||
else:
|
else:
|
||||||
return face
|
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:
|
def upload_prompt(self) -> None:
|
||||||
faces = list(set([
|
faces = list(set([
|
||||||
card[lang].face
|
card[lang].face
|
||||||
|
@ -63,17 +91,3 @@ class CardDB:
|
||||||
self.__face_to_url[face] = face_url
|
self.__face_to_url[face] = face_url
|
||||||
|
|
||||||
self.__pickle()
|
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()
|
|
||||||
|
|
|
@ -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
|
RESOLUTION = Grid((429, 600)) # default in TTsim: 480x670 pixels per card
|
||||||
DECKS_DIR_NAME = "decks" # name of decks directory
|
DECKS_DIR_NAME = "decks" # name of decks directory
|
||||||
IMAGES_DIR_NAME = "images" # name of images 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 (image by Aurik)
|
||||||
CARD_BACK_URL = "http://cloud-3.steamusercontent.com/ugc/948455238665576576/85063172B8C340602E8D6C783A457122F53F7843/"
|
CARD_BACK_URL = "http://cloud-3.steamusercontent.com/ugc/948455238665576576/85063172B8C340602E8D6C783A457122F53F7843/"
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue