Example #1
0
 def test_challenge_index(self):
     Challenge.create(user_from=self.ch_player2, user_to=self.ch_player1,
                     ignore_questions=True)
     response = self.c.get(reverse('challenge_index_view'))
     # Test if both challenges are displayed
     self.assertContains(response, 'testuser1</a> vs')
     self.assertContains(response, 'testuser2</a> vs')
Example #2
0
 def test_challenge_index(self):
     Challenge.create(user_from=self.ch_player2, user_to=self.ch_player1,
                     ignore_questions=True)
     response = self.c.get(reverse('challenge_index_view'))
     # Test if both challenges are displayed
     self.assertContains(response, 'testuser1</a> vs')
     self.assertContains(response, 'testuser2</a> vs')
Example #3
0
    def test_weakness(self):
        """
         Test for weakness
        """

        initial_points = 10

        player = self._get_player()
        player2 = self._get_player(2)
        chall_user = player.get_extension(ChallengeUser)

        scoring.setup_scoring()
        Coin.add('points')
        scoring.score_simple(chall_user, 'points', initial_points)
        self.assertEqual(player.points, initial_points)

        # Points won before Weakness is applied
        chall = Challenge.create(user_from=chall_user, user_to=player2, ignore_questions=True)
        chall.set_won_by_player(chall_user)
        points_no_weakness = player.points

        # Applying Weakness
        weakness = Spell.objects.create(name='challenge-affect-scoring', available=True, price=10, percents=-66, type='n')
        obs = PlayerSpellDue.objects.create(player=chall_user, source=chall_user, spell=weakness, due=datetime.now() + timedelta(days=1))
        self.assertTrue(chall_user.magic.has_modifier('challenge-affect-scoring'))

        player.points = initial_points
        chall = Challenge.create(user_from=chall_user, user_to=player2, ignore_questions=True)
        chall.set_won_by_player(chall_user)

        # Player should win 66% less points with weakness applied
        self.assertEqual(player.points, points_no_weakness - 0.66 * (points_no_weakness - initial_points))
Example #4
0
    def test_charge(self):
        """
         Test for Charge spell
        """
        initial_points = 10

        player = self._get_player()
        player2 = self._get_player(2)
        chall_user = player.get_extension(ChallengeUser)

        scoring.setup_scoring()
        Coin.add('points')
        scoring.score_simple(chall_user, 'points', initial_points)
        self.assertEqual(player.points, initial_points)

        # Points won before Charge is applied
        chall = Challenge.create(user_from=chall_user, user_to=player2, ignore_questions=True)
        chall.set_won_by_player(chall_user)
        points_no_charge = player.points

        # Apply Charge
        charge = Spell.objects.create(name='challenge-affect-scoring', available=True, price=10, percents=33, type='p')
        obs = PlayerSpellDue.objects.create(player=chall_user, source=chall_user, spell=charge, due=datetime.now() + timedelta(days=1))
        self.assertTrue(chall_user.magic.has_modifier('challenge-affect-scoring'))

        player.points = initial_points
        chall = Challenge.create(user_from=chall_user, user_to=player2, ignore_questions=True)
        chall.set_won_by_player(chall_user)

        # Player should have 33% more points with charge applied
        self.assertEqual(player.points, points_no_charge + 0.33 * (points_no_charge - initial_points))
Example #5
0
    def test_evade(self):
        """
         Test for Evade spell
        """
        player = self._get_player()
        player2 = self._get_player(2)

        initial_points = 10

        scoring.setup_scoring()
        Coin.add('points')
        scoring.score_simple(player, 'points', initial_points)
        self.assertEqual(player.points, initial_points)

        # Create and apply evade
        evade = Spell.objects.create(name='challenge-evade',
                                     available=True,
                                     price=25,
                                     percents=100,
                                     type='p')
        obs = PlayerSpellDue.objects.create(player=player,
                                            source=player,
                                            spell=evade,
                                            due=datetime.now() +
                                            timedelta(days=1))
        self.assertTrue(player.magic.has_modifier('challenge-evade'))

        # Get 'chall-lost' expression. By default you still win 2 points when losing a challenge
        formulas = ChallengeGame.get_formulas()
        exp = formulas[1]['expression']  # this will be 'points=XX'
        index = exp.find('=') + 1  # get position of '='
        points = int(
            exp[index:])  # get XX (nr of points won when losing challenge)

        # Create challenge and make first player lose it
        chall = Challenge.create(user_from=player2,
                                 user_to=player,
                                 ignore_questions=True)
        chall.set_won_by_player(player2)

        # If evade spell worked losing player should have initial_points + 'chall-lost' points

        # Evade has 20% chance of activation so play challenge in loop while it activates
        while player.points != initial_points + points:
            player.points = initial_points
            chall.set_expired()
            chall = Challenge.create(user_from=player2,
                                     user_to=player,
                                     ignore_questions=True)
            chall.set_won_by_player(player2)

        # Check if final score is ok
        self.assertEqual(player.points, initial_points + points)
