-
Notifications
You must be signed in to change notification settings - Fork 0
/
contest.py
302 lines (275 loc) · 11.7 KB
/
contest.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
import os
import msgpack
import math
import datetime
import time
import tornado.web
from operator import attrgetter
from user import UserConst
from pro import ProConst
from req import RequestHandler
from req import reqenv
from req import Service
class ContestConst:
STATUS_ONLINE = 0
STATUS_HIDDEN = 1
STATUS_OFFLINE = 2
class ContestService:
def __init__(self,db,rs):
self.db = db
self.rs = rs
def get_list(self):
return msgpack.unpackb(self.rs.get('contest_list'),encoding = 'utf-8')
def get(self,cont_name):
if cont_name == 'default':
data = self.rs.get('contest')
if data == None:
return (None,{
'class':0,
'status':ContestConst.STATUS_OFFLINE,
'start':datetime.datetime.now().replace(
tzinfo = datetime.timezone(
datetime.timedelta(hours = 8))),
'end':datetime.datetime.now().replace(
tzinfo = datetime.timezone(
datetime.timedelta(hours = 8))),
})
meta = msgpack.unpackb(data,encoding = 'utf-8')
start = datetime.datetime.fromtimestamp(meta['start'])
meta['start'] = start.replace(tzinfo = datetime.timezone(
datetime.timedelta(hours = 8)))
end = datetime.datetime.fromtimestamp(meta['end'])
meta['end'] = end.replace(tzinfo = datetime.timezone(
datetime.timedelta(hours = 8)))
return (None,meta)
else:
cont_list = msgpack.unpackb(self.rs.get('contest_list'),encoding = 'utf-8')
if cont_name in cont_list:
meta = msgpack.unpackb(self.rs.get(cont_name+'_contest'),encoding = 'utf-8')
start = datetime.datetime.fromtimestamp(meta['start'])
meta['start'] = start.replace(tzinfo = datetime.timezone(
datetime.timedelta(hours = 8)))
end = datetime.datetime.fromtimestamp(meta['end'])
meta['end'] = end.replace(tzinfo = datetime.timezone(
datetime.timedelta(hours = 8)))
return (None,meta)
else:
return ('Eexist',None)
def remove_cont(self,cont_name):
cont_list = self.get_list()
if not cont_name in cont_list:
return ('Eexist',None)
cont_list.remove(cont_name)
self.rs.set('contest_list',msgpack.packb(cont_list))
self.rs.delete(cont_name+'_contest')
def set(self,cont_name,clas,status,start,end,pro_list = None,acct_list = None):
def _mp_encoder(obj):
if isinstance(obj,datetime.datetime):
return obj.astimezone(datetime.timezone.utc).timestamp()
return obj
if cont_name == 'default':
self.rs.set('contest',msgpack.packb({
'class':clas,
'status':status,
'start':start,
'end':end
},default = _mp_encoder))
self.rs.delete('rate@kernel_True')
self.rs.delete('rate@kernel_False')
return (None,None)
else:
pro = str(pro_list).replace(' ','').split(',')
pro_list = []
for p in pro:
if p != '':
pro_list.append(int(p))
acct = str(acct_list).replace(' ','').split(',')
acct_list = []
for a in acct:
if a != '':
if a.isnumeric():
acct_list.append(int(a))
elif a.find('_group') != -1:
gacct = yield from Service.Group.list_acct_in_group(a[:-6])
for ga in gacct:
acct_list.append(int(ga['acct_id']))
acct_list = list(set(acct_list))
cont_list = self.get_list()
if not cont_name in cont_list:
cont_list.append(cont_name)
self.rs.set('contest_list',msgpack.packb(cont_list))
self.rs.set(cont_name+'_contest',msgpack.packb({'status':status,'start':start,'end':end,'pro_list':pro_list,'acct_list':acct_list},default = _mp_encoder))
self.rs.delete('rate@kernel_True')
self.rs.delete('rate@kernel_False')
return (None,None)
def running(self):
err,meta = self.get('default')
if meta['status'] == ContestConst.STATUS_OFFLINE:
return (None,False)
now = datetime.datetime.now().replace(
tzinfo = datetime.timezone(datetime.timedelta(hours = 8)))
if meta['start'] > now or meta['end'] <= now:
return (None,False)
return (None,True)
class BoardHandler(RequestHandler):
@reqenv
def get(self):
try:
cont_name = str(self.get_argument('cont'))
except tornado.web.HTTPError:
cont_name = 'default'
err,meta = Service.Contest.get(cont_name)
if err:
self.finish(err)
return
delta = meta['end']-datetime.datetime.now().replace(tzinfo = datetime.timezone(datetime.timedelta(hours = 8)))
deltasecond = delta.days*24*60*60+delta.seconds
cont_list = Service.Contest.get_list()
boardtempl = 'board'
if self.acct['acct_type'] == UserConst.ACCTTYPE_KERNEL and cont_name != 'default':
if os.path.exists('/srv/oj/backend/templ/' + cont_name + '_board.templ'):
boardtempl = cont_name + '_board'
if meta['status'] == ContestConst.STATUS_OFFLINE:
self.error('Eacces')
return
if (meta['status'] == ContestConst.STATUS_HIDDEN and
self.acct['acct_type'] != UserConst.ACCTTYPE_KERNEL):
self.error('Eacces')
return
if cont_name == 'default':
clas = meta['class']
err,prolist = yield from Service.Pro.list_pro(acct = self.acct,
clas = clas)
err,acctlist = yield from Service.Rate.list_rate(acct = self.acct,
clas = clas)
err,ratemap = yield from Service.Rate.map_rate(clas = clas,
starttime = meta['start'],endtime = meta['end'])
for acct in acctlist:
acct_id = acct['acct_id']
acct['rate'] = 0
for pro in prolist:
pro_id = pro['pro_id']
if acct_id in ratemap and pro_id in ratemap[acct_id]:
rate = ratemap[acct_id][pro_id]
acct['rate'] += rate['rate']
def turn(acct):
acct_id=acct['acct_id']
count=0
for pro in prolist:
pro_id=pro['pro_id']
if acct_id in ratemap and pro_id in ratemap[acct_id]:
rate=ratemap[acct_id][pro_id]
if rate['rate'] > 0:
count=count-rate['count']
return (acct['rate'],count)
submit_count={None:None}
for acct in acctlist:
acct_id = acct['acct_id']
submit_count.update({acct_id:turn(acct)})
acctlist.sort(key = lambda acct:submit_count[acct['acct_id']], reverse = True)
acct_submit={None:None}
for acct in acctlist:
acct_id = acct['acct_id']
acct_submit.update({acct_id:0})
pro_sc_sub={None:None}
for pro in prolist:
pro_id = pro['pro_id']
sc_add = 0
sub_add = 0
for acct in acctlist:
acct_id = acct['acct_id']
if acct_id in ratemap and pro_id in ratemap[acct_id]:
rate = ratemap[acct_id][pro_id]
sub_add += rate['count']
sc_add += rate['rate']
if rate['rate'] > 0:
acct_submit[acct_id] += rate['count']
pro_sc_sub.update({pro_id:(sc_add,sub_add)})
rank = 0
last_rate = None
last_submit = None
for acct in acctlist:
submit = submit_count[acct['acct_id']][1]
if acct['rate'] != last_rate:
rank += 1
last_rate = acct['rate']
last_submit = submit
elif submit != last_submit and last_rate != 0:
rank +=1
last_submit = submit
acct['rank'] = rank
self.render('board',
prolist = prolist,
acctlist = acctlist,
ratemap = ratemap,
pro_sc_sub = pro_sc_sub,
acct_submit = acct_submit,
cont_name = cont_name,
cont_list = cont_list,
end = str(meta['end']).split('+')[0].replace('-','/'),
timestamp = int(time.time()))
else:
err,prolist = yield from Service.Pro.list_pro(acct = self.acct)
err,acctlist = yield from Service.Rate.list_rate(acct = self.acct)
err,ratemap = yield from Service.Rate.map_rate(starttime = meta['start'],endtime = meta['end'])
prolist2 = []
for pro in prolist:
if pro['pro_id'] in meta['pro_list']:
prolist2.append(pro)
acctlist2 = []
submit_count = {}
for acct in acctlist:
if acct['acct_id'] in meta['acct_list']:
acct['rate'] = 0
acct_id = acct['acct_id']
count = 0
for pro in prolist2:
pro_id = pro['pro_id']
if acct_id in ratemap and pro_id in ratemap[acct_id]:
rate = ratemap[acct_id][pro_id]
acct['rate'] += rate['rate']
if rate['rate'] > 0:
count -= rate['count']
acctlist2.append(acct)
submit_count.update({acct_id:(acct['rate'],count)})
acctlist2.sort(key = lambda acct:submit_count[acct['acct_id']],reverse = True)
rank = 0
last_sc = None
last_sb = None
acct_submit = {}
for acct in acctlist2:
acct_id = acct['acct_id']
sc = submit_count[acct_id][0]
sb = -submit_count[acct_id][1]
acct_submit.update({acct_id:sb})
if sc != last_sc:
last_sc = sc
last_sb = sb
rank += 1
elif sb != last_sb:
last_sb = sb
rank += 1
acct['rank'] = rank
pro_sc_sub = {}
for pro in prolist2:
pro_id = pro['pro_id']
sc = 0
sub = 0
for acct in acctlist2:
acct_id = acct['acct_id']
if acct_id in ratemap and pro_id in ratemap[acct_id]:
rate = ratemap[acct_id][pro_id]
sc += rate['rate']
sub += rate['count']
pro_sc_sub.update({pro_id:(sc,sub)})
self.render(boardtempl,
prolist = prolist2,
acctlist = acctlist2,
ratemap = ratemap,
pro_sc_sub = pro_sc_sub,
acct_submit = acct_submit,
cont_name = cont_name,
cont_list= cont_list,
end = str(meta['end']).split('+')[0].replace('-','/'),
timestamp = int(time.time()))
return