forked from HorriblePeople/CardMachine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameGen.py
156 lines (136 loc) · 6.24 KB
/
GameGen.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
'''
Master Game Gen
1.1
'''
import os, glob
import PIL_Helper
import argparse
from OS_Helper import Delete, CleanDirectory, BuildPage, BuildBack
from sys import exit
import json
def main(folder="TSSSF", filepath="Core Deck/cards.json"):
'''
@param folder: The base game folder where we'll be working.
E.g. TSSSF, BaBOC
@param filepath: The filepath (relative to the base folder) where the
file that defines the different cards in the game are stored.
'''
CardFile = open(os.path.join(folder, filepath))
# Read first line of file to determine format and/or module
first_line = CardFile.readline()
if first_line == "{\n":
file_type = 'json'
# Load Card File
CardFile.seek(0)
data = json.load(CardFile)
CardFile.close()
module_name = data['deck']['module']
cards = data['cards']
else:
file_type = 'pon'
if first_line == "TSSSF_CardGen":
print('Warning: .pon files are DEPRECATED for TSSSF. Support for this format may be removed soon. Please use the pontojson.py converter to convert this file to JSON format.')
module_name = first_line
# Load Card File and strip out comments
cards = [line for line in CardFile if not line[0] in ('#', ';', '/')]
CardFile.close()
try:
module = __import__(module_name.strip())
except ValueError:
print("Failed to load module: " + str(ValueError))
return
card_set = os.path.dirname(filepath)
if file_type == 'json':
if data['deck'].get('version', '') != '':
card_set_text = '{} {}'.format(data['deck']['name'], data['deck']['version'])
else:
card_set_text = data['deck']['name']
module.CardSet = card_set_text
module.ARTIST = data['deck'].get('defaultArtist', module.ARTIST)
if 'symbol' in data['deck']:
module.Expansion_Icon = module.GetExpansionIcon(data['deck']['symbol'])
else:
module.CardSet = card_set
module.card_set = module.CardSet
# Create workspace for card images
workspace_path = CleanDirectory(path=folder, mkdir="workspace", rmstring="*.*")
# Create image directories
bleed_path = CleanDirectory(path=folder + "/" + card_set, mkdir="bleed-images", rmstring="*.*")
module.BleedsPath = bleed_path
cropped_path = CleanDirectory(path=folder + "/" + card_set, mkdir="cropped-images", rmstring="*.*")
module.CropPath = cropped_path
vassal_path = CleanDirectory(path=folder + "/" + card_set, mkdir="vassal-images", rmstring="*.*")
module.VassalPath = vassal_path
TGC_path = CleanDirectory(path=folder+"/"+card_set, mkdir="TGC-images",rmstring="*.*")
module.TGCPath = TGC_path
# Create output directory
output_folder = CleanDirectory(path=folder, mkdir=card_set, rmstring="*.pdf")
# Make pages
card_list = []
back_list = []
page_num = 0
for line in cards:
card_list.append(module.BuildCard(line))
back_list.append(module.BuildBack(line))
# If the card_list is big enough to make a page
# do that now, and set the card list to empty again
if len(card_list) >= module.TOTAL_CARDS:
page_num += 1
print("Building Page {}...".format(page_num))
BuildPage(card_list, page_num, module.PAGE_WIDTH, module.PAGE_HEIGHT, workspace_path)
BuildBack(back_list, page_num, module.PAGE_WIDTH, module.PAGE_HEIGHT, workspace_path)
card_list = []
back_list = []
# If there are leftover cards, fill in the remaining
# card slots with blanks and gen the last page
if len(card_list) > 0:
# Fill in the missing slots with blanks
while len(card_list) < module.TOTAL_CARDS:
card_list.append(module.BuildCard("BLANK"))
back_list.append(module.BuildCard("BLANK"))
page_num += 1
print("Building Page {}...".format(page_num))
BuildPage(card_list, page_num, module.PAGE_WIDTH, module.PAGE_HEIGHT, workspace_path)
BuildBack(back_list, page_num, module.PAGE_WIDTH, module.PAGE_HEIGHT, workspace_path)
# Build Vassal
module.CompileVassalModule()
print("\nCreating PDF...")
os.system(r'magick convert "{}/page_*.png" "{}/{}.pdf"'.format(workspace_path, output_folder, os.path.basename(card_set)))
print("\nCreating PDF of backs...")
os.system(r'magick convert "{}/backs_*.png" "{}/backs_{}.pdf"'.format(workspace_path, output_folder, os.path.basename(card_set)))
print("Done!")
if __name__ == '__main__':
# To run this script, you have two options:
# 1) Run it from the command line with arguments. E.g.:
# python GameGen -b TSSSF -f "Core 1.0.3/cards.pon"
# 2) Comment out "main(args.basedir, args.set_file)" in this file
# and add a new line with the proper folder and card set
# in the arguments.
# See the main() docstring for more info on the use of the arguments
parser = argparse.ArgumentParser(prog="GameGen")
parser.add_argument('-f', '--set-file', \
help="Location of set file to be parsed",
default="cards.json")
parser.add_argument('-b', '--basedir',
help="Workspace base directory with resources output directory",
default="TSSSF")
args = parser.parse_args()
#main(args.basedir, args.set_file)
#main('TSSSF', 'Core Deck/cards.pon')
main('TSSSF', 'Mini Expansions/Multiplicity 0.0.1a/cards.json')
#main('TSSSF', '1.1.0 Patch/cards.pon')
#main('TSSSF', '2014 Con Exclusives/cards.pon')
#main('TSSSF', 'BABScon 2015/cards.pon')
#main('TSSSF', 'Core 1.0.5/cards.pon')
#main('TSSSF', 'Core 1.0.5 Delta/cards.pon')
#main('TSSSF', 'Core 1.1.0/cards.pon')
#main('TSSSF', 'Core 1.1.0 Test/cards.pon')
#main('TSSSF', 'Custom Card for/cards.pon')
#main('TSSSF', 'Extra Credit 0.10.4/cards.pon')
#main('TSSSF', 'Indiegogo/cards.pon')
#main('TSSSF', 'Patreon Expansion 1/cards.pon')
#main('TSSSF', 'Ponycon Panel 2015/cards.pon')
#main('TSSSF', 'Ponyville University 1.0.1/cards.pon')
#main('TSSSF', 'Ponyville University 0.0.2/cards.pon')
#main('TSSSF', 'Ponyville University 1.0.2/cards.pon')
#main('TSSSF', 'Thank You/cards.pon')