Exemple #1
0
    def test_player_chart_does_not_show_boundary_if_promoted(self):
        PlayerStatisticsFactory.create(player=self.player, matchday=self.matchday, strength=2, awp=2800)
        matchday_9 = MatchdayFactory.create(number=9)
        PlayerStatisticsFactory.create(player=self.player, matchday=matchday_9, strength=3, awp=3500)

        awp_boundaries = AwpBoundaries.get_or_create_from_matchday(self.matchday)
        awp_boundaries[2] = 2000
        awp_boundaries[3] = 3000
        awp_boundaries[4] = 3800
        awp_boundaries9 = AwpBoundaries.get_or_create_from_matchday(matchday_9)
        awp_boundaries9[2] = 2000
        awp_boundaries9[3] = 3000
        awp_boundaries9[4] = 4000

        response = self.client.get(reverse('core:ofm:players_chart_json'), {'player_id': self.player.id})
        self.assertEqual(response.status_code, 200)
        returned_json_data = json.loads(response.content.decode('utf-8'))
        self.assertTrue('series' in returned_json_data)

        self.assertEqual('AWP', returned_json_data['series'][0]['name'])
        self.assertTrue('data' in returned_json_data['series'][0])

        self.assertEqual('AWP-Grenze: 4', returned_json_data['series'][1]['name'])
        self.assertTrue('data' in returned_json_data['series'][1])
        self.assertEqual([3800, 4000], returned_json_data['series'][1]['data'])
Exemple #2
0
    def get(self, request):
        current_season_number = Matchday.objects.all()[0].season.number
        season_number = request.GET.get('season_number', default=current_season_number)
        player_id = request.GET.get('player_id')
        player = Player.objects.filter(id=player_id)
        player_statistics = PlayerStatistics.objects.filter(player=player, matchday__season__number=season_number)
        awps = [player_stat.awp for player_stat in player_statistics]

        chart_json = {
            "series": [{
                "name": 'AWP',
                "data": awps
            }],
            "categories": [player_stat.matchday.number for player_stat in player_statistics]
        }

        matchdays = [p.matchday for p in player_statistics]
        current_player_statistics = PlayerStatistics.objects.filter(player=player).order_by('matchday')[0]

        current_awp_boundaries = AwpBoundaries.get_from_matchday(current_player_statistics.matchday)

        for strength in current_awp_boundaries:
            if current_awp_boundaries[strength] >= min(awps) and strength > current_player_statistics.strength:
                awp_boundary_values = [AwpBoundaries.get_from_matchday(matchday)[strength] for matchday in matchdays]
                chart_json['series'].append({'name': 'AWP-Grenze: %s' % strength, 'data': awp_boundary_values})
            if current_awp_boundaries[strength] >= max(awps):
                break

        return self.render_json_response(chart_json)
    def test_user_can_see_players_diff_to_next_awp_boundary_given_no_matchday(self):
        awp_boundaries = AwpBoundaries.get_or_create_from_matchday(self.matchday)
        awp_boundaries[2] = 20

        response = self.client.get(reverse('core:ofm:player_statistics_json'))

        returned_json_data = json.loads(response.content.decode('utf-8'))
        self.assertEqual(returned_json_data[0]['awp_to_next_bound'], 16)
Exemple #4
0
    def test_user_can_see_players_diff_to_next_awp_boundary_given_no_matchday(
            self):
        awp_boundaries = AwpBoundaries.get_or_create_from_matchday(
            self.matchday)
        awp_boundaries[2] = 20

        response = self.client.get(reverse('core:ofm:player_statistics_json'))

        returned_json_data = json.loads(response.content.decode('utf-8'))
        self.assertEqual(returned_json_data[0]['awp_to_next_bound'], 16)
Exemple #5
0
    def get(self, request):
        current_season_number = Matchday.objects.all()[0].season.number
        season_number = request.GET.get('season_number',
                                        default=current_season_number)
        player_id = request.GET.get('player_id')
        player = Player.objects.filter(id=player_id)
        player_statistics = PlayerStatistics.objects.filter(
            player=player, matchday__season__number=season_number)
        awps = [player_stat.awp for player_stat in player_statistics]

        chart_json = {
            "series": [{
                "name": 'AWP',
                "data": awps
            }],
            "categories":
            [player_stat.matchday.number for player_stat in player_statistics]
        }

        matchdays = [p.matchday for p in player_statistics]
        current_player_statistics = PlayerStatistics.objects.filter(
            player=player).order_by('matchday')[0]

        current_awp_boundaries = AwpBoundaries.get_from_matchday(
            current_player_statistics.matchday)

        for strength in current_awp_boundaries:
            if current_awp_boundaries[strength] >= min(
                    awps) and strength > current_player_statistics.strength:
                awp_boundary_values = [
                    AwpBoundaries.get_from_matchday(matchday)[strength]
                    for matchday in matchdays
                ]
                chart_json['series'].append({
                    'name': 'AWP-Grenze: %s' % strength,
                    'data': awp_boundary_values
                })
            if current_awp_boundaries[strength] >= max(awps):
                break

        return self.render_json_response(chart_json)