Example #6
0
    def testLaunch(self):
        chall = Challenge.create(user_from=self.chall_user, user_to=self.chall_user2, ignore_questions=True)

        self.assertTrue(isinstance(chall, Challenge))
        self.assertTrue(chall.is_launched())

        chall.refuse()
        self.assertTrue(chall.is_refused())
        chall.delete()

        chall = Challenge.create(user_from=self.chall_user, user_to=self.chall_user2, ignore_questions=True)
        chall.accept()
        self.assertTrue(chall.is_runnable())
        self.assertFalse(chall.is_refused())
        chall.delete()
Example #7
0
    def testLaunch(self):
        chall = Challenge.create(user_from=self.chall_user, user_to=self.chall_user2, ignore_questions=True)

        self.assertTrue(isinstance(chall, Challenge))
        self.assertTrue(chall.is_launched())

        chall.refuse()
        self.assertTrue(chall.is_refused())
        chall.delete()

        chall = Challenge.create(user_from=self.chall_user, user_to=self.chall_user2, ignore_questions=True)
        chall.accept()
        self.assertTrue(chall.is_runnable())
        self.assertFalse(chall.is_refused())
        chall.delete()
Example #8
0
    def test_post_challenge(self):
        # create an active challenge, with fake questions
        Formula.add('chall-warranty')
        Formula.add('chall-timer')
        category = Category.add('challenge')
        for i in range(Challenge.LIMIT + 1):
            q = Question.objects.create(text='text %s' % i, category=category, active=True)
            for j in range(5):
                Answer.objects.create(correct=j==1, question=q)
        chall = Challenge.create(user_from=self.challuser2, user_to=self.challuser)
        chall.accept()
        response = self.client.get('/api/challenge/{id}/'.format(id=chall.id))
        data = json.loads(response.content)

        self.assertTrue(data)
        self.assertEqual(data['status'], 'A')
        self.assertEqual(data['to'], self.challuser.user.username)
        self.assertEqual(len(data['questions']), Challenge.LIMIT)

        # attempt post
        data = {}
        for q in Question.objects.all():
            answers = []
            for a in q.correct_answers:
                answers.append(str(a.id))

            data[q.id] = ','.join(answers)

        response = self.client.post('/api/challenge/{id}/'.format(id=chall.id), data)
        self.assertEqual(response.status_code, 200)

        data = json.loads(response.content)

        self.assertTrue(data['success'])
        self.assertEqual(data['result']['points'], Challenge.LIMIT * 100)
Example #9
0
    def test_post_challenge(self):
        # create an active challenge, with fake questions
        Formula.objects.create(id='chall-warranty')
        category = Category.objects.create(name='challenge')
        for i in range(5):
            q = Question.objects.create(text='text %s' % i, category=category, active=True)
            for j in range(5):
                Answer.objects.create(correct=j==1, question=q)
        chall = Challenge.create(user_from=self.challuser2, user_to=self.challuser)
        chall.accept()
        response = self.client.get('/api/challenge/{id}/'.format(id=chall.id))
        data = json.loads(response.content)

        self.assertTrue(data)
        self.assertEqual(data['status'], 'A')
        self.assertEqual(data['to'], self.challuser.user.username)
        self.assertEqual(len(data['questions']), 5)

        # attempt post
        data = {}
        for q in Question.objects.all():
            answers = []
            for a in q.correct_answers:
                answers.append(str(a.id))

            data[q.id] = ','.join(answers)

        response = self.client.post('/api/challenge/{id}/'.format(id=chall.id), data)
        self.assertEqual(response.status_code, 200)

        data = json.loads(response.content)

        self.assertTrue(data['success'])
        self.assertEqual(data['result']['points'], 500)
Example #10
0
    def test_run_is_started(self):
        chall = Challenge.create(user_from=self.chall_user, user_to=self.chall_user2, ignore_questions=True)

        chall.accept()
        chall.set_start(self.chall_user)
        self.assertTrue(chall.is_started_for_user(self.chall_user))
        self.assertFalse(chall.is_started_for_user(self.chall_user2))
Example #11
0
    def test_scoring_loss(self):
        initial_points = 10
        winner = self._get_player(1).get_extension(ChallengeUser)
        loser = self._get_player(2).get_extension(ChallengeUser)

        # Setup scoring
        scoring.setup_scoring()
        Coin.add('points')

        # Give initial points
        scoring.score_simple(winner, 'points', initial_points)
        scoring.score_simple(loser, 'points', initial_points)

        # Set formula
        loss_points = -2
        formula = Formula.get('chall-lost')
        formula.expression = 'points=' + str(loss_points)
        formula.save()

        # Play challenge
        chall = Challenge.create(user_from=winner,
                                 user_to=loser,
                                 ignore_questions=True)
        chall.set_won_by_player(winner)

        self.assertEqual(loser.player_ptr.points, initial_points +
                         loss_points)  # loss_points is negative
