-
Notifications
You must be signed in to change notification settings - Fork 0
/
contest.py.bak
168 lines (140 loc) · 5.26 KB
/
contest.py.bak
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
import msgpack
import math
import datetime
import time
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(self):
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)
def set(self,clas,status,start,end):
def _mp_encoder(obj):
if isinstance(obj,datetime.datetime):
return obj.astimezone(datetime.timezone.utc).timestamp()
return obj
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)
def running(self):
err,meta = self.get()
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):
err,meta = Service.Contest.get()
if err:
self.error(err)
return
delta = meta['end']-datetime.datetime.now().replace(tzinfo = datetime.timezone(datetime.timedelta(hours = 8)))
deltasecond = delta.days*24*60*60+delta.seconds
if deltasecond < 3600 and self.acct['acct_type'] != UserConst.ACCTTYPE_KERNEL:
self.render('pboard')
return
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
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
)
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]
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']
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,
end = str(meta['end']).split('+')[0].replace('-','/'))
return