Skip to content

Commit

Permalink
clean up utils.scores a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
zardus committed Jan 26, 2024
1 parent 7de55ff commit c343a3a
Showing 1 changed file with 18 additions and 14 deletions.
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_id")
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

0 comments on commit c343a3a

Please sign in to comment.