Exemple #6
0
    def parse_html(self, soup):
        """
        :param soup: BeautifulSoup of awp boundaries forum page
        :return: parsed awp boundaries
        :rtype: AWP Boundaries object
        """

        boundaries_raw = soup.find_all('pre', 'bbcode_code')[-1]
        boundaries = boundaries_raw.text.split()[2:]

        try:
            awp_boundaries = AwpBoundaries.get_from_matchday(self.matchday)
        except Dictionary.DoesNotExist:
            awp_boundaries = AwpBoundaries.get_or_create_from_matchday(
                self.matchday)

        for i in range(26):
            strength = boundaries[i * 4]
            awp = boundaries[i * 4 + 1]
            awp_boundaries[strength] = awp

        return awp_boundaries
Exemple #7
0
    def _get_player_statistics_diff_in_json(newer_player_statistics, older_player_statistics):
        """
        Args:
            newer_player_statistics: newer statistic
            older_player_statistics: older statistic

        Returns:
            A dictionary of player statistics data. If st2 is None st1 is returned
        """

        strength = newer_player_statistics.strength
        if older_player_statistics:
            strength = newer_player_statistics.strength - older_player_statistics.strength
        ep = newer_player_statistics.ep
        if older_player_statistics:
            ep = newer_player_statistics.ep - older_player_statistics.ep
        tp = newer_player_statistics.tp
        if older_player_statistics:
            tp = newer_player_statistics.tp - older_player_statistics.tp
        awp = newer_player_statistics.awp
        if older_player_statistics:
            awp = newer_player_statistics.awp - older_player_statistics.awp
        freshness = newer_player_statistics.freshness
        if older_player_statistics:
            freshness = newer_player_statistics.freshness - older_player_statistics.freshness

        awp_boundaries = AwpBoundaries.get_from_matchday(newer_player_statistics.matchday)
        awp_to_next_bound = awp_boundaries[newer_player_statistics.strength + 1] - newer_player_statistics.awp

        statistic_diff = dict()
        statistic_diff['position'] = newer_player_statistics.player.position
        statistic_diff['age'] = newer_player_statistics.age
        statistic_diff['strength'] = strength
        statistic_diff['name'] = '<a href="%s">%s</a>' % (newer_player_statistics.player.get_absolute_url(),
                                                          newer_player_statistics.player.name)
        statistic_diff['ep'] = ep
        statistic_diff['tp'] = tp
        statistic_diff['awp'] = awp
        statistic_diff['freshness'] = freshness
        statistic_diff['games_in_season'] = newer_player_statistics.games_in_season
        statistic_diff['goals_in_season'] = newer_player_statistics.goals_in_season
        statistic_diff['won_tacklings_in_season'] = newer_player_statistics.won_tacklings_in_season
        statistic_diff['lost_tacklings_in_season'] = newer_player_statistics.lost_tacklings_in_season
        statistic_diff['won_friendly_tacklings_in_season'] = newer_player_statistics.won_friendly_tacklings_in_season
        statistic_diff['lost_friendly_tacklings_in_season'] = newer_player_statistics.lost_friendly_tacklings_in_season
        statistic_diff['yellow_cards_in_season'] = newer_player_statistics.yellow_cards_in_season
        statistic_diff['red_cards_in_season'] = newer_player_statistics.red_cards_in_season
        statistic_diff['equity'] = newer_player_statistics.equity
        statistic_diff['awp_to_next_bound'] = awp_to_next_bound

        return statistic_diff
