Esempio n. 1
0
    def test_SetPlayerPuzzleScores_many_players(self):  # pylint: disable=invalid-name
        """
        Check that when we send scores from multiple users, the correct order
        of scores is displayed. Note that, before being processed by
        display_score, lower scores are better.
        """
        puzzle_id = ['1']
        player1_score = 0.08
        player2_score = 0.02
        self.make_puzzle_score_request(puzzle_id, player1_score, self.user)

        # There should now be a score in the db.
        top_10 = Score.get_tops_n(10, puzzle_id)
        self.assertEqual(len(top_10), 1)
        self.assertEqual(top_10[0]['score'],
                         Score.display_score(player1_score))

        self.make_puzzle_score_request(puzzle_id, player2_score, self.user2)

        # There should now be two scores in the db
        top_10 = Score.get_tops_n(10, puzzle_id)
        self.assertEqual(len(top_10), 2)

        # Top score should be player2_score. Second should be player1_score
        self.assertAlmostEqual(top_10[0]['score'],
                               Score.display_score(player2_score),
                               delta=0.5)
        self.assertAlmostEqual(top_10[1]['score'],
                               Score.display_score(player1_score),
                               delta=0.5)

        # Top score user should be self.user2.username
        self.assertEqual(top_10[0]['username'], self.user2.username)
Esempio n. 2
0
    def test_SetPlayerPuzzleScores_manyplayers(self):
        """
        Check that when we send scores from multiple users, the correct order
        of scores is displayed. Note that, before being processed by
        display_score, lower scores are better.
        """
        puzzle_id = ['1']
        player1_score = 0.08
        player2_score = 0.02
        response1 = self.make_puzzle_score_request(puzzle_id, player1_score,
                self.user)

        # There should now be a score in the db.
        top_10 = Score.get_tops_n(10, puzzle_id)
        self.assertEqual(len(top_10), 1)
        self.assertEqual(top_10[0]['score'], Score.display_score(player1_score))

        response2 = self.make_puzzle_score_request(puzzle_id, player2_score,
                self.user2)

        # There should now be two scores in the db
        top_10 = Score.get_tops_n(10, puzzle_id)
        self.assertEqual(len(top_10), 2)

        # Top score should be player2_score. Second should be player1_score
        self.assertAlmostEqual(top_10[0]['score'],
                Score.display_score(player2_score),
                delta=0.5)
        self.assertAlmostEqual(top_10[1]['score'],
                Score.display_score(player1_score),
                delta=0.5)

        # Top score user should be self.user2.username
        self.assertEqual(top_10[0]['username'], self.user2.username)
Esempio n. 3
0
    def test_SetPlayerPuzzleScores_multiple_courses(self):  # pylint: disable=invalid-name
        puzzle_id = "1"

        player1_score = 0.05
        player2_score = 0.06

        course_list_1 = [self.course_id]
        course_list_2 = [self.course_id2]

        self.make_puzzle_score_request(puzzle_id, player1_score, self.user)

        course_1_top_10 = Score.get_tops_n(10, puzzle_id, course_list_1)
        course_2_top_10 = Score.get_tops_n(10, puzzle_id, course_list_2)
        total_top_10 = Score.get_tops_n(10, puzzle_id)

        #  player1 should now be in the top 10 of course 1 and not in course 2
        self.assertEqual(len(course_1_top_10), 1)
        self.assertEqual(len(course_2_top_10), 0)
        self.assertEqual(len(total_top_10), 1)

        self.make_puzzle_score_request(puzzle_id, player2_score, self.user2)

        course_2_top_10 = Score.get_tops_n(10, puzzle_id, course_list_2)
        total_top_10 = Score.get_tops_n(10, puzzle_id)

        #  player2 should now be in the top 10 of course 2 and not in course 1
        self.assertEqual(len(course_1_top_10), 1)
        self.assertEqual(len(course_2_top_10), 1)
        self.assertEqual(len(total_top_10), 2)
Esempio n. 4
0
    def test_SetPlayerPuzzleScores_multiple_courses(self):  # pylint: disable=invalid-name
        puzzle_id = "1"

        player1_score = 0.05
        player2_score = 0.06

        course_list_1 = [self.course_id]
        course_list_2 = [self.course_id2]

        self.make_puzzle_score_request(puzzle_id, player1_score, self.user)

        course_1_top_10 = Score.get_tops_n(10, puzzle_id, course_list_1)
        course_2_top_10 = Score.get_tops_n(10, puzzle_id, course_list_2)
        total_top_10 = Score.get_tops_n(10, puzzle_id)

        #  player1 should now be in the top 10 of course 1 and not in course 2
        self.assertEqual(len(course_1_top_10), 1)
        self.assertEqual(len(course_2_top_10), 0)
        self.assertEqual(len(total_top_10), 1)

        self.make_puzzle_score_request(puzzle_id, player2_score, self.user2)

        course_2_top_10 = Score.get_tops_n(10, puzzle_id, course_list_2)
        total_top_10 = Score.get_tops_n(10, puzzle_id)

        #  player2 should now be in the top 10 of course 2 and not in course 1
        self.assertEqual(len(course_1_top_10), 1)
        self.assertEqual(len(course_2_top_10), 1)
        self.assertEqual(len(total_top_10), 2)