Example #12
0
    def setUp(self):
        super(TestChallengeViews, self).setUp()
        self.ch_player1 = self._get_player(1)
        self.ch_player2 = self._get_player(2)
        race = Race.objects.create(name=u'testrace', can_play=True)
        self.ch_player1.race = race
        self.ch_player2.race = race
        self.ch_player1.points = 100
        self.ch_player2.points = 100
        self.ch_player1.save()
        self.ch_player2.save()
        self.ch_player1 = self.ch_player1.get_extension(ChallengeUser)
        self.ch_player2 = self.ch_player2.get_extension(ChallengeUser)
        scoring.setup_scoring()

        self.category = Category.add('challenge')
        self.question1 = Question.objects.create(text='question1', answer_type='F',
                                                 category=self.category, active=True)
        self.answer1 = Answer.objects.create(text='first answer', correct=True,
                                             question=self.question1)
        self.question2 = Question.objects.create(text='question2', answer_type='F',
                                                 category=self.category, active=True)
        self.answer2 = Answer.objects.create(text='second answer', correct=True,
                                             question=self.question2)
        self.ch = Challenge.create(user_from=self.ch_player1, user_to=self.ch_player2,
                        ignore_questions=True)
        self.ch.questions.add(self.question1)
        self.ch.questions.add(self.question2)

        self.c = Client()
        self.c.login(username='******', password='******')
Example #13
0
    def setUp(self):
        super(TestChallengeViews, self).setUp()
        self.ch_player1 = self._get_player(1)
        self.ch_player2 = self._get_player(2)
        race = Race.objects.create(name=u'testrace', can_play=True)
        self.ch_player1.race = race
        self.ch_player2.race = race
        self.ch_player1.points = 100
        self.ch_player2.points = 100
        self.ch_player1.save()
        self.ch_player2.save()
        self.ch_player1 = self.ch_player1.get_extension(ChallengeUser)
        self.ch_player2 = self.ch_player2.get_extension(ChallengeUser)
        scoring.setup_scoring()

        self.category = Category.add('challenge')
        self.question1 = Question.objects.create(text='question1', answer_type='F',
                                                 category=self.category, active=True)
        self.answer1 = Answer.objects.create(text='first answer', correct=True,
                                             question=self.question1)
        self.question2 = Question.objects.create(text='question2', answer_type='F',
                                                 category=self.category, active=True)
        self.answer2 = Answer.objects.create(text='second answer', correct=True,
                                             question=self.question2)
        self.ch = Challenge.create(user_from=self.ch_player1, user_to=self.ch_player2,
                        ignore_questions=True)
        self.ch.questions.add(self.question1)
        self.ch.questions.add(self.question2)

        self.c = Client()
        self.c.login(username='******', password='******')
Example #14
0
    def test_run_is_started(self):
        chall = Challenge.create(user_from=self.chall_user, user_to=self.chall_user2, ignore_questions=True)

        chall.accept()
        chall.set_start(self.chall_user)
        self.assertTrue(chall.is_started_for_user(self.chall_user))
        self.assertFalse(chall.is_started_for_user(self.chall_user2))
Example #15
0
    def test_frenzy_loss(self):
        """
         If user loses while affected by frenzy he should lose frenzy.percents more points
        """
        initial_points = 100
        loss_points = -10
        warranty_points = -3

        player_frenzy = self._get_player(1).get_extension(ChallengeUser)
        player_dummy = self._get_player(2).get_extension(ChallengeUser)

        scoring.setup_scoring()
        Coin.add('points')
        scoring.score_simple(player_frenzy, 'points', initial_points)

        formula = Formula.get('chall-lost')
        formula.expression = 'points=' + str(loss_points)
        formula.save()

        formula = Formula.get('chall-warranty')
        formula.expression = 'points=' + str(warranty_points)
        formula.save()

        # Apply frenzy
        frenzy = Spell.objects.create(name='challenge-affect-scoring', available=True, price=25, percents=66, type='o')
        obs = PlayerSpellDue.objects.create(player=player_frenzy, source=player_frenzy, spell=frenzy, due=datetime.now() + timedelta(days=1))

        # Win challenge with dummy player to see the amount of points lost by the player affected with frenzy
        chall = Challenge.create(user_from=player_frenzy, user_to=player_dummy, ignore_questions=True)
        chall.set_won_by_player(player_dummy)

        # Player should lose frenzy.percents more points with frenzy applied
        target_points = initial_points + loss_points + frenzy.percents / 100.0 * loss_points + warranty_points

        self.assertEqual(player_frenzy.player_ptr.points, target_points)
Example #16
0
    def test_charge(self):
        """
         Test for Charge spell
        """
        initial_points = 100
        win_points = 10

        player_charge = self._get_player(1).get_extension(ChallengeUser)
        player_dummy = self._get_player(2).get_extension(ChallengeUser)

        scoring.setup_scoring()
        Coin.add('points')
        scoring.score_simple(player_charge, 'points', initial_points)

        formula = Formula.get('chall-won')
        formula.expression = 'points=' + str(win_points)
        formula.save()

        # Apply charge
        charge = Spell.objects.create(name='challenge-affect-scoring-won', available=True, price=10, percents=33, type='p')
        obs = PlayerSpellDue.objects.create(player=player_charge, source=player_charge, spell=charge, due=datetime.now() + timedelta(days=1))

        chall = Challenge.create(user_from=player_charge, user_to=player_dummy, ignore_questions=True)
        chall.set_won_by_player(player_charge)

        # Player should win weakness.percents more points with charge applied
        target_points = initial_points + win_points + charge.percents / 100.0 * win_points
        self.assertEqual(player_charge.player_ptr.points, target_points)
Example #17
0
    def test_run_accept(self):
        chall = Challenge.create(user_from=self.chall_user,
                                 user_to=self.chall_user2,
                                 ignore_questions=True)

        chall.accept()
        self.assertTrue(chall.is_runnable())
