forked from itszn/ctfd-matrix-scoreboard-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
94 lines (79 loc) · 3.5 KB
/
__init__.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
from flask import render_template, jsonify, Blueprint
from CTFd import utils, scoreboard, challenges
from CTFd.utils import override_template
from CTFd.models import db, Teams, Solves, Awards, Challenges
from sqlalchemy.sql import or_
import itertools
import os
def load(app):
dir_path = os.path.dirname(os.path.realpath(__file__))
template_path = os.path.join(dir_path, 'scoreboard-matrix.html')
override_template('scoreboard.html', open(template_path).read())
matrix = Blueprint('matrix', __name__, static_folder='static')
app.register_blueprint(matrix, url_prefix='/matrix')
def get_standings():
standings = scoreboard.get_standings()
# TODO faster lookup here
jstandings = []
for team in standings:
teamid = team[0]
solves = db.session.query(Solves.chalid.label('chalid')).filter(Solves.teamid==teamid)
freeze = utils.get_config('freeze')
if freeze:
freeze = utils.unix_time_to_utc(freeze)
if teamid != session.get('id'):
solves = solves.filter(Solves.date < freeze)
solves = solves.all()
jsolves = []
for solve in solves:
jsolves.append(solve.chalid)
jstandings.append({'teamid':team.teamid, 'score':team.score, 'name':team.name,'solves':jsolves})
db.session.close()
return jstandings
def get_challenges():
if not utils.is_admin():
if not utils.ctftime():
if utils.view_after_ctf():
pass
else:
return []
if utils.user_can_view_challenges() and (utils.ctf_started() or utils.is_admin()):
chals = db.session.query(
Challenges.id,
Challenges.name,
Challenges.category
).filter(or_(Challenges.hidden != True, Challenges.hidden == None)).all()
jchals = []
for x in chals:
jchals.append({
'id':x.id,
'name':x.name,
'category':x.category
})
# Sort into groups
categories = set(map(lambda x:x['category'], jchals))
jchals = [j for c in categories for j in jchals if j['category'] == c]
return jchals
return []
def scoreboard_view():
if utils.get_config('view_scoreboard_if_authed') and not utils.authed():
return redirect(url_for('auth.login', next=request.path))
if utils.hide_scores():
return render_template('scoreboard.html',
errors=['Scores are currently hidden'])
standings = get_standings()
return render_template('scoreboard.html', teams=standings,
score_frozen=utils.is_scoreboard_frozen(), challenges=get_challenges())
def scores():
json = {'standings': []}
if utils.get_config('view_scoreboard_if_authed') and not utils.authed():
return redirect(url_for('auth.login', next=request.path))
if utils.hide_scores():
return jsonify(json)
standings = get_standings()
for i, x in enumerate(standings):
json['standings'].append({'pos': i + 1, 'id': x['name'], 'team': x['name'],
'score': int(x['score']), 'solves':x['solves']})
return jsonify(json)
app.view_functions['scoreboard.scoreboard_view'] = scoreboard_view
app.view_functions['scoreboard.scores'] = scores