-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrate.py
277 lines (231 loc) · 8.87 KB
/
rate.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
import msgpack
import math
from user import UserConst
from pro import ProConst
from req import RequestHandler
from req import reqenv
from req import Service
LEVEL_GAP = list()
for i in range(0,8):
LEVEL_GAP.append(1 * 0.38 * i / 8)
for i in range(0,10):
LEVEL_GAP.append(1 * 0.38 + (1 * 0.62 * i / 10))
LEVEL_NAME = [
'無',
'七級',
'六級',
'五級',
'四級',
'三級',
'二級',
'一級',
'初段',
'二段',
'三段',
'四段',
'五段',
'七段',
'八段',
'九段',
'十段',
'★皆伝★'
]
class RateService:
def __init__(self,db,rs):
self.db = db
self.rs = rs
def list_rate(self,acct = None,clas = None):
if acct != None and acct['acct_type'] == UserConst.ACCTTYPE_KERNEL:
kernel = True
else:
kernel = False
key = 'rate@kernel_' + str(kernel)
data = self.rs.hgetall(key)
if len(data) > 0:
acctlist = list()
for acct in data.values():
acctlist.append(msgpack.unpackb(acct,encoding = 'utf-8'))
acctlist.sort(key = lambda acct : acct['rate'],reverse = True)
return (None,acctlist)
if kernel:
min_type = UserConst.ACCTTYPE_KERNEL
max_status = ProConst.STATUS_HIDDEN
else:
min_type = UserConst.ACCTTYPE_USER
max_status = ProConst.STATUS_ONLINE
if clas != None:
qclas = [clas]
else:
qclas = [1,2]
cur = yield self.db.cursor()
yield cur.execute(('select "sum"."acct_id",sum("sum"."rate") from ('
' select "challenge"."acct_id","challenge"."pro_id",'
' max("challenge_state"."rate" * '
' case when "challenge"."timestamp" < "problem"."expire" '
' then 1 else '
' (1 - (greatest(date_part(\'days\',justify_interval('
' age("challenge"."timestamp","problem"."expire") '
' + \'1 days\')),-1)) * 0.15) '
' end) '
' as "rate" '
' from "challenge" '
' inner join "problem" '
' on "challenge"."pro_id" = "problem"."pro_id" '
' inner join "account" '
' on "challenge"."acct_id" = "account"."acct_id" '
' inner join "challenge_state" '
' on "challenge"."chal_id" = "challenge_state"."chal_id" '
' where "problem"."class" && %s '
' and "account"."class" && "problem"."class" '
' and "account"."acct_type" >= %s '
' and "problem"."status" <= %s '
' group by "challenge"."acct_id","challenge"."pro_id"'
') as "sum" '
'group by "sum"."acct_id" order by "sum"."acct_id" asc;'),
(qclas,min_type,max_status))
ratemap = {}
for acct_id,rate in cur:
ratemap[acct_id] = rate
'''
yield cur.execute(('SELECT "rank"."acct_id","rank"."pro_id",'
'(0.3 * power(0.66,("rank"."rank" - 1))) AS "weight" FROM ('
' SELECT "challenge"."acct_id","challenge"."pro_id",'
' row_number() OVER ('
' PARTITION BY "challenge"."pro_id" ORDER BY MIN('
' "challenge"."chal_id") ASC) AS "rank" '
' FROM "challenge" '
' INNER JOIN "problem" '
' ON "challenge"."pro_id" = "problem"."pro_id" '
' INNER JOIN "account" '
' ON "challenge"."acct_id" = "account"."acct_id" '
' INNER JOIN "challenge_state" '
' ON "challenge"."chal_id" = "challenge_state"."chal_id" '
' WHERE "account"."class" && "problem"."class" '
' AND "challenge_state"."state" = 1 '
' AND "account"."acct_type" = %s '
' AND "problem"."status" = %s '
' GROUP BY "challenge"."acct_id","challenge"."pro_id"'
') AS "rank" WHERE "rank"."rank" < 17;'),
(UserConst.ACCTTYPE_USER,ProConst.STATUS_ONLINE))
bonusmap = {}
for acct_id,pro_id,weight in cur:
ratemap[acct_id] += promap[pro_id] * float(weight)
'''
err,prolist = yield from Service.Pro.list_pro(acct = acct)
promap = {}
for pro in prolist:
promap[pro['pro_id']] = pro['rate']
err,tmplist = yield from Service.Acct.list_acct(min_type = min_type)
acctlist = list()
for acct in tmplist:
if acct['class'] not in qclas:
continue
acct_id = acct['acct_id']
if acct_id in ratemap:
acct['rate'] = math.floor(ratemap[acct_id])
else:
acct['rate'] = 0
acctlist.append(acct)
acctlist.sort(key = lambda acct : acct['rate'],reverse = True)
pipe = self.rs.pipeline()
for acct in acctlist:
pipe.hset(key,acct['acct_id'],msgpack.packb(acct))
pipe.execute()
return (None,acctlist)
def list_state(self):
cur = yield self.db.cursor()
yield cur.execute(('SELECT "challenge"."acct_id","challenge"."pro_id",'
'MIN("challenge_state"."state") AS "state" '
'FROM "challenge" '
'INNER JOIN "challenge_state" '
'ON "challenge"."chal_id" = "challenge_state"."chal_id" '
'GROUP BY "challenge"."acct_id","challenge"."pro_id";'))
statemap = {}
for acct_id,pro_id,state in cur:
if acct_id not in statemap:
statemap[acct_id] = {}
statemap[acct_id][pro_id] = state
return (None,statemap)
def map_rate(self,clas = None,
starttime = '1970-01-01 00:00:00.000',endtime = '2100-01-01 00:00:00.000'):
if clas != None:
qclas = [clas]
else:
qclas = [1,2]
cur = yield self.db.cursor()
yield cur.execute(('select "challenge"."acct_id","challenge"."pro_id",'
'max("challenge_state"."rate") as "score",'
'count("challenge_state") as "count" '
'from "challenge" '
'inner join "challenge_state" '
'on "challenge"."chal_id" = "challenge_state"."chal_id" '
'inner join "problem" '
'on "challenge"."pro_id" = "problem"."pro_id" '
'where ("problem"."class" && %s) '
'AND ("challenge"."timestamp" >= %s AND "challenge"."timestamp" <= %s) '
'group by "challenge"."acct_id","challenge"."pro_id";'),
(qclas,starttime,endtime))
statemap = {}
for acct_id,pro_id,rate,count in cur:
if acct_id not in statemap:
statemap[acct_id] = {}
statemap[acct_id][pro_id] = {
'rate':rate,
'count':count
}
return (None,statemap)
class ScbdHandler(RequestHandler):
def _get_level(self,ratio):
l = 0
r = len(LEVEL_GAP) - 1
level = 0
while l <= r:
mid = (l + r) // 2
if ratio < LEVEL_GAP[mid]:
r = mid - 1
else:
level = mid
l = mid + 1
return level
@reqenv
def get(self):
err,acctlist = yield from Service.Rate.list_rate()
err,prolist = yield from Service.Pro.list_pro()
err,statemap = yield from Service.Rate.list_state()
cur = yield self.db.cursor()
yield cur.execute(('select '
'"account"."acct_id",'
'sum("test_valid_rate"."rate") AS "rate" '
'from "test_valid_rate" '
'join "problem" '
'on "test_valid_rate"."pro_id" = "problem"."pro_id" '
'join "account" '
'on "problem"."class" && "account"."class" '
'where "account"."acct_type" = %s '
'and "problem"."status" = %s '
'group by "account"."acct_id";'),
(UserConst.ACCTTYPE_USER,ProConst.STATUS_ONLINE))
fullmap = {}
for acct_id,rate in cur:
fullmap[acct_id] = rate
for acct in acctlist:
acct_id = acct['acct_id']
if acct_id in fullmap:
fullrate = fullmap[acct_id]
acct['level'] = self._get_level(acct['rate'] / fullrate)
else:
acct['level'] = None
alglist = list()
langlist = list()
for pro in prolist:
clas = pro['class']
if clas == 2:
alglist.append(pro)
elif clas == 1:
langlist.append(pro)
self.render('scbd',
acctlist = acctlist,
alglist = alglist,
langlist = langlist,
statemap = statemap)
return