Ejemplo n.º 1
0
    def test_experience_with_sources(self):
        class TestSource(ExperienceSource):
            """
            This class simulates desync with real experience, initially
            get_experience() returns one value, but after force_recalculate()
            return totally different one
            """
            def __init__(self, initial, after_recalc):
                self.value = initial
                self.recalc = after_recalc

            def get_experience(self, user):
                return self.value

            def force_recalculate(self, user):
                self.value = self.recalc

        # To level up to lvl 1 you need the ExpMultiplier experience
        test_source = TestSource(1, ExpMultiplier)
        test_source1 = TestSource(3, 1)
        try:
            Experience.add_experience_source(test_source)
            Experience.add_experience_source(test_source1)

            test_user = User.objects.get(username='******')

            exp = Experience(test_user)

            self.assertEquals(exp.current_experience, 4)
            self.assertEquals(exp.current_level, 0)
            self.assertEquals(
                exp.required_experience_to_lvlup,
                ExpMultiplier
            )
            self.assertEquals(exp.level_exp_tuple, (0, 4))

            exp.force_recalculate()

            self.assertEquals(exp.current_experience, 1)
            self.assertEquals(exp.current_level, 1)
            self.assertEquals(
                exp.required_experience_to_lvlup,
                ExpBase*ExpMultiplier
            )
            self.assertEquals(exp.level_exp_tuple, (1, 1))

            Experience.clear_experience_sources()

            self.assertEquals(exp.level_exp_tuple, (0, 0))

        finally:
            Experience.clear_experience_sources()
Ejemplo n.º 2
0
    def test_experience_with_sources(self):
        class TestSource(ExperienceSource):
            """
            This class simulates desync with real experience, initially
            get_experience() returns one value, but after force_recalculate()
            return totally different one
            """
            def __init__(self, initial, after_recalc):
                self.value = initial
                self.recalc = after_recalc

            def get_experience(self, user):
                return self.value

            def force_recalculate(self, user):
                self.value = self.recalc

        # To level up to lvl 1 you need the ExpMultiplier experience
        test_source = TestSource(1, ExpMultiplier)
        test_source1 = TestSource(3, 1)
        try:
            Experience.add_experience_source(test_source)
            Experience.add_experience_source(test_source1)

            test_user = User.objects.get(username='******')

            exp = Experience(test_user)

            self.assertEquals(exp.current_experience, 4)
            self.assertEquals(exp.current_level, 0)
            self.assertEquals(exp.required_experience_to_lvlup, ExpMultiplier)
            self.assertEquals(exp.level_exp_tuple, (0, 4))

            exp.force_recalculate()

            self.assertEquals(exp.current_experience, 1)
            self.assertEquals(exp.current_level, 1)
            self.assertEquals(exp.required_experience_to_lvlup,
                              ExpBase * ExpMultiplier)
            self.assertEquals(exp.level_exp_tuple, (1, 1))

            Experience.clear_experience_sources()

            self.assertEquals(exp.level_exp_tuple, (0, 0))

        finally:
            Experience.clear_experience_sources()
Ejemplo n.º 3
0
    def test_softcap(self):
        class ConstSource(ExperienceSource):
            """
            This source always returns the same value
            """
            def __init__(self, value):
                self.value = value

            def get_experience(self, user):
                return self.value

            def force_recalculate(self, user):
                pass

        def total_exp_to_lvl(level):
            total = 0
            for i in xrange(1, level + 1):
                total += Experience.exp_to_lvl(i)
            return total

        try:
            Experience.add_experience_source(
                ConstSource(total_exp_to_lvl(SoftCapLevel))
            )

            test_user = User.objects.get(username='******')

            exp = Experience(test_user)

            self.assertEquals(exp.current_level, SoftCapLevel)
            self.assertEquals(
                exp.required_experience_to_lvlup,
                Experience.exp_to_lvl(SoftCapLevel) + LinearMultiplier
            )

            Experience.add_experience_source(
                ConstSource(Experience.exp_to_lvl((SoftCapLevel + 1)))
            )

            self.assertEquals(
                exp.required_experience_to_lvlup,
                Experience.exp_to_lvl(SoftCapLevel) + 2*LinearMultiplier
            )

        finally:
            Experience.clear_experience_sources()
Ejemplo n.º 4
0
    def test_experience_counter(self):
        url = reverse('view_current_profile')
        url_other = reverse('view_profile', args=['test_user2'])

        self.client.login(username='******')
        response = self.client.get(url)
        self.assertIn('0</text>', response.content)
        self.assertIn('Level: 0', response.content)
        self.assertIn('0%', response.content)

        response = self.client.get(url_other)
        self.assertIn('0</text>', response.content)
        self.assertIn('Level: 0', response.content)
        self.assertIn('0%', response.content)

        exp_to_lvl = Experience.exp_to_lvl

        class TrivialSource(ExperienceSource):
            def get_experience(self, user):
                if user.username == 'test_user':
                    return exp_to_lvl(1) + 10
                else:
                    return exp_to_lvl(1) + exp_to_lvl(2) + 10

            def force_recalculate(self, user):
                pass

        try:
            Experience.add_experience_source(TrivialSource())
            response = self.client.get(url)
            self.assertIn('1</text>', response.content)
            self.assertIn('Level: 1', response.content)
            self.assertIn('Experience: %d%%' % (100 * 10 / exp_to_lvl(2)),
                          response.content)

            response = self.client.get(url_other)
            self.assertIn('2</text>', response.content)
            self.assertIn('Level: 2', response.content)
            self.assertIn('Experience: %d%%' % (100 * 10 / exp_to_lvl(3)),
                          response.content)
        finally:
            Experience.clear_experience_sources()
Ejemplo n.º 5
0
    def test_problem_source(self):
        try:
            Experience.add_experience_source(PROBLEM_EXPERIENCE_SOURCE)

            user = User.objects.get(username="******")

            # test_user has solved a trivial task
            user_exp = Experience(user)
            user_exp.force_recalculate()
            self.assertEquals(user_exp.current_level, 0)
            self.assertEquals(user_exp.current_experience, Lvl1TaskExp)

            # Update on problem difficulty change
            pd = ProblemDifficulty.objects.get(problem__id=10)
            pd.difficulty = DIFFICULTY.EASY
            pd.save()
            self.assertEquals(user_exp.current_level, 1)
            self.assertEquals(user_exp.current_experience, 0)

        finally:
            Experience.clear_experience_sources()
Ejemplo n.º 6
0
    def test_experience_counter(self):
        url = reverse("view_current_profile")
        url_other = reverse("view_profile", args=["test_user2"])

        self.client.login(username="******")
        response = self.client.get(url)
        self.assertIn("0</text>", response.content)
        self.assertIn("Level: 0", response.content)
        self.assertIn("0%", response.content)

        response = self.client.get(url_other)
        self.assertIn("0</text>", response.content)
        self.assertIn("Level: 0", response.content)
        self.assertIn("0%", response.content)

        exp_to_lvl = Experience.exp_to_lvl

        class TrivialSource(ExperienceSource):
            def get_experience(self, user):
                if user.username == "test_user":
                    return exp_to_lvl(1) + 10
                else:
                    return exp_to_lvl(1) + exp_to_lvl(2) + 10

            def force_recalculate(self, user):
                pass

        try:
            Experience.add_experience_source(TrivialSource())
            response = self.client.get(url)
            self.assertIn("1</text>", response.content)
            self.assertIn("Level: 1", response.content)
            self.assertIn("Experience: %d%%" % (100 * 10 / exp_to_lvl(2)), response.content)

            response = self.client.get(url_other)
            self.assertIn("2</text>", response.content)
            self.assertIn("Level: 2", response.content)
            self.assertIn("Experience: %d%%" % (100 * 10 / exp_to_lvl(3)), response.content)
        finally:
            Experience.clear_experience_sources()