forked from secretshipfic/CardMachine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pontojson.py
150 lines (121 loc) · 5.54 KB
/
pontojson.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
#!/usr/bin/env python
"""
pontojson.py: Convert Horrible People Games' cards.pon files to .json format
For use with the TSSSF Card Generator module
"""
__author__ = "Jouva Moufette"
__copyright__ = "Copyright 2016, Children of Kefentse"
__license__ = "CC-BY-SA"
__version__ = "1.0.0"
__maintainer__ = "Jouva Moufette"
__email__ = "[email protected]"
import sys
import json
import os
import distutils.version
from collections import OrderedDict
TYPE, PICTURE, SYMBOLS, TITLE, KEYWORDS, BODY, FLAVOR, EXPANSION, CLIENT = range(9)
DEFAULT_ARTIST = "Pixel Prism"
Expansion_Icons = {
"Indiegogo": "symbol-Indiegogo.png",
"Summer": "symbol-summer-lovin.png",
"Xtra": "symbol-extracredit.png",
"Power": "symbol-power.png",
"Multiplicity": "symbol-multiplicity.png",
"Canon": "symbol-canon.png",
"Comics": "symbol-comics.png"
}
def convert_files(pon_file_path, json_file_path, artist):
# Initialize and setup important data
meta_key_order = ['name', 'version', 'module', 'symbol', 'defaultArtist', 'copyright']
card_key_order = ['type', 'title', 'picture', 'artist', 'symbols', 'keywords', 'body', 'flavor', 'client']
pack_key_order = ['deck', 'cards']
special_card_types = ['Rules1', 'Rules3', 'Rules5', 'Warning', 'Card']
cards = []
expansions = []
# Grab module name from first line and rest of file and close it
pon_file = open(pon_file_path)
module_name = pon_file.readline()
cardlines = [line for line in pon_file if not line[0] in ('#', ';', '/')]
pon_file.close()
# Use the directory that holds the cards.pon file as the pack name and version
pack_directory = os.path.basename(os.path.dirname(os.path.abspath(pon_file_path)))
pack_directory_words = pack_directory.split(' ')
pack_version_string = pack_directory_words.pop()
try:
pack_name = ' '.join(pack_directory_words)
pack_version = distutils.version.StrictVersion(pack_version_string)
except Exception:
pack_name = pack_directory
pack_version = distutils.version.StrictVersion("1.0.0")
pack_metadata = {
'name': pack_name,
'version': str(pack_version),
'module': module_name.strip(),
'defaultArtist': artist,
'copyright': "TSSSF by Horrible People Games."
}
for line in cardlines:
card = {}
row_data = line.strip('\n').strip('\r').replace(r'\n', '\n').split('`')
card['type'] = row_data[TYPE]
card['picture'] = row_data[PICTURE]
# Special cards only have 2 columns of data; add the data we have, ignore the rest and move on
if card['type'] in special_card_types:
cards.append(card)
continue
card['title'] = row_data[TITLE]
card['artist'] = artist
# Clean up symbols: Ignore goal/ship as a symbol and don't make a list of them if there are none
if row_data[SYMBOLS] != '':
symbols = row_data[SYMBOLS].split('!')
# if row_data[TYPE].lower() != 'pony' and row_data[TYPE] in symbols:
# symbols.remove(row_data[TYPE])
if len(symbols) > 0:
card['symbols'] = symbols
# Don't make a list of keywords if there are none
if row_data[KEYWORDS] != "":
card['keywords'] = [keywords.strip() for keywords in row_data[KEYWORDS].split(',')]
if row_data[BODY] != "":
card['body'] = row_data[BODY]
card['flavor'] = row_data[FLAVOR]
# Custom cards sometimes have a "client" attached to it (e.g. "for Horsefamous Pone")
if len(row_data) - 1 >= CLIENT:
card['client'] = row_data[CLIENT]
# Handle expansion icons: Not all cards have expansions, but keep track to warn user of multiple expansions referenced
if len(row_data) - 1 >= EXPANSION:
if row_data[EXPANSION] not in expansions and row_data[EXPANSION] != '':
expansions.append(row_data[EXPANSION])
cards.append(card)
# Keep the JSON of each card sorted in the order we want
cards_ordered = [OrderedDict(sorted(item.iteritems(), key=lambda (k, v): card_key_order.index(k)))
for item in cards]
# Core deck doesn't have an expansion so don't run this. Warn user of multiple expansions referenced
if len(expansions) > 0:
if len(expansions) > 1:
print "Warning: Found more than one expansion referenced! Using the first entry of {0}".format(
expansions[0])
pack_metadata['symbol'] = Expansion_Icons.get(expansions[0])
meta_ordered = OrderedDict(sorted(pack_metadata.iteritems(), key=lambda (k, v): meta_key_order.index(k)))
# Put together cards and deck info and make sure deck is at the top of the JSON
unordered_pack_data = {
'deck': meta_ordered,
'cards': cards_ordered
}
pack_data = OrderedDict(sorted(unordered_pack_data.items(), key=lambda (k, v): pack_key_order.index(k)))
json_file = open(json_file_path, mode='w')
json.dump(pack_data, json_file, indent=4)
return True
if len(sys.argv) < 3:
print "{} pon_file json_file [artist]".format(sys.argv[0])
print "Example: {} \"Core Deck 1.1.6\"/cards.pon \"Core Deck 1.1.6\"/cards.json".format(sys.argv[0])
print "Example: {} \"Core Deck 1.1.6\"/cards.pon \"Core Deck 1.1.6\"/cards.json \"Your Name\"".format(sys.argv[0])
exit(1)
pon_file_path = sys.argv[1]
json_file_path = sys.argv[2]
artist = DEFAULT_ARTIST
if len(sys.argv) > 3:
artist = sys.argv[3]
success = convert_files(pon_file_path, json_file_path, artist)
if success:
print "Done converting"