-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.py
More file actions
208 lines (176 loc) · 9.09 KB
/
Copy pathapp.py
File metadata and controls
208 lines (176 loc) · 9.09 KB
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
import get_data, do_math
from flask import Flask, render_template, request, redirect, url_for
import json, os
app = Flask(__name__)
@app.route("/", methods=['GET', 'POST'])
def front_page():
if request.method == 'GET':
return render_template('index.html')
if request.method == 'POST':
if request.form['button'] == 'submit':
summoner1 = request.form['sum1'].lower().replace(" ", "")
summoner2 = request.form['sum2'].lower().replace(" ", "")
summoner3 = request.form['sum3'].lower().replace(" ", "")
summoner4 = request.form['sum4'].lower().replace(" ", "")
summoner5 = request.form['sum5'].lower().replace(" ", "")
region = request.form['region']
champ_count = request.form['champions']
picked_list = []
banned_list = []
return redirect(url_for('.results', sum1=summoner1, sum2=summoner2, sum3=summoner3, sum4=summoner4,
sum5=summoner5, region=region, champnum=champ_count, picked=picked_list,
banned=banned_list))
if request.form['button'] == 'about':
return redirect(url_for('.about'))
@app.route("/about", methods=['GET', 'POST'])
def about():
if request.method == 'GET':
return render_template('about.html')
if request.method == 'POST':
if request.form['button'] == 'home':
return redirect(url_for('.front_page'))
@app.route("/error", methods=['GET', 'POST'])
def error():
if request.method == 'GET':
error_message = request.args['values']
return render_template('error.html', error=error_message)
if request.method == 'POST':
if request.form['button'] == 'home':
return redirect(url_for('.front_page'))
@app.route("/results", methods=['GET', 'POST'])
def results():
if request.method == 'GET':
summoner1 = request.args['sum1']
summoner2 = request.args['sum2']
summoner3 = request.args['sum3']
summoner4 = request.args['sum4']
summoner5 = request.args['sum5']
region = request.args['region']
champ_count = int(request.args['champnum'])
# Get a dictionary that maps champion id to champion name
dir_path = os.path.dirname(os.path.realpath(__file__))
champ_file = '{0}/{1}'.format(dir_path, 'champion_dict.json')
with open(champ_file) as f:
champ_id_to_name = json.load(f)
# Get the champion name for later use, because 'champion 35' doesn't mean much to people
champ_id_to_name = {key: value['name'] for key, value in champ_id_to_name.items()}
try:
picked_list = set(request.args['picked'].split(','))
except KeyError:
picked_list = set()
try:
banned_list = set(request.args['banned'].split(','))
except KeyError:
banned_list = set()
try:
assert len(picked_list) <= 5
assert len(banned_list) <= 15
for champ in picked_list:
assert champ in champ_id_to_name.values()
for champ in banned_list:
assert champ in champ_id_to_name.values()
except AssertionError:
return redirect(url_for('.error', values='You tampered with the picked or banned list.'
' Please don\'t do that!'))
# Generation size should scale with picks/bans
generation_size = min(5000, (champ_count-len(banned_list) - 1)**(5 - len(picked_list)))
if generation_size == 1:
return redirect(url_for('.error', values='You already have your whole team picked? Our job is done here'))
summoners = [summoner1, summoner2, summoner3, summoner4, summoner5]
if region not in ['NA', 'BR', 'EUNE', 'EUW', 'JP', 'KR', 'LAN', 'LAS', 'OCE', 'TR', 'RU']:
return redirect(url_for('.error', values='Region not valid'))
if champ_count < 15 or champ_count > 30:
return redirect(url_for('.error', values='Champ count not valid'))
try:
summoner_data = gather_info(summoners, champ_count, champ_id_to_name, region)
except KeyError as inst:
err_message = str(inst)[1:-1]
return redirect(url_for('.error', values=err_message))
dream_team = build_team(summoner_data, picked_list, banned_list, generation_size)
answer0 = [dream_team[0].player, dream_team[0].role, dream_team[0], dream_team[0].id]
answer1 = [dream_team[1].player, dream_team[1].role, dream_team[1], dream_team[1].id]
answer2 = [dream_team[2].player, dream_team[2].role, dream_team[2], dream_team[2].id]
answer3 = [dream_team[3].player, dream_team[3].role, dream_team[3], dream_team[3].id]
answer4 = [dream_team[4].player, dream_team[4].role, dream_team[4], dream_team[4].id]
team = [answer0, answer1, answer2, answer3, answer4]
return render_template('results.html', results=team)
if request.method == 'POST':
if request.form['button'] == 'home':
return redirect(url_for('.front_page'))
if request.form['button'] == 'recalculate':
summoner1 = request.args['sum1']
summoner2 = request.args['sum2']
summoner3 = request.args['sum3']
summoner4 = request.args['sum4']
summoner5 = request.args['sum5']
region = request.args['region']
champ_count = int(request.args['champnum'])
try:
past_picked = set(request.args['picked'].split(','))
except KeyError:
past_picked = set()
try:
past_banned = set(request.args['banned'].split(','))
except KeyError:
past_banned = set()
try:
champ1 = request.form['pb1']
except KeyError:
champ1 = None
try:
champ2 = request.form['pb2']
except KeyError:
champ2 = None
try:
champ3 = request.form['pb3']
except KeyError:
champ3 = None
try:
champ4 = request.form['pb4']
except KeyError:
champ4 = None
try:
champ5 = request.form['pb5']
except KeyError:
champ5 = None
pb_list = [champ1, champ2, champ3, champ4, champ5]
pb_list = {value for value in pb_list if value is not None}
if len(past_picked) == 0:
picked_list = ",".join([name[:-1] for name in pb_list if name[-1] == 'P'])
else:
picked_list = ",".join([name[:-1] for name in pb_list if name[-1] == 'P'] + list(past_picked))
if len(past_banned) == 0:
banned_list = ",".join([name[:-1] for name in pb_list if name[-1] == 'B'])
else:
banned_list = ",".join([name[:-1] for name in pb_list if name[-1] == 'B'] + list(past_banned))
if picked_list != '' and banned_list != '':
return redirect(url_for('.results', sum1=summoner1, sum2=summoner2, sum3=summoner3, sum4=summoner4,
sum5=summoner5, region=region, champnum=champ_count, picked=picked_list,
banned=banned_list))
elif picked_list != '':
return redirect(url_for('.results', sum1=summoner1, sum2=summoner2, sum3=summoner3, sum4=summoner4,
sum5=summoner5, region=region, champnum=champ_count, picked=picked_list))
elif banned_list != '':
return redirect(url_for('.results', sum1=summoner1, sum2=summoner2, sum3=summoner3, sum4=summoner4,
sum5=summoner5, region=region, champnum=champ_count, banned=banned_list))
def gather_info(summoners, champ_count, champ_id_to_name, region):
summoner_data = {}
for player in summoners:
summoner_id, summoner_name, account_id = get_data.get_summoner_data(player, region)
champ_points_pair = get_data.get_summoners_mastery(summoner_id, summoner_name, champ_count, region)
champ_counters = get_data.lanes_and_roles(account_id, summoner_name, champ_points_pair, champ_id_to_name, region)
get_data.percentages(champ_counters)
summoner_data[summoner_name] = get_data.data_compile(summoner_name, champ_counters,
champ_id_to_name, champ_points_pair)
return summoner_data
def build_team(summoner_data, picked_list, banned_list, generation_size):
population = do_math.populate_generation(summoner_data, generation_size, picked_list, banned_list)
health = 0
new_health = 1
while health / new_health < .995:
health = do_math.grade_generation(population)
population = do_math.evolve(population, picked_list, banned_list)
population = do_math.mutate(population, summoner_data, picked_list, banned_list)
new_health = do_math.grade_generation(population)
dream_team = population[0]
return dream_team