Skip to content

Commit

Permalink
feat: revamp single player score display
Browse files Browse the repository at this point in the history
  • Loading branch information
BrennanB committed Oct 24, 2024
1 parent b36ea21 commit e2c238e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 22 deletions.
22 changes: 11 additions & 11 deletions home/templates/home/user_profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ <h2>Highscores</h2>
</div>
</div>

{% if games|length > 0 %}
{% if games %}
{% for game_name, game in games.items %}
<div class="row">
<div
Expand All @@ -72,33 +72,33 @@ <h3>
<h4>{{game.overall}}</h4>
</div>

{% for score in game.scores %}
{% for entry in game.leaderboards %}
<div class="col-4">
<div
class="position-relative overflow-hidden p-3 p-md-3 shadowy m-md-3 text-center bg-dark"
>
<h3>
<a
href="/highscores/{{game.slug}}/{{score.leaderboard.name}}/"
href="/highscores/{{entry.leaderboard.game_slug}}/{{entry.leaderboard.name}}/"
style="color: #fff; text-decoration: none"
>{{score.leaderboard.name}}</a
>{{entry.leaderboard.name}}</a
>
</h3>
<h4>{{score.score}}</h4>
{% if score.score > 0 %}
{% if "youtube" in score.source or "streamable" in score.source %}
<h4>{{entry.score|default:"No Score"}}</h4>
{% if entry.score %}
{% if "youtube" in entry.source or "streamable" in entry.source %}
<iframe
class="img-fluid rounded"
src="{{score.source}}"
src="{{entry.source}}"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen
></iframe>
{% else %}
{% if "https://i.imgur.com/bUUfB8c.png" not in score.source %}
{% if "https://i.imgur.com/bUUfB8c.png" not in entry.source %}
<img
src="{{score.source}}"
src="{{entry.source}}"
class="img-fluid rounded"
alt="{{score.leaderboard.name}} score screenshot"
alt="{{entry.leaderboard.name}} score screenshot"
width="300"
/>
{% endif %}
Expand Down
42 changes: 31 additions & 11 deletions home/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
from django.contrib.auth.decorators import login_required
from django.utils.translation import gettext_lazy as _

from highscores.models import CleanCodeSubmission, Score
from highscores.models import CleanCodeSubmission, Score, Leaderboard
from django.db.models import OuterRef, Subquery, Exists


def index(response):
Expand Down Expand Up @@ -101,17 +102,32 @@ def user_profile(request, user_id: int):
except (User.DoesNotExist, OverflowError):
return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))

scores = Score.objects.filter(
player=user, approved=True).order_by('-time_set')
# Fetch all leaderboards
all_leaderboards = Leaderboard.objects.all()

# Fetch scores for the user
scores = Score.objects.filter(player=user, approved=True).order_by('-time_set')

# Create a dictionary to hold game data
games = {}
for score in scores:
if score.leaderboard.game not in games:
games[score.leaderboard.game] = {
"overall": score.score, "slug": score.leaderboard.game_slug, "scores": [score]}
else:
games[score.leaderboard.game]["scores"] += [score]
games[score.leaderboard.game]["overall"] += score.score
for leaderboard in all_leaderboards:
game_name = leaderboard.game
if game_name not in games:
games[game_name] = {
"slug": leaderboard.game_slug,
"leaderboards": [],
"overall": 0
}

# Check if the user has a score for this leaderboard
user_score = scores.filter(leaderboard=leaderboard).first()
score_value = user_score.score if user_score else 0
games[game_name]["leaderboards"].append({
"leaderboard": leaderboard,
"score": score_value,
"source": user_score.source if user_score else None
})
games[game_name]["overall"] += score_value

all_game_modes = GameMode.objects.all()
player_elos = PlayerElo.objects.filter(player=user)
Expand All @@ -120,7 +136,11 @@ def user_profile(request, user_id: int):
for game_mode in all_game_modes:
elos_by_game[game_mode] = player_elos.filter(game_mode=game_mode).first()

context = {"games": games, "user": user, "elos_by_game": elos_by_game}
context = {
"games": games,
"user": user,
"elos_by_game": elos_by_game
}
return render(request, "home/user_profile.html", context)


Expand Down

0 comments on commit e2c238e

Please sign in to comment.