def test_disguise_expire_on_dispell(self): player = self._get_player() Coin.add('points') scoring.score_simple(player, 'points', 10) disguise = Spell.objects.create(name='top-disguise', available=True, price=10, percents=50, type='s') player.magic.add_spell(disguise) player.magic.cast_spell(disguise, player, datetime.now() + timedelta(days=1)) self.assertEqual(player.points, 15) dispell = Spell.objects.create(name='dispell', available=True, price=10) player.magic.add_spell(dispell) player.magic.cast_spell(dispell, player) self.assertFalse(player.magic.has_modifier('top-disguise')) player = Player.objects.get(pk=player.pk) self.assertEqual(player.points, 10)
def test_gold_amount(self): player = self._get_player() coin = Coin.add('gold') scoring.score_simple(player, coin, amount=100) self.assertEqual(gold_amount(player), 100)
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)
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)
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)
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
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))
def test_not_in_same_division(self): n = 100 points_offset = 10000 division_range = 20 players = [self._get_player(i).get_extension(ChallengeUser) for i in xrange(n)] # Add an offset value to every user's points in order to avoid top overlapping with other test users. for i in xrange(n): scoring.score_simple(players[i], 'points', points_offset + i*10) # Update players top. for i, u in enumerate(Player.objects.all().order_by('-points')): topuser = u.get_extension(TopUser) position = i + 1 hs, created = History.objects.get_or_create(user=topuser, date=datetime.now().date(), relative_to=None) hs.position, hs.points = position, u.points hs.save() # Check first player. self.assertFalse(players[n-1].in_same_division(players[max(0, n-1-division_range-1)])) # Check last player. self.assertFalse(players[0].in_same_division(players[min(n-1, division_range+1)])) # Check middle player. t = n / 2 self.assertFalse(players[t].in_same_division(players[max(0, t-division_range-1)])) self.assertFalse(players[t].in_same_division(players[min(n-1, t+division_range+1)]))
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))
def test_user_points(self): coin = Coin.add('points') player = self._get_player() scoring.score_simple(player, 'points', 10) up = History.user_points(user=player.user) self.assertTrue('wouso' in up) self.assertTrue(coin.name in up['wouso']) self.assertEqual(up['wouso'][coin.name], 10)
def test_user_points(self): coin = Coin.add('points') player = self._get_player() scoring.score_simple(player, 'points', 10) up = History.user_points(player.user) self.assertTrue(up.has_key('wouso')) self.assertTrue(up['wouso'].has_key(coin)) self.assertEqual(up['wouso'][coin], 10)
def test_user_points(self): coin = Coin.add("points") player = self._get_player() scoring.score_simple(player, "points", 10) up = History.user_points(player.user) self.assertTrue(up.has_key("wouso")) self.assertTrue(up["wouso"].has_key(coin)) self.assertEqual(up["wouso"][coin], 10)
def test_gold_amount_ach(self): Artifact.objects.create(group=None, name='ach-gold-300') player = self._get_player() coin = Coin.add('gold') scoring.score_simple(player, coin, amount=500) signals.addActivity.send(sender=None, user_from=player, user_to=player, action='gold-won', game=None) self.assertTrue(player.magic.has_modifier('ach-gold-300'))
def testScoreSimple(self): scoring.score_simple(self.user.get_profile(), self.coin, game=self.game, external_id=2, amount=10) multiple = scoring.history_for(self.user, self.game, external_id=2) self.assertTrue(isinstance(multiple, QuerySet)) self.assertEqual(len(multiple), 1) history = list(multiple)[0] self.assertTrue(isinstance(history, History)) self.assertEqual(history.amount, 10)
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)
def test_disguise_simple(self): """ Test if top-disguise spell works """ player = self._get_player() Coin.add('points') scoring.score_simple(player, 'points', 10) self.assertEqual(player.points, 10) disguise = Spell.objects.create(name='top-disguise', available=True, price=10, percents=50, type='s') player.magic.add_spell(disguise) player.magic.cast_spell(disguise, player, datetime.now() + timedelta(days=1)) self.assertTrue(player.magic.has_modifier('top-disguise')) self.assertEqual(player.points, 15)
def test_buy_spell(self): Coin.add('gold') Formula.add('buy-spell', expression="gold=-{price}") spell = Spell.objects.create(name='test-spell', available=True, price=10) player = User.objects.create_user('test', '*****@*****.**', password='******').get_profile() scoring.score_simple(player, 'gold', 100) self.assertEqual(player.coins['gold'], 100) response = self.client.get(reverse('bazaar_home')) self.assertTrue('test-spell' in response.content) self.client.login(username='******', password='******') response = self.client.get(reverse('bazaar_buy', kwargs={'spell': spell.id})) self.assertFalse('error' in response.content) player = Player.objects.get(user__username='******') self.assertEqual(player.coins['gold'], 90)
def test_buy_spell(self): Coin.add("gold") Formula.add("buy-spell", definition="gold=-{price}") spell = Spell.objects.create(name="test-spell", available=True, price=10) player = User.objects.create_user("test", "*****@*****.**", password="******").get_profile() scoring.score_simple(player, "gold", 100) self.assertEqual(player.coins["gold"], 100) # TODO: interface test should not be here response = self.client.get(reverse("bazaar_home")) self.assertTrue("test-spell" in response.content) self.client.login(username="******", password="******") response = self.client.get(reverse("bazaar_buy", kwargs={"spell": spell.id})) self.assertFalse("error" in response.content) player = Player.objects.get(user__username="******") self.assertEqual(player.coins["gold"], 90)
def test_buy_spell(self): Coin.add('gold') Formula.add('buy-spell', expression="gold=-{price}") spell = Spell.objects.create(name='test-spell', available=True, price=10) player = User.objects.create_user('test', '*****@*****.**', password='******').get_profile() scoring.score_simple(player, 'gold', 100) self.assertEqual(player.coins['gold'], 100) # TODO: interface test should not be here response = self.client.get(reverse('bazaar_home')) self.assertTrue('test-spell' in response.content) self.client.login(username='******', password='******') response = self.client.get(reverse('bazaar_buy', kwargs={'spell': spell.id})) self.assertFalse('error' in response.content) player = Player.objects.get(user__username='******') self.assertEqual(player.coins['gold'], 90)
def test_in_same_division(self): n = 100 points_offset = 10000 division_range = 20 players = [ self._get_player(i).get_extension(ChallengeUser) for i in xrange(n) ] # Add an offset value to every user's points in order to avoid top overlapping with other test users. for i in xrange(n): scoring.score_simple(players[i], 'points', points_offset + i * 10) # Update players top. for i, u in enumerate(Player.objects.all().order_by('-points')): topuser = u.get_extension(TopUser) position = i + 1 hs, created = History.objects.get_or_create( user=topuser, date=datetime.now().date(), relative_to=None) hs.position, hs.points = position, u.points hs.save() # Check first player. self.assertTrue(players[n - 1].in_same_division(players[n - 2])) self.assertTrue(players[n - 1].in_same_division(players[max( 0, n - 1 - division_range)])) # Check last player. self.assertTrue(players[0].in_same_division(players[1])) self.assertTrue(players[0].in_same_division(players[min( n - 1, division_range)])) # Check middle player. t = n / 2 self.assertTrue(players[t].in_same_division(players[max( 0, t - division_range)])) self.assertTrue(players[t].in_same_division(players[min( n - 1, t + division_range)])) self.assertTrue(players[t].in_same_division(players[t - 1])) self.assertTrue(players[t].in_same_division(players[t + 1]))
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)
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)
def test_user_coins(self): Coin.add('points') Coin.add('gold') player = self._get_player() scoring.score_simple(player, 'points', 10) self.assertIn('points', History.user_coins(player.user))