|
| 1 | +from braces.views import CsrfExemptMixin, JsonRequestResponseMixin |
| 2 | +from django.contrib.auth.decorators import login_required |
| 3 | +from django.utils.decorators import method_decorator |
| 4 | +from django.views import View |
| 5 | +from django.views.generic import TemplateView, DetailView |
| 6 | + |
| 7 | +from core.models import Matchday, Contract, PlayerStatistics, AwpBoundaries, Player |
| 8 | +from core.rename_me_views.view_utils import validate_filtered_field |
| 9 | + |
| 10 | + |
| 11 | +@method_decorator(login_required, name='dispatch') |
| 12 | +class PlayerStatisticsView(TemplateView): |
| 13 | + template_name = 'core/ofm/player_statistics.html' |
| 14 | + |
| 15 | + def get_context_data(self, **kwargs): |
| 16 | + matchdays = Matchday.objects.filter(player_statistics__isnull=False).distinct() |
| 17 | + |
| 18 | + context = super(PlayerStatisticsView, self).get_context_data(**kwargs) |
| 19 | + context['matchdays'] = matchdays |
| 20 | + context['players_count'] = Contract.objects.filter(user=self.request.user, sold_on_matchday=None).count() |
| 21 | + |
| 22 | + return context |
| 23 | + |
| 24 | + |
| 25 | +@method_decorator(login_required, name='dispatch') |
| 26 | +class PlayerStatisticsAsJsonView(CsrfExemptMixin, JsonRequestResponseMixin, View): |
| 27 | + def get(self, request, *args, **kwargs): |
| 28 | + contracts = Contract.objects.filter(user=self.request.user, sold_on_matchday=None) |
| 29 | + players = [contract.player for contract in contracts] |
| 30 | + current_matchday = Matchday.objects.all()[0] |
| 31 | + |
| 32 | + newer_matchday_season = self.request.GET.get('newer_matchday_season', default=current_matchday.season.number) |
| 33 | + newer_matchday = self.request.GET.get('newer_matchday', default=current_matchday.number) |
| 34 | + older_matchday_season = self.request.GET.get('older_matchday_season') |
| 35 | + older_matchday = self.request.GET.get('older_matchday') |
| 36 | + diff_mode_enabled = older_matchday and older_matchday_season |
| 37 | + |
| 38 | + player_statistics_tuples = [] |
| 39 | + for player in players: |
| 40 | + newer_player_statistics, older_player_statistics = self._get_statistics_from_player_and_matchday( |
| 41 | + player, |
| 42 | + newer_matchday_season, newer_matchday, |
| 43 | + older_matchday_season, older_matchday |
| 44 | + ) |
| 45 | + if newer_player_statistics and (older_player_statistics or not diff_mode_enabled): |
| 46 | + player_statistics_tuples.append((newer_player_statistics, older_player_statistics)) |
| 47 | + |
| 48 | + player_statistics_json = [ |
| 49 | + self._get_player_statistics_diff_in_json(newer_player_statistics, older_player_statistics) |
| 50 | + for (newer_player_statistics, older_player_statistics) in player_statistics_tuples |
| 51 | + ] |
| 52 | + |
| 53 | + return self.render_json_response(player_statistics_json) |
| 54 | + |
| 55 | + @staticmethod |
| 56 | + def _get_statistics_from_player_and_matchday(player, |
| 57 | + newer_matchday_season, newer_matchday, |
| 58 | + older_matchday_season, older_matchday): |
| 59 | + |
| 60 | + newer_player_statistics = PlayerStatistics.objects.filter( |
| 61 | + player=player, |
| 62 | + matchday__season__number=newer_matchday_season, |
| 63 | + matchday__number=newer_matchday |
| 64 | + ) |
| 65 | + older_player_statistics = PlayerStatistics.objects.filter( |
| 66 | + player=player, |
| 67 | + matchday__season__number=older_matchday_season, |
| 68 | + matchday__number=older_matchday |
| 69 | + ) |
| 70 | + |
| 71 | + newer_player_statistics = validate_filtered_field(newer_player_statistics) |
| 72 | + older_player_statistics = validate_filtered_field(older_player_statistics) |
| 73 | + |
| 74 | + if not newer_player_statistics: |
| 75 | + newer_player_statistics = PlayerStatistics.objects.filter( |
| 76 | + player=player, |
| 77 | + matchday__season__number=newer_matchday_season |
| 78 | + ).order_by('matchday')[0] |
| 79 | + |
| 80 | + return newer_player_statistics, older_player_statistics |
| 81 | + |
| 82 | + @staticmethod |
| 83 | + def _get_player_statistics_diff_in_json(newer_player_statistics, older_player_statistics): |
| 84 | + """ |
| 85 | + Args: |
| 86 | + newer_player_statistics: newer statistic |
| 87 | + older_player_statistics: older statistic |
| 88 | +
|
| 89 | + Returns: |
| 90 | + A dictionary of player statistics data. If st2 is None st1 is returned |
| 91 | + """ |
| 92 | + |
| 93 | + strength = newer_player_statistics.strength |
| 94 | + if older_player_statistics: |
| 95 | + strength = newer_player_statistics.strength - older_player_statistics.strength |
| 96 | + ep = newer_player_statistics.ep |
| 97 | + if older_player_statistics: |
| 98 | + ep = newer_player_statistics.ep - older_player_statistics.ep |
| 99 | + tp = newer_player_statistics.tp |
| 100 | + if older_player_statistics: |
| 101 | + tp = newer_player_statistics.tp - older_player_statistics.tp |
| 102 | + awp = newer_player_statistics.awp |
| 103 | + if older_player_statistics: |
| 104 | + awp = newer_player_statistics.awp - older_player_statistics.awp |
| 105 | + freshness = newer_player_statistics.freshness |
| 106 | + if older_player_statistics: |
| 107 | + freshness = newer_player_statistics.freshness - older_player_statistics.freshness |
| 108 | + |
| 109 | + awp_boundaries = AwpBoundaries.get_from_matchday(newer_player_statistics.matchday) |
| 110 | + awp_to_next_bound = awp_boundaries[newer_player_statistics.strength + 1] - newer_player_statistics.awp |
| 111 | + |
| 112 | + statistic_diff = dict() |
| 113 | + statistic_diff['position'] = newer_player_statistics.player.position |
| 114 | + statistic_diff['age'] = newer_player_statistics.age |
| 115 | + statistic_diff['strength'] = strength |
| 116 | + statistic_diff['name'] = '<a href="%s">%s</a>' % (newer_player_statistics.player.get_absolute_url(), |
| 117 | + newer_player_statistics.player.name) |
| 118 | + statistic_diff['ep'] = ep |
| 119 | + statistic_diff['tp'] = tp |
| 120 | + statistic_diff['awp'] = awp |
| 121 | + statistic_diff['freshness'] = freshness |
| 122 | + statistic_diff['games_in_season'] = newer_player_statistics.games_in_season |
| 123 | + statistic_diff['goals_in_season'] = newer_player_statistics.goals_in_season |
| 124 | + statistic_diff['won_tacklings_in_season'] = newer_player_statistics.won_tacklings_in_season |
| 125 | + statistic_diff['lost_tacklings_in_season'] = newer_player_statistics.lost_tacklings_in_season |
| 126 | + statistic_diff['won_friendly_tacklings_in_season'] = newer_player_statistics.won_friendly_tacklings_in_season |
| 127 | + statistic_diff['lost_friendly_tacklings_in_season'] = newer_player_statistics.lost_friendly_tacklings_in_season |
| 128 | + statistic_diff['yellow_cards_in_season'] = newer_player_statistics.yellow_cards_in_season |
| 129 | + statistic_diff['red_cards_in_season'] = newer_player_statistics.red_cards_in_season |
| 130 | + statistic_diff['equity'] = newer_player_statistics.equity |
| 131 | + statistic_diff['awp_to_next_bound'] = awp_to_next_bound |
| 132 | + |
| 133 | + return statistic_diff |
| 134 | + |
| 135 | + |
| 136 | +@method_decorator(login_required, name='dispatch') |
| 137 | +class PlayerDetailView(DetailView): |
| 138 | + context_object_name = 'player' |
| 139 | + template_name = 'core/ofm/player_detail.html' |
| 140 | + queryset = Player.objects.all() |
| 141 | + |
| 142 | + def get_context_data(self, **kwargs): |
| 143 | + context = super(PlayerDetailView, self).get_context_data(**kwargs) |
| 144 | + |
| 145 | + player = self.get_object() |
| 146 | + current_season = Matchday.objects.all()[0].season |
| 147 | + seasons = [] |
| 148 | + player_stats = PlayerStatistics.objects.filter(player=player).order_by('matchday') |
| 149 | + for player_stat in player_stats: |
| 150 | + if player_stat.matchday.season not in seasons: |
| 151 | + seasons.append(player_stat.matchday.season) |
| 152 | + |
| 153 | + context['seasons'] = seasons |
| 154 | + if player: |
| 155 | + context['player_age'] = current_season.number - player.birth_season.number |
| 156 | + context['player_strength'] = player_stats[0].strength |
| 157 | + |
| 158 | + return context |
| 159 | + |
| 160 | + def get_object(self, **kwargs): |
| 161 | + player = super(PlayerDetailView, self).get_object() |
| 162 | + contracts = Contract.objects.filter(user=self.request.user, player=player, sold_on_matchday=None) |
| 163 | + return player if contracts.count() > 0 else None |
| 164 | + |
| 165 | + |
| 166 | +@method_decorator(login_required, name='dispatch') |
| 167 | +class PlayerChartView(CsrfExemptMixin, JsonRequestResponseMixin, View): |
| 168 | + def get(self, request, *args, **kwargs): |
| 169 | + current_season_number = Matchday.objects.all()[0].season.number |
| 170 | + season_number = self.request.GET.get('season_number', default=current_season_number) |
| 171 | + player_id = self.request.GET.get('player_id') |
| 172 | + player = Player.objects.filter(id=player_id) |
| 173 | + player_statistics = PlayerStatistics.objects.filter(player=player, matchday__season__number=season_number) |
| 174 | + awps = [player_stat.awp for player_stat in player_statistics] |
| 175 | + |
| 176 | + chart_json = { |
| 177 | + "series": [{ |
| 178 | + "name": 'AWP', |
| 179 | + "data": awps |
| 180 | + }], |
| 181 | + "categories": [player_stat.matchday.number for player_stat in player_statistics] |
| 182 | + } |
| 183 | + |
| 184 | + matchdays = [p.matchday for p in player_statistics] |
| 185 | + current_player_statistics = PlayerStatistics.objects.filter(player=player).order_by('matchday')[0] |
| 186 | + |
| 187 | + current_awp_boundaries = AwpBoundaries.get_from_matchday(current_player_statistics.matchday) |
| 188 | + |
| 189 | + for strength in current_awp_boundaries: |
| 190 | + if current_awp_boundaries[strength] >= min(awps) and strength > current_player_statistics.strength: |
| 191 | + awp_boundary_values = [AwpBoundaries.get_from_matchday(matchday)[strength] for matchday in matchdays] |
| 192 | + chart_json['series'].append({'name': 'AWP-Grenze: %s' % strength, 'data': awp_boundary_values}) |
| 193 | + if current_awp_boundaries[strength] >= max(awps): |
| 194 | + break |
| 195 | + |
| 196 | + return self.render_json_response(chart_json) |
0 commit comments