-
Notifications
You must be signed in to change notification settings - Fork 0
/
tplib.py
184 lines (165 loc) · 5.59 KB
/
tplib.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
import math
import hashlib
from datetime import datetime
import tools
ADMINS = (
1, # bh (hehe, just use one account to be safe
)
MODERATORS = (
7392, # WhiteHalmos
1, # bh
2840 # aurick
)
USER_SALT = 'bbiuyxzoswekl;jgtohgoaboih'
def is_mod(sess):
return 'logged_in' in sess and sess.user_id in MODERATORS
def is_admin(sess):
return 'logged_in' in sess and sess.user_id in ADMINS
def is_logged_in(sess):
return 'logged_in' in sess and sess.logged_in
def get_pref(pref, sess):
if pref in sess:
return sess[pref]
return None
def clean(message):
if not message:
return ''
return message.strip().lower()
def msghash(message):
return hashlib.sha256(clean(message.encode('utf-8'))).hexdigest()
def time_ago(timestamp):
diff = datetime.now() - datetime.fromtimestamp(timestamp)
second_diff = diff.seconds
day_diff = diff.days
if day_diff < 0:
return 'just now'
elif day_diff == 0:
if second_diff < 60:
return '%ds ago' % second_diff
elif second_diff < 3600:
return '%dm ago' % (second_diff / 60)
else:
return '%dh ago' % (second_diff / 3600)
elif day_diff < 30:
return '%dd ago' % day_diff
elif day_diff < 365:
return '%dmo ago' % (day_diff / 30)
if day_diff / 365 < 40:
return '%dyr ago' % (day_diff / 365)
return 'long ago'
def timedelta_ago(timestamp):
return datetime.now() - datetime.fromtimestamp(timestamp)
def rounded(num, digits=None):
if digits is None:
return int(round(num))
return round(num, digits)
def dec(rating):
result = ''
clr = 'red'
if isinstance(rating, basestring):
return rating
elif rating >= 3600:
result = '''
<i class="fa fa-stack rating-container">
<i class="fa fa-bullseye fa-stack-1x rating-front"></i>
<i class="fa fa-circle rating-middle rating-white"></i>
</i>{0}'''
elif rating >= 3000:
result = '''
<i class="fa fa-stack rating-container">
<i class="fa fa-dot-circle-o fa-stack-1x rating-front"></i>
<i class="fa fa-circle rating-middle rating-white"></i>
</i>{0}'''
else:
if rating >= 2400:
clr = 'red'
per = (rating - 2400) / 600.0
elif rating >= 1800:
clr = 'yellow'
per = (rating - 1800) / 600.0
elif rating >= 1200:
clr = 'blue'
per = (rating - 1200) / 600.0
elif rating >= 600:
clr = 'green'
per = (rating - 600) / 600.0
else:
clr = 'gray'
per = rating / 600.0
n = round((1 - per) * 11)
if n > 0:
n += 2
result = '''
<i class="fa fa-stack rating-container">
<i class="fa fa-circle-thin fa-stack-1x rating-front"></i>
<i class="fa fa-circle fa-stack-1x rating-middle rating-i rating-white"
style="height: {0}"></i>
<i class="fa fa-circle fa-stack-1x rating-back"></i>
</i>{{0}}'''.format(n)
frating = '<strong>{0}</strong>'.format(rating)
return '<span class="rating-{0}">{1}</span>'.format(
clr, result.format(frating))
def rating_string(rating, decimal=False, ranking=None):
if rating is None:
return 'unrated'
if decimal:
rounded = round(rating, 1)
else:
rounded = int(round(rating))
if ranking is not None:
return '{0} Rank: {1}'.format(rounded, ranking + 1)
return rounded
def drating(*args, **kwargs):
return dec(rating(*args, **kwargs))
def rating(robot, decimal=False):
return rating_string(robot.rating, decimal)
def rating_diff(robot):
if robot.rating is None:
rating = tools.DEFAULT_RATING
else:
rating = robot.rating
last_rating = robot.last_rating
return round(rating - last_rating, 1)
def rating_diff_class(robot):
diff = rating_diff(robot)
bounds = [16, 6, 2, -16, -6, -2]
classes = [
'fa-caret-up bold',
'fa-angle-double-up bold',
'fa-angle-up bold',
'fa-caret-down bold',
'fa-angle-double-down bold',
'fa-angle-down bold',
]
k = tools.get_k_factor(robot.last_rating)
for i in range(len(bounds)):
if diff > 0 and bounds[i] > 0 and diff > bounds[i] * k:
return classes[i] + ' green'
elif diff < 0 and bounds[i] < 0 and diff < bounds[i] * k:
return classes[i] + ' red'
return ''
def match_rating(match, rid, other=False, decimal=False):
if (match.r1_id == rid) ^ other:
return rating_string(match.r1_rating, decimal, match.r1_ranking)
return rating_string(match.r2_rating, decimal, match.r2_ranking)
def new_rating(r1, r2, result, k_factor):
expected = 1.0/(1 + pow(10, (r2 - r1)/400.0))
return r1 + k_factor * (result - expected)
def get_rating_diff(rating1, rating2, score1, score2, k_factor=32.0):
if score1 == score2:
score = 0.5
elif score1 > score2:
score = 1
else:
score = 0
return new_rating(rating1, rating2, score, k_factor) - rating1
def rating_change(match, rid, other=False):
if (match.r1_id == rid) ^ other:
diff = get_rating_diff(match.r1_rating, match.r2_rating,
match.r1_score, match.r2_score,
match.k_factor)
else:
diff = get_rating_diff(match.r2_rating, match.r1_rating,
match.r2_score, match.r1_score,
match.k_factor)
return round(diff, 1)