Esempio n. 5
0
def save_scores(user, puzzle_scores):
    score_responses = []
    for score in puzzle_scores:
        log.debug("score: %s", score)
        # expected keys ScoreType, PuzzleID (int),
        # BestScore (energy), CurrentScore (Energy), ScoreVersion (int)

        puzzle_id = score['PuzzleID']
        best_score = score['BestScore']
        current_score = score['CurrentScore']
        score_version = score['ScoreVersion']

        # SetPlayerPuzzleScoreResponse object
        # Score entries are unique on user/unique_user_id/puzzle_id/score_version
        try:
            obj = Score.objects.get(user=user,
                                    unique_user_id=unique_id_for_user(user),
                                    puzzle_id=puzzle_id,
                                    score_version=score_version)
            obj.current_score = current_score
            obj.best_score = best_score

        except Score.DoesNotExist:
            obj = Score(user=user,
                        unique_user_id=unique_id_for_user(user),
                        puzzle_id=puzzle_id,
                        current_score=current_score,
                        best_score=best_score,
                        score_version=score_version)
        obj.save()

        score_responses.append({'PuzzleID': puzzle_id, 'Status': 'Success'})

    return {"OperationID": "SetPlayerPuzzleScores", "Value": score_responses}
Esempio n. 6
0
    def test_SetPlayerPuzzleScores(self):

        puzzle_id = 994391
        best_score = 0.078034
        response = self.make_puzzle_score_request(puzzle_id, [best_score])

        self.assertEqual(response.content, json.dumps(
            [{"OperationID": "SetPlayerPuzzleScores",
              "Value": [{
                  "PuzzleID": puzzle_id,
                  "Status": "Success"}]}]))

        # There should now be a score in the db.
        top_10 = Score.get_tops_n(10, puzzle_id)
        self.assertEqual(len(top_10), 1)
        self.assertEqual(top_10[0]['score'], Score.display_score(best_score))
Esempio n. 7
0
    def puzzle_leaders(self, n=10):
        """
        Returns a list of n pairs (user, score) corresponding to the top
        scores; the pairs are in descending order of score.
        """
        from foldit.models import Score

        leaders = [(e['username'], e['score']) for e in Score.get_tops_n(10)]
        leaders.sort(key=lambda x:-x[1])

        return leaders
Esempio n. 8
0
    def puzzle_leaders(self, n=10):
        """
        Returns a list of n pairs (user, score) corresponding to the top
        scores; the pairs are in descending order of score.
        """
        from foldit.models import Score

        leaders = [(e['username'], e['score']) for e in Score.get_tops_n(10)]
        leaders.sort(key=lambda x: -x[1])

        return leaders
Esempio n. 9
0
def save_scores(user, puzzle_scores):
    score_responses = []
    for score in puzzle_scores:
        log.debug("score: %s", score)
        # expected keys ScoreType, PuzzleID (int),
        # BestScore (energy), CurrentScore (Energy), ScoreVersion (int)

        puzzle_id = score['PuzzleID']
        best_score = score['BestScore']
        current_score = score['CurrentScore']
        score_version = score['ScoreVersion']

        # SetPlayerPuzzleScoreResponse object
        # Score entries are unique on
        # user/unique_user_id/puzzle_id/score_version
        try:
            obj = Score.objects.get(
                user=user,
                unique_user_id=unique_id_for_user(user),
                puzzle_id=puzzle_id,
                score_version=score_version)
            obj.current_score = current_score
            obj.best_score = best_score

        except Score.DoesNotExist:
            obj = Score(
                user=user,
                unique_user_id=unique_id_for_user(user),
                puzzle_id=puzzle_id,
                current_score=current_score,
                best_score=best_score,
                score_version=score_version)
        obj.save()

        score_responses.append({'PuzzleID': puzzle_id,
                                'Status': 'Success'})

    return {"OperationID": "SetPlayerPuzzleScores", "Value": score_responses}
Esempio n. 10
0
    def puzzle_leaders(self, n=10, courses=None):
        """
        Returns a list of n pairs (user, score) corresponding to the top
        scores; the pairs are in descending order of score.
        """
        from foldit.models import Score

        if courses is None:
            courses = [self.location.course_key]

        leaders = [(leader['username'], leader['score']) for leader in Score.get_tops_n(10, course_list=courses)]
        leaders.sort(key=lambda x: -x[1])

        return leaders
Esempio n. 11
0
    def puzzle_leaders(self, n=10, courses=None):
        """
        Returns a list of n pairs (user, score) corresponding to the top
        scores; the pairs are in descending order of score.
        """
        from foldit.models import Score

        if courses is None:
            courses = [self.location.course_key]

        leaders = [(leader['username'], leader['score']) for leader in Score.get_tops_n(10, course_list=courses)]
        leaders.sort(key=lambda x: -x[1])

        return leaders
Esempio n. 12
0
    def test_SetPlayerPuzzleScores_multiple(self):
        """
        Check that multiple posts with the same id are handled properly
        (keep latest for each user, have multiple users work properly)
        """
        orig_score = 0.07
        puzzle_id = '1'
        response = self.make_puzzle_score_request([puzzle_id], [orig_score])

        # There should now be a score in the db.
        top_10 = Score.get_tops_n(10, puzzle_id)
        self.assertEqual(len(top_10), 1)
        self.assertEqual(top_10[0]['score'], Score.display_score(orig_score))

        # Reporting a better score should overwrite
        better_score = 0.06
        response = self.make_puzzle_score_request([1], [better_score])

        top_10 = Score.get_tops_n(10, puzzle_id)
        self.assertEqual(len(top_10), 1)

        # Floats always get in the way, so do almostequal
        self.assertAlmostEqual(
            top_10[0]['score'],
            Score.display_score(better_score),
            delta=0.5
        )

        # reporting a worse score shouldn't
        worse_score = 0.065
        response = self.make_puzzle_score_request([1], [worse_score])

        top_10 = Score.get_tops_n(10, puzzle_id)
        self.assertEqual(len(top_10), 1)
        # should still be the better score
        self.assertAlmostEqual(
            top_10[0]['score'],
            Score.display_score(better_score),
            delta=0.5
        )