forked from durkinza/CTFd_Split_Scoreboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscores.py
264 lines (236 loc) · 8.81 KB
/
scores.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
from sqlalchemy.sql.expression import union_all
from sqlalchemy import func
from CTFd.cache import cache
from CTFd.models import db, Teams, Users, Solves, Awards, Challenges, TeamFieldEntries, Fields
from CTFd.utils.dates import unix_time_to_utc
from CTFd.utils import get_config
from CTFd.utils.modes import get_model
@cache.memoize(timeout=60)
def get_team_ids():
attr_id = get_config("split_scoreboard_attr", 0)
attr_value = get_config("split_scoreboard_value", "hidden")
team_ids = []
if attr_id == "-1": # Where team size is <value>
teams = Teams.query.outerjoin(Teams.members).group_by(Teams).having(func.count_(Teams.members) == attr_value)
for team in teams:
team_ids.append(team.id)
elif attr_id == "-2": # Where team size is less than <value>
teams = Teams.query.outerjoin(Teams.members).group_by(Teams).having(func.count_(Teams.members) <= attr_value)
for team in teams:
team_ids.append(team.id)
elif attr_id == "-3": # Where team size is greater than <value>
teams = Teams.query.outerjoin(Teams.members).group_by(Teams).having(func.count_(Teams.members) >= attr_value)
for team in teams:
team_ids.append(team.id)
else:
teams = TeamFieldEntries.query.filter_by(
field_id = attr_id
).filter(
func.lower(TeamFieldEntries.value) == func.lower(str(attr_value))
)
for team in teams:
team_ids.append(team.team_id)
return team_ids
#@cache.memoize(timeout=60)
def get_scores(admin=False):
scores = (
db.session.query(
Solves.account_id.label("account_id"),
db.func.sum(Challenges.value).label("score"),
db.func.max(Solves.id).label("id"),
db.func.max(Solves.date).label("date"),
)
.join(Challenges)
.filter(Challenges.value != 0)
.group_by(Solves.account_id)
)
awards = (
db.session.query(
Awards.account_id.label("account_id"),
db.func.sum(Awards.value).label("score"),
db.func.max(Awards.id).label("id"),
db.func.max(Awards.date).label("date"),
)
.filter(Awards.value != 0)
.group_by(Awards.account_id)
)
"""
Filter out solves and awards that are before a specific time point.
"""
freeze = get_config("freeze")
if not admin and freeze:
scores = scores.filter(Solves.date < unix_time_to_utc(freeze))
awards = awards.filter(Awards.date < unix_time_to_utc(freeze))
"""
Combine awards and solves with a union. They should have the same amount of columns
"""
results = union_all(scores, awards).alias("results")
"""
Sum each of the results by the team id to get their score.
"""
sumscores = (
db.session.query(
results.columns.account_id,
db.func.sum(results.columns.score).label("score"),
db.func.max(results.columns.id).label("id"),
db.func.max(results.columns.date).label("date"),
)
.group_by(results.columns.account_id)
.subquery()
)
return sumscores
@cache.memoize(timeout=60)
def get_unmatched_standings(count=None, admin=False, fields=[]):
"""
Get standings as a list of tuples containing account_id, name, and score e.g. [(account_id, team_name, score)].
Ties are broken by who reached a given score first based on the solve ID. Two users can have the same score but one
user will have a solve ID that is before the others. That user will be considered the tie-winner.
Challenges & Awards with a value of zero are filtered out of the calculations to avoid incorrect tie breaks.
"""
if fields is None:
fields = []
Model = get_model()
team_ids = get_team_ids()
sumscores = get_scores(admin)
"""
Admins can see scores for all users but the public cannot see banned users.
Filters out banned users.
Properly resolves value ties by ID.
Different databases treat time precision differently so resolve by the row ID instead.
"""
if admin:
standings_query = (
db.session.query(
Model.id.label("account_id"),
Model.oauth_id.label("oauth_id"),
Model.name.label("name"),
Model.hidden,
Model.banned,
sumscores.columns.score,
*fields,
)
.join(sumscores, Model.id == sumscores.columns.account_id)
.filter(Model.id.notin_(team_ids))
.order_by(sumscores.columns.score.desc(), sumscores.columns.id)
)
else:
standings_query = (
db.session.query(
Model.id.label("account_id"),
Model.oauth_id.label("oauth_id"),
Model.name.label("name"),
sumscores.columns.score,
*fields,
)
.join(sumscores, Model.id == sumscores.columns.account_id)
.filter(Model.banned == False, Model.hidden == False)
.filter(Model.id.notin_(team_ids))
.order_by(sumscores.columns.score.desc(), sumscores.columns.id)
)
"""
Only select a certain amount of users if asked.
"""
if count is None:
standings = standings_query.all()
else:
standings = standings_query.limit(count).all()
return standings
@cache.memoize(timeout=60)
def get_custom_standings(count=None, admin=False, team_ids=[], fields=[]):
if fields is None:
fields = []
Model = get_model()
sumscores = get_scores(admin)
"""
Admins can see scores for all users but the public cannot see banned users.
Filters out banned users.
Properly resolves value ties by ID.
Different databases treat time precision differently so resolve by the row ID instead.
"""
if admin:
standings_query = (
db.session.query(
Model.id.label("account_id"),
Model.oauth_id.label("oauth_id"),
Model.name.label("name"),
Model.hidden,
Model.banned,
sumscores.columns.score,
*fields,
)
.join(sumscores, Model.id == sumscores.columns.account_id)
.filter(Model.id.in_(team_ids))
.order_by(sumscores.columns.score.desc(), sumscores.columns.id)
)
else:
standings_query = (
db.session.query(
Model.id.label("account_id"),
Model.oauth_id.label("oauth_id"),
Model.name.label("name"),
sumscores.columns.score,
*fields,
)
.join(sumscores, Model.id == sumscores.columns.account_id)
.filter(Model.banned == False, Model.hidden == False)
.filter(Model.id.in_(team_ids))
.order_by(sumscores.columns.score.desc(), sumscores.columns.id)
)
"""
Only select a certain amount of users if asked.
"""
if count is None:
standings = standings_query.all()
else:
standings = standings_query.limit(count).all()
return standings
@cache.memoize(timeout=60)
def get_matched_standings(count=None, admin=False, fields=None):
if fields is None:
fields = []
Model = get_model()
team_ids = get_team_ids()
sumscores = get_scores(admin)
"""
Admins can see scores for all users but the public cannot see banned users.
Filters out banned users.
Properly resolves value ties by ID.
Different databases treat time precision differently so resolve by the row ID instead.
"""
if admin:
standings_query = (
db.session.query(
Model.id.label("account_id"),
Model.oauth_id.label("oauth_id"),
Model.name.label("name"),
Model.hidden,
Model.banned,
sumscores.columns.score,
*fields,
)
.join(sumscores, Model.id == sumscores.columns.account_id)
.filter(Model.id.in_(team_ids))
.order_by(sumscores.columns.score.desc(), sumscores.columns.id)
)
else:
standings_query = (
db.session.query(
Model.id.label("account_id"),
Model.oauth_id.label("oauth_id"),
Model.name.label("name"),
sumscores.columns.score,
*fields,
)
.join(sumscores, Model.id == sumscores.columns.account_id)
.filter(Model.banned == False, Model.hidden == False)
.filter(Model.id.in_(team_ids))
.order_by(sumscores.columns.score.desc(), sumscores.columns.id)
)
"""
Only select a certain amount of users if asked.
"""
if count is None:
standings = standings_query.all()
else:
standings = standings_query.limit(count).all()
return standings