Exemple #8
0
    def test_player_chart_shows_different_awp_boundaries(self):
        matchday_9 = MatchdayFactory.create(number=9)

        PlayerStatisticsFactory.create(player=self.player, matchday=self.matchday)
        PlayerStatisticsFactory.create(player=self.player, matchday=matchday_9)

        awp_boundaries = AwpBoundaries.get_or_create_from_matchday(self.matchday)
        awp_boundaries[2] = 2000

        awp_boundaries_9 = AwpBoundaries.get_or_create_from_matchday(matchday_9)
        awp_boundaries_9[2] = 3000

        response = self.client.get(reverse('core:ofm:players_chart_json'), {'player_id': self.player.id})
        self.assertEqual(response.status_code, 200)
        returned_json_data = json.loads(response.content.decode('utf-8'))
        self.assertTrue('series' in returned_json_data)

        self.assertEqual('AWP', returned_json_data['series'][0]['name'])
        self.assertTrue('data' in returned_json_data['series'][0])

        self.assertEqual('AWP-Grenze: 2', returned_json_data['series'][1]['name'])
        self.assertTrue('data' in returned_json_data['series'][1])
        self.assertEqual([2000, 3000], returned_json_data['series'][1]['data'])
Exemple #9
0
    def test_player_chart_shows_awp_boundaries_which_are_only_greater_than_my_strength(self):
        PlayerStatisticsFactory.create(player=self.player, matchday=self.matchday, strength=3, awp=3500)

        awp_boundaries = AwpBoundaries.get_or_create_from_matchday(self.matchday)
        awp_boundaries[2] = 2000
        awp_boundaries[3] = 3000
        awp_boundaries[4] = 4000

        response = self.client.get(reverse('core:ofm:players_chart_json'), {'player_id': self.player.id})
        self.assertEqual(response.status_code, 200)
        returned_json_data = json.loads(response.content.decode('utf-8'))
        self.assertTrue('series' in returned_json_data)

        self.assertEqual('AWP', returned_json_data['series'][0]['name'])
        self.assertTrue('data' in returned_json_data['series'][0])

        self.assertEqual('AWP-Grenze: 4', returned_json_data['series'][1]['name'])
        self.assertTrue('data' in returned_json_data['series'][1])
        self.assertEqual([4000], returned_json_data['series'][1]['data'])
Exemple #10
0
    def _get_player_statistics_diff_in_json(newer_player_statistics,
                                            older_player_statistics):
        """
        Args:
            newer_player_statistics: newer statistic
            older_player_statistics: older statistic

        Returns:
            A dictionary of player statistics data. If st2 is None st1 is returned
        """

        strength = newer_player_statistics.strength
        if older_player_statistics:
            strength = newer_player_statistics.strength - older_player_statistics.strength
        ep = newer_player_statistics.ep
        if older_player_statistics:
            ep = newer_player_statistics.ep - older_player_statistics.ep
        tp = newer_player_statistics.tp
        if older_player_statistics:
            tp = newer_player_statistics.tp - older_player_statistics.tp
        awp = newer_player_statistics.awp
        if older_player_statistics:
            awp = newer_player_statistics.awp - older_player_statistics.awp
        freshness = newer_player_statistics.freshness
        if older_player_statistics:
            freshness = newer_player_statistics.freshness - older_player_statistics.freshness

        awp_boundaries = AwpBoundaries.get_from_matchday(
            newer_player_statistics.matchday)
        awp_to_next_bound = awp_boundaries[newer_player_statistics.strength +
                                           1] - newer_player_statistics.awp

        statistic_diff = dict()
        statistic_diff['position'] = newer_player_statistics.player.position
        statistic_diff['age'] = newer_player_statistics.age
        statistic_diff['strength'] = strength
        statistic_diff['name'] = '<a href="%s">%s</a>' % (
            newer_player_statistics.player.get_absolute_url(),
            newer_player_statistics.player.name)
        statistic_diff['ep'] = ep
        statistic_diff['tp'] = tp
        statistic_diff['awp'] = awp
        statistic_diff['freshness'] = freshness
        statistic_diff[
            'games_in_season'] = newer_player_statistics.games_in_season
        statistic_diff[
            'goals_in_season'] = newer_player_statistics.goals_in_season
        statistic_diff[
            'won_tacklings_in_season'] = newer_player_statistics.won_tacklings_in_season
        statistic_diff[
            'lost_tacklings_in_season'] = newer_player_statistics.lost_tacklings_in_season
        statistic_diff[
            'won_friendly_tacklings_in_season'] = newer_player_statistics.won_friendly_tacklings_in_season
        statistic_diff[
            'lost_friendly_tacklings_in_season'] = newer_player_statistics.lost_friendly_tacklings_in_season
        statistic_diff[
            'yellow_cards_in_season'] = newer_player_statistics.yellow_cards_in_season
        statistic_diff[
            'red_cards_in_season'] = newer_player_statistics.red_cards_in_season
        statistic_diff['equity'] = newer_player_statistics.equity
        statistic_diff['awp_to_next_bound'] = awp_to_next_bound

        return statistic_diff