Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rework belt logic so that black belts can be granted and displayed #308

Merged
merged 5 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 6 additions & 13 deletions dojo_plugin/utils/awards.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from ..models import Dojos, Belts, Emojis


BELT_ORDER = [ "orange", "yellow", "green", "purple", "blue", "brown", "red", "black" ]
BELT_REQUIREMENTS = {
"orange": "intro-to-cybersecurity",
"yellow": "program-security",
Expand Down Expand Up @@ -50,12 +51,7 @@ def get_belts():
"ranks": {},
}

for n,(color,dojo_id) in enumerate(BELT_REQUIREMENTS.items()):
dojo = Dojos.query.filter_by(id=dojo_id).first()
if not dojo:
# We are likely missing the correct dojos in the DB (e.g., custom deployment)
break

for color in reversed(BELT_ORDER):
result["dates"][color] = {}
result["ranks"][color] = []

Expand All @@ -64,20 +60,17 @@ def get_belts():
continue

result["dates"][color][belt.user.id] = str(belt.date)
if belt.user.id in result["users"]:
continue

result["ranks"][color].append(belt.user.id)
result["users"][belt.user.id] = {
"handle": belt.user.name,
"site": belt.user.website,
"color": color,
"date": str(belt.date),
"rank_id": n,
}

for user_id in result["users"]:
result["ranks"][result["users"][user_id]["color"]].append(user_id)

for rank in result["ranks"].values():
rank.sort(key=lambda uid: result["users"][uid]["date"])

return result

def update_awards(user):
Expand Down
32 changes: 18 additions & 14 deletions dojo_plugin/utils/scores.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,28 @@
from CTFd.cache import cache
from ..models import Dojos, DojoChallenges

@cache.memoize(timeout=1200)
def dojo_scores():

def scores_query(granularity, dojo_filter):
solve_count = db.func.count(Solves.id).label("solve_count")
last_solve_id = db.func.max(Solves.id).label("last_solve_id")
dsc_query = db.session.query(Dojos.id, Solves.user_id, solve_count, last_solve_id).where(
last_solve_date = db.func.max(Solves.date).label("last_solve_date")
fields = granularity + [ Solves.user_id, solve_count, last_solve_date ]
grouping = granularity + [ Solves.user_id ]

dsc_query = db.session.query(*fields).where(
Dojos.dojo_id == DojoChallenges.dojo_id, DojoChallenges.challenge_id == Solves.challenge_id,
or_(Dojos.data["type"] == "public", Dojos.official)
).group_by(Dojos.id, Solves.user_id).order_by(Dojos.id, solve_count.desc(), last_solve_id)
dojo_filter
).group_by(*grouping).order_by(Dojos.id, solve_count.desc(), last_solve_date)

return dsc_query

@cache.memoize(timeout=1200)
def dojo_scores():
dsc_query = scores_query([Dojos.id], or_(Dojos.data["type"] == "public", Dojos.official))

user_ranks = { }
user_solves = { }
dojo_ranks = { }
for dojo_id, user_id, solve_count, last_solve_id in dsc_query:
for dojo_id, user_id, solve_count, _ in dsc_query:
dojo_ranks.setdefault(dojo_id, [ ]).append(user_id)
user_ranks.setdefault(user_id, {})[dojo_id] = len(dojo_ranks[dojo_id])
user_solves.setdefault(user_id, {})[dojo_id] = solve_count
Expand All @@ -28,17 +37,12 @@ def dojo_scores():

@cache.memoize(timeout=1200)
def module_scores():
solve_count = db.func.count(Solves.id).label("solve_count")
last_solve_id = db.func.max(Solves.id).label("last_solve_id")
dsc_query = db.session.query(Dojos.id, DojoChallenges.module_index, Solves.user_id, solve_count, last_solve_id).where(
Dojos.dojo_id == DojoChallenges.dojo_id, DojoChallenges.challenge_id == Solves.challenge_id,
or_(Dojos.data["type"] == "public", Dojos.official)
).group_by(Dojos.id, DojoChallenges.module_index, Solves.user_id).order_by(Dojos.id, DojoChallenges.module_index, solve_count.desc(), last_solve_id)
dsc_query = scores_query([Dojos.id, DojoChallenges.module_index], or_(Dojos.data["type"] == "public", Dojos.official))

user_ranks = { }
user_solves = { }
module_ranks = { }
for dojo_id, module_idx, user_id, solve_count, last_solve_id in dsc_query:
for dojo_id, module_idx, user_id, solve_count, _ in dsc_query:
module_ranks.setdefault(dojo_id, {}).setdefault(module_idx, []).append(user_id)
user_ranks.setdefault(user_id, {}).setdefault(dojo_id, {})[module_idx] = len(module_ranks[dojo_id][module_idx])
user_solves.setdefault(user_id, {}).setdefault(dojo_id, {})[module_idx] = solve_count
Expand Down
13 changes: 13 additions & 0 deletions dojo_theme/static/img/dojo/brown.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions dojo_theme/static/img/dojo/purple.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions dojo_theme/static/img/dojo/red.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion dojo_theme/templates/belts.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ <h1>The Belted</h1>
Below is a list of true hackers: those who stared at the yellow box, and rather than flinching, dove in.
</p>

{% for belt_color in belt_data.ranks|reverse %}
{% for belt_color in ("blue", "green", "yellow", "orange") %}
<h2>{{belt_color.title()}} Belts</h2>
<p>
{% if belt_color == "blue" %}
Expand Down
Loading