This repository has been archived by the owner on Oct 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
345 lines (311 loc) · 11.2 KB
/
app.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
339
340
341
342
343
344
345
"""
*********************************************
*** AsH UCS-Py-Web , A Web interface to UCS ***
*********************************************
By Ali SH,GPL v3 License.you are asked to read the license and obey it terms in order to use this project.
"""
#TODO:Connect on request,this prevents error:mysql server has gone away
#TODO:Make Error handling on all pages as they are in index page.
""" Import neccessery things for running. this is the main file.
Flask is a web framework for python,like Django"""
from flask import Flask , render_template ,abort ,url_for ,request,session,redirect
#base64 is used to encode user and clan IDs
from base64 import b64encode,b64decode
#api is used to get information
from api import *
import core
#setting up the Flask
app = Flask(__name__)
WTF_CSRF_ENABLED = True
app.secret_key = 'My own very very secret key that i know you wont guess it!'
perpage=20#results per page
#error handeling while indexing
def error1(page):
if page < 1:
return True
def error2(page,result):
if not result and page != 1:
return True
#Home Index
@app.route('/', defaults={'page':1})
@app.route('/users/page/<int:page>')
def index(page):
try:
if error1(page):
return redirect(url_for('index'))
con = core.connect_mdb()
con.commit()
cur = con.cursor()
startat=(page*perpage)-perpage
r=get_info_all(cur,perpage,startat)
result=r[0]
total=r[1]
if error2(page,result):
cur.close()
con.close()
return redirect(url_for('index'))
return render_template('main.html',
title='Home',
info=result,
base64=b64encode,
active='home',
page=page,
total=(total//perpage)+2,
perpage=perpage,
)
except Exception as e:
return render_template('error.html',
title='Home',
error=str(e),
)
cur.close()
con.close()
#Ban Users
@app.route('/bans', defaults={'page':1})
@app.route('/bans/page/<int:page>')
def bans(page):
try:
if error1(page):
return redirect(url_for('bans'))
con = core.connect_mdb()
con.commit()
cur = con.cursor()
startat=(page*perpage)-perpage
con.commit()
r=get_info_bans(cur,perpage,startat)
info=r[0]#['player_info']
#print(player_info)
total=r[1]
if error2(page,info):
cur.close()
con.close()
return redirect(url_for('bans'))
return render_template('main.html',
title='BanList',
info=info,
base64=b64encode,
page=page,
total=(total//perpage)+2,
perpage=perpage,
)
cur.close()
con.close()
except Exception as e:
return render_template('error.html',
title='Home',
error=str(e),
)
#Server Statistics
@app.route('/stats')
def stats():
con = core.connect_mdb()
con.commit()
cur = con.cursor()
try:
r=get_ucs_detailed_info(cur)
return render_template('stats.html',
title='Stats',
info=r,
)
except Exception as e :
return render_template('error.html',
title='Error',
error='Cannot Get Information:%s' % str(e),
)
cur.close()
con.close()
#Game Administrators
@app.route('/admins', defaults={'page':1})
@app.route('/admins/page/<int:page>')
def admins(page):
try:
if error1(page):
return redirect(url_for('admins'))
con = core.connect_mdb()
con.commit()
cur = con.cursor()
startat=(page*perpage)-perpage
r=get_info_admins(cur,perpage,startat)
info=r[0]#['player_info']
total=r[1]
if error2(page,info):
cur.close()
con.close()
return redirect(url_for('admins'))
return render_template('main.html',
title='Administrators',
info=info,
base64=b64encode,
page=page,
total=(total//perpage)+2,
perpage=perpage,
)
cur.close()
con.close()
except Exception as e:
return render_template('error.html',
title='Home',
error=str(e),
)
#Clans
@app.route('/clans', defaults={'page':1})#
@app.route('/clans/page/<int:page>')
def clans(page):
try:
if error1(page):
return redirect(url_for('clans'))
con = core.connect_mdb()
con.commit()
cur = con.cursor()
startat=(page*perpage)-perpage
r=get_info_clan_all(cur,perpage,startat)
info=r[0]#['player_info']
total=r[1]
if error2(page,info):
cur.close()
con.close()
return redirect(url_for('clans'))
return render_template('clans.html',
title='Clans',
info=info,
base64=b64encode,
page=page,
total=(total//perpage)+2,
perpage=perpage,
)
cur.close()
con.close()
except Exception as e:
return render_template('error.html',
title='Home',
error=str(e),
)
#Player Profile
@app.route('/user/<userid>')
def show_user_profile(userid):
try:
if error1(page):
return redirect(url_for('index'))
con = core.connect_mdb()
con.commit()
cur = con.cursor()
userid=b64decode(userid.encode('utf-8')).decode("utf-8")
if does_exist_player(userid,cur) is True:
general_info=get_player_info_general(userid,cur)
unit_level=get_player_units_level(userid,cur)
spell_level=get_spell_level(userid,cur)
hero_level=get_hero_level(userid,cur)
units=get_player_units(userid,cur)
spells=get_player_spells(userid,cur)
clan=get_clan_info(general_info['clan'],cur)
buildings=get_buildings(userid,cur)
obstacles=get_obstacles(userid,cur)
resources=get_resources(userid,cur)
decos=get_decos(userid,cur)
traps=get_traps(userid,cur)
if error2(page,result):
cur.close()
con.close()
return redirect(url_for('index'))
return render_template('player.html',
title=general_info['name'],
general_info=general_info,
unit_level=unit_level,
spell_level=spell_level,
hero_level=hero_level,
units=units,
spells=spells,
clan=clan,
buildings=buildings,
obstacles=obstacles,
resources=resources,
decos=decos,
traps=traps,
base64=b64encode,
)
else:
return render_template('error.html',
title='Error',
error="Cannot Find this player!",
)
cur.close()
con.close()
except Exception as e:
return render_template('error.html',
title='Home',
error=str(e),
)
#clan profile
@app.route('/clan/<clanid>')
def show_clan_profile(clanid):
try:
if error1(page):
return redirect(url_for('index'))
con = core.connect_mdb()
con.commit()
cur = con.cursor()
clanid=b64decode(clanid.encode('utf-8')).decode("utf-8")
if does_exist_clan(clanid,cur) is True:
clan=get_clan_info(clanid,cur)
return render_template('clan.html',
title=clan['name'],
clan=clan,
base64=b64encode,
)
else:
return render_template('error.html',
title='Error',
error="Cannot Find this Clan!",
)
cur.close()
con.close()
except Exception as e:
return render_template('error.html',
title='Home',
error=str(e),
)
#Administrator Options
@app.route('/admin')
def admin_panel():
return render_template('admin.html',title='Admin Panel')
#Signin
@app.route('/signin', methods=['GET', 'POST'])
def signin():
if session.get('username'):
return redirect(url_for('admin'))
else:
return render_template('signin.html')
#validate login
@app.route('/validateLogin',methods=['POST'])
def validateLogin():
def check_password(user,pw):
if user==config['login']['username'] and pw==config['login']['password']:#TODO:Make A Database For users and settings.
return True
return False#beware that if True is returned,Then false will Not be returned.
try:
_username = request.form['inputusername']
_password = request.form['inputPassword']
if check_password(_username,_password)==True:
session['username'] = _username
return redirect(url_for('index'))
else:
return render_template('error.html',
title='Unauthorized Access',
error = 'Wrong Username or Password.',
)
except Exception as e:
return render_template('error.html',
title='Unauthorized Access',
error = str(e),
)
#signout
@app.route('/signout')
def signout():
if 'username' not in session:
return redirect(url_for('signin'))
session.pop('username', None)
return redirect(url_for('index'))
#run the app.
if __name__ == '__main__':
app.debug = True
app.run(host='0.0.0.0', port = 5001, threaded=True)