-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.py
329 lines (294 loc) · 12.9 KB
/
utils.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import base64
import json
import os
import re
from io import BytesIO
from django.shortcuts import render
from .forms import makeform, SmashggForm
from .stuff import games, base_path, templates, categories, categories_order
from .generar.getsets import event_data, challonge_data, tonamel_data
from .generar.perro import generate_banner
def graphic_from_request(request, game, hasextra=True, icon_sizes=(64, 32), default_bg="bg"):
if request.POST["lcolor1"] == "#ff281a" :
c1 = None
else :
c1 = request.POST["lcolor1"]
if request.POST["lcolor2"] == "#ffb60c" :
c2 = None
else :
c2 = request.POST["lcolor2"]
if "background" not in request.FILES :
bg = None
else :
bg = request.FILES["background"]
# toggles
darkbg = "darken_bg" in request.POST
cshadow = "charshadow" in request.POST
pr = "prmode" in request.POST
blacksq = "blacksquares" in request.POST
names = []
twitter = []
chars = []
seconds = [[] for i in range(8)]
flags = []
custom_flags = []
portraits = []
for i in range(1,9) :
names.append(request.POST["player"+str(i)+"_name"])
flag = request.POST["player"+str(i)+"_flag"]
flags.append(flag if flag != "None" else None)
if "player{}_portrait".format(i) in request.FILES :
portraits.append(request.FILES["player{}_portrait".format(i)])
else :
portraits.append(None)
if "player{}_custom_flag".format(i) in request.FILES :
custom_flags.append(request.FILES["player{}_custom_flag".format(i)])
else :
custom_flags.append(None)
if request.POST["player"+str(i)+"_twitter"] == "" :
twitter.append(None)
else :
twitter.append(request.POST["player"+str(i)+"_twitter"])
if game == "efz" and "player"+str(i)+"_palette" in request.FILES :
chars.append( (request.POST["player"+str(i)+"_char"],
request.FILES["player"+str(i)+"_palette"])
)
else :
chars.append( (request.POST["player"+str(i)+"_char"],
request.POST["player"+str(i)+"_color"])
)
if hasextra :
for k in range(1,3) :
if request.POST["player"+str(i)+"_extra"+str(k)] == "None" :
continue
else :
seconds[i-1].append((request.POST["player"+str(i)+"_extra"+str(k)],
request.POST["player"+str(i)+"_extra_color"+str(k)]))
if "logo" in request.FILES:
logo = request.FILES["logo"]
else:
logo = None
players = [{"tag" : names[j],
"char" : chars[j],
"twitter" : twitter[j],
"secondaries" : seconds[j],
"flag": flags[j],
"custom_flag": custom_flags[j],
"portrait": portraits[j]
}
for j in range(8)]
datos = { "players" : players,
"toptext" : request.POST["ttext"],
"bottomtext" : request.POST["btext"],
"url" : request.POST["url"],
"game" : game,
"logo": logo
}
fuente = request.POST["fontt"]
if fuente == "auto" :
fuente = None
if "font_file" in request.FILES :
fuente = request.FILES["font_file"]
img = generate_banner(datos,
customcolor= c1,
customcolor2=c2,
custombg=bg,
darkenbg=darkbg,
shadow=cshadow,
prmode=pr,
blacksquares=blacksq,
icon_sizes=icon_sizes,
font=fuente,
font_color1=request.POST["fcolor1"],
font_shadow1=request.POST["fscolor1"],
font_color2=request.POST["fcolor2"],
font_shadow2=request.POST["fscolor2"],
default_bg=default_bg
)
buffered = BytesIO()
img.save(buffered, format="PNG")
img = base64.b64encode(buffered.getvalue())
return str(img)[2:-1]
def hestia(request, game, FormClass,
hasextra=True, color_guide=None, icon_sizes=(64, 32), color_dict=None,
default_bg="bg"):
games_categories = [
{
'category_name': c,
'games': [{'slug': game[0], 'path': game[1]} for game in categories[c]]
}
for c in categories_order
]
if hasextra : has_extra = "true"
else : has_extra = "false"
if request.method == 'POST':
form = FormClass(request.POST, request.FILES)
form2 = SmashggForm(request.POST, request.FILES)
v1 = form.is_valid()
v2 = form2.is_valid()
if not v1:
print("\n", form.errors,"\n")
if v2 :
event = request.POST["event"]
# This is a copy of the validation code in forms.py, consider refactoring
startgg_match = re.match(form2.startgg_re, event)
if startgg_match is not None:
slug = startgg_match.groups()[-1]
datos = event_data(slug)
challonge_match = re.match(form2.challonge_re, event)
if challonge_match is not None:
org, slug = challonge_match.groups()
datos = challonge_data(slug, org=org)
tonamel_match = re.match(form2.tonamel_re, event)
if tonamel_match is not None:
datos = tonamel_data(tonamel_match.group(1))
init_data = {}
init_data["ttext"] = datos["toptext"]
init_data["btext"] = datos["bottomtext"]
init_data["url"] = datos["url"]
for i in range(8) :
try :
init_data["player"+str(i+1)] = {}
init_data["player"+str(i+1)]["name"] = datos["players"][i]["tag"]
init_data["player"+str(i+1)]["twitter"] = datos["players"][i]["twitter"]
init_data["player"+str(i+1)]["char"] = datos["players"][i]["char"][0]
except :
pass
context = { "hasextra" : has_extra,
"form" : FormClass(initial=init_data),
"form2" : SmashggForm(),
"off" : 2,
"color_guide" : color_guide,
"color_dict" : color_dict,
"game" : game,
"result" : None,
"games_categories": games_categories,
}
return render(request, 'old_form.html' , context)
if v1 :
img = graphic_from_request(request, game, hasextra=hasextra, icon_sizes=icon_sizes, default_bg=default_bg)
init_data = {}
field_keys = filter(lambda k: not "player" in k and not "csrf" in k, request.POST.keys())
for key in field_keys :
init_data[key] = request.POST[key]
check_field_keys = ["darken_bg", "blacksquares", "charshadow", "prmode"]
for key in check_field_keys :
init_data[key] = key in request.POST
for i in range(1,9) :
try :
init_data["player{}".format(i)] = {
"name": request.POST["player{}_name".format(i)],
"twitter": request.POST["player{}_twitter".format(i)],
"char": request.POST["player{}_char".format(i)],
"color": request.POST["player{}_color".format(i)],
"flag": request.POST["player{}_flag".format(i)],
}
for field in ["extra1", "extra_color1", "extra2", "extra_color2"] :
f = "player{}_{}".format(i, field)
if f in request.POST :
init_data["player{}".format(i)][field] = request.POST[f]
except :
pass
context = { "hasextra" : has_extra,
"form" : FormClass(initial=init_data),
"form2" : SmashggForm(),
"off" : 2,
"color_guide" : color_guide,
"color_dict" : color_dict,
"game" : game,
"result" : img,
"base_url" : request.get_host(),
"games_categories": games_categories,
}
return render(request, 'old_form.html' , context)
else :
context = {
"hasextra" : has_extra,
"color_guide" : color_guide,
"color_dict" : color_dict,
"game" : game,
"result" : None,
"games_categories": games_categories,
}
if "event" in request.POST :
form = FormClass()
context["off"] = 1
else :
form2 = SmashggForm()
context["off"] = 2
context["form"] = form
context["form2"] = form2
return render(request, 'old_form.html' , context)
else :
form = FormClass()
form2 = SmashggForm()
context = {
"form" : form,
"form2" : form2,
"off" : 2,
"hasextra" : has_extra,
"color_guide" : color_guide,
"color_dict" : color_dict,
"game" : game,
"result" : None,
"base_url" : request.get_host(),
"games_categories": games_categories,
}
return render(request, 'old_form.html' , context)
def game_data_from_json(game_path):
base_path = os.path.realpath(__file__)
base_path = os.path.abspath(os.path.join(base_path, os.pardir))
data_path = os.path.join(base_path, "generar", "assets", game_path, "game.json")
with open(data_path, "r") as f:
game_data = json.loads(f.read())
if game_data["colors"] is None:
game_data["colors"] = {c:["Default"] for c in game_data["characters"]}
if "iconColors" not in game_data or game_data["iconColors"] is None:
game_data["iconColors"] = game_data["colors"] #{c:["Default"] for c in game_data["characters"]}
game_data["maxColors"] = max([len(colors) for colors in game_data["colors"].values()])
game_data["maxIconColors"] = max([len(colors) for colors in game_data["iconColors"].values()])
if not "characterShadows" in game_data:
game_data["characterShadows"] = True
return game_data
def response_from_json(request, game_path):
game_data = game_data_from_json(game_path)
iconColors = game_data.get("iconColors")
FormClass = makeform(game=game_path,
chars=game_data["characters"],
echars=None if iconColors is None else list(iconColors.keys()),
numerito=game_data["maxColors"],
numerito_extra=game_data["maxIconColors"],
hasextra=game_data["hasIcons"],
color1=game_data["defaultLayoutColors"][0],
color2=game_data["defaultLayoutColors"][1],
default_black_squares=game_data.get("blackSquares", True),
default_character_shadows=game_data.get("characterShadows", True))
if iconColors:
color_dict = iconColors.copy()
color_dict.update(game_data["colors"])
else:
color_dict = game_data["colors"]
color_dict = json.dumps({game_path: color_dict})[1:-1]
return hestia(request, game_path, FormClass,
hasextra=game_data["hasIcons"], color_guide=game_data.get("colorGuide"),
color_dict=color_dict)
def is_url(string):
if type(string) is not str:
return False
url_pattern = r"(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))"
return re.match(url_pattern, string) is not None
def read_game_data(game):
if game in [g for _, g in games]:
data_path = os.path.join(base_path, "generar", "assets", game, "game.json")
with open(data_path, "r") as f:
game_data = f.read()
return json.loads(game_data)
def read_template_data(template, complete=False):
if template in templates:
data_path = os.path.join(base_path, "generar", "templates", template, "template.json")
with open(data_path, "r") as f:
template_data = f.read()
template_data = json.loads(template_data)
if not complete:
template_data.pop("layers")
return template_data