Example #18
0
    def test_weakness_and_charge(self):
        """
         If both Weakness and Charge are active, a player should win weakness.percents + charge.percents less/more points
         after winning a challenge
        """
        initial_points = 100
        win_points = 10

        player = self._get_player(1).get_extension(ChallengeUser)
        player_dummy = self._get_player(2).get_extension(ChallengeUser)

        scoring.setup_scoring()
        Coin.add('points')
        scoring.score_simple(player, 'points', initial_points)

        formula = Formula.get('chall-won')
        formula.expression = 'points=' + str(win_points)
        formula.save()

        # Apply charge
        charge = Spell.objects.create(name='challenge-affect-scoring-won', available=True, price=10, percents=33, type='p')
        obs = PlayerSpellDue.objects.create(player=player, source=player, spell=charge, due=datetime.now() + timedelta(days=1))

        # Apply weakness
        weakness = Spell.objects.create(name='challenge-affect-scoring-won', available=True, price=10, percents=-66, type='p')
        obs = PlayerSpellDue.objects.create(player=player, source=player, spell=weakness, due=datetime.now() + timedelta(days=1))

        chall = Challenge.create(user_from=player, user_to=player_dummy, ignore_questions=True)
        chall.set_won_by_player(player)

        percents = (charge.percents + weakness.percents) / 100.0
        target_points = initial_points + win_points + percents * win_points

        self.assertEqual(player.player_ptr.points, target_points)
Example #19
0
 def setUp(self):
     user_from = self._get_player(1)
     user_to = self._get_player(2)
     chall_user1 = user_from.get_extension(ChallengeUser)
     chall_user2 = user_to.get_extension(ChallengeUser)
     scoring.setup_scoring()
     self.chall = Challenge.create(user_from=chall_user1, user_to=chall_user2, ignore_questions=True)
Example #20
0
    def user_can_interact_with(self, user_from, user_to, game=None):
        if game is not None:
            game = str(game.__name__)

        if game == 'ChallengeGame':
            from datetime import datetime
            from math import ceil
            from wouso.interface.top.models import TopUser
            from wouso.games.challenge.models import Challenge

            lastch = Challenge.last_between(user_from, user_to)
            if lastch:
                elapsed_days = (datetime.now() - lastch.date).days
                position_diff = abs(
                    user_from.get_extension(TopUser).position -
                    user_to.get_extension(TopUser).position)
                rule = ceil(position_diff * 0.1)
                if rule > 7:
                    rule = 7  # nu bloca mai mult de 7 zile
                #print "AICI", user_from, user_to, lastch, elapsed_days,'days', rule,'rule'
                if rule <= elapsed_days:
                    return True
                return False

        return True
Example #21
0
    def test_custom_create(self):
        Challenge.WARRANTY = False
        game = ChallengeGame.get_instance()
        p1, p2 = self._get_player(1), self._get_player(2)

        challenge = Challenge.create_custom(p1, p2, [], game)
        self.assertTrue(challenge)
        self.assertEqual(challenge.owner, game)
Example #22
0
 def setUp(self):
     super(FlawlessVictoryTest, self).setUp()
     self.user_from = self._get_player(1)
     self.user_to   = self._get_player(2)
     self.chall_user1 = self.user_from.get_extension(ChallengeUser)
     self.chall_user2 = self.user_to.get_extension(ChallengeUser)
     scoring.setup_scoring()
     self.chall = Challenge.create(user_from=self.chall_user1, user_to=self.chall_user2, ignore_questions=True)
Example #23
0
    def test_custom_create(self):
        Challenge.WARRANTY = False
        game = ChallengeGame.get_instance()
        p1, p2 = self._get_player(1), self._get_player(2)

        challenge = Challenge.create_custom(p1, p2, [], game)
        self.assertTrue(challenge)
        self.assertEqual(challenge.owner, game)
Example #24
0
 def setUp(self):
     user_from = self._get_player(1)
     user_to = self._get_player(2)
     chall_user1 = user_from.get_extension(ChallengeUser)
     chall_user2 = user_to.get_extension(ChallengeUser)
     scoring.setup_scoring()
     self.chall = Challenge.create(user_from=chall_user1,
                                   user_to=chall_user2,
                                   ignore_questions=True)
Example #25
0
    def test_custom_create(self):
        game = ChallengeGame.get_instance()
        p1, p2 = self._get_player(1), self._get_player(2)

        challenge = Challenge.create_custom(p1, p2, [], game)
        self.assertTrue(challenge)
        self.assertEqual(challenge.owner, game)


# TODO: add page tests (views) for challenge run
Example #26
0
 def create(cls, user_from, user_to, round):
     """ Create a new Challenge and automatically accept it.
     """
     grand_challenge = cls.objects.create(round=round)
     user_from = user_from.user.get_profile()
     user_to = user_to.user.get_profile()
     grand_challenge.challenge = Challenge.create(user_from.get_extension(ChallengeUser), user_to.get_extension(ChallengeUser))
     grand_challenge.challenge.accept()
     grand_challenge.save()
     return grand_challenge
