Skip to content

Commit

Permalink
Try and avoid migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
BrennanB committed Sep 13, 2024
1 parent 5578c07 commit 717b6ca
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 42 deletions.
15 changes: 2 additions & 13 deletions ranked/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class Match(models.Model):
red_starting_elo = models.FloatField()
blue_starting_elo = models.FloatField()

winner = models.CharField(max_length=10, choices=[('red', 'Red'), ('blue', 'Blue'), ('draw', 'Draw')])

def get_red_players(self):
return self.red_alliance.all()

Expand All @@ -41,19 +43,6 @@ def __str__(self):
return f"{self.match_number} - {self.game_mode} - {self.time}"


class MatchPlayer(models.Model):
match = models.ForeignKey(Match, on_delete=models.CASCADE)
player = models.ForeignKey(User, on_delete=models.CASCADE)
team = models.CharField(max_length=10) # 'red' or 'blue'
score = models.IntegerField(default=0)

class Meta:
unique_together = ('match', 'player')

def __str__(self):
return f"{self.player} - {self.match} - {self.team}"


class PlayerElo(models.Model):
player = models.ForeignKey(User, on_delete=models.CASCADE)
game_mode = models.ForeignKey(GameMode, on_delete=models.CASCADE)
Expand Down
14 changes: 7 additions & 7 deletions ranked/templates/ranked/player_info.html
Original file line number Diff line number Diff line change
Expand Up @@ -168,23 +168,23 @@ <h5 class="mb-0">
}]
},
options: {
maintainAspectRatio: false,
},
maintainAspectRatio: false
}
});
const eloHistoryChart = new Chart(document.getElementById('chart-elo-history').getContext('2d'), {
type: 'line',
data: {
labels: JSON.parse("{{ match_labels }}"),
labels: JSON.parse("{{ match_labels|safe }}"),
datasets: [{
label: 'ELO',
data: JSON.parse("{{ elo_history }}"),
data: JSON.parse("{{ elo_history|safe }}"),
backgroundColor: '#ffc107',
borderColor: '#ffc107',
borderColor: '#ffc107'
}]
},
options: {
maintainAspectRatio: false,
},
maintainAspectRatio: false
}
});
</script>

Expand Down
78 changes: 56 additions & 22 deletions ranked/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
from datetime import datetime, timedelta
import math

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

# Create your views here.

Expand Down Expand Up @@ -110,37 +111,70 @@ def player_info(request, name, player_id):
elo_history = [eh.elo for eh in elo_history]

# Get all matches for the player
matches = Match.objects.filter(matchplayer__player=player.player, game_mode=player.game_mode)
matches = Match.objects.filter(
Q(red_alliance=player.player) | Q(blue_alliance=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_with = User.objects.filter(
Q(red_alliance__in=matches.filter(red_alliance=player.player)) |
Q(blue_alliance__in=matches.filter(blue_alliance=player.player))
).exclude(id=player.player.id).distinct()

players_with_stats = []
for played_with in players_with:
matches_together = matches.filter(
Q(red_alliance=player.player, red_alliance=played_with) |
Q(blue_alliance=player.player, blue_alliance=played_with)
)
total_matches = matches_together.count()
wins = matches_together.filter(
Q(red_alliance=player.player, red_alliance=played_with, winner='red') |
Q(blue_alliance=player.player, blue_alliance=played_with, winner='blue')
).count()
win_rate = (wins / total_matches * 100) if total_matches > 0 else 0
players_with_stats.append({
'player__username': played_with.username,
'total_matches': total_matches,
'win_rate': win_rate
})

players_with_stats = sorted(players_with_stats, 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']))
players_against = User.objects.filter(
Q(red_alliance__in=matches.filter(blue_alliance=player.player)) |
Q(blue_alliance__in=matches.filter(red_alliance=player.player))
).distinct()

players_against_stats = []
for played_against in players_against:
matches_against = matches.filter(
Q(red_alliance=player.player, blue_alliance=played_against) |
Q(blue_alliance=player.player, red_alliance=played_against)
)
total_matches = matches_against.count()
wins = matches_against.filter(
Q(red_alliance=player.player, winner='red') |
Q(blue_alliance=player.player, winner='blue')
).count()
win_rate = (wins / total_matches * 100) if total_matches > 0 else 0
players_against_stats.append({
'player__username': played_against.username,
'total_matches': total_matches,
'win_rate': win_rate
})

players_against_stats = sorted(players_against_stats, 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,
'players_with': players_with_stats,
'players_against': players_against_stats,
}
return render(request, 'ranked/player_info.html', context)

Expand Down

0 comments on commit 717b6ca

Please sign in to comment.