def test_damage_queue_poison(self): self.assertEqual(self.context.poison_damage, None) self.context.use_damage_queue_poison([Damage(0, 0)]) self.assertEqual(self.context.poison_damage, None) self.context.use_damage_queue_poison([Damage(5, 5), Damage(5, 5), Damage(5, 5)]) self.assertEqual(self.context.damage_queue_poison, [Damage(0, 0), Damage(5, 5), Damage(5, 5), Damage(5, 5)]) self.context.on_own_turn() self.assertEqual(self.context.damage_queue_poison, [Damage(5, 5), Damage(5, 5), Damage(5, 5)]) self.assertEqual(self.context.poison_damage, Damage(5, 5)) self.context.use_damage_queue_poison([Damage(10, 10), Damage(5, 5), Damage(5, 5)]) self.context.on_own_turn() self.assertEqual(self.context.damage_queue_poison, [Damage(15, 15), Damage(10, 10), Damage(5, 5)]) self.assertEqual(self.context.poison_damage, Damage(15, 15)) self.context.on_own_turn() self.assertEqual(self.context.damage_queue_poison, [Damage(10, 10), Damage(5, 5)]) self.assertEqual(self.context.poison_damage, Damage(10, 10)) self.context.use_damage_queue_poison([Damage(1, 1), Damage(1, 1), Damage(1, 1), Damage(1, 1)]) self.assertEqual(self.context.damage_queue_poison, [Damage(10, 10), Damage(6, 6), Damage(1, 1), Damage(1, 1), Damage(1, 1)]) self.context.on_own_turn() self.assertEqual(self.context.damage_queue_poison, [Damage(6, 6), Damage(1, 1), Damage(1, 1), Damage(1, 1)]) self.assertEqual(self.context.poison_damage, Damage(6, 6)) self.assertEqual(self.context.serialize(), BattleContext.deserialize(self.context.serialize()).serialize())
def test_choose_ability__additional_companion_abilities(self): from the_tale.game.heroes.habilities import ABILITIES from the_tale.game.companions.abilities import effects as companions_effects from the_tale.game.companions.abilities import container as abilities_container from the_tale.game.companions import logic as companions_logic from the_tale.game.companions import relations as companions_relations abilities = [ ability for ability in companions_effects.ABILITIES.records if (isinstance(ability.effect, companions_effects.BaseBattleAbility ) and ability.effect.ABILITY.get_id() != 'hit') ] companion_ability = random.choice(abilities) all_abilities = [ ability(level=ability.MAX_LEVEL) for ability in list(ABILITIES.values()) if ability.get_id() != companion_ability.effect.ABILITY.get_id() ] active_abilities = set(ability.get_id() for ability in list(ABILITIES.values()) if ability.ACTIVATION_TYPE.is_ACTIVE) companion_record = companions_logic.create_random_companion_record( 'battle', abilities=abilities_container.Container( start=(companion_ability, )), state=companions_relations.STATE.ENABLED) self.hero.set_companion( companions_logic.create_companion(companion_record)) self.hero.health = 1 # allow regeneration actor = battle.Actor(self.hero, BattleContext()) chosen_abilities = set() # mock abilities modify_attribute instead of hereos, since we test correct work of it def modify_attribute(self, modifier, value): if modifier.is_ADDITIONAL_ABILITIES: return all_abilities return value with mock.patch( 'the_tale.game.heroes.habilities.AbilitiesPrototype.modify_attribute', modify_attribute): for i in range(1000): chosen_abilities.add(actor.choose_ability().get_id()) self.assertEqual(len(active_abilities), len(chosen_abilities) + 1) self.assertEqual( active_abilities - set([companion_ability.effect.ABILITY.get_id()]), chosen_abilities)
def test_process_effects(self): actor = battle.Actor(self.hero, BattleContext()) actor.context.use_damage_queue_fire([p.Damage(50, 50), p.Damage(50, 50)]) actor.context.use_damage_queue_poison([p.Damage(50, 50), p.Damage(50, 50)]) actor.context.on_own_turn() actor.context.use_incoming_damage_modifier(physic=1.0, magic=0.8) actor.process_effects(self.hero) self.assertEqual(self.hero.health, self.hero.max_health - 180) actor.context.on_own_turn() actor.context.use_incoming_damage_modifier(physic=1.2, magic=1.0) actor.process_effects(self.hero) self.assertEqual(self.hero.health, self.hero.max_health - 180 - 220)
def test_choose_ability__additional_abilities(self): from the_tale.game.heroes.habilities import ABILITIES all_abilities = [ability(level=ability.MAX_LEVEL) for ability in ABILITIES.values()] active_abilities = set(ability.get_id() for ability in all_abilities if ability.activation_type.is_ACTIVE) self.hero.health = 1 # allow regeneration actor = battle.Actor(self.hero, BattleContext()) chosen_abilities = set() with mock.patch('the_tale.game.heroes.objects.Hero.additional_abilities', all_abilities): for i in xrange(1000): chosen_abilities.add(actor.choose_ability().get_id()) self.assertEqual(active_abilities, chosen_abilities)
def test_modify_outcoming_damage(self): self.assertEqual(self.context.modify_outcoming_damage(Damage(10, 11)).total, 21) self.assertEqual(self.context.modify_outcoming_damage(Damage(10.4, 11.4)).total, 22) self.assertEqual(self.context.modify_outcoming_damage(Damage(10.5, 11.5)).total, 22) self.assertEqual(self.context.modify_outcoming_damage(Damage(10.8, 11.8)).total, 23) # advantage_modifier self.context.use_pvp_advantage(0.75) self.assertEqual(self.context.modify_outcoming_damage(Damage(20, 10)), Damage(20*(1+c.DAMAGE_PVP_ADVANTAGE_MODIFIER*0.75), 10*(1+c.DAMAGE_PVP_ADVANTAGE_MODIFIER*0.75))) self.assertFalse(self.context.pvp_advantage_used) self.context.use_pvp_advantage(-0.75) self.assertEqual(self.context.modify_outcoming_damage(Damage(20, 10)), Damage(20, 10)) self.assertFalse(self.context.pvp_advantage_used) self.assertEqual(self.context.serialize(), BattleContext.deserialize(self.context.serialize()).serialize())
def test_mob_actor(self): mob = mobs_storage.get_random_mob(self.hero) mob.health = 10 mob.abilities.add(RUN_UP_PUSH.get_id()) actor = battle.Actor(mob, BattleContext()) self.assertEqual(mob.initiative, actor.initiative) self.assertEqual(mob.name, actor.name) self.assertEqual(mob.utg_name, actor.utg_name) self.assertEqual(mob.basic_damage, actor.basic_damage) self.assertEqual(mob.health, actor.health) self.assertEqual(mob.max_health, actor.max_health) self.assertEqual(actor.change_health(-5), -5) self.assertEqual(actor.health, 5) self.assertEqual(actor.change_health(-50), -5) self.assertEqual(actor.health, 0) self.assertEqual(actor.change_health(actor.max_health + 50), actor.max_health) self.assertEqual(actor.health, actor.max_health) hit_selected = False run_up_push_selected = False vampire_strike_selected = False for i in range(100): ability = actor.choose_ability() if ability.get_id() == HIT.get_id(): hit_selected = True elif ability.get_id() == RUN_UP_PUSH.get_id(): run_up_push_selected = True elif ability.get_id() == VAMPIRE_STRIKE.get_id(): vampire_strike_selected = True self.assertTrue(hit_selected) self.assertTrue(run_up_push_selected) self.assertTrue(vampire_strike_selected) self.storage._test_save()
def get_actors(self): mob = mobs_storage.get_random_mob(self.hero) actor_1 = battle.Actor(self.hero, BattleContext()) actor_2 = battle.Actor(mob, BattleContext()) return actor_1, actor_2
def test_initiative_change(self): actor = battle.Actor(self.hero, BattleContext()) actor.context.use_initiative([2]) self.assertEqual(actor.initiative, self.hero.initiative*2)
class BattleContextTest(testcase.TestCase): def setUp(self): super(BattleContextTest, self).setUp() self.context = BattleContext() def tearDown(self): pass def check_empty_values(self): self.assertEqual(self.context.ability_magic_mushroom, []) self.assertEqual(self.context.ability_sidestep, []) self.assertEqual(self.context.stun_length, 0) self.assertEqual(self.context.crit_chance, 0) self.assertEqual(self.context.berserk_damage_modifier, 1.0) self.assertEqual(self.context.ninja, 0) self.assertEqual(self.context.damage_queue_fire, []) self.assertEqual(self.context.damage_queue_poison, []) self.assertEqual(self.context.initiative_queue, []) self.assertEqual(self.context.incoming_magic_damage_modifier, 1.0) self.assertEqual(self.context.incoming_physic_damage_modifier, 1.0) self.assertEqual(self.context.outcoming_magic_damage_modifier, 1.0) self.assertEqual(self.context.outcoming_physic_damage_modifier, 1.0) self.assertEqual(self.context.pvp_advantage, 0) self.assertFalse(self.context.pvp_advantage_used) self.assertEqual(self.context.pvp_advantage_strike_damage, Damage(0, 0)) def test_create(self): self.check_empty_values() @mock.patch('the_tale.game.balance.constants.DAMAGE_DELTA', 0) def test_damage(self): damage = Damage(100, 50) damage.multiply(0.5, 2) self.assertEqual(damage, Damage(50, 100)) damage = Damage(100.5, 50.5) damage.randomize() self.assertEqual(damage.total, 151) def test_damage_queue_fire(self): self.assertEqual(self.context.fire_damage, None) self.context.use_damage_queue_fire([Damage(0, 0)]) self.assertEqual(self.context.fire_damage, None) self.context.use_damage_queue_fire([Damage(5, 5), Damage(5, 5), Damage(5, 5)]) self.assertEqual(self.context.damage_queue_fire, [Damage(0, 0), Damage(5, 5), Damage(5, 5), Damage(5, 5)]) self.context.on_own_turn() self.assertEqual(self.context.damage_queue_fire, [Damage(5, 5), Damage(5, 5), Damage(5, 5)]) self.assertEqual(self.context.fire_damage, Damage(5, 5)) self.context.use_damage_queue_fire([Damage(10, 10), Damage(5, 5)]) self.assertEqual(self.context.damage_queue_fire, [Damage(5, 5), Damage(15, 15), Damage(10, 10)]) self.context.on_own_turn() self.assertEqual(self.context.fire_damage, Damage(15, 15)) self.context.on_own_turn() self.assertEqual(self.context.damage_queue_fire, [Damage(10, 10)]) self.assertEqual(self.context.fire_damage, Damage(10, 10)) self.context.use_damage_queue_fire([Damage(1, 1), Damage(1, 1), Damage(1, 1), Damage(1, 1)]) self.assertEqual(self.context.damage_queue_fire, [Damage(10, 10), Damage(1, 1), Damage(1, 1), Damage(1, 1), Damage(1, 1)]) self.assertEqual(self.context.fire_damage, Damage(10, 10)) self.context.on_own_turn() self.assertEqual(self.context.fire_damage, Damage(1, 1)) self.assertEqual(self.context.serialize(), BattleContext.deserialize(self.context.serialize()).serialize()) def test_damage_queue_poison(self): self.assertEqual(self.context.poison_damage, None) self.context.use_damage_queue_poison([Damage(0, 0)]) self.assertEqual(self.context.poison_damage, None) self.context.use_damage_queue_poison([Damage(5, 5), Damage(5, 5), Damage(5, 5)]) self.assertEqual(self.context.damage_queue_poison, [Damage(0, 0), Damage(5, 5), Damage(5, 5), Damage(5, 5)]) self.context.on_own_turn() self.assertEqual(self.context.damage_queue_poison, [Damage(5, 5), Damage(5, 5), Damage(5, 5)]) self.assertEqual(self.context.poison_damage, Damage(5, 5)) self.context.use_damage_queue_poison([Damage(10, 10), Damage(5, 5), Damage(5, 5)]) self.context.on_own_turn() self.assertEqual(self.context.damage_queue_poison, [Damage(15, 15), Damage(10, 10), Damage(5, 5)]) self.assertEqual(self.context.poison_damage, Damage(15, 15)) self.context.on_own_turn() self.assertEqual(self.context.damage_queue_poison, [Damage(10, 10), Damage(5, 5)]) self.assertEqual(self.context.poison_damage, Damage(10, 10)) self.context.use_damage_queue_poison([Damage(1, 1), Damage(1, 1), Damage(1, 1), Damage(1, 1)]) self.assertEqual(self.context.damage_queue_poison, [Damage(10, 10), Damage(6, 6), Damage(1, 1), Damage(1, 1), Damage(1, 1)]) self.context.on_own_turn() self.assertEqual(self.context.damage_queue_poison, [Damage(6, 6), Damage(1, 1), Damage(1, 1), Damage(1, 1)]) self.assertEqual(self.context.poison_damage, Damage(6, 6)) self.assertEqual(self.context.serialize(), BattleContext.deserialize(self.context.serialize()).serialize()) def test_initiative_queue(self): self.assertEqual(self.context.initiative, 1.0) self.context.use_initiative([90, 90, 90, 90]) self.context.on_own_turn() self.assertEqual(self.context.initiative_queue, [90, 90, 90]) self.assertEqual(self.context.initiative, 90) self.context.use_initiative([11, 9]) self.context.on_own_turn() self.assertEqual(self.context.initiative_queue, [810, 90]) self.assertEqual(self.context.initiative, 810) self.context.on_own_turn() self.assertEqual(self.context.initiative_queue, [90]) self.assertEqual(self.context.initiative, 90) self.context.use_initiative([10, 10, 10, 10]) self.assertEqual(self.context.initiative_queue, [900, 10, 10, 10]) self.context.on_own_turn() self.assertEqual(self.context.initiative, 10) @mock.patch('the_tale.game.balance.constants.DAMAGE_DELTA', 0) def test_ability_magic_mushroom(self): self.context.use_ability_magic_mushroom([2.0, 1.0, 0.5]) self.assertEqual(self.context.ability_magic_mushroom, [None, 2.0, 1.0, 0.5]) self.context.on_own_turn() self.assertEqual(self.context.ability_magic_mushroom, [2.0, 1.0, 0.5]) self.assertEqual(self.context.modify_outcoming_damage(Damage(10, 5)), Damage(20, 10)) self.context.on_own_turn() self.assertEqual(self.context.ability_magic_mushroom, [1.0, 0.5]) self.assertEqual(self.context.modify_outcoming_damage(Damage(10, 5)), Damage(10, 5)) self.context.on_own_turn() self.assertEqual(self.context.ability_magic_mushroom, [0.5]) self.assertEqual(self.context.modify_outcoming_damage(Damage(10, 5)).total, 8) self.context.on_own_turn() self.assertEqual(self.context.ability_magic_mushroom, []) self.assertEqual(self.context.modify_outcoming_damage(Damage(10, 5)), Damage(10, 5)) def test_ability_sidestep(self): self.context.use_ability_sidestep([1.0, 0.5, 0.0]) self.assertEqual(self.context.ability_sidestep, [None, 1.0, 0.5, 0]) self.context.on_own_turn() self.assertEqual(self.context.ability_sidestep, [1.0, 0.5, 0]) self.assertTrue(self.context.should_miss_attack()) self.context.on_own_turn() self.assertEqual(self.context.ability_sidestep, [0.5, 0]) self.assertTrue(self.context.should_miss_attack() in [True, False]) self.context.on_own_turn() self.assertEqual(self.context.ability_sidestep, [0]) self.assertTrue(not self.context.should_miss_attack()) self.context.on_own_turn() self.assertEqual(self.context.ability_sidestep, []) self.assertTrue(not self.context.should_miss_attack()) def test_stun(self): self.context.use_stun(3) self.assertEqual(self.context.stun_length, 4) # longes stun must be used self.context.use_stun(1) self.assertEqual(self.context.stun_length, 4) for i in xrange(3): self.context.on_own_turn() self.assertEqual(self.context.stun_length, 3-i) self.assertTrue(self.context.is_stunned) self.context.on_own_turn() self.assertEqual(self.context.stun_length, 0) self.assertTrue(not self.context.is_stunned) @mock.patch('the_tale.game.balance.constants.DAMAGE_DELTA', 0) def test_modify_outcoming_damage(self): self.assertEqual(self.context.modify_outcoming_damage(Damage(10, 11)).total, 21) self.assertEqual(self.context.modify_outcoming_damage(Damage(10.4, 11.4)).total, 22) self.assertEqual(self.context.modify_outcoming_damage(Damage(10.5, 11.5)).total, 22) self.assertEqual(self.context.modify_outcoming_damage(Damage(10.8, 11.8)).total, 23) # advantage_modifier self.context.use_pvp_advantage(0.75) self.assertEqual(self.context.modify_outcoming_damage(Damage(20, 10)), Damage(20*(1+c.DAMAGE_PVP_ADVANTAGE_MODIFIER*0.75), 10*(1+c.DAMAGE_PVP_ADVANTAGE_MODIFIER*0.75))) self.assertFalse(self.context.pvp_advantage_used) self.context.use_pvp_advantage(-0.75) self.assertEqual(self.context.modify_outcoming_damage(Damage(20, 10)), Damage(20, 10)) self.assertFalse(self.context.pvp_advantage_used) self.assertEqual(self.context.serialize(), BattleContext.deserialize(self.context.serialize()).serialize()) @mock.patch('the_tale.game.balance.constants.DAMAGE_DELTA', 0) def test_modify_outcoming_damage_advantage_strike(self): self.context.use_pvp_advantage(1.0) self.assertEqual(self.context.modify_outcoming_damage(Damage(20, 10)), Damage(0, 0)) # pvp_advantage_strike_damage not set self.context.use_pvp_advantage_stike_damage(Damage(333, 666)) self.assertEqual(self.context.modify_outcoming_damage(Damage(20, 10)), Damage(333, 666)) self.assertTrue(self.context.pvp_advantage_used) @mock.patch('the_tale.game.balance.constants.DAMAGE_DELTA', 0) def test_critical_hit(self): old_damage = self.context.modify_outcoming_damage(Damage(100, 1000)) self.context.use_crit_chance(100) new_damage = self.context.modify_outcoming_damage(Damage(100, 1000)) self.assertTrue(old_damage.physic < new_damage.physic) self.assertTrue(old_damage.magic < new_damage.magic) @mock.patch('the_tale.game.balance.constants.DAMAGE_DELTA', 0) def test_berserk(self): old_damage = self.context.modify_outcoming_damage(Damage(100, 10)) self.context.use_berserk(1.0) self.assertEqual(old_damage, self.context.modify_outcoming_damage(Damage(100, 10))) self.context.use_berserk(1.5) new_damage = self.context.modify_outcoming_damage(Damage(100, 10)) self.assertTrue(old_damage.physic < new_damage.physic) self.assertTrue(old_damage.magic < new_damage.magic) def test_ninja(self): self.context.use_ninja(1.0) for i in xrange(100): self.assertTrue(self.context.should_miss_attack()) @mock.patch('the_tale.game.balance.constants.DAMAGE_DELTA', 0) def test_outcoming_damage_modifier(self): self.assertEqual(self.context.modify_outcoming_damage(Damage(100, 1000)), Damage(100, 1000)) self.context.use_outcoming_damage_modifier(5, 0.25) self.assertEqual(self.context.modify_outcoming_damage(Damage(100, 1000)), Damage(500, 250)) self.context.use_outcoming_damage_modifier(2, 10) self.assertEqual(self.context.modify_outcoming_damage(Damage(100, 1000)), Damage(1000, 2500)) def test_incoming_damage_modifier(self): self.assertEqual(self.context.modify_incoming_damage(Damage(100, 1000)), Damage(100, 1000)) self.context.use_incoming_damage_modifier(5, 0.25) self.assertEqual(self.context.modify_incoming_damage(Damage(100, 1000)), Damage(500, 250)) self.context.use_incoming_damage_modifier(2, 10) self.assertEqual(self.context.modify_incoming_damage(Damage(100, 1000)), Damage(1000, 2500)) def test_on_own_turn_with_empty_values(self): self.context.on_own_turn() self.check_empty_values()
def setUp(self): super(BattleContextTest, self).setUp() self.context = BattleContext()