Example #27
0
 def setUp(self):
     super(FlawlessVictoryTest, self).setUp()
     self.user_from = self._get_player(1)
     self.user_to = self._get_player(2)
     self.chall_user1 = self.user_from.get_extension(ChallengeUser)
     self.chall_user2 = self.user_to.get_extension(ChallengeUser)
     scoring.setup_scoring()
     self.chall = Challenge.create(user_from=self.chall_user1,
                                   user_to=self.chall_user2,
                                   ignore_questions=True)
Example #28
0
    def test_get_challenge(self):
        # create an active challenge
        Formula.objects.create(id='chall-warranty')
        chall = Challenge.create(user_from=self.challuser2, user_to=self.challuser, ignore_questions=True)
        chall.accept()
        response = self.client.get('/api/challenge/{id}/'.format(id=chall.id))
        data = json.loads(response.content)

        self.assertTrue(data)
        self.assertEqual(data['status'], 'A')
        self.assertEqual(data['to'], self.challuser.user.username)
Example #29
0
    def test_get_challenge(self):
        # create an active challenge
        Formula.add('chall-warranty')
        Formula.add('chall-timer')
        chall = Challenge.create(user_from=self.challuser2, user_to=self.challuser, ignore_questions=True)
        chall.accept()
        response = self.client.get('/api/challenge/{id}/'.format(id=chall.id))
        data = json.loads(response.content)

        self.assertTrue(data)
        self.assertEqual(data['status'], 'A')
        self.assertEqual(data['to'], self.challuser.user.username)
Example #30
0
 def update_challenge(self):
     """
     Create real challenge if status is Active
     """
     if self.is_active():
         if self.real_challenge:
             return
         self.real_challenge = Challenge.create_custom(self.player_from, self.player_to, self.questions.all(), SpecialChallengeGame.get_instance())
         self.save()
         if self.real_challenge:
             self.status = STATUS_PLAYABLE
             self.save()
Example #31
0
 def update_challenge(self):
     """
     Create real challenge if status is Active
     """
     if self.is_active():
         if self.real_challenge:
             return
         self.real_challenge = Challenge.create_custom(self.player_from, self.player_to, self.questions.all(), SpecialChallengeGame.get_instance())
         self.save()
         if self.real_challenge:
             self.status = STATUS_PLAYABLE
             self.save()
Example #32
0
    def test_evade(self):
        """
         Test for Evade spell
        """
        player = self._get_player()
        player2 = self._get_player(2)

        initial_points = 10

        scoring.setup_scoring()
        Coin.add('points')
        scoring.score_simple(player, 'points', initial_points)
        self.assertEqual(player.points, initial_points)

        # Create and apply evade
        evade = Spell.objects.create(name='challenge-evade', available=True, price=25, percents=100, type='p')
        obs = PlayerSpellDue.objects.create(player=player, source=player, spell=evade, due=datetime.now() + timedelta(days=1))
        self.assertTrue(player.magic.has_modifier('challenge-evade'))

        # Get 'chall-lost' expression. By default you still win 2 points when losing a challenge
        formulas = ChallengeGame.get_formulas()
        exp = formulas[1]['expression'] # this will be 'points=XX'
        index = exp.find('=') + 1 # get position of '='
        points = int(exp[index:]) # get XX (nr of points won when losing challenge)

        # Create challenge and make first player lose it
        chall = Challenge.create(user_from=player2, user_to=player, ignore_questions=True)
        chall.set_won_by_player(player2)

        # If evade spell worked losing player should have initial_points + 'chall-lost' points

        # Evade has 20% chance of activation so play challenge in loop while it activates
        while player.points != initial_points + points:
            player.points = initial_points
            chall.set_expired()
            chall = Challenge.create(user_from=player2, user_to=player, ignore_questions=True)
            chall.set_won_by_player(player2)

        # Check if final score is ok
        self.assertEqual(player.points, initial_points + points)
Example #33
0
    def test_weakness_and_charge(self):
        """
         If both Weakness and Charge are active, a player should win weakness.percents + charge.percents less/more points
         after winning a challenge
        """
        initial_points = 100
        win_points = 10

        player = self._get_player(1).get_extension(ChallengeUser)
        player_dummy = self._get_player(2).get_extension(ChallengeUser)

        scoring.setup_scoring()
        Coin.add('points')
        scoring.score_simple(player, 'points', initial_points)

        formula = Formula.get('chall-won')
        formula.expression = 'points=' + str(win_points)
        formula.save()

        # Apply charge
        charge = Spell.objects.create(name='challenge-affect-scoring-won',
                                      available=True,
                                      price=10,
                                      percents=33,
                                      type='p')
        obs = PlayerSpellDue.objects.create(player=player,
                                            source=player,
                                            spell=charge,
                                            due=datetime.now() +
                                            timedelta(days=1))

        # Apply weakness
        weakness = Spell.objects.create(name='challenge-affect-scoring-won',
                                        available=True,
                                        price=10,
                                        percents=-66,
                                        type='p')
        obs = PlayerSpellDue.objects.create(player=player,
                                            source=player,
                                            spell=weakness,
                                            due=datetime.now() +
                                            timedelta(days=1))

        chall = Challenge.create(user_from=player,
                                 user_to=player_dummy,
                                 ignore_questions=True)
        chall.set_won_by_player(player)

        percents = (charge.percents + weakness.percents) / 100.0
        target_points = initial_points + win_points + percents * win_points

        self.assertEqual(player.player_ptr.points, target_points)
