forked from crhallberg/json-against-humanity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile.py
93 lines (88 loc) · 3.07 KB
/
compile.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
import os
import re
import json
'''
JSON format:
{
cards:
black: []
white: []
decks: [
abbr: {
name: "",
icon: "",
description: "",
official: true/false,
black: [indices],
white: [indices]
}
]
'''
blackCards = set([])
whiteCards = set([])
for deckDir in os.listdir('src/'):
with open('src/' + deckDir + '/black.md.txt') as f:
bcards = [x.strip() for x in f.readlines()]
blackCards.update(bcards)
with open('src/' + deckDir + '/white.md.txt') as f:
wcards = [x.strip() for x in f.readlines()]
whiteCards.update(wcards)
blackCards = list(blackCards)
whiteCards = list(whiteCards)
print ('cards - b:%u + w:%u = %7s' % (len(blackCards), len(whiteCards), '{:,}'.format(len(blackCards)+len(whiteCards))))
def treatCards(card):
# Trim
# Fix ending punctuation
# Convert to regular line breaks
return re.sub(r'([^\.\?!])$', '\g<1>.', card.strip()).replace('\\n', '\n')
officialBlack = 0
officialWhite = 0
blackJSON = []
whiteJSON = []
decks = {}
for deckDir in os.listdir('src/'):
with open('src/%s/metadata.json' % deckDir) as j:
metadata = json.load(j)
with open('src/' + deckDir + '/black.md.txt') as f:
bcards = [blackCards.index(x.strip()) for x in f.readlines()]
metadata['black'] = bcards
if metadata['official']:
officialBlack += len(bcards)
with open('src/' + deckDir + '/black.md.txt') as f:
blackJSON.extend([{ 'text': treatCards(x), 'pick': max(1, x.count('_')), 'deck': deckDir, 'icon': metadata['icon'] } for x in f.readlines()])
with open('src/' + deckDir + '/white.md.txt') as f:
wcards = [whiteCards.index(x.strip()) for x in f.readlines()]
metadata['white'] = wcards
if metadata['official']:
officialWhite += len(wcards)
with open('src/' + deckDir + '/white.md.txt') as f:
whiteJSON.extend([{ 'text': treatCards(x), 'deck': deckDir, 'icon': metadata['icon'] } for x in f.readlines()])
decks[metadata['abbr']] = metadata
del decks[metadata['abbr']]['abbr']
print ('official - b:%4u + w:%u = %7s' % (officialBlack, officialWhite, '{:,}'.format(officialBlack + officialWhite)))
#Compact format
compact = {
'cards': {
'black': [{ 'text': treatCards(x), 'pick': max(1, x.count('_')) } for x in blackCards],
'white': [{ 'text': treatCards(x) } for x in whiteCards]
},
'decks': decks
}
compactdump = json.dumps(compact).encode('utf8')
with open('compact.md.json', 'w') as outfile:
outfile.write(compactdump)
outfile.flush()
# Full format
print ('w/ dups - b:%u + w:%u = %7s' % (len(blackJSON), len(whiteJSON), '{:,}'.format(len(blackJSON)+len(whiteJSON))))
for i in decks:
del decks[i]['black']
del decks[i]['white']
full = {
"black": blackJSON,
"white": whiteJSON,
"metadata": decks
}
fulldump = json.dumps(full).encode('utf8')
with open('full.md.json', 'w') as outfile:
outfile.write(fulldump)
outfile.flush()