/
deck.py
72 lines (59 loc) · 2.46 KB
/
deck.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import os
import re
import shutil
from card import Card
from constants import DECK_INPUT_DIR, SPRITE_SHEET_OUTPUT_DIR
from sprite_sheet import SpriteSheet
class Deck(object):
def __init__(self, filename):
self.name = filename.rsplit(".", 1)[0]
self.filename = os.path.join(DECK_INPUT_DIR, filename)
self._cards = None
def __str__(self):
return self.name
def save(self):
primary_images = [card.image for card in self.cards]
secondary_images = [image for c in self.cards for image in c.secondary_images]
filename = os.path.join(SPRITE_SHEET_OUTPUT_DIR, self.name)
if os.path.exists(filename):
shutil.rmtree(filename)
os.mkdir(filename)
SpriteSheet.create_sprite_sheets(filename, "deck", primary_images)
if secondary_images:
SpriteSheet.create_sprite_sheets(filename, "secondary", secondary_images)
@property
def cards(self):
if self._cards is None:
with open(self.filename) as deck:
cards = []
for line in deck:
if line.rstrip():
quantity, card = line.rstrip().split(" ", 1)
quantity = int(quantity)
for i in range(quantity):
cards.append(Card(card))
self._cards = cards
return self._cards
@property
def output_dir(self):
return os.path.join(SPRITE_SHEET_OUTPUT_DIR, self.name)
def is_outdated(self):
input_last_modified = os.path.getmtime(self.filename)
output_last_modified = 0
# deck_name_regex = re.compile("^" + self.name + "_[0-9.]")
# output_files = [os.path.join(SPRITE_SHEET_OUTPUT_DIR, f) for f in self.__class__.output_files if deck_name_regex.match(f)]
if os.path.exists(self.output_dir) and os.path.isdir(self.output_dir):
output_last_modified = os.path.getmtime(self.output_dir)
return output_last_modified < input_last_modified
@classmethod
def get_all_decks(cls):
return [cls(f) for f in os.listdir(DECK_INPUT_DIR)]
@classmethod
def reload_outdated_decks(cls):
print("Reloading outdated decks")
outdated_decks = [deck for deck in cls.get_all_decks() if deck.is_outdated()]
for deck in outdated_decks:
print("Reloading outdated deck", deck)
deck.save()
if __name__ == "__main__":
Deck.reload_outdated_decks()