Example #34
0
    def test_cache_points(self):
        scoring.setup_scoring()
        Challenge.WARRANTY = True
        p1, p2 = self._get_player(1), self._get_player(2)

        initial_points = p1.points
        challenge = Challenge.create(p1, p2, ignore_questions=True)
        p1 = Player.objects.get(pk=p1.pk)
        self.assertNotEqual(p1.points, initial_points)
        self.assertTrue(challenge)
        challenge.refuse()
        p1 = p1.user.get_profile()
        self.assertEqual(p1.points, initial_points)
Example #35
0
    def test_cache_points(self):
        scoring.setup_scoring()
        Challenge.WARRANTY = True
        p1, p2 = self._get_player(1), self._get_player(2)

        initial_points = p1.points
        challenge = Challenge.create(p1, p2, ignore_questions=True)
        p1 = Player.objects.get(pk=p1.pk)
        self.assertNotEqual(p1.points, initial_points)
        self.assertTrue(challenge)
        challenge.refuse()
        p1 = p1.user.get_profile()
        self.assertEqual(p1.points, initial_points)
Example #36
0
    def test_run_expires(self):
        chall = Challenge.create(user_from=self.chall_user, user_to=self.chall_user2, ignore_questions=True)

        chall.accept()
        chall.set_start(self.chall_user)

        just_now = datetime.now()
        with patch('wouso.games.challenge.models.datetime') as mock_datetime:
            # after three minutes, challenge is still available
            mock_datetime.now.return_value = just_now + timedelta(minutes=5)
            self.assertTrue(chall.is_expired_for_user(self.chall_user))
            # pass some more time, challenge cannot be submited any more
            mock_datetime.now.return_value = just_now + timedelta(minutes=10)
            self.assertFalse(chall.check_timedelta(self.chall_user))
Example #37
0
    def test_run_expires(self):
        chall = Challenge.create(user_from=self.chall_user, user_to=self.chall_user2, ignore_questions=True)

        chall.accept()
        chall.set_start(self.chall_user)

        just_now = datetime.now()
        with patch('wouso.games.challenge.models.datetime') as mock_datetime:
            # after three minutes, challenge is still available
            mock_datetime.now.return_value = just_now + timedelta(minutes=5)
            self.assertTrue(chall.is_expired_for_user(self.chall_user))
            # pass some more time, challenge cannot be submited any more
            mock_datetime.now.return_value = just_now + timedelta(minutes=10)
            self.assertFalse(chall.check_timedelta(self.chall_user))
Example #38
0
    def testScoring(self):
        chall = Challenge.create(user_from=self.chall_user, user_to=self.chall_user2, ignore_questions=True)

        chall.user_from.seconds_took = 10
        chall.user_from.score = 100
        chall.user_from.save()
        chall.user_to.seconds_took = 10
        chall.user_to.score = 10
        chall.user_to.save()

        formula = Formula.objects.get(id='chall-won')
        formula.formula = 'points=10 + min(10, int(3 * {winner_points}/{loser_points}))'
        formula.save()
        chall.played()
Example #39
0
    def testScoring(self):
        chall = Challenge.create(user_from=self.chall_user, user_to=self.chall_user2, ignore_questions=True)

        chall.user_from.seconds_took = 10
        chall.user_from.score = 100
        chall.user_from.save()
        chall.user_to.seconds_took = 10
        chall.user_to.score = 10
        chall.user_to.save()

        # TODO: improve usage of formulas inside tests.
        formula = Formula.get('chall-won')
        formula.expression = 'points=10 + min(10, int(3 * {winner_points}/{loser_points}))'
        formula.save()
        chall.played()
Example #40
0
    def testScoring(self):
        chall = Challenge.create(user_from=self.chall_user, user_to=self.chall_user2, ignore_questions=True)

        chall.user_from.seconds_took = 10
        chall.user_from.score = 100
        chall.user_from.save()
        chall.user_to.seconds_took = 10
        chall.user_to.score = 10
        chall.user_to.save()

        # TODO: improve usage of formulas inside tests.
        formula = Formula.get('chall-won')
        formula.expression = 'points=10 + min(10, int(3 * {winner_points}/{loser_points}))'
        formula.save()
        chall.played()
Example #41
0
    def test_list_active(self):
        response = self.client.get('/api/challenge/list/')

        self.assertEqual(response.status_code, 200)

        data = json.loads(response.content)
        self.assertFalse(data)

        # create an active challenge
        Formula.add('chall-warranty')
        chall = Challenge.create(user_from=self.challuser2, user_to=self.challuser, ignore_questions=True)
        response = self.client.get('/api/challenge/list/')
        data = json.loads(response.content)

        self.assertTrue(data)
        data = data[0]
        self.assertEqual(data['id'], chall.id)
Example #42
0
    def test_list_active(self):
        response = self.client.get('/api/challenge/list/')

        self.assertEqual(response.status_code, 200)

        data = json.loads(response.content)
        self.assertFalse(data)

        # create an active challenge
        Formula.objects.create(id='chall-warranty')
        chall = Challenge.create(user_from=self.challuser2, user_to=self.challuser, ignore_questions=True)
        response = self.client.get('/api/challenge/list/')
        data = json.loads(response.content)

        self.assertTrue(data)
        data = data[0]
        self.assertEqual(data['id'], chall.id)
