Skip to content

Commit

Permalink
display earned badges (for visible dojos) on scoreboards
Browse files Browse the repository at this point in the history
  • Loading branch information
zardus committed Jan 20, 2024
1 parent 9852a41 commit 7c6d1a2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
19 changes: 16 additions & 3 deletions dojo_plugin/api/v1/scoreboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from sqlalchemy import event
from sqlalchemy.orm.session import Session

from ...models import Dojos, DojoChallenges, DojoUsers, DojoMembers, DojoAdmins, DojoStudents, DojoModules, DojoChallengeVisibilities
from ...models import Dojos, DojoChallenges, DojoUsers, DojoMembers, DojoAdmins, DojoStudents, DojoModules, DojoChallengeVisibilities, Emojis
from ...utils import dojo_standings, user_dojos, first_bloods, daily_solve_counts
from ...utils.dojo import dojo_route, dojo_accessible
from ...utils.awards import get_belts, belt_asset
Expand Down Expand Up @@ -89,6 +89,20 @@ def get_scoreboard_page(model, duration=None, page=1, per_page=20):
start_idx = (page - 1) * per_page
end_idx = start_idx + per_page
pagination = Pagination(None, page, per_page, len(results), results[start_idx:end_idx])
user = get_current_user()

viewable_dojos = { dojo.reference_id for dojo in Dojos.viewable(user=user) }
emojis = { }
for emoji in Emojis.query.order_by(Emojis.date).all():
if emoji.category not in viewable_dojos:
continue

emojis.setdefault(emoji.user.id, []).append({
"text": emoji.description,
"emoji": emoji.name,
"count": 1,
"url": url_for("pwncollege_dojo.listing", dojo=emoji.category)
})

def standing(item):
if not item:
Expand All @@ -97,7 +111,7 @@ def standing(item):
result["url"] = url_for("pwncollege_users.view_other", user_id=result["user_id"])
result["symbol"] = email_symbol_asset(result.pop("email"))
result["belt"] = belt_asset(belt_data["users"].get(result["user_id"], {"color":None})["color"])
result["badges"] = [] # TODO
result["badges"] = emojis.get(result["user_id"], [])
return result

result = {
Expand All @@ -106,7 +120,6 @@ def standing(item):

pages = set(page for page in pagination.iter_pages() if page)

user = get_current_user()
if user and not user.hidden:
me = None
for r in results:
Expand Down
9 changes: 5 additions & 4 deletions dojo_plugin/utils/awards.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def get_user_emojis(user):
if not emoji:
continue
if dojo.completed(user):
emojis.append(emoji)
emojis.append((emoji, dojo.name, dojo.reference_id))
return emojis

def get_belts():
Expand Down Expand Up @@ -87,9 +87,10 @@ def update_awards(user):
db.session.commit()

current_emojis = get_user_emojis(user)
for emoji in current_emojis:
emoji_award = Emojis.query.filter_by(user=user, name=emoji).first()
for emoji,dojo_name,dojo_id in current_emojis:
# note: the category filter is critical, since SQL seems to be unable to query by emoji!
emoji_award = Emojis.query.filter_by(user=user, name=emoji, category=dojo_id).first()
if emoji_award:
continue
db.session.add(Emojis(user=user, name=emoji))
db.session.add(Emojis(user=user, name=emoji, description=f"Awarded for completing the {dojo_name} dojo.", category=dojo_id))
db.session.commit()

0 comments on commit 7c6d1a2

Please sign in to comment.