Skip to content

Commit

Permalink
Try to display more info on ranked partners
Browse files Browse the repository at this point in the history
  • Loading branch information
BrennanB committed Sep 13, 2024
1 parent bb45242 commit 41bd2c7
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 7 deletions.
11 changes: 7 additions & 4 deletions change_match_game_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def change_match_game_modes(matches):
url = f"{API_BASE_URL}/api/ranked/change-match-game-modes/"
headers = {
'Content-Type': 'application/json',
'X-API-KEY': API_KEY
'X-API-KEY': API_KEY+"5"
}
payload = {"matches": matches}

Expand All @@ -43,14 +43,15 @@ def change_match_game_modes(matches):
return None

if __name__ == "__main__":
old_mode = prompt("Enter the current game mode: ", validator=GameModeValidator())
new_mode = prompt("Enter the new game mode: ", validator=GameModeValidator())
match_range = prompt("Enter the range of matches to change (e.g., 1-5): ", validator=MatchRangeValidator())

start_match, end_match = map(int, match_range.split('-'))
matches = [{"new_game_mode": new_mode, "match_number": i} for i in range(start_match, end_match + 1)]
matches = [{"new_game_mode": new_mode, "match_number": i, "old_game_mode": old_mode} for i in range(start_match, end_match + 1)]

print("\nReady to change game modes for matches.")
print(f"Changing matches {start_match} to {end_match} to {new_mode}")
print(f"Changing matches {start_match} to {end_match} from {old_mode} to {new_mode}")
confirmation = prompt("Do you want to proceed with these changes? (y/n): ").lower()

if confirmation == 'y':
Expand All @@ -59,8 +60,10 @@ def change_match_game_modes(matches):
for result in results:
if 'error' in result:
print(f"Failed to update match: {result['error']}")
elif 'status' in result and result['status'] == 'success':
print(f"Successfully updated match {result['match']['match_number']} from {result['old_game_mode']} to {result['new_game_mode']}")
else:
print(f"Successfully updated match {result['match_number']} to {new_mode}")
print(f"Unexpected result format: {result}")
else:
print("Failed to change match game modes. Please check the error message above.")
else:
Expand Down
70 changes: 70 additions & 0 deletions ranked/templates/ranked/player_info.html
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,76 @@ <h2 class="display-6 fw-normal">
</div>
</div>

<!-- New sections -->
<div class="row mt-4">
<div class="col-md-6">
<div class="card">
<div class="card-header" id="playersWithHeader">
<h5 class="mb-0">
<button class="btn btn-link" data-toggle="collapse" data-target="#playersWithCollapse" aria-expanded="true" aria-controls="playersWithCollapse">
Players Played With
</button>
</h5>
</div>
<div id="playersWithCollapse" class="collapse show" aria-labelledby="playersWithHeader">
<div class="card-body">
<table class="table table-striped">
<thead>
<tr>
<th>Player</th>
<th>Win Rate</th>
<th>Total Matches</th>
</tr>
</thead>
<tbody>
{% for player_with in players_with %}
<tr>
<td>{{ player_with.player__username }}</td>
<td>{{ player_with.win_rate|floatformat:2 }}%</td>
<td>{{ player_with.total_matches }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
</div>
<div class="col-md-6">
<div class="card">
<div class="card-header" id="playersAgainstHeader">
<h5 class="mb-0">
<button class="btn btn-link" data-toggle="collapse" data-target="#playersAgainstCollapse" aria-expanded="true" aria-controls="playersAgainstCollapse">
Players Played Against
</button>
</h5>
</div>
<div id="playersAgainstCollapse" class="collapse show" aria-labelledby="playersAgainstHeader">
<div class="card-body">
<table class="table table-striped">
<thead>
<tr>
<th>Player</th>
<th>Win Rate</th>
<th>Total Matches</th>
</tr>
</thead>
<tbody>
{% for player_against in players_against %}
<tr>
<td>{{ player_against.player__username }}</td>
<td>{{ player_against.win_rate|floatformat:2 }}%</td>
<td>{{ player_against.total_matches }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>

<script>
const winRateChart = new Chart(document.getElementById('chart-win-rate').getContext('2d'), {
type: 'doughnut',
Expand Down
37 changes: 34 additions & 3 deletions ranked/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from datetime import datetime, timedelta
import math

from .models import EloHistory, GameMode, PlayerElo
from .models import EloHistory, GameMode, PlayerElo, Match, MatchPlayer
from .templatetags.rank_filter import mmr_to_rank
from django.db.models.functions import Exp

Expand Down Expand Up @@ -109,8 +109,39 @@ def player_info(request, name, player_id):
match_labels = [eh.match_number for eh in elo_history]
elo_history = [eh.elo for eh in elo_history]

context = {'player': player, 'mmr': mmr,
'elo_history': elo_history, 'match_labels': match_labels}
# Get all matches for the player
matches = Match.objects.filter(matchplayer__player=player.player, game_mode=player.game_mode)

# Players Played With
players_with = MatchPlayer.objects.filter(match__in=matches, team=F('match__matchplayer__team'))
players_with = players_with.exclude(player=player.player)
players_with = players_with.values('player__id', 'player__username')
players_with = players_with.annotate(
total_matches=Count('id'),
wins=Count('match', filter=Q(match__winner=F('team'))),
win_rate=ExpressionWrapper(F('wins') * 100.0 / F('total_matches'), output_field=FloatField())
)
players_with = sorted(players_with, key=lambda x: (-x['win_rate'], -x['total_matches']))

# Players Played Against
players_against = MatchPlayer.objects.filter(match__in=matches).exclude(team=F('match__matchplayer__team'))
players_against = players_against.exclude(player=player.player)
players_against = players_against.values('player__id', 'player__username')
players_against = players_against.annotate(
total_matches=Count('id'),
wins=Count('match', filter=Q(match__winner=F('team'))),
win_rate=ExpressionWrapper(F('wins') * 100.0 / F('total_matches'), output_field=FloatField())
)
players_against = sorted(players_against, key=lambda x: (-x['win_rate'], -x['total_matches']))

context = {
'player': player,
'mmr': mmr,
'elo_history': elo_history,
'match_labels': match_labels,
'players_with': players_with,
'players_against': players_against,
}
return render(request, 'ranked/player_info.html', context)

def mmr_calc(elo, matches_played, delta_hours):
Expand Down

0 comments on commit 41bd2c7

Please sign in to comment.