Example #43
0
    def test_frenzy_loss(self):
        """
         If user loses while affected by frenzy he should lose frenzy.percents more points
        """
        initial_points = 100
        loss_points = -10
        warranty_points = -3

        player_frenzy = self._get_player(1).get_extension(ChallengeUser)
        player_dummy = self._get_player(2).get_extension(ChallengeUser)

        scoring.setup_scoring()
        Coin.add('points')
        scoring.score_simple(player_frenzy, 'points', initial_points)

        formula = Formula.get('chall-lost')
        formula.expression = 'points=' + str(loss_points)
        formula.save()

        formula = Formula.get('chall-warranty')
        formula.expression = 'points=' + str(warranty_points)
        formula.save()

        # Apply frenzy
        frenzy = Spell.objects.create(name='challenge-affect-scoring',
                                      available=True,
                                      price=25,
                                      percents=66,
                                      type='o')
        obs = PlayerSpellDue.objects.create(player=player_frenzy,
                                            source=player_frenzy,
                                            spell=frenzy,
                                            due=datetime.now() +
                                            timedelta(days=1))

        # Win challenge with dummy player to see the amount of points lost by the player affected with frenzy
        chall = Challenge.create(user_from=player_frenzy,
                                 user_to=player_dummy,
                                 ignore_questions=True)
        chall.set_won_by_player(player_dummy)

        # Player should lose frenzy.percents more points with frenzy applied
        target_points = initial_points + loss_points + frenzy.percents / 100.0 * loss_points + warranty_points

        self.assertEqual(player_frenzy.player_ptr.points, target_points)
Example #44
0
 def __oldinit__(self, user_from, user_to):
     # TODO: change this constructor to a classmethod
     if not GrandChallengeGame.is_final() and not GrandChallengeGame.is_winner():
         self.branch = max(user_from.lost, user_to.lost)
     else:
         self.branch = min(user_from.lost, user_to.lost)
     self.user_from = user_from
     self.user_to = user_to
     self.__class__.ALL.append(self)
     self.won, self.lost = None, None
     self.active = True
     self.round_number = None
     challenge_user_to = user_to.user.get_profile().get_extension(ChallengeUser)
     challenge_user_from = user_from.user.get_profile().get_extension(ChallengeUser)
     chall = Challenge.create(challenge_user_from, challenge_user_to)
     chall.accept()
     self.challenge_id = chall.id
     self.__class__.CHALLENGES.append(chall.id)
Example #45
0
    def test_rule_challenge_was_set_up(self):

        #run a challenge
        chall = Challenge.create(user_from=self.chall_user,
                    user_to=self.chall_user2, ignore_questions=True)

        chall.user_from.seconds_took = 10
        chall.user_from.score = 100
        chall.user_from.save()
        chall.user_to.seconds_took = 10
        chall.user_to.score = 10
        chall.user_to.save()
        chall.played()

        #test penalty points
        #20 is the default formula value for a chall-was-set-up
        self.assertEqual(20,
                scoring.History.user_coins(self.user2.player_related.get())['penalty'])
Example #46
0
def user_profile(request, id, page=u'1'):
    profile = get_object_or_404(Player, id=id)

    activity_list = Activity.get_player_activity(profile)

    top_user = profile.get_extension(TopUser)
    history = History.user_points(profile.user)
    paginator = Paginator(activity_list, 10)

    try:
        activity = paginator.page(page)
    except (EmptyPage, InvalidPage):
        activity = paginator.page(paginator.num_pages)

        # some hackish introspection
        if hasattr(g, 'user_model'):
            model = getattr(g, 'user_model')
            setattr(profile, model.__name__.lower(),
                    profile.get_extension(model))
    # Fix to show succes message from report user form
    if 'report_msg' in request.session:
        message = request.session['report_msg']
        del request.session['report_msg']
    else:
        message = ''

    challenge_launched_recently = Challenge.exist_last_day(
        date.today(), request.user.get_profile(), profile)
    specialquest_button = SpecialQuestGame.get_specialquest_user_button(
        request, profile)
    config_disable_magic = BoolSetting.get(
        'setting-magic').get_value() is False

    return render_to_response('profile/profile.html', {
        'profile': profile,
        'activity': activity,
        'top': top_user,
        'scoring': history,
        'challenge_launched_recently': challenge_launched_recently,
        'specialquest_button': specialquest_button,
        'config_disable_magic': config_disable_magic,
        'message': message
    },
                              context_instance=RequestContext(request))
Example #47
0
    def test_rule_challenge_was_set_up(self):

        #run a challenge
        chall = Challenge.create(user_from=self.chall_user,
                                 user_to=self.chall_user2,
                                 ignore_questions=True)

        chall.user_from.seconds_took = 10
        chall.user_from.score = 100
        chall.user_from.save()
        chall.user_to.seconds_took = 10
        chall.user_to.score = 10
        chall.user_to.save()
        chall.played()

        #test penalty points
        #20 is the default formula value for a chall-was-set-up
        self.assertEqual(
            20,
            scoring.History.user_coins(
                self.user2.player_related.get())['penalty'])
