forked from SteuerSchleuder1000/VS_Test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uploadDeckData.py
executable file
·207 lines (149 loc) · 5.41 KB
/
uploadDeckData.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import pyrebase # learn more: https://python.org/pypi/Pyrebase
import csv
from datetime import datetime
import glob, os
import random
path = 'Sources/' # +hsFormat/hsClass/
UPLOAD_CLASS_TXT = True # Should the script upload class description texts?
UPLOAD_DECKS = False
config = { # Fenoms Firebase
"apiKey": "AIzaSyAt0uIAVOFjB42_bkwrEIqhSWkMT_VmluI",
"authDomain": "data-reaper.firebaseapp.com",
"databaseURL": "https://data-reaper.firebaseio.com",
"projectId": "data-reaper",
"storageBucket": "data-reaper.appspot.com",
"messagingSenderId": "1079276848174"
}
firebase = pyrebase.initialize_app(config)
auth = firebase.auth()
user = auth.sign_in_with_email_and_password('[email protected]', '2\!vEJ:6L]mh5R[z')
DB = firebase.database()
hsClasses = ['Druid','Hunter','Mage','Paladin','Priest','Rogue','Shaman','Warlock','Warrior']
hsFormats = ['Standard', 'Wild']
def getCSVFile(path):
f = open(path,encoding='latin-1')
reader=csv.reader(f)
return list(reader)
CARDS = getCSVFile(path+'cards.csv')
def getCardRarity(cardName):
for c in CARDS:
if c[0] != cardName:
continue
if len(c) >= 5:
r = c[4]
if r == 'Free':
r = 'Basic'
if r == '':
continue
#print(cardName,r)
return r
else:
return 'Basic'
return 'Basic'
def getCardType(cardName):
for c in CARDS:
if c[0] != cardName:
continue
if len(c) >= 3:
t = c[2]
return t
else:
return 'Minion'
return 'Minion'
def readDeckCode(file,hsClass, hsFormat):
title = ''
deckCode = ''
archetype = ''
author = ''
timestamp = ''
gameplay = ''
cardTypes = {'Minion': 0, 'Spell': 0, 'Weapon': 0, 'Hero': 0}
cards = []
readingCards = 'waiting'
count = 0
for row in file:
if len(row) <= 3:
if readingCards == 'reading':
readingCards = 'finished'
elif readingCards == 'waiting':
readingCards = 'reading'
continue
if '###' in row:
title = row[4:-1]
continue
if '#' not in row and deckCode == '':
deckCode = row[:-1]
continue
if '# Format: Wild' in row and hsFormat != 'Wild':
print('ERROR: decklist not Wild format! DeckName: '+title)
#return 0
if '# Format: Standard' in row and hsFormat != 'Standard':
print('ERROR: decklist not Standard format! DeckName: '+title)
#return 0
# Our markers:
if '# Archetype:' in row:
archetype = row[13:-1]
if '# Author:' in row:
author = row[10:-1]
if '# Gameplay:' in row:
gameplay = row
if '# Timestamp:' in row:
timestamp = row
# Cards
if readingCards == 'reading':
quantity = row[2]
manaCost = row[6]
if row[7] != ')': # check if double digit
manaCost = row[6:8]
name = row[10:-1]
else:
name = row[9:-1]
rarity = getCardRarity(name)
cardType = getCardType(name)
cardTypes[cardType] += int(quantity)
cards.append({'name':name,'manaCost':manaCost,'quantity':quantity, 'rarity':rarity})
count += 1
if archetype == '':
archetype = 'Other '+hsClass
if timestamp == '':
dt = datetime.utcnow()
timestamp = dt.strftime("%Y-%m-%d")
return {'name':title, 'cards':cards, 'deckCode': deckCode, 'gameplay': gameplay,
'author':author, 'timestamp':timestamp, 'cardTypes':cardTypes}, archetype
def upload(hsFormat):
# Delete Existing Files
if UPLOAD_DECKS:
for hsClass in hsClasses:
DB.child('deckData').child(hsFormat).child(hsClass).child('archetypes').remove(user['idToken'])
pass
for hsClass in hsClasses:
# moves dir to /Sources/hsClass/ and looks for all .txt files
os.chdir(path+hsFormat+'/'+hsClass)
for file in glob.glob("*.txt"):
# Class Description Texts should be labeled 'Druid.txt', 'Hunter.txt' etc. (first letter Capital)
if file == hsClass+'.txt':
if not UPLOAD_CLASS_TXT:
continue
f = open(file)
txt = f.read()
txt = txt.replace('<strong>',"<strong style='font-weight:bold'>")
txt = txt.replace('</p>','</p><br>')
txt = txt.replace('\n','<br>')
DB.child('deckData').child(hsFormat).child(hsClass).child('text').set(txt,user['idToken'])
continue
# Decklist files can be named anything other than [hsClass].txt
# returns deckFile = {name, cards: [{cardname,manacost,quantity},...], deckCode, color} and archetype
# returns 0 if hsFormats don't agree
f = open(file)
decklist, arch = readDeckCode(f.readlines(),hsClass, hsFormat)
if decklist != 0 and UPLOAD_DECKS:
DB.child('deckData').child(hsFormat).child(hsClass).child('archetypes').child(arch).push(decklist,user['idToken'])
pass
os.chdir('..')
os.chdir('..')
os.chdir('..')
# Execute main:
def main():
for f in hsFormats:
upload(f)
main()