/
LeagueCircuit.py
327 lines (290 loc) · 15.8 KB
/
LeagueCircuit.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
import psycopg2
from flask import Flask, render_template, redirect, flash, session
import requests
from flask.ext.wtf import Form
from wtforms import StringField, PasswordField
import riotwatcher
import atexit
app = Flask(__name__)
app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT'
app.service = 'http://127.0.0.1:5001/api/'
key = '1dbf97cc-5028-4196-a05c-6645adc80bef'
w = riotwatcher.RiotWatcher(key)
@app.route('/')
def index():
return render_template("index.html")
@app.route('/sign-out')
def sign_out():
if 'username' in session:
del session['username']
return redirect('/')
@app.route('/sign-up', methods=['GET', 'POST'])
def sign_up():
class SignUpForm(Form):
email = StringField()
password = PasswordField()
confirm = PasswordField()
summoner_name = StringField()
form = SignUpForm()
if form.validate_on_submit():
mail = form.email.data.encode('ascii', 'ignore').lower()
name = form.summoner_name.data.encode('ascii', 'ignore').lower()
connect.cursor.execute("SELECT username FROM LEAGUE.USER WHERE username = '{0}'".format(mail))
err = False
try:
w.get_summoner(name)
except:
flash("Invalid summoner name. Please enter a valid summoner name", 'signup')
err = True
if connect.cursor.fetchall():
flash('User id already taken.', 'signup')
err = True
#return redirect('sign-up')
#need to put error message
connect.cursor.execute("SELECT sum_name FROM LEAGUE.USER WHERE sum_name = '{0}'".format(name))
if connect.cursor.fetchall():
flash('Summoner name already registered.', 'signup')
err = True
#return redirect('sign-up')
if form.password.data.encode('ascii', 'ignore') != form.confirm.data.encode('ascii', 'ignore'):
flash('Passwords do not match.', 'signup')
err = True
if not err:
connect.cursor.execute("INSERT INTO LEAGUE.USER VALUES ('{0}','{2}','{1}')".format(mail, form.password.data.encode('ascii', 'ignore'), name))
connect.conn.commit()
return redirect('log-in')
else:
return redirect('sign-up')
return render_template("signup.html", form=form)
@app.route('/home', methods=['GET', 'POST'])
def home():
if 'username' in session:
#connect.cursor.execute("SELECT summoner_id, game_id FROM LEAGUE.PLAYER WHERE summoner_name = '{0}'".format(session['username']))
#passing = connect.cursor.fetchall()
lastgame = requests.get(app.service + 'lastGame/' + str(id)).json()['last']
pleb = requests.get(app.service + 'pleb/'+str(match_id)).json()['pleb']
topkills = requests.get(app.service + 'topkills/' + str(match_id)).json()['top']
champdata = requests.get(app.service + 'champdata').json()['champ']
freechamps = requests.get(app.service + 'freeChamps').json()['data']
fastgame = requests.get(app.service + 'fastmatch/' + str(id)).json()['fast']
wins = requests.get(app.service + 'wins/' + str(id)).json()['wins']
count = requests.get(app.service + 'count/' + str(id)).json()['count']
secondary = requests.get(app.service + 'secondarystats/' + str(match_id)).json()['secondary']
kdr = requests.get(app.service + 'kdr/' + str(match_id)).json()['kdr']
return render_template("home.html", summoner=session['username'], data=freechamps, last=lastgame, top=topkills, fast=fastgame, count=count, pleb=pleb, champ=champdata, wins=wins, secondary=secondary, kdr=kdr)
return redirect('log-in')
@app.route('/log-in', methods=['GET', 'POST'])
def log_in():
class LogInForm(Form):
email = StringField()
password = PasswordField()
def __str__(self):
return form.email.data
form = LogInForm()
if form.validate_on_submit():
value = form.email.data.encode('ascii', 'ignore')
value = value.lower()
connect.cursor.execute("SELECT * FROM LEAGUE.USER WHERE username = '{0}'" .format(value))
records = connect.cursor.fetchall()
password = form.password.data.encode('ascii', 'ignore')
#champupdate()
if records:
print "loginpart1"
if records[0][2] == password:
print "loginpart2"
sumname = records[0][1]
summoner = w.get_summoner(sumname)
global id
global match_id
id = summoner['id']
newid = id
try:
match_history = w.get_recent_games(id)
match_history = match_history.get('games')
checkteam = match_history[0].get('stats').get('team')
checkchamp = match_history[0].get('championId')
for x in range(0, len(match_history)):
match_id = match_history[x].get('gameId')
connect.cursor.execute("UPDATE LEAGUE.MATCHLIST SET sumid = '{0}' WHERE id = '{1}'".format(id, match_id))
if connect.cursor.rowcount == 0:
connect.cursor.execute("INSERT INTO LEAGUE.MATCHLIST VALUES ('{0}', '{1}')".format(id, match_id))
match_found = True
match_id = match_history[0].get('gameId')
except:
print 'No match history found'
match_id = 0
try:
team = w.get_teams_for_summoner(id)
win5v5 = 0
win3v3 = 0
for x in range(0, len(team)):
teamid = team[x].get('fullId')
connect.cursor.execute("UPDATE LEAGUE.TEAMLIST SET sumid = '{0}' WHERE id = '{1}'".format(id, teamid))
if connect.cursor.rowcount == 0:
connect.cursor.execute("INSERT INTO LEAGUE.TEAMLIST VALUES('{0}', '{1}')".format(id, teamid))
teamid = team[x].get('fullId')
teamstat = team[x].get('teamStatDetails')
team_record = w.get_team(teamid)
tname = team_record.get('name')
tname = tname.encode('ascii', 'ignore')
team_record = team_record.get('teamStatDetails')
for x in range(0, len(team_record)):
if team_record[x].get('teamStatType') == 'RANKED_TEAM_3x3':
twin3v3 = team_record[x].get('wins')
win3v3 += twin3v3
tloss3v3 = team_record[x].get('losses')
elif team_record[x].get('teamStatType') == 'RANKED_TEAM_5x5':
twin5v5 = team_record[x].get('wins')
win5v5 += twin5v5
tloss5v5 = team_record[x].get('losses')
connect.cursor.execute("UPDATE LEAGUE.TEAM SET team_name = '{0}', wins3v3 = '{1}', losses3v3 = '{2}', wins5v5 = '{3}', losses5v5 = '{4}' WHERE team_id = '{5}'".format(tname, twin3v3, tloss3v3, twin5v5, tloss5v5, teamid))
if connect.cursor.rowcount == 0:
connect.cursor.execute("INSERT INTO LEAGUE.TEAM VALUES ('{0}','{1}','{2}','{3}','{4}','{5}')".format(teamid, tname, twin3v3, tloss3v3, twin5v5, tloss5v5))
except:
print 'No team data found'
teamid = 0
win5v5 = 0
win3v3 = 0
try:
stat = w.get_stat_summary(id)
unranked = stat.get('playerStatSummaries')
for x in range(0, len(unranked)-1):
k = unranked[x].get('playerStatSummaryType')
if k == 'Unranked':
unranked = unranked[x].get('wins')
break
except:
print 'No stats found'
unranked = 0
try:
match = w.get_match(match_id)
match_found = True
except:
match_found = False
level = summoner.get('summonerLevel')
mId = match_id
#print match
msumid = []
msumname = []
cid = []
tid = []
win = []
clevel = []
kills = []
deaths = []
assists = []
cs = []
partid = []
goldearned = []
damagedealt = []
if match_found == True:
for x in xrange(len(match_history)):
mId = match_history[x].get('gameId')
mDuration = match_history[x].get('stats').get('timePlayed')
connect.cursor.execute("UPDATE LEAGUE.MATCH SET duration = '{0}' WHERE match_id = '{1}' AND summoner_id = '{2}'".format(mDuration, mId, id))
if connect.cursor.rowcount == 0:
connect.cursor.execute("INSERT INTO LEAGUE.MATCH VALUES ('{0}','{1}',0,0,0,0,True, '{2}')".format(id, mId, mDuration))
mId = match_id
player = match.get('participants')
playerid = match.get('participantIdentities')
mType = match.get('queueType')
mDuration = match.get('matchDuration')
#mDuration = "{0}:{1}".format(mDuration / 60, mDuration % 60)
length = len(player)
for x in xrange(length):
playerids = playerid[x].get('player')
if playerids:
msumid.append(playerids.get('summonerId'))
msumname.append(playerids.get('summonerName'))
msumname[x] = msumname[x].encode('ascii', 'ignore')
else:
msumid.append(x+1)
msumname.append(x+1)
cid.append(player[x].get('championId'))
tid.append(player[x].get('teamId'))
if cid[x] == checkchamp and tid[x] == checkteam:
msumid[x] = id
msumname[x] = sumname
partid.append(player[x].get('participantId'))
pstats = player[x].get('stats')
win.append(pstats.get('winner'))
clevel.append(pstats.get('champLevel'))
kills.append(pstats.get('kills'))
deaths.append(pstats.get('deaths'))
assists.append(pstats.get('assists'))
cs.append(pstats.get('minionsKilled'))
goldearned.append(pstats.get('goldEarned'))
damagedealt.append(pstats.get('totalDamageDealtToChampions'))
connect.cursor.execute("UPDATE LEAGUE.MATCH SET champion_id = '{0}', participant_id = '{1}', team_id = '{2}', game_type = '{3}', winner = '{4}', duration = '{5}' WHERE match_id = '{6}' AND summoner_id = '{7}'".format(cid[x], partid[x], tid[x], mType, win[x], mDuration, mId, msumid[x]))
if connect.cursor.rowcount == 0:
connect.cursor.execute("INSERT INTO LEAGUE.MATCH VALUES ('{0}','{1}','{2}','{3}','{4}','{5}','{6}', '{7}')".format(msumid[x], mId, cid[x], partid[x], tid[x], mType, win[x], mDuration))
connect.cursor.execute("UPDATE LEAGUE.MATCH_STATS SET champion_id = '{0}', champlevel = '{1}', kills = '{2}', deaths = '{3}', assists = '{4}', creep_kills = '{5}', gold_earned = '{6}', damage_dealt_to_champs = '{7}' WHERE match_id = '{8}' AND participant_id = '{9}'".format(cid[x], clevel[x], kills[x], deaths[x], assists[x], cs[x], goldearned[x], damagedealt[x], mId, partid[x]))
if connect.cursor.rowcount == 0:
connect.cursor.execute("INSERT INTO LEAGUE.MATCH_STATS VALUES ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}')".format(mId, partid[x], cid[x], clevel[x], kills[x], deaths[x], assists[x], cs[x], goldearned[x], damagedealt[x]))
connect.cursor.execute("UPDATE LEAGUE.PLAYER SET summoner_id = '{0}', player_level = '{1}', game_id = '{2}', team_id = '{3}', unranked_win = '{4}', ranked_win3v3 = '{5}', ranked_win5v5 = '{6}' WHERE summoner_name = '{7}'".format(id, level, match_id, teamid, unranked, win3v3, win5v5, sumname))
if teamid != 0:
connect.cursor.execute("UPDATE LEAGUE.TEAM SET team_name = '{0}', wins3v3 = '{1}', losses3v3 = '{2}', wins5v5 = '{3}', losses5v5 = '{4}' WHERE team_id = '{5}'".format(tname, twin3v3, tloss3v3, twin5v5, tloss5v5, teamid))
if connect.cursor.rowcount == 0:
connect.cursor.execute("INSERT INTO LEAGUE.TEAM VALUES ('{0}','{1}','{2}','{3}','{4}','{5}')".format(teamid, tname, twin3v3, tloss3v3, twin5v5, tloss5v5))
connect.cursor.execute("UPDATE LEAGUE.PLAYER SET summoner_id = '{0}', player_level = '{1}', game_id = '{2}', team_id = '{3}', unranked_win = '{4}', ranked_win3v3 = '{5}', ranked_win5v5 = '{6}' WHERE summoner_name = '{7}'".format(id, level, match_id, teamid, unranked, win3v3, win5v5, sumname))
if connect.cursor.rowcount == 0:
connect.cursor.execute("INSERT INTO LEAGUE.PLAYER VALUES ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}')".format(id, sumname, level, match_id, teamid, unranked, win3v3, win5v5))
connect.conn.commit()
session['username'] = sumname
return redirect('home')
else:
return render_template("login.html", form=form)
return render_template("login.html", form=form)
@app.route('/about')
def about():
return render_template("about.html")
@app.route('/contact')
def contact():
return render_template("contact.html")
class connect():
conn_string = "host='localhost' dbname='LEAGUE_CIRCUIT' user='postgres' password='testdb'"
conn = psycopg2.connect(conn_string)
cursor = conn.cursor()
class champupdate():
champions = w.get_all_champions()
champions = champions.get('champions')
c1 = []
c2 = []
c3 = []
c4 = []
for x in xrange(len(champions)):
c = champions[x]
c1.append(c.get('id'))
c2.append(c.get('rankedPlayEnabled'))
c3.append(c.get('botEnabled'))
c4.append(c.get('freeToPlay'))
connect.cursor.execute("UPDATE LEAGUE.CHAMPION SET ranked_play_enabled = '{0}', bot_enabled = '{1}', free_to_play = '{2}' WHERE champ_id = '{3}'".format(c2[x], c3[x], c4[x], c1[x]))
if connect.cursor.rowcount == 0:
connect.cursor.execute("INSERT INTO LEAGUE.CHAMPION VALUES ('{0}','{1}','{2}','{3}')".format(c1[x], c2[x], c3[x], c4[x]))
connect.conn.commit()
def exit_handler():
print "Application ending"
atexit.register(exit_handler)
if __name__ == '__main__':
app.run(debug=True)
#if __name__ == "__main__":
# main()
#How to fill Table Champion
#champname = "insert champ name here"
#try:
#champid = connect.cursor.execute("SELECT id FROM LEAGUE.CHAMPNAME WHERE name = champname")
#champion = w.get_champion(champid)
#rankedPlayEnable = champion.get('rankedPlayEnabled')
#botEnabled = champion.get('botEnabled')
#freeToPlay = champion.get('freeToPlay')
#except:
#print "Invalid champion id"
#How to fill Table Match
#match = w.get_match('1647417800')
#get sumid sumid = match.get
#get matchid
#get champid
#get teamid
#get gametype
#get winner
#get length