Example #48
0
def user_profile(request, id, page=u'1'):
    profile = get_object_or_404(Player, id=id)

    activity_list = Activity.get_player_activity(profile)

    top_user = profile.get_extension(TopUser)
    history = History.user_points(profile.user)
    paginator = Paginator(activity_list, 10)

    try:
        activity = paginator.page(page)
    except (EmptyPage, InvalidPage):
        activity = paginator.page(paginator.num_pages)

        # some hackish introspection
        if hasattr(g, 'user_model'):
            model = getattr(g, 'user_model')
            setattr(profile, model.__name__.lower(), profile.get_extension(model))
    #Fix to show succes message from report user form
    if 'report_msg' in request.session:
        message = request.session['report_msg']
        del request.session['report_msg']
    else:
        message=''

    challenge_launched_recently = Challenge.exist_last_day(date.today(),
                                        request.user.get_profile(), profile)
    specialquest_button = SpecialQuestGame.get_specialquest_user_button(request, profile)
    config_disable_magic = BoolSetting.get('setting-magic').get_value() is False

    return render_to_response('profile/profile.html',
                              {'profile': profile,
                               'activity': activity,
                               'top': top_user,
                               'scoring': history,
                               'challenge_launched_recently': challenge_launched_recently,
                               'specialquest_button': specialquest_button,
                               'config_disable_magic': config_disable_magic,
                               'message': message},
                              context_instance=RequestContext(request))
Example #49
0
    def test_weakness(self):
        """
         Test for Weakness spell
        """
        initial_points = 100
        win_points = 10

        player_weakness = self._get_player(1).get_extension(ChallengeUser)
        player_dummy = self._get_player(2).get_extension(ChallengeUser)

        scoring.setup_scoring()
        Coin.add('points')
        scoring.score_simple(player_weakness, 'points', initial_points)

        formula = Formula.get('chall-won')
        formula.expression = 'points=' + str(win_points)
        formula.save()

        # Apply weakness
        weakness = Spell.objects.create(name='challenge-affect-scoring-lost',
                                        available=True,
                                        price=10,
                                        percents=-66,
                                        type='n')
        obs = PlayerSpellDue.objects.create(player=player_weakness,
                                            source=player_weakness,
                                            spell=weakness,
                                            due=datetime.now() +
                                            timedelta(days=1))

        # Win challenge with player_weakness
        chall = Challenge.create(user_from=player_weakness,
                                 user_to=player_dummy,
                                 ignore_questions=True)
        chall.set_won_by_player(player_weakness)

        # Player should win weakness.percents less points with weakness applied
        target_points = initial_points + win_points + weakness.percents / 100.0 * win_points
        self.assertEqual(player_weakness.player_ptr.points, target_points)
Example #50
0
    def user_can_interact_with(self, player_from, player_to, game=None):
        if game is not None:
            game = str(game.__name__)

        if game == 'ChallengeGame':
            from datetime import datetime
            from math import ceil
            from wouso.interface.top.models import TopUser
            from wouso.games.challenge.models import Challenge

            lastch = Challenge.last_between(player_from, player_to)
            if lastch:
                elapsed_days = (datetime.now() - lastch.date).days
                position_diff = abs(player_from.get_extension(TopUser).position - player_to.get_extension(TopUser).position)
                rule = ceil(position_diff * 0.1)
                if rule > 7:
                    rule = 7 # nu bloca mai mult de 7 zile
                if rule <= elapsed_days:
                    return True
                return False

        return True
Example #51
0
    def test_scoring_loss(self):
        initial_points = 10
        winner = self._get_player(1).get_extension(ChallengeUser)
        loser = self._get_player(2).get_extension(ChallengeUser)

        # Setup scoring
        scoring.setup_scoring()
        Coin.add('points')

        # Give initial points
        scoring.score_simple(winner, 'points', initial_points)
        scoring.score_simple(loser, 'points', initial_points)

        # Set formula
        loss_points = -2
        formula = Formula.get('chall-lost')
        formula.expression = 'points=' + str(loss_points)
        formula.save()

        # Play challenge
        chall = Challenge.create(user_from=winner, user_to=loser, ignore_questions=True)
        chall.set_won_by_player(winner)

        self.assertEqual(loser.player_ptr.points, initial_points + loss_points) # loss_points is negative
Example #52
0
 def test_all_answers(self):
     q = self.get_question(4, 3)
     post = self.fake_answers(q, 4, 3)
     self.assertEqual(Challenge._calculate_points(post)['points'], 0)
Example #53
0
 def test_no_correct_and_partial_wrong(self):
     q = self.get_question(4, 3)
     post = self.fake_answers(q, 0, 2)
     self.assertEqual(Challenge._calculate_points(post)['points'], 0)
Example #54
0
 def test_all_answers(self):
     q = self.get_question(4, 3)
     post = self.fake_answers(q, 4, 3)
     self.assertEqual(Challenge._calculate_points(post)['points'], 0)
Example #55
0
    def test_run_accept(self):
        chall = Challenge.create(user_from=self.chall_user, user_to=self.chall_user2, ignore_questions=True)

        chall.accept()
        self.assertTrue(chall.is_runnable())