예제 #1
0
    def test_player_stats(self):
        """
        Tests a player stats is calculated correctly.
        """
        # fresh player test
        player = Player(first_name='New', last_name='Player')
        self.assertEqual(player.player_stats(), {
            'played': "-",
            'win_rate': "- %",
            'average': "-"
        })

        # player with matches test
        player = Player.objects.first()
        stats = player.player_stats()
        results = Result.objects.filter(player=player)

        # assert games played is correct
        games_played = results.count()
        self.assertEqual(stats['played'], games_played)

        # assert completion rate is correct
        match_count = 0
        for league in League.objects.filter(player=player):
            match_count += league.ladder.league_set.count() - 1

        self.assertEqual(
            stats['completion_rate'],
            "{0:.2f} %".format(games_played / match_count * 100.00))

        # assert win rate is correct
        won = player.result_player.filter(result=9).count()
        self.assertEqual(stats['win_rate'],
                         "{0:.2f} %".format(won / games_played * 100.00))

        # assert average is correct
        # two points for winning + 1 point for playing
        additional_points = ((won * 2) + games_played) / games_played
        average = list(player.result_player.aggregate(
            Avg('result')).values())[0]
        average_with_additional = average + additional_points
        self.assertEqual(stats['average'],
                         "{0:.2f}".format(average_with_additional))
예제 #2
0
    def test_player_stats(self):
        """
        Tests a player stats is calculated correctly.
        """
        # fresh player test
        player = Player(first_name='New', last_name='Player')
        self.assertEqual(player.player_stats(), {
            'played': "-",
            'win_rate': "- %",
            'average': "-"
        })

        # player with matches test
        player = Player.objects.first()
        stats = player.player_stats()
        results = Result.objects.filter(player=player)

        # assert games played is correct
        games_played = results.count()
        self.assertEqual(stats['played'], games_played)

        # assert completion rate is correct
        match_count = 0
        for league in League.objects.filter(player=player):
            match_count += league.ladder.league_set.count() - 1

        self.assertEqual(stats['completion_rate'], "{0:.2f} %".format(games_played / match_count * 100.00))

        # assert win rate is correct
        won = player.result_player.filter(result=9).count()
        self.assertEqual(stats['win_rate'], "{0:.2f} %".format(won / games_played * 100.00))

        # assert average is correct
        # two points for winning + 1 point for playing
        additional_points = ((won * 2) + games_played) / games_played
        average = list(player.result_player.aggregate(Avg('result')).values())[0]
        average_with_additional = average + additional_points
        self.assertEqual(stats['average'], "{0:.2f}".format(average_with_additional))
예제 #3
0
    def handle(self, *args, **options):

        # make arg checks
        if options['file'] is False:
            raise CommandError('--file location is not set')

        file_location = options['file']

        if os.access(file_location, os.R_OK) is False:
            raise CommandError('Directory (%s) is not writeable, change in code' % file_location)

        # make arg checks
        if options['season'] is False:
            raise CommandError('--season id option not set')

        book = open_workbook(file_location)

        season = Season.objects.get(pk=options['season'])
        sh1 = book.sheet_by_index(0)  # sheet1, aways first sheet
        player_list = defaultdict(dict)  # initialize defaultdict for our player list.
        current_div = {}  # set the division counter to 0

        # save all players then set up ladder
        for rownum in xrange(sh1.nrows):
            rows = sh1.row_values(rownum)

            #Find out division numbers
            if not rows[0] and rows[1] != 'NAME' and rows[1] != 'ROUND':
                for div in rows:
                    if isinstance(div, float):
                        current_div = ('%.2f' % div).rstrip('0').rstrip('.')
                        try:
                            ladder = Ladder.objects.get(season=season, division=current_div)
                        except:
                            ladder = Ladder(season=season, division=current_div, ladder_type="First to 9")
                            ladder.save()

            #save players
            if rows[0] and rows[1] != 'NAME':
                position = rows[0]
                first_name = rows[1]
                last_name = rows[2]

                #populate our array for later use
                player_list[current_div][position] = {'first_name': first_name, 'last_name': last_name}

                try:
                    player_object = Player.objects.get(first_name=first_name, last_name=last_name)
                except:
                    print 'No match for player: ' + first_name + last_name
                    try:
                        player_object = Player(first_name=first_name, last_name=last_name)
                        player_object.save()
                    except:
                        print 'Failed to create player: ' + first_name + last_name

                try:
                    league_object = League.objects.get(ladder=ladder, player=player_object)
                except League.MultipleObjectsReturned:
                    pass
                except:
                    league_object = League.objects.create(ladder=ladder, player=player_object, sort_order=position * 10)

        print 'built good'

        current_div = {}  # reset the division counter to 0
        # save the results
        for rownum in xrange(sh1.nrows):

            try:
                rows = sh1.row_values(rownum)

                #Well lazy copy pasta for division info
                if not rows[0] and rows[1] != 'NAME' and rows[1] != 'ROUND':
                    for div in rows:
                        if isinstance(div, float):
                            current_div = ('%.2f' % div).rstrip('0').rstrip('.')
                            print current_div

                if rows[1] == 'NAME':
                    i = 3
                    while rows[i] != 'Div':  # flag to end scores normally 'Div'
                        i += 1

                    count = i - 3

                if rows[0]:
                    try:
                        position = rows[0]
                        first_name = rows[1]
                        last_name = rows[2]
                    except:
                        print 'index error for row: ' + rows[0]

                    for c in xrange(count):

                        if rows[c + 3] != '':
                            score = rows[c + 3]

                            if not isinstance(score, float):
                                continue

                            opp_last_name = player_list[current_div][c + 1]['last_name']
                            opp_first_name = player_list[current_div][c + 1]['first_name']

                            try:
                                ladder_object = Ladder.objects.get(season=season, division=current_div)
                            except:
                                print 'No ladder matching: ' + ' ' + unicode(current_div)
                                break
                            try:
                                player_object = Player.objects.get(first_name=first_name, last_name=last_name)
                            except:
                                print 'No match for player: ' + first_name + last_name
                            try:
                                opp_object = Player.objects.get(first_name=opp_first_name, last_name=opp_last_name)
                            except:
                                print 'No match for opp: ' + opp_first_name + opp_last_name

                            try:
                                result_object = Result.objects.get(ladder=ladder_object, player=player_object,
                                                                   opponent=opp_object)
                            except Result.MultipleObjectsReturned:
                                pass
                            except:
                                result_object = Result(ladder=ladder_object, player=player_object, opponent=opp_object,
                                                       result=score, date_added=season.end_date)
                                result_object.save()
            except Exception, e:
                print 'problem @ row: ' + rownum.__str__() + e.__str__()
                raise CommandError(e.__str__())
예제 #4
0
    else:
        if len(split) == 2 and split[0] != "DIVISION":
            print split[1]

        elif len(split) == 3:
            first_name = split[1]
            last_name = split[2]
        elif len(split) == 4:
            first_name = split[1]
            last_name = split[2]

        try:
            player_object = Player.objects.get(first_name=first_name, last_name=last_name)
        except:
            print 'No match for player: ' + first_name + last_name
            try:
                player_object = Player(first_name=first_name, last_name=last_name)
                player_object.save()
            except:
                print 'Failed to create player: ' + first_name + last_name

        try:
            league_object = League.objects.get(ladder=ladder, player=player_object)
        except League.MultipleObjectsReturned:
            pass
        except:
            league_object = League.objects.create(ladder=ladder, player=player_object, sort_order=position * 10)
            position += 1

    first_name = ''
    last_name = ''