diff --git a/ranked/templates/ranked/leaderboard.html b/ranked/templates/ranked/leaderboard.html
index 1328b60..d4abb82 100644
--- a/ranked/templates/ranked/leaderboard.html
+++ b/ranked/templates/ranked/leaderboard.html
@@ -1,4 +1,6 @@
-{% extends 'home/base.html' %} {% block content %} {% load rank_filter %}
+{% extends 'home/base.html' %}
+{% block content %}
+{% load rank_filter %}
@@ -34,7 +36,7 @@
{{ player.elo|floatformat:1 }} |
{{ player.mmr|floatformat:1 }} |
- {{player.mmr|get_rank}} |
+ {% mmr_to_rank player.mmr highest_mmr lowest_mmr %} |
{{player.matches_played}} |
{{ player.win_rate|floatformat:2 }}% |
{{player.matches_won}} |
diff --git a/ranked/templatetags/rank_filter.py b/ranked/templatetags/rank_filter.py
index fa695b9..770f42e 100644
--- a/ranked/templatetags/rank_filter.py
+++ b/ranked/templatetags/rank_filter.py
@@ -2,26 +2,32 @@
register = template.Library()
-def mmr_to_rank(mmr):
- if mmr > 1600:
+@register.simple_tag
+def mmr_to_rank(mmr, highest_mmr, lowest_mmr):
+ if highest_mmr == lowest_mmr:
+ return 'Stone' # All players have the same MMR, edge case
+
+ # Calculate the percentile
+ percentile = (highest_mmr - mmr) / (highest_mmr - lowest_mmr)
+
+ # Determine rank based on percentile
+ if percentile <= 0.1:
return 'Challenger'
- elif mmr > 1500:
+ elif percentile <= 0.2:
return 'Grandmaster'
- elif mmr > 1400:
+ elif percentile <= 0.3:
return 'Master'
- elif mmr > 1300:
+ elif percentile <= 0.4:
return 'Diamond'
- elif mmr > 1200:
+ elif percentile <= 0.5:
return 'Platinum'
- elif mmr > 1100:
+ elif percentile <= 0.6:
return 'Gold'
- elif mmr > 1000:
+ elif percentile <= 0.7:
return 'Silver'
- elif mmr > 900:
+ elif percentile <= 0.8:
return 'Bronze'
- elif mmr > 800:
+ elif percentile <= 0.9:
return 'Iron'
else:
return 'Stone'
-
-register.filter('get_rank', mmr_to_rank)
\ No newline at end of file
diff --git a/ranked/views.py b/ranked/views.py
index 3ff09f5..f860b68 100644
--- a/ranked/views.py
+++ b/ranked/views.py
@@ -1,6 +1,6 @@
from django.shortcuts import render
from django.http.response import HttpResponseRedirect
-from django.db.models import Max, F
+from django.db.models import Max, Min, F
from django.db.models import Q, Count
from django.db.models import IntegerField, ExpressionWrapper, FloatField
from django.db.models.functions import ExtractHour
@@ -44,14 +44,22 @@ def leaderboard(request, name):
players = players.annotate(time_delta=ExpressionWrapper(datetime.now(timezone.utc) - F('last_match_played_time'), output_field=FloatField()) / 3600000000)
- # players = players.annotate(mmr=F('elo') * 2 / ((1 + pow(math.e, 1/168 * pow(F('time_delta'), 0.63))) *
- # (1 + pow(math.e, -0.33 * F('matches_played')))))
+ # Calculate MMR
players = players.annotate(mmr=mmr_calc(F('elo'), F('matches_played'), F('time_delta')))
+ # Get highest and lowest MMR values
+ highest_mmr = players.aggregate(Max('mmr'))['mmr__max']
+ lowest_mmr = players.aggregate(Min('mmr'))['mmr__min']
+
players = players.order_by('-mmr')
- context = {'leaderboard_code': gamemode.short_code,
- 'leaderboard_name': gamemode.name, 'players': players}
+ context = {
+ 'leaderboard_code': gamemode.short_code,
+ 'leaderboard_name': gamemode.name,
+ 'players': players,
+ 'highest_mmr': highest_mmr,
+ 'lowest_mmr': lowest_mmr
+ }
return render(request, "ranked/leaderboard.html", context)