-
Notifications
You must be signed in to change notification settings - Fork 1
/
__init__.py
338 lines (272 loc) · 11.8 KB
/
__init__.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
330
331
332
333
334
335
336
337
338
# -*- coding: utf-8 -*-
from flask import Flask,render_template,Markup,request,redirect,flash,jsonify
app = Flask(__name__)
NUM_TABS = 4
DEBUG_MODE = False
@app.route("/")
def hello():
return render_template("index.html")
@app.route("/sexy")
def new_tenman():
navbar_status = [""]*NUM_TABS
navbar_status[0] = "active"
return render_template("tenman/tenman_index.html",navbar_status=navbar_status)
@app.route("/tenman/players")
def players():
try:
from database_management import db_interaction as dbi
conn = dbi.get_database_connection()
num_players = dbi.get_number_of_players(conn)
navbar_status = [""]*NUM_TABS
navbar_status[1] = "active"
return render_template("tenman/players.html",num_players=num_players,navbar_status=navbar_status)
except Exception as e:
return handle_error(e)
@app.route("/tenman/get_season_data",methods=["POST","GET"])
def get_season_data():
try:
from database_management import db_interaction as dbi
table = request.form.get("text")
connection = dbi.get_database_connection()
data = dbi.get_top_season_players(connection,table)
return jsonify(data)
except Exception as e:
return jsonify(str(e))
@app.route("/livesearch",methods=["POST","GET"])
def livesearch():
try:
from database_management import db_interaction as dbi
searchbox = request.form.get("text")
if searchbox == "":
r = ""
return jsonify(r)
else:
connection = dbi.get_database_connection()
result = dbi.search_table(connection,"players","nick",searchbox,"pop_id","nick","hltv_rating","img_url","wins","losses")
return jsonify(result)
except Exception as e:
return jsonify(str(e))
@app.route("/livesearch_matches",methods=["POST","GET"])
def livesearch_match():
try:
from database_management import db_interaction as dbi
searchbox = request.form.get("text")
connection1 = dbi.get_database_connection()
connection2 = dbi.get_database_connection()
select = ("match_id","map_name","map_img_url","date","s1","s2")
if searchbox == "":
#r = dbi.search_table(connection1,"matches","match_id",searchbox,*select)
r = ""
return jsonify(r)
elif searchbox == "d" or searchbox == "de" or searchbox == "de_":
r = result_date = dbi.search_table(connection2,"matches","date",searchbox,*select)
return jsonify(r)
result_id = dbi.search_table(connection1,"matches","match_id",searchbox,*select)
result_map = dbi.search_table(connection2,"matches","map_name",searchbox,*select)
result_date = dbi.search_table(connection2,"matches","date",searchbox,*select)
results = result_id + result_date + result_map
return jsonify(results)
except Exception as e:
return jsonify(str(e))
@app.route("/tenman")
def tenman_index():
PLAYER_THRESHOLD = 6
try:
navbar_status = [""]*NUM_TABS
navbar_status[0] = "active"
from database_management import db_interaction as dbi
from match_extraction import popflash_scraper as ps
if dbi.get_number_of_players(dbi.get_database_connection()) == 0:
return render_template("tenman/no_players.html",num_matches=0,num_players=0,navbar_status=navbar_status)
conn = dbi.get_database_connection()
top_players = dbi.get_top_players(conn,threshold=PLAYER_THRESHOLD)
num_matches = dbi.get_number_of_matches(conn)
num_players = dbi.get_number_of_players(conn)
# Get latest match
data = dbi.get_table_data(conn,"matches")
last_match = data[len(data)-1]
from match_extraction.Match import Match
from match_extraction.Team import Team
from match_extraction.Player import Player
latest_match = Match(int(last_match[0]),team1=Team(),team2=Team(),map_img_url=last_match[2],date=last_match[3],map_name=last_match[1])
return render_template("tenman/tenman_landing.html",navbar_status=navbar_status,top_players=top_players,num_matches=num_matches,num_players=num_players,latest_match=latest_match,threshold=PLAYER_THRESHOLD)
except Exception as e:
return handle_error(e)
@app.route("/tenman/user/<pop_id>")
def user_page(pop_id):
try:
navbar_status = [""]*NUM_TABS
navbar_status[1] = "active"
from database_management import db_interaction as dbi
p = dbi.get_player_data(int(pop_id))
matches, ratings = dbi.get_player_recent_matches(p.get_pop_id())
recent_matches = []
for m in matches:
r = dbi.get_match_row(m)
recent_matches.append(r)
return render_template("/tenman/user_profile.html",player=p,navbar_status=navbar_status,recent_matches=recent_matches,ratings=ratings)
except Exception as e:
return handle_error(e)
@app.route("/tenman/matches")
def matches():
navbar_status = [""]*NUM_TABS
navbar_status[2] = "active"
try:
from database_management import db_interaction as dbi
navbar_status = ["","","active",""]
conn = dbi.get_database_connection()
num_matches = dbi.get_number_of_matches(conn)
top_maps,maps_img,map_rounds = dbi.get_most_frequent_maps(conn,8)
hours_dict, days_dict, months_dict = dbi.get_playtime_statistics(conn)
return render_template(
"/tenman/matches.html",
navbar_status=navbar_status,
num_matches=num_matches,
top_maps = top_maps,
maps_img = maps_img,
map_rounds = map_rounds,
hours_dict=hours_dict,
days_dict=days_dict,
months_dict=months_dict)
except Exception as e:
return handle_error(e)
@app.route("/tenman/match/<match_id>")
def match_page(match_id):
try:
navbar_status = [""]*NUM_TABS
navbar_status[2] = "active"
return render_template("tenman/loading_match.html",navbar_status=navbar_status,match_id=match_id)
except Exception as e:
return handle_error(e)
@app.route("/tenman/loading_match")
def loading():
try:
navbar_status = [""]*NUM_TABS
navbar_status[2] = "active"
return render_template("tenman/loading_match.html",navbar_status=navbar_status)
except Exception as e:
return handle_error(e)
@app.route("/get_match_data/<match_id>")
def get_match_data(match_id):
try:
from match_extraction import popflash_scraper as ps
from database_management import db_interaction as dbi
from match_extraction.Match import Match
from match_extraction.Team import Team
from match_extraction.Player import Player
navbar_status = [""]*NUM_TABS
navbar_status[2] = "active"
match = ps.get_match_data(match_id)
# Get team balance ratings
team1 = match.get_team_1()
team2 = match.get_team_2()
team1_total = 0
team2_total = 0
his_avg_team_1 = Team()
his_avg_team_2 = Team()
for player in team1:
p1 = dbi.get_player_data(int(player.get_pop_id()))
team1_total += p1.get_hltv_rating()
his_avg_team_1.add_player(p1)
for player in team2:
p2 = dbi.get_player_data(int(player.get_pop_id()))
team2_total += p2.get_hltv_rating()
his_avg_team_2.add_player(p2)
team1_avg_rating = float(team1_total) / 5.0
team2_avg_rating = float(team2_total) / 5.0
team1_percentage = 100*(team1_avg_rating / (team1_avg_rating + team2_avg_rating))
team2_percentage = 100*(team2_avg_rating / (team1_avg_rating + team2_avg_rating))
return render_template("tenman/match_page.html",
match=match,
navbar_status=navbar_status,
team1_avg_rating=team1_avg_rating,
team2_avg_rating=team2_avg_rating,
team1_percentage=team1_percentage,
team2_percentage=team2_percentage,
his_avg_team_1 = his_avg_team_1,
his_avg_team_2 = his_avg_team_2
)
except Exception as e:
return handle_error(e)
@app.route("/tenman/seasons")
def seasons_prepare():
try:
navbar_status = [""]*NUM_TABS
navbar_status[3] = "active"
from database_management import db_interaction as dbi
from datetime import date
connection = dbi.get_database_connection()
today = date.today()
month = today.strftime("%B %d, %Y")[:3]
table = "players_" + month
if not dbi.table_exists(connection,table):
# Create table
query = "CREATE TABLE " + table + \
"""(pop_id int(10), points int(5), hltv_rating float(7,4), kd_ratio float(7,4), wins int(4), losses int(4), streak int(4))"""
cur = connection.cursor()
cur.execute(query)
cur.close()
season_data = dbi.get_top_season_players(connection,table)
return render_template("/tenman/seasons.html",navbar_status=navbar_status,season_data=season_data)
except Exception as e:
return handle_error(e)
@app.route("/test")
def test():
try:
return render_template("test.html")
except Exception as e:
return str(e)
@app.route("/tenman/add_match", methods=["GET","POST"])
def add_pop_match():
try:
if request.method == "GET":
return handle_error("Im sorry bro, you can't access this endpoint this way.")
elif request.method == "POST":
from database_management import db_interaction as dbi
from match_extraction import popflash_scraper as ps
pop_id = request.form["pop_id"]
conn = dbi.get_database_connection()
if dbi.exists_in_table(conn,"matches",int(pop_id)):
flash_str = "Match " + str(pop_id) + " has already been added to the database! Player data has not been affected."
flash(flash_str)
return redirect("/tenman")
if dbi.lock_database_flag():
url = "/tenman/add_match/" + str(pop_id)
return redirect(url)
else:
flash_str = "A match is already beeing added to the database by someone else. To avoid match duplicates, try again in a minute."
flash(flash_str)
return redirect("/tenman")
except Exception as e:
return str(e)
@app.route("/tenman/add_match/<match_id>")
def add_match_after_flag(match_id):
from database_management import db_interaction as dbi
from match_extraction import popflash_scraper as ps
conn = dbi.get_database_connection()
# To protect against users entering url not from add match page
if not dbi.exists_in_table(conn,"matches",int(match_id)):
pop_match = ps.get_match_data(match_id)
dbi.add_match_data(conn,pop_match)
if not pop_match.is_tie():
dbi.update_season_player_data(conn,pop_match)
flash_str = "Successfully added match " + str(match_id) + " and updated player data."
flash(flash_str)
conn.close()
dbi.relase_database_flag()
return redirect("/tenman")
@app.route("/error")
def error():
try:
navbar_status = [""]*NUM_TABS
return render_template("tenman/error.html",navbar_status=navbar_status)
except Exception as e:
return str(e)
def handle_error(error):
if not DEBUG_MODE:
navbar_status = [""]*NUM_TABS
return render_template("tenman/error.html",navbar_status=navbar_status)
else:
return str(error)
if __name__ == "__main__":
app.run(debug=True)