Skip to content

Commit

Permalink
Try global leaderboard stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
BrennanB committed Sep 23, 2024
1 parent cbcba17 commit b739537
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 3 deletions.
26 changes: 26 additions & 0 deletions ranked/templates/ranked/global_leaderboard.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{% extends 'base.html' %}

{% block content %}
<h1>Global Elo Leaderboard</h1>
<table class="table table-dark table-striped table-hover table-sm" style="text-align: center">
<thead>
<tr>
<th>Rank</th>
<th>Player</th>
<th>Elo</th>
<th>Rank Title</th>
</tr>
</thead>
<tbody>
{% for entry in players_with_rank %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ entry.player.username }}</td>
<td>{{ entry.global_elo }}</td>
<!-- Updated line to use CSS class instead of inline style -->
<td class="{{ entry.color }}">{{ entry.rank }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
5 changes: 3 additions & 2 deletions ranked/templates/ranked/leaderboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ <h1 style="color: white" class="display-4 fw-normal">
<a href="/ranked/{{ leaderboard_code }}/{{ player_data.player.player.id }}">{{ player_data.player.player }}</a>
</td>
<td>{{ player_data.player.elo|floatformat:1 }}</td>
<td>{{ player_data.player.mmr|floatformat:1 }}</td>
<td style="color: {{ player_data.color }}">{{ player_data.rank }}</td>
<td>{{ player_data.mmr|floatformat:1 }}</td>
<!-- Updated line to use CSS class instead of inline style -->
<td class="{{ player_data.color }}">{{ player_data.rank }}</td>
<td>{{ player_data.player.matches_played }}</td>
<td>{{ player_data.player.win_rate|floatformat:2 }}%</td>
<td>{{ player_data.player.matches_won }}</td>
Expand Down
1 change: 1 addition & 0 deletions ranked/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
path('', views.ranked_home, name='home'),
path('<str:name>/', views.leaderboard, name='leaderboard'),
path('<str:name>/<str:player_id>', views.player_info, name='player_info'),
path('global_leaderboard/', views.global_leaderboard, name='global_leaderboard'),
]
66 changes: 65 additions & 1 deletion ranked/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.shortcuts import render, HttpResponseRedirect
from django.db.models import Max, Min, F, Q, Count, ExpressionWrapper, FloatField, Case, When, Value
from django.db.models import Max, Min, F, Q, Count, ExpressionWrapper, FloatField, Case, When, Value, Sum
from django.utils import timezone
from datetime import datetime, timedelta
import math
Expand Down Expand Up @@ -115,3 +115,67 @@ def player_info(request, name, player_id):

def mmr_calc(elo, matches_played, delta_hours):
return elo * 2 / ((1 + pow(math.e, 1/168 * pow(delta_hours, 0.63))) * (1 + pow(math.e, -0.33 * matches_played)))

def global_leaderboard(request):
# Fetch all players
players = PlayerElo.objects.all()

global_scores = []

for player in players:
# Fetch all game modes for the player
game_modes = GameMode.objects.filter(playerelo=player)

if not game_modes.exists():
continue

# Determine the most played game mode per game type
game_type_dict = {}
for mode in game_modes:
game_type = mode.game
match_count = mode.match_count # Assuming 'match_count' exists
if game_type not in game_type_dict or match_count > game_type_dict[game_type]['match_count']:
game_type_dict[game_type] = {'mode': mode, 'match_count': match_count}

# Calculate weighted Elo
total_weight = 0
weighted_elo = 0
for game_type, data in game_type_dict.items():
weight = data['match_count'] # Full weight for the most played game
weighted_elo += data['mode'].elo * weight
total_weight += weight

# Adjust weights for less played games (1/3 weight)
for game_type, data in game_type_dict.items():
if data['match_count'] < max(d['match_count'] for d in game_type_dict.values()):
weighted_elo += data['mode'].elo * (data['match_count'] / 3)
total_weight += data['match_count'] / 3

if total_weight > 0:
global_elo = weighted_elo / total_weight
global_scores.append({'player': player, 'global_elo': global_elo})

# Sort players by global Elo in descending order
global_scores = sorted(global_scores, key=lambda x: x['global_elo'], reverse=True)

# Assign ranks and colors
highest_elo = global_scores[0]['global_elo'] if global_scores else 0
lowest_elo = global_scores[-1]['global_elo'] if global_scores else 0

players_with_rank = []
for entry in global_scores:
rank, color = mmr_to_rank(entry['global_elo'], highest_elo, lowest_elo)
players_with_rank.append({
'player': entry['player'],
'rank': rank,
'color': color,
'global_elo': round(entry['global_elo'], 1),
})

context = {
'leaderboard_code': 'global',
'leaderboard_name': 'Global Elo Leaderboard',
'players_with_rank': players_with_rank,
}

return render(request, "ranked/global_leaderboard.html", context)

0 comments on commit b739537

Please sign in to comment.