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

clean up utils.scores a bit #307

Merged
merged 1 commit into from
Jan 26, 2024
Merged
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
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
Loading