-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstats.py
More file actions
137 lines (118 loc) · 4.9 KB
/
Copy pathstats.py
File metadata and controls
137 lines (118 loc) · 4.9 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
from auth import *
from globvars import *
from log import CUserLog
class PStat(PAuth):
""" stat page Class"""
@web.authenticated
def get(self, *args, **kwargs):
"""
Handler of stat page
:param args:
:param kwargs:
:return:
"""
un = None
try:
# web.authenticated cannot determine if the cid is valid, it can only know if cid was set.
if self.hasLoggedin():
un = str(self.get_secure_cookie('u'), encoding="utf-8")
# self.write('Welcome, {0}<br>'.format(un))
self.showstats(un)
else:
self.render('auth.html', color="rgba(0,0,0,0.2)", jump=CTools.jumpJsGen('/login', REDIRECT_TIME),
msg="You have not logged in yet. <br> "
"You will be redirected to the login page in {0} seconds.".format(
str(REDIRECT_TIME)))
except Exception as e:
msg = 'Func: {0} Error on {1}, line {2}:{3}'.format(
sys._getframe().f_code.co_name, __file__, sys._getframe().f_lineno, str(e))
slog.e(msg)
rlog.e(msg)
if un is not None:
ulog = CUserLog(un)
ulog.e(msg)
self.send_error()
finally:
rlog.i('Statistics page request. ip:{0}, username:{1}'.format(
self.request.remote_ip, 'None' if un is None else un))
def showstats(self, un):
"""
obtain and organize statistical data
:param un: username
:return: result, msg
"""
db = CDb()
result = None
try:
# query user info
sql = 'select rid, un1, un2, winner from Game where status=?'
qsuc, qdata = db.e(sql, ('closed',)) # Secured sql execution
if qsuc:
gen_room_html = ''
ulist = self.getAllUser(qdata)
num_user = len(ulist)
user_stat = self.getUsersStat(ulist, qdata)
for i in range(0, num_user):
gen_room_html += '<li>Username: {0} -> win: {1}, lost: {2}, draw: {3}'.format(
ulist[i],
user_stat[ulist[i]][0],
user_stat[ulist[i]][1],
user_stat[ulist[i]][2])
self.render('game.html', color="rgba(0,0,0,0.2)", jump='',
msg=gen_room_html, usrname=un,
tt='Game Statistics (<a href="/game" style="font-size: 15px; color: #0066FF; '
'text-decoration: underline;">Retern to lobby</a>)',
show_btn='none')
else:
self.render('game.html', color="rgba(0,0,0,0.2)", jump='',
msg='', usrname=un)
raise RuntimeError('DB Error.')
except Exception as e:
msg = 'Func: {0} Error on {1}, line {2}:{3}'.format(
sys._getframe().f_code.co_name, __file__, sys._getframe().f_lineno, str(e))
slog.e(msg)
rlog.e(msg)
if un is not None:
ulog = CUserLog(un)
ulog.e(msg)
self.send_error()
finally:
rlog.i('Statistics page request. ip:{0}, username:{1}'.format(
self.request.remote_ip, 'None' if un is None else un))
def getAllUser(self, data):
"""
return a list including all user
:param data: data from database
:return: list of users
"""
ulist = []
len1 = len(data)
for i in range(0, len1):
ulist.append(data[i][1])
ulist.append(data[i][2])
return list(set(ulist))
def getUsersStat(self, ulist, data):
"""
Analyze retrieved data and count the number of win, lose, and, draw
:param ulist: list of users
:param data: db data
:return: analyze result.
"""
re_dic = {}
dlen1 = len(data)
ulen1 = len(ulist)
for j in range(0, ulen1):
win = 0
lost = 0
draw = 0
for i in range(0, dlen1):
if(data[i][3] == 'red' and data[i][1] == ulist[j]) or \
(data[i][3] == 'black' and data[i][2] == ulist[j]):
win += 1
if(data[i][3] == 'red' and data[i][2] == ulist[j]) or \
(data[i][3] == 'black' and data[i][1] == ulist[j]):
lost += 1
if data[i][3] == 'Draw' and (data[i][2] == ulist[j] or data[i][1] == ulist[j]):
draw += 1
re_dic[ulist[j]] = [str(win), str(lost), str(draw)]
return re_dic