class LongTeleportTests(CardsTestMixin, testcase.TestCase): CARD = effects.LongTeleport def setUp(self): super(LongTeleportTests, self).setUp() self.place_1, self.place_2, self.place_3 = create_test_map() result, account_1_id, bundle_id = register_user("test_user", "*****@*****.**", "111111") self.account_1 = AccountPrototype.get_by_id(account_1_id) self.storage = LogicStorage() self.storage.load_account_data(self.account_1) self.hero = self.storage.accounts_to_heroes[self.account_1.id] self.hero.position.set_place(self.place_1) self.card = self.CARD() @mock.patch("the_tale.game.heroes.objects.Hero.is_battle_start_needed", lambda self: False) def test_moving(self): self.assertFalse(self.hero.actions.current_action.TYPE.is_MOVE_TO) result, step, postsave_actions = self.card.use(**self.use_attributes(storage=self.storage, hero=self.hero)) self.assertEqual( (result, step, postsave_actions), (ComplexChangeTask.RESULT.FAILED, ComplexChangeTask.STEP.ERROR, ()) ) @mock.patch("the_tale.game.heroes.objects.Hero.is_battle_start_needed", lambda self: False) def test_use(self): actions_prototypes.ActionMoveToPrototype.create(hero=self.hero, destination=self.place_3) self.storage.process_turn(continue_steps_if_needed=False) self.assertTrue(self.hero.actions.current_action.state == actions_prototypes.ActionMoveToPrototype.STATE.MOVING) self.assertTrue(self.hero.position.percents < 1) result, step, postsave_actions = self.card.use(**self.use_attributes(storage=self.storage, hero=self.hero)) self.assertEqual( (result, step, postsave_actions), (ComplexChangeTask.RESULT.SUCCESSED, ComplexChangeTask.STEP.SUCCESS, ()) ) self.assertTrue(self.hero.position.place.id, self.place_3.id) @mock.patch("the_tale.game.heroes.objects.Hero.is_battle_start_needed", lambda self: False) def test_use__wrong_state(self): actions_prototypes.ActionMoveToPrototype.create(hero=self.hero, destination=self.place_3) self.assertTrue(self.hero.actions.current_action.state != actions_prototypes.ActionMoveToPrototype.STATE.MOVING) with self.check_not_changed(lambda: self.hero.actions.current_action.percents): result, step, postsave_actions = self.card.use(**self.use_attributes(storage=self.storage, hero=self.hero)) self.assertEqual( (result, step, postsave_actions), (ComplexChangeTask.RESULT.FAILED, ComplexChangeTask.STEP.ERROR, ()) ) self.assertTrue(self.hero.position.place.id, self.place_1.id)
class DoNothingActionTest(testcase.TestCase): @mock.patch('the_tale.game.actions.prototypes.ActionBase.get_description', lambda self: 'abrakadabra') def setUp(self): super(DoNothingActionTest, self).setUp() create_test_map() account = self.accounts_factory.create_account(is_fast=True) self.storage = LogicStorage() self.storage.load_account_data(account) self.hero = self.storage.accounts_to_heroes[account.id] self.action_idl = self.hero.actions.current_action self.action_donothing = ActionDoNothingPrototype.create( hero=self.hero, duration=7, messages_prefix='QUEST_HOMETOWN_JOURNAL_CHATTING', messages_probability=0.3) def tearDown(self): pass def test_create(self): self.assertEqual(self.action_idl.leader, False) self.assertEqual(self.action_donothing.leader, True) self.assertEqual(self.action_donothing.textgen_id, 'QUEST_HOMETOWN_JOURNAL_CHATTING') self.assertEqual(self.action_donothing.percents_barier, 7) self.assertEqual(self.action_donothing.extra_probability, 0.3) self.assertEqual(self.action_donothing.bundle_id, self.action_idl.bundle_id) self.storage._test_save() def test_not_ready(self): self.storage.process_turn() self.assertEqual(len(self.hero.actions.actions_list), 2) self.assertEqual(self.hero.actions.current_action, self.action_donothing) self.storage._test_save() def test_full(self): current_time = TimePrototype.get_current_time() for i in range(7): self.assertEqual(len(self.hero.actions.actions_list), 2) self.assertTrue(self.action_donothing.leader) self.storage.process_turn(continue_steps_if_needed=False) current_time.increment_turn() self.assertEqual(len(self.hero.actions.actions_list), 1) self.assertTrue(self.action_idl.leader) self.storage._test_save()
class CommonTests(testcase.TestCase): def setUp(self): super(CommonTests, self).setUp() create_test_map() self.account = self.accounts_factory.create_account() self.storage = LogicStorage() self.storage.load_account_data(self.account) self.hero = self.storage.accounts_to_heroes[self.account.id] def test_rarities_abilities(self): for rarity, rarity_abilities in helpers.RARITIES_ABILITIES.items(): companion = logic.create_random_companion_record( '%s companion' % rarity, abilities=rarity_abilities) self.assertEqual(companion.rarity, rarity) @mock.patch('the_tale.game.companions.objects.Companion.max_coherence', 100) @mock.patch( 'the_tale.game.heroes.habilities.companions.THOUGHTFUL.MULTIPLIER', [1, 1, 1, 1, 1]) @mock.patch( 'the_tale.game.heroes.habilities.companions._CompanionHealBase.PROBABILITY', [0, 0, 0, 0, 0]) def _test_companion_death_speed(self): companion_record = logic.create_random_companion_record( 'test companion', state=relations.STATE.ENABLED, dedication=relations.DEDICATION.BRAVE) #,#,; companion = logic.create_companion(companion_record) self.hero.set_companion(companion) self.hero.preferences.set_companion_dedication( heroes_relations.COMPANION_DEDICATION.NORMAL) old_health = self.hero.companion.health while self.hero.companion: self.hero.companion.coherence = 50 self.storage.process_turn() turn.increment() self.hero.randomized_level_up() if not self.hero.is_alive: if hasattr(self.hero.actions.current_action, 'fast_resurrect'): self.hero.actions.current_action.fast_resurrect() if self.hero.companion: old_health = self.hero.companion.health
class DoNothingActionTest(testcase.TestCase): @mock.patch('the_tale.game.actions.prototypes.ActionBase.get_description', lambda self: 'abrakadabra') def setUp(self): super(DoNothingActionTest, self).setUp() create_test_map() account = self.accounts_factory.create_account(is_fast=True) self.storage = LogicStorage() self.storage.load_account_data(account) self.hero = self.storage.accounts_to_heroes[account.id] self.action_idl = self.hero.actions.current_action self.action_donothing = ActionDoNothingPrototype.create(hero=self.hero, duration=7, messages_prefix='QUEST_HOMETOWN_JOURNAL_CHATTING', messages_probability=0.3) def tearDown(self): pass def test_create(self): self.assertEqual(self.action_idl.leader, False) self.assertEqual(self.action_donothing.leader, True) self.assertEqual(self.action_donothing.textgen_id, 'QUEST_HOMETOWN_JOURNAL_CHATTING') self.assertEqual(self.action_donothing.percents_barier, 7) self.assertEqual(self.action_donothing.extra_probability, 0.3) self.assertEqual(self.action_donothing.bundle_id, self.action_idl.bundle_id) self.storage._test_save() def test_not_ready(self): self.storage.process_turn() self.assertEqual(len(self.hero.actions.actions_list), 2) self.assertEqual(self.hero.actions.current_action, self.action_donothing) self.storage._test_save() def test_full(self): current_time = TimePrototype.get_current_time() for i in range(7): self.assertEqual(len(self.hero.actions.actions_list), 2) self.assertTrue(self.action_donothing.leader) self.storage.process_turn(continue_steps_if_needed=False) current_time.increment_turn() self.assertEqual(len(self.hero.actions.actions_list), 1) self.assertTrue(self.action_idl.leader) self.storage._test_save()
class AddExperienceTestMixin(CardsTestMixin): CARD = None def setUp(self): super(AddExperienceTestMixin, self).setUp() create_test_map() result, account_1_id, bundle_id = register_user('test_user', '*****@*****.**', '111111') self.account_1 = AccountPrototype.get_by_id(account_1_id) self.storage = LogicStorage() self.storage.load_account_data(self.account_1) self.hero = self.storage.accounts_to_heroes[self.account_1.id] self.card = self.CARD() @mock.patch('the_tale.game.heroes.prototypes.HeroPrototype.is_short_quest_path_required', False) @mock.patch('the_tale.game.heroes.prototypes.HeroPrototype.is_first_quest_path_required', False) def test_use(self): self.action_quest = ActionQuestPrototype.create(hero=self.hero) quests_helpers.setup_quest(self.hero) self.assertTrue(self.hero.quests.has_quests) old_ui_experience = self.hero.quests.current_quest.current_info.ui_info(self.hero)['experience'] with mock.patch('the_tale.game.quests.container.QuestsContainer.mark_updated') as mark_updated: with self.check_not_changed(lambda: self.hero.experience): with self.check_not_changed(lambda: self.hero.level): with self.check_not_changed(lambda: self.hero.quests.current_quest.current_info.experience): with self.check_delta(lambda: self.hero.quests.current_quest.current_info.experience_bonus, self.CARD.EXPERIENCE): result, step, postsave_actions = self.card.use(**self.use_attributes(storage=self.storage, hero=self.hero)) self.assertEqual((result, step, postsave_actions), (ComplexChangeTask.RESULT.SUCCESSED, ComplexChangeTask.STEP.SUCCESS, ())) self.assertEqual(mark_updated.call_count, 1) while self.hero.quests.has_quests: self.assertEqual(self.hero.quests.current_quest.quests_stack[0].experience_bonus, self.CARD.EXPERIENCE) self.assertEqual(self.hero.quests.current_quest.quests_stack[0].ui_info(self.hero)['experience'], old_ui_experience + self.CARD.EXPERIENCE) self.storage.process_turn() def test_no_quest(self): self.assertFalse(self.hero.quests.has_quests) result, step, postsave_actions = self.card.use(**self.use_attributes(storage=self.storage, hero=self.hero)) self.assertEqual((result, step, postsave_actions), (ComplexChangeTask.RESULT.FAILED, ComplexChangeTask.STEP.ERROR, ()))
class AddExperienceTestMixin(CardsTestMixin): CARD = None def setUp(self): super(AddExperienceTestMixin, self).setUp() create_test_map() result, account_1_id, bundle_id = register_user('test_user', '*****@*****.**', '111111') self.account_1 = AccountPrototype.get_by_id(account_1_id) self.storage = LogicStorage() self.storage.load_account_data(self.account_1) self.hero = self.storage.accounts_to_heroes[self.account_1.id] self.card = self.CARD() @mock.patch('the_tale.game.heroes.objects.Hero.is_short_quest_path_required', False) @mock.patch('the_tale.game.heroes.objects.Hero.is_first_quest_path_required', False) def test_use(self): self.action_quest = ActionQuestPrototype.create(hero=self.hero) quests_helpers.setup_quest(self.hero) self.assertTrue(self.hero.quests.has_quests) old_ui_experience = self.hero.quests.current_quest.current_info.ui_info(self.hero)['experience'] with mock.patch('the_tale.game.quests.container.QuestsContainer.mark_updated') as mark_updated: with self.check_not_changed(lambda: self.hero.experience): with self.check_not_changed(lambda: self.hero.level): with self.check_not_changed(lambda: self.hero.quests.current_quest.current_info.experience): with self.check_delta(lambda: self.hero.quests.current_quest.current_info.experience_bonus, self.CARD.EXPERIENCE): result, step, postsave_actions = self.card.use(**self.use_attributes(storage=self.storage, hero=self.hero)) self.assertEqual((result, step, postsave_actions), (ComplexChangeTask.RESULT.SUCCESSED, ComplexChangeTask.STEP.SUCCESS, ())) self.assertEqual(mark_updated.call_count, 1) while self.hero.quests.has_quests: self.assertEqual(self.hero.quests.current_quest.quests_stack[0].experience_bonus, self.CARD.EXPERIENCE) self.assertEqual(self.hero.quests.current_quest.quests_stack[0].ui_info(self.hero)['experience'], old_ui_experience + self.CARD.EXPERIENCE) self.storage.process_turn() def test_no_quest(self): self.assertFalse(self.hero.quests.has_quests) result, step, postsave_actions = self.card.use(**self.use_attributes(storage=self.storage, hero=self.hero)) self.assertEqual((result, step, postsave_actions), (ComplexChangeTask.RESULT.FAILED, ComplexChangeTask.STEP.ERROR, ()))
class GameTest(testcase.TestCase): def test_statistics_consistency(self): create_test_map() account = self.accounts_factory.create_account(is_fast=True) self.storage = LogicStorage() self.storage.load_account_data(account) self.hero = self.storage.accounts_to_heroes[account.id] for i in range(10000): self.storage.process_turn() turn.increment() self.assertEqual( self.hero.money, self.hero.statistics.money_earned - self.hero.statistics.money_spend)
class GameTest(testcase.TestCase): def test_statistics_consistency(self): create_test_map() result, account_id, bundle_id = register_user("test_user") self.storage = LogicStorage() self.storage.load_account_data(AccountPrototype.get_by_id(account_id)) self.hero = self.storage.accounts_to_heroes[account_id] current_time = TimePrototype.get_current_time() for i in xrange(10000): self.storage.process_turn() current_time.increment_turn() self.assertEqual(self.hero.money, self.hero.statistics.money_earned - self.hero.statistics.money_spend)
class GameTest(testcase.TestCase): def test_statistics_consistency(self): create_test_map() account = self.accounts_factory.create_account(is_fast=True) self.storage = LogicStorage() self.storage.load_account_data(account) self.hero = self.storage.accounts_to_heroes[account.id] current_time = TimePrototype.get_current_time() for i in range(10000): self.storage.process_turn() current_time.increment_turn() self.assertEqual(self.hero.money, self.hero.statistics.money_earned - self.hero.statistics.money_spend)
class GameTest(testcase.TestCase): def test_statistics_consistency(self): create_test_map() result, account_id, bundle_id = register_user('test_user') self.storage = LogicStorage() self.storage.load_account_data(AccountPrototype.get_by_id(account_id)) self.hero = self.storage.accounts_to_heroes[account_id] current_time = TimePrototype.get_current_time() for i in xrange(10000): self.storage.process_turn() current_time.increment_turn() self.assertEqual(self.hero.money, self.hero.statistics.money_earned - self.hero.statistics.money_spend)
class FirstStepsActionTest(testcase.TestCase): def setUp(self): super(FirstStepsActionTest, self).setUp() create_test_map() self.account = self.accounts_factory.create_account(is_fast=True) self.storage = LogicStorage() self.storage.load_account_data(self.account) self.hero = self.storage.accounts_to_heroes[self.account.id] self.action_idl = self.hero.actions.current_action with self.check_calls_count( 'the_tale.game.heroes.logic.push_message_to_diary', 1): self.action_first_steps = ActionFirstStepsPrototype.create( hero=self.hero) def test_create(self): self.assertEqual(self.action_idl.leader, False) self.assertEqual(self.action_first_steps.leader, True) self.assertEqual(self.action_first_steps.bundle_id, self.action_idl.bundle_id) self.storage._test_save() def test_processed(self): current_time = TimePrototype.get_current_time() self.assertEqual(self.hero.journal.messages_number(), 2) with self.check_calls_count( 'the_tale.game.heroes.logic.push_message_to_diary', 0): self.storage.process_turn() current_time.increment_turn() self.assertEqual(self.hero.journal.messages_number(), 3) self.storage.process_turn() current_time.increment_turn() self.assertEqual(self.hero.journal.messages_number(), 4) self.storage.process_turn(continue_steps_if_needed=False) current_time.increment_turn() self.assertEqual(self.hero.journal.messages_number(), 5) self.assertTrue(self.hero.actions.current_action.TYPE.is_IDLENESS) self.storage._test_save()
class FirstStepsActionTest(testcase.TestCase): def setUp(self): super(FirstStepsActionTest, self).setUp() create_test_map() self.account = self.accounts_factory.create_account(is_fast=True) self.storage = LogicStorage() self.storage.load_account_data(self.account) self.hero = self.storage.accounts_to_heroes[self.account.id] self.action_idl = self.hero.actions.current_action with self.check_calls_count("the_tale.game.heroes.logic.push_message_to_diary", 1): self.action_first_steps = ActionFirstStepsPrototype.create(hero=self.hero) def test_create(self): self.assertEqual(self.action_idl.leader, False) self.assertEqual(self.action_first_steps.leader, True) self.assertEqual(self.action_first_steps.bundle_id, self.action_idl.bundle_id) self.storage._test_save() def test_processed(self): current_time = TimePrototype.get_current_time() self.assertEqual(self.hero.journal.messages_number(), 2) with self.check_calls_count("the_tale.game.heroes.logic.push_message_to_diary", 0): self.storage.process_turn() current_time.increment_turn() self.assertEqual(self.hero.journal.messages_number(), 3) self.storage.process_turn() current_time.increment_turn() self.assertEqual(self.hero.journal.messages_number(), 4) self.storage.process_turn(continue_steps_if_needed=False) current_time.increment_turn() self.assertEqual(self.hero.journal.messages_number(), 5) self.assertTrue(self.hero.actions.current_action.TYPE.is_IDLENESS) self.storage._test_save()
class CommonTests(testcase.TestCase): def setUp(self): super(CommonTests, self).setUp() create_test_map() self.account = self.accounts_factory.create_account() self.storage = LogicStorage() self.storage.load_account_data(self.account) self.hero = self.storage.accounts_to_heroes[self.account.id] def test_rarities_abilities(self): for rarity, rarity_abilities in helpers.RARITIES_ABILITIES.items(): companion = logic.create_random_companion_record( '%s companion' % rarity, abilities=rarity_abilities) self.assertEqual(companion.rarity, rarity) @mock.patch('the_tale.game.companions.objects.Companion.max_coherence', 100) @mock.patch( 'the_tale.game.heroes.habilities.companions.THOUGHTFUL.MULTIPLIER', [1, 1, 1, 1, 1]) @mock.patch( 'the_tale.game.heroes.habilities.companions._CompanionHealBase.PROBABILITY', [0, 0, 0, 0, 0]) def _test_companion_death_speed(self): current_time = game_prototypes.TimePrototype.get_current_time() companion_record = logic.create_random_companion_record( 'test companion', state=relations.STATE.ENABLED, dedication=relations.DEDICATION.BRAVE) #,#,; # abilities=abilities_container.Container(start=(effects.ABILITIES.BODYGUARD,)),# effects.ABILITIES.PUNY)), # dedication=relations.DEDICATION.HEROIC) # abilities=abilities_container.Container(common=(effects.ABILITIES.COWARDLY, )), # dedication=relations.DEDICATION.INDECISIVE) companion = logic.create_companion(companion_record) self.hero.set_companion(companion) # self.hero.preferences.set_companion_dedication(heroes_relations.COMPANION_DEDICATION.EGOISM) self.hero.preferences.set_companion_dedication( heroes_relations.COMPANION_DEDICATION.NORMAL) # self.hero.preferences.set_companion_dedication(heroes_relations.COMPANION_DEDICATION.ALTRUISM) # self.hero.preferences.set_companion_dedication(heroes_relations.COMPANION_DEDICATION.EVERY_MAN_FOR_HIMSELF) old_health = self.hero.companion.health print('defend_probability: ', self.hero.companion.defend_in_battle_probability) # for i in xrange(50): # self.hero.randomized_level_up(increment_level=True) while self.hero.companion: self.hero.companion.coherence = 50 self.storage.process_turn() current_time.increment_turn() self.hero.randomized_level_up() if not self.hero.is_alive: if hasattr(self.hero.actions.current_action, 'fast_resurrect'): self.hero.actions.current_action.fast_resurrect() print('!') if self.hero.companion: if old_health != self.hero.companion.health: print( '%.2f:\t%s -> %s [%s] c%s' % ((current_time.turn_number / c.TURNS_IN_HOUR / 24.0), self.hero.companion.health - self.hero.companion.max_health, self.hero.companion.health, self.hero.companion.health - old_health, self.hero.companion.coherence)) old_health = self.hero.companion.health
class LogicStorageTests(testcase.TestCase): def setUp(self): super(LogicStorageTests, self).setUp() self.p1, self.p2, self.p3 = create_test_map() self.storage = LogicStorage() self.account_1 = self.accounts_factory.create_account() self.account_2 = self.accounts_factory.create_account() self.storage.load_account_data(self.account_1) self.storage.load_account_data(self.account_2) self.hero_1 = self.storage.accounts_to_heroes[self.account_1.id] self.hero_2 = self.storage.accounts_to_heroes[self.account_2.id] self.action_idl_1 = self.hero_1.actions.current_action self.action_idl_2 = self.hero_2.actions.current_action self.bundle_1_id = self.action_idl_1.bundle_id self.bundle_2_id = self.action_idl_2.bundle_id def test_load_account_data(self): self.assertEqual(len(self.storage.heroes), 2) self.assertEqual(len(self.storage.accounts_to_heroes), 2) self.assertEqual( self.storage.bundles_to_accounts, { self.hero_1.actions.current_action.bundle_id: set([self.account_1.id]), self.hero_2.actions.current_action.bundle_id: set([self.account_2.id]) }) action_regenerate = actions_prototypes.ActionRegenerateEnergyPrototype.create( hero=self.hero_1) self.assertEqual(self.action_idl_1.storage, self.storage) self.assertEqual(action_regenerate.storage, self.storage) storage = LogicStorage() storage.load_account_data(AccountPrototype.get_by_id( self.account_1.id)) storage.load_account_data(AccountPrototype.get_by_id( self.account_2.id)) self.assertEqual(len(storage.heroes), 2) self.assertEqual(len(storage.accounts_to_heroes), 2) self.assertEqual( storage.bundles_to_accounts, { self.hero_1.actions.current_action.bundle_id: set([self.account_1.id]), self.hero_2.actions.current_action.bundle_id: set([self.account_2.id]) }) def test_load_account_data_with_meta_action(self): bundle_id = 666 meta_action_battle = meta_actions.ArenaPvP1x1.create( self.storage, self.hero_1, self.hero_2) proxy_action_1 = actions_prototypes.ActionMetaProxyPrototype.create( hero=self.hero_1, _bundle_id=bundle_id, meta_action=meta_action_battle) proxy_action_2 = actions_prototypes.ActionMetaProxyPrototype.create( hero=self.hero_2, _bundle_id=bundle_id, meta_action=meta_action_battle) self.assertEqual(len(self.storage.meta_actions), 1) self.assertEqual(len(self.storage.meta_actions_to_actions), 1) self.assertEqual( self.storage.meta_actions_to_actions[meta_action_battle.uid], set([ LogicStorage.get_action_uid(proxy_action_1), LogicStorage.get_action_uid(proxy_action_2) ])) self.storage.save_changed_data() self.assertIs(self.hero_1.actions.current_action.meta_action, self.hero_2.actions.current_action.meta_action) self.assertIs(self.hero_1.actions.current_action.saved_meta_action, self.hero_2.actions.current_action.saved_meta_action) storage = LogicStorage() storage.load_account_data(AccountPrototype.get_by_id( self.account_1.id)) storage.load_account_data(AccountPrototype.get_by_id( self.account_2.id)) self.assertEqual(len(storage.meta_actions), 1) self.assertEqual(len(storage.meta_actions_to_actions), 1) self.assertEqual( storage.meta_actions_to_actions[meta_action_battle.uid], set([ LogicStorage.get_action_uid(proxy_action_1), LogicStorage.get_action_uid(proxy_action_2) ])) self.assertEqual( storage.bundles_to_accounts, { self.hero_1.actions.current_action.bundle_id: set([self.account_1.id, self.account_2.id]) }) hero_1 = storage.accounts_to_heroes[self.account_1.id] hero_2 = storage.accounts_to_heroes[self.account_2.id] self.assertIs(hero_1.actions.current_action.meta_action, hero_2.actions.current_action.meta_action) self.assertIsNot(hero_1.actions.current_action.saved_meta_action, hero_2.actions.current_action.saved_meta_action) self.assertEqual( hero_1.actions.current_action.saved_meta_action.serialize(), hero_2.actions.current_action.saved_meta_action.serialize()) def test_add_duplicate_hero(self): self.assertRaises(exceptions.HeroAlreadyRegisteredError, self.storage._add_hero, self.hero_1) def test_action_release_account_data(self): actions_prototypes.ActionRegenerateEnergyPrototype.create( hero=self.hero_1) self.storage.skipped_heroes.add(self.hero_1.id) self.storage.release_account_data(self.account_1.id) self.assertEqual(len(self.storage.heroes), 1) self.assertEqual(len(self.storage.accounts_to_heroes), 1) self.assertEqual(self.storage.bundles_to_accounts, { self.hero_2.actions.current_action.bundle_id: set([self.account_2.id]) }) self.assertEqual(self.storage.heroes.values()[0].id, self.hero_2.id) self.assertFalse(self.storage.skipped_heroes) def test_save_hero_data(self): self.hero_1.health = 1 self.hero_2.health = 1 self.hero_1.actions.updated = True self.storage._save_hero_data(self.hero_1.id) self.assertEqual(self.hero_1.health, HeroPrototype.get_by_id(self.hero_1.id).health) self.assertNotEqual(self.hero_2.health, HeroPrototype.get_by_id(self.hero_2.id).health) self.assertFalse(self.hero_1.actions.updated) def test_save_all(self): self.hero_1.health = 1 self.hero_2.health = 1 self.hero_1.actions.updated = True self.storage.save_all() self.assertEqual(self.hero_1.health, HeroPrototype.get_by_id(self.hero_1.id).health) self.assertEqual(self.hero_2.health, HeroPrototype.get_by_id(self.hero_2.id).health) self.assertFalse(self.hero_1.actions.updated) def test_save_hero_data_with_meta_action(self): bundle_id = 666 meta_action_battle = meta_actions.ArenaPvP1x1.create( self.storage, self.hero_1, self.hero_2) actions_prototypes.ActionMetaProxyPrototype.create( hero=self.hero_1, _bundle_id=bundle_id, meta_action=meta_action_battle) actions_prototypes.ActionMetaProxyPrototype.create( hero=self.hero_2, _bundle_id=bundle_id, meta_action=meta_action_battle) self.storage._save_hero_data(self.hero_1.id) self.storage._save_hero_data(self.hero_2.id) self.hero_1.reload() self.hero_2.reload() self.assertEqual( meta_action_battle.serialize(), self.hero_1.actions.current_action.saved_meta_action.serialize()) self.assertEqual( meta_action_battle.serialize(), self.hero_2.actions.current_action.saved_meta_action.serialize()) def test_switch_caches(self): self.assertEqual(self.storage.previous_cache, {}) self.assertEqual(self.storage.current_cache, {}) self.storage.previous_cache[1] = 2 self.storage.current_cache[3] = 4 self.storage.switch_caches() self.assertEqual(self.storage.previous_cache, {3: 4}) self.assertEqual(self.storage.current_cache, {}) self.storage.current_cache[5] = 6 self.storage.switch_caches() self.assertEqual(self.storage.previous_cache, {5: 6}) self.assertEqual(self.storage.current_cache, {}) def test_process_cache_queue__with_update(self): self.assertEqual(self.storage.cache_queue, set()) self.storage.cache_queue.add(self.hero_2.id) self.storage.cache_queue.add(self.hero_1.id) self.storage.process_cache_queue(update_cache=True) self.assertItemsEqual( self.storage.current_cache.keys(), (self.hero_1.cached_ui_info_key, self.hero_2.cached_ui_info_key)) self.assertEqual(self.storage.cache_queue, set()) def test_process_cache_queue__without_update(self): self.assertEqual(self.storage.cache_queue, set()) self.storage.cache_queue.add(self.hero_2.id) self.storage.cache_queue.add(self.hero_1.id) self.storage.process_cache_queue(update_cache=False) self.assertItemsEqual(self.storage.current_cache.keys(), ()) self.assertEqual(self.storage.cache_queue, set()) def test_process_cache_queue__update_cache__with_update(self): self.assertEqual(self.storage.cache_queue, set()) self.storage.current_cache[self.hero_1.cached_ui_info_key] = 1 self.storage.current_cache[self.hero_2.cached_ui_info_key] = 2 self.storage.cache_queue.add(self.hero_2.id) self.storage.process_cache_queue(update_cache=True) self.assertEqual( self.storage.current_cache[self.hero_1.cached_ui_info_key], 1) self.assertNotEqual( self.storage.current_cache[self.hero_2.cached_ui_info_key], 2) def test_process_cache_queue__update_cache__without_update(self): self.assertEqual(self.storage.cache_queue, set()) self.storage.current_cache[self.hero_1.cached_ui_info_key] = 1 self.storage.current_cache[self.hero_2.cached_ui_info_key] = 2 self.storage.cache_queue.add(self.hero_2.id) self.storage.process_cache_queue(update_cache=False) self.assertEqual( self.storage.current_cache[self.hero_1.cached_ui_info_key], 1) self.assertEqual( self.storage.current_cache[self.hero_2.cached_ui_info_key], 2) @mock.patch('the_tale.game.heroes.conf.heroes_settings.DUMP_CACHED_HEROES', True) def test_process_turn(self): self.assertEqual(self.storage.skipped_heroes, set()) self.storage.process_turn() self.assertEqual(self.storage.skipped_heroes, set()) with mock.patch( 'the_tale.game.logic_storage.LogicStorage._save_hero_data' ) as save_hero_data: self.storage.save_changed_data() self.assertEqual(save_hero_data.call_count, 2) @mock.patch('the_tale.game.heroes.conf.heroes_settings.DUMP_CACHED_HEROES', True) def test_process_turn__switch_caches(self): self.assertEqual(self.storage.previous_cache, {}) self.assertEqual(self.storage.current_cache, {}) self.storage.process_turn() self.storage.save_changed_data() self.assertEqual(self.storage.previous_cache, {}) self.assertNotEqual(self.storage.current_cache, {}) old_cache = self.storage.current_cache self.storage.process_turn() self.storage.save_changed_data() self.assertEqual(self.storage.previous_cache, old_cache) self.assertNotEqual(self.storage.current_cache, old_cache) def test_process_turn_single_hero__runned_outside_storage(self): action_1 = actions_prototypes.ActionRegenerateEnergyPrototype.create( hero=self.hero_1) action_1.state = action_1.STATE.PROCESSED action_2 = actions_prototypes.ActionMoveToPrototype.create( hero=self.hero_1, destination=self.p1) action_2.state = action_2.STATE.PROCESSED action_3 = actions_prototypes.ActionInPlacePrototype.create( hero=self.hero_1) action_3.state = action_3.STATE.PROCESSED self.assertEqual(self.hero_1.actions.number, 4) self.storage.process_turn__single_hero(hero=self.hero_1, logger=None, continue_steps_if_needed=True) self.assertEqual(self.hero_1.actions.number, 2) self.assertEqual(self.hero_1.actions.current_action.TYPE, actions_prototypes.ActionQuestPrototype.TYPE) self.storage.process_turn() # just nothing was broken def test_process_turn__process_action_chain(self): action_1 = actions_prototypes.ActionRegenerateEnergyPrototype.create( hero=self.hero_1) action_1.state = action_1.STATE.PROCESSED action_2 = actions_prototypes.ActionMoveToPrototype.create( hero=self.hero_1, destination=self.p1) action_2.state = action_2.STATE.PROCESSED action_3 = actions_prototypes.ActionInPlacePrototype.create( hero=self.hero_1) action_3.state = action_3.STATE.PROCESSED self.assertEqual(self.hero_1.actions.number, 4) self.storage.process_turn() self.assertEqual(self.hero_1.actions.number, 2) self.assertEqual(self.hero_1.actions.current_action.TYPE, actions_prototypes.ActionQuestPrototype.TYPE) @mock.patch('the_tale.game.heroes.conf.heroes_settings.DUMP_CACHED_HEROES', False) def test_process_turn__without_dump(self): self.assertEqual(self.storage.skipped_heroes, set()) self.storage.process_turn() self.assertEqual(self.storage.skipped_heroes, set()) with mock.patch( 'the_tale.game.logic_storage.LogicStorage._save_hero_data' ) as save_hero_data: self.storage.save_changed_data() self.assertEqual( save_hero_data.call_count, 1 ) # save only game_settings.SAVED_UNCACHED_HEROES_FRACTION bundles number def test_process_turn__process_created_action(self): from the_tale.game.actions.prototypes import ActionMoveToPrototype place = self.p1 def process_action(self): ActionMoveToPrototype.create(hero=self.hero, destination=place) with mock.patch( 'the_tale.game.actions.prototypes.ActionIdlenessPrototype.process', process_action): with mock.patch( 'the_tale.game.actions.prototypes.ActionMoveToPrototype.process' ) as move_to_process: self.storage.process_turn() self.assertEqual(move_to_process.call_count, 2) @mock.patch('the_tale.game.heroes.conf.heroes_settings.DUMP_CACHED_HEROES', True) def test_process_turn_with_skipped_hero(self): # skipped heroes saved, but not processed self.storage.skipped_heroes.add(self.hero_1.id) with mock.patch( 'the_tale.game.actions.prototypes.ActionBase.process_turn' ) as action_process_turn: self.storage.process_turn() self.assertEqual(action_process_turn.call_count, 1) with mock.patch( 'the_tale.game.logic_storage.LogicStorage._save_hero_data' ) as save_hero_data: self.storage.save_changed_data() self.assertEqual(save_hero_data.call_count, 2) @mock.patch('the_tale.game.heroes.conf.heroes_settings.DUMP_CACHED_HEROES', False) def test_process_turn_with_skipped_hero__without_cache_dump(self): # skipped heroes saved, but not processed self.storage.skipped_heroes.add(self.hero_1.id) with mock.patch( 'the_tale.game.actions.prototypes.ActionBase.process_turn' ) as action_process_turn: self.storage.process_turn() self.assertEqual(action_process_turn.call_count, 1) with mock.patch( 'the_tale.game.logic_storage.LogicStorage._save_hero_data' ) as save_hero_data: self.storage.save_changed_data() self.assertEqual(save_hero_data.call_count, 1) @mock.patch( 'the_tale.game.heroes.prototypes.HeroPrototype.can_process_turn', lambda self, turn: True) def test_process_turn__can_process_turn(self): with mock.patch( 'the_tale.game.actions.prototypes.ActionBase.process_turn' ) as action_process_turn: self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(action_process_turn.call_count, 2) @mock.patch( 'the_tale.game.heroes.prototypes.HeroPrototype.can_process_turn', lambda self, turn: False) def test_process_turn__can_not_process_turn(self): with mock.patch( 'the_tale.game.actions.prototypes.ActionBase.process_turn' ) as action_process_turn: self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(action_process_turn.call_count, 0) def test_process_turn___exception_raises(self): def process_turn_raise_exception(action): if action.hero.id == self.hero_2.id: raise Exception('error') with mock.patch( 'the_tale.game.actions.prototypes.ActionBase.process_turn', process_turn_raise_exception): with mock.patch( 'the_tale.game.logic_storage.LogicStorage._save_on_exception' ) as _save_on_exception: with mock.patch('django.conf.settings.TESTS_RUNNING', False): self.storage.process_turn() self.assertIn(self.hero_2.actions.current_action.bundle_id, self.storage.ignored_bundles) self.assertEqual(_save_on_exception.call_count, 1) self.assertEqual(_save_on_exception.call_args, mock.call()) @mock.patch('the_tale.game.conf.game_settings.SAVE_ON_EXCEPTION_TIMEOUT', 0) def test_save_on_exception(self): # hero 1 not saved due to one bundle with hero 3 # hero 2 saved # hero 3 not saved # hero 4 saved result, account_3_id, bundle_3_id = register_user( 'test_user_3', '*****@*****.**', '111111') self.storage.load_account_data( AccountPrototype.get_by_id(account_3_id)) hero_3 = self.storage.accounts_to_heroes[account_3_id] result, account_4_id, bundle_4_id = register_user( 'test_user_4', '*****@*****.**', '111111') self.storage.load_account_data( AccountPrototype.get_by_id(account_4_id)) hero_4 = self.storage.accounts_to_heroes[account_4_id] self.hero_1.actions.current_action.bundle_id = hero_3.actions.current_action.bundle_id saved_heroes = set() def save_hero_data(storage, hero_id, **kwargs): saved_heroes.add(hero_id) self.storage.ignored_bundles.add( hero_3.actions.current_action.bundle_id) with mock.patch( 'the_tale.game.logic_storage.LogicStorage._save_hero_data', save_hero_data): self.storage._save_on_exception() self.assertEqual(saved_heroes, set([self.hero_2.id, hero_4.id])) def test_save_on_exception__time_border(self): # hero 1 not saved due to one bundle with hero 3 # hero 2 saved # hero 3 not saved # hero 4 saved result, account_3_id, bundle_3_id = register_user( 'test_user_3', '*****@*****.**', '111111') self.storage.load_account_data( AccountPrototype.get_by_id(account_3_id)) hero_3 = self.storage.accounts_to_heroes[account_3_id] result, account_4_id, bundle_4_id = register_user( 'test_user_4', '*****@*****.**', '111111') self.storage.load_account_data( AccountPrototype.get_by_id(account_4_id)) hero_4 = self.storage.accounts_to_heroes[account_4_id] self.hero_1.actions.current_action.bundle_id = hero_3.actions.current_action.bundle_id saved_heroes = set() self.hero_2._model.saved_at = datetime.datetime.now( ) - datetime.timedelta( seconds=conf.game_settings.SAVE_ON_EXCEPTION_TIMEOUT + 1) def save_hero_data(storage, hero_id, **kwargs): saved_heroes.add(hero_id) self.storage.ignored_bundles.add( hero_3.actions.current_action.bundle_id) with mock.patch( 'the_tale.game.logic_storage.LogicStorage._save_hero_data', save_hero_data): self.storage._save_on_exception() self.assertEqual(saved_heroes, set([self.hero_2.id])) def test_save_changed_data(self): self.storage.process_turn() with mock.patch('dext.common.utils.cache.set_many') as set_many: with mock.patch( 'the_tale.game.heroes.prototypes.HeroPrototype.ui_info' ) as ui_info: self.storage.save_changed_data() self.assertEqual(set_many.call_count, 1) self.assertEqual(ui_info.call_count, 2) self.assertEqual(ui_info.call_args_list, [ mock.call(actual_guaranteed=True, old_info=None), mock.call(actual_guaranteed=True, old_info=None) ]) def test_old_info(self): self.storage.process_turn() calls = [] def ui_info(hero, **kwargs): calls.append(kwargs) return {'hero': hero.id} with mock.patch( 'the_tale.game.heroes.prototypes.HeroPrototype.ui_info', ui_info): self.storage.save_changed_data() self.storage.process_turn() self.storage.save_changed_data() self.assertEqual(calls, [{ 'actual_guaranteed': True, 'old_info': None }, { 'actual_guaranteed': True, 'old_info': None }, { 'actual_guaranteed': True, 'old_info': { 'hero': self.hero_1.id } }, { 'actual_guaranteed': True, 'old_info': { 'hero': self.hero_2.id } }]) def test_save_changed_data__old_info(self): self.storage.process_turn() self.storage.save_changed_data() self.storage.process_turn() with mock.patch('dext.common.utils.cache.set_many') as set_many: with mock.patch( 'the_tale.game.heroes.prototypes.HeroPrototype.ui_info' ) as ui_info: self.storage.save_changed_data() self.assertEqual(set_many.call_count, 1) self.assertEqual(ui_info.call_count, 2) self.assertNotEqual(ui_info.call_args_list[0][1]['old_info'], None) self.assertNotEqual(ui_info.call_args_list[1][1]['old_info'], None) @mock.patch('the_tale.game.heroes.conf.heroes_settings.DUMP_CACHED_HEROES', True) def test_save_changed_data__with_unsaved_bundles(self): self.storage.process_turn() self.assertEqual(len(self.storage.heroes), 2) with mock.patch( 'the_tale.game.logic_storage.LogicStorage._get_bundles_to_save', lambda x: [self.bundle_2_id]): with mock.patch( 'the_tale.game.logic_storage.LogicStorage._save_hero_data' ) as save_hero_data: with mock.patch( 'the_tale.game.heroes.prototypes.HeroPrototype.ui_info' ) as ui_info: self.storage.save_changed_data() self.assertEqual(ui_info.call_count, 2) # cache all heroes, since they are new self.assertEqual(ui_info.call_args_list, [ mock.call(actual_guaranteed=True, old_info=None), mock.call(actual_guaranteed=True, old_info=None) ]) self.assertEqual(save_hero_data.call_args, mock.call(self.hero_2.id)) def test_save_changed_data__with_unsaved_bundles__without_dump(self): self.storage.process_turn() self.assertEqual(len(self.storage.heroes), 2) self.hero_2.ui_caching_started_at = datetime.datetime.fromtimestamp(0) with mock.patch( 'the_tale.game.logic_storage.LogicStorage._get_bundles_to_save', lambda x: [self.bundle_2_id]): with mock.patch( 'the_tale.game.logic_storage.LogicStorage._save_hero_data' ) as save_hero_data: with mock.patch( 'the_tale.game.heroes.prototypes.HeroPrototype.ui_info' ) as ui_info: self.storage.save_changed_data() self.assertEqual(ui_info.call_count, 1) # cache only first hero self.assertEqual(ui_info.call_args, mock.call(actual_guaranteed=True, old_info=None)) self.assertEqual(save_hero_data.call_args, mock.call(self.hero_2.id)) def test_remove_action__from_middle(self): actions_prototypes.ActionRegenerateEnergyPrototype.create( hero=self.hero_1) self.assertRaises(exceptions.RemoveActionFromMiddleError, self.storage.remove_action, self.action_idl_1) def test_remove_action__metaaction(self): bundle_id = 666 meta_action_battle = meta_actions.ArenaPvP1x1.create( self.storage, self.hero_1, self.hero_2) proxy_action_1 = actions_prototypes.ActionMetaProxyPrototype.create( hero=self.hero_1, _bundle_id=bundle_id, meta_action=meta_action_battle) proxy_action_2 = actions_prototypes.ActionMetaProxyPrototype.create( hero=self.hero_2, _bundle_id=bundle_id, meta_action=meta_action_battle) self.assertEqual(len(self.storage.meta_actions), 1) self.assertEqual(len(self.storage.meta_actions_to_actions), 1) self.assertEqual( self.storage.meta_actions_to_actions[meta_action_battle.uid], set([ LogicStorage.get_action_uid(proxy_action_1), LogicStorage.get_action_uid(proxy_action_2) ])) self.storage.remove_action(proxy_action_2) self.assertEqual(len(self.storage.meta_actions), 1) self.assertEqual(len(self.storage.meta_actions_to_actions), 1) self.assertEqual( self.storage.meta_actions_to_actions[meta_action_battle.uid], set([LogicStorage.get_action_uid(proxy_action_1)])) self.storage.remove_action(proxy_action_1) self.assertEqual(len(self.storage.meta_actions), 0) self.assertEqual(len(self.storage.meta_actions_to_actions), 0) @mock.patch('the_tale.game.heroes.conf.heroes_settings.DUMP_CACHED_HEROES', True) @mock.patch( 'the_tale.game.conf.game_settings.SAVED_UNCACHED_HEROES_FRACTION', 0) def test_get_bundles_to_save(self): # hero 1 not saved # hero 2 saved by quota # hero 3 saved by caching # hero 4 not saved result, account_3_id, bundle_3_id = register_user( 'test_user_3', '*****@*****.**', '111111') result, account_4_id, bundle_4_id = register_user( 'test_user_4', '*****@*****.**', '111111') result, account_5_id, bundle_5_id = register_user( 'test_user_5', '*****@*****.**', '111111') self.storage.load_account_data( AccountPrototype.get_by_id(account_3_id)) self.storage.load_account_data( AccountPrototype.get_by_id(account_4_id)) self.storage.load_account_data( AccountPrototype.get_by_id(account_5_id)) hero_3 = self.storage.accounts_to_heroes[account_3_id] hero_4 = self.storage.accounts_to_heroes[account_4_id] self.hero_1._model.saved_at = datetime.datetime.now() self.hero_1.ui_caching_started_at = datetime.datetime.fromtimestamp(0) self.hero_2.ui_caching_started_at = datetime.datetime.fromtimestamp(0) hero_4.ui_caching_started_at = datetime.datetime.fromtimestamp(0) self.assertTrue(self.hero_1.saved_at > self.hero_2.saved_at) self.assertFalse(self.hero_1.is_ui_caching_required) self.assertFalse(self.hero_2.is_ui_caching_required) self.assertTrue(hero_3.is_ui_caching_required) self.assertFalse(hero_4.is_ui_caching_required) self.assertEqual(self.storage._get_bundles_to_save(), set([self.bundle_2_id, bundle_3_id, bundle_5_id])) @mock.patch('the_tale.game.heroes.conf.heroes_settings.DUMP_CACHED_HEROES', True) @mock.patch( 'the_tale.game.conf.game_settings.SAVED_UNCACHED_HEROES_FRACTION', 0) def test_get_bundles_to_save__force_save_required(self): # hero 1 not saved # hero 2 saved by quota # hero 3 saved by caching # hero 4 saved by force result, account_3_id, bundle_3_id = register_user( 'test_user_3', '*****@*****.**', '111111') result, account_4_id, bundle_4_id = register_user( 'test_user_4', '*****@*****.**', '111111') result, account_5_id, bundle_5_id = register_user( 'test_user_5', '*****@*****.**', '111111') self.storage.load_account_data( AccountPrototype.get_by_id(account_3_id)) self.storage.load_account_data( AccountPrototype.get_by_id(account_4_id)) self.storage.load_account_data( AccountPrototype.get_by_id(account_5_id)) hero_3 = self.storage.accounts_to_heroes[account_3_id] hero_4 = self.storage.accounts_to_heroes[account_4_id] hero_4.force_save_required = True self.hero_1._model.saved_at = datetime.datetime.now() self.hero_1.ui_caching_started_at = datetime.datetime.fromtimestamp(0) self.hero_2.ui_caching_started_at = datetime.datetime.fromtimestamp(0) hero_4.ui_caching_started_at = datetime.datetime.fromtimestamp(0) self.assertTrue(self.hero_1.saved_at > self.hero_2.saved_at) self.assertFalse(self.hero_1.is_ui_caching_required) self.assertFalse(self.hero_2.is_ui_caching_required) self.assertTrue(hero_3.is_ui_caching_required) self.assertFalse(hero_4.is_ui_caching_required) self.assertEqual( self.storage._get_bundles_to_save(), set([self.bundle_2_id, bundle_3_id, bundle_4_id, bundle_5_id])) self.assertFalse(hero_4.force_save_required) @mock.patch('the_tale.game.heroes.conf.heroes_settings.DUMP_CACHED_HEROES', False) @mock.patch( 'the_tale.game.conf.game_settings.SAVED_UNCACHED_HEROES_FRACTION', 0) def test_get_bundles_to_save__without_cache_dump(self): # hero 1 not saved # hero 2 saved by quota # hero 3 does not saved by caching # hero 4 not saved result, account_3_id, bundle_3_id = register_user( 'test_user_3', '*****@*****.**', '111111') result, account_4_id, bundle_4_id = register_user( 'test_user_4', '*****@*****.**', '111111') self.storage.load_account_data( AccountPrototype.get_by_id(account_3_id)) self.storage.load_account_data( AccountPrototype.get_by_id(account_4_id)) hero_3 = self.storage.accounts_to_heroes[account_3_id] hero_4 = self.storage.accounts_to_heroes[account_4_id] self.hero_1._model.saved_at = datetime.datetime.now() self.hero_1.ui_caching_started_at = datetime.datetime.fromtimestamp(0) self.hero_2.ui_caching_started_at = datetime.datetime.fromtimestamp(0) hero_4.ui_caching_started_at = datetime.datetime.fromtimestamp(0) self.assertTrue(self.hero_1.saved_at > self.hero_2.saved_at) self.assertFalse(self.hero_1.is_ui_caching_required) self.assertFalse(self.hero_2.is_ui_caching_required) self.assertTrue(hero_3.is_ui_caching_required) self.assertFalse(hero_4.is_ui_caching_required) self.assertEqual(self.storage._get_bundles_to_save(), set([self.bundle_2_id])) @mock.patch('the_tale.game.heroes.conf.heroes_settings.DUMP_CACHED_HEROES', True) @mock.patch( 'the_tale.game.conf.game_settings.SAVED_UNCACHED_HEROES_FRACTION', 0) def test_save_changed_data__with_multiple_heroes_to_bundle(self): # hero 1 saved by bundle from hero 3 # hero 2 saved by quota # hero 3 saved by caching result, account_3_id, bundle_3_id = register_user( 'test_user_3', '*****@*****.**', '111111') self.storage.load_account_data( AccountPrototype.get_by_id(account_3_id)) hero_3 = self.storage.accounts_to_heroes[account_3_id] self.hero_1._model.saved_at = datetime.datetime.now() self.hero_1.ui_caching_started_at = datetime.datetime.fromtimestamp(0) self.hero_1.actions.current_action.bundle_id = hero_3.actions.current_action.bundle_id self.hero_2.ui_caching_started_at = datetime.datetime.fromtimestamp(0) self.assertTrue(self.hero_1.saved_at > self.hero_2.saved_at) self.assertFalse(self.hero_1.is_ui_caching_required) self.assertFalse(self.hero_2.is_ui_caching_required) self.assertTrue(hero_3.is_ui_caching_required) self.assertEqual(self.storage._get_bundles_to_save(), set([self.bundle_2_id, bundle_3_id])) self.storage.process_turn() with mock.patch('dext.common.utils.cache.set_many') as set_many: with mock.patch( 'the_tale.game.logic_storage.LogicStorage._save_hero_data' ) as save_hero_data: with mock.patch( 'the_tale.game.heroes.prototypes.HeroPrototype.ui_info' ) as ui_info: self.storage.save_changed_data() self.assertEqual(set_many.call_count, 1) self.assertEqual(save_hero_data.call_count, 3) self.assertEqual(ui_info.call_count, 2) self.assertEqual(ui_info.call_args_list, [ mock.call(actual_guaranteed=True, old_info=None), mock.call(actual_guaranteed=True, old_info=None) ]) @mock.patch('the_tale.game.heroes.conf.heroes_settings.DUMP_CACHED_HEROES', False) @mock.patch( 'the_tale.game.conf.game_settings.SAVED_UNCACHED_HEROES_FRACTION', 0) def test_save_changed_data__with_multiple_heroes_to_bundle__without_cache_dump( self): # hero 1 saved by bundle from hero 2 # hero 2 saved by quota # hero 3 does not saved by caching result, account_3_id, bundle_3_id = register_user( 'test_user_3', '*****@*****.**', '111111') self.storage.load_account_data( AccountPrototype.get_by_id(account_3_id)) hero_3 = self.storage.accounts_to_heroes[account_3_id] self.hero_1._model.saved_at = datetime.datetime.now() self.hero_1.ui_caching_started_at = datetime.datetime.fromtimestamp(0) self.hero_1.actions.current_action.bundle_id = self.hero_2.actions.current_action.bundle_id self.hero_2.ui_caching_started_at = datetime.datetime.fromtimestamp(0) self.assertTrue(self.hero_1.saved_at > self.hero_2.saved_at) self.assertFalse(self.hero_1.is_ui_caching_required) self.assertFalse(self.hero_2.is_ui_caching_required) self.assertTrue(hero_3.is_ui_caching_required) self.assertEqual(self.storage._get_bundles_to_save(), set([self.bundle_2_id])) self.storage.process_turn() with mock.patch('dext.common.utils.cache.set_many') as set_many: with mock.patch( 'the_tale.game.logic_storage.LogicStorage._save_hero_data' ) as save_hero_data: with mock.patch( 'the_tale.game.heroes.prototypes.HeroPrototype.ui_info' ) as ui_info: self.storage.save_changed_data() self.assertEqual(set_many.call_count, 1) self.assertEqual(save_hero_data.call_count, 2) self.assertEqual(ui_info.call_count, 1) self.assertEqual(ui_info.call_args, mock.call(actual_guaranteed=True, old_info=None)) def test_merge_bundles(self): storage = LogicStorage() storage.bundles_to_accounts[555] = set([1, 2]) storage.bundles_to_accounts[666] = set([3]) storage.merge_bundles([555, 666], 777) self.assertEqual(storage.bundles_to_accounts, {777: set([1, 2, 3])}) def test_merge_bundles__in_existed_bundle(self): storage = LogicStorage() storage.bundles_to_accounts[555] = set([1, 2]) storage.bundles_to_accounts[666] = set([3]) storage.bundles_to_accounts[777] = set([4, 5]) storage.merge_bundles([555, 666], 777) self.assertEqual(storage.bundles_to_accounts, {777: set([1, 2, 3, 4, 5])}) def test_unmerge_bundles__in_existed_bundle(self): storage = LogicStorage() storage.bundles_to_accounts[555] = set([1, 2]) storage.bundles_to_accounts[666] = set([3]) storage.bundles_to_accounts[777] = set([4, 5]) storage.unmerge_bundles(4, 777, 666) self.assertEqual(storage.bundles_to_accounts, { 555: set([1, 2]), 666: set([3, 4]), 777: set([5]) }) def test_unmerge_bundles__last_account_in_bundle(self): storage = LogicStorage() storage.bundles_to_accounts[555] = set([1, 2]) storage.bundles_to_accounts[666] = set([3]) storage.bundles_to_accounts[777] = set([4, 5]) storage.unmerge_bundles(3, 666, 555) self.assertEqual(storage.bundles_to_accounts, { 555: set([1, 2, 3]), 777: set([4, 5]) }) def test_unmerge_bundles(self): storage = LogicStorage() storage.bundles_to_accounts[555] = set([1, 2]) storage.bundles_to_accounts[666] = set([3]) storage.bundles_to_accounts[777] = set([4, 5]) storage.unmerge_bundles(4, 777, 888) self.assertEqual(storage.bundles_to_accounts, { 555: set([1, 2]), 666: set([3]), 777: set([5]), 888: set([4]) }) def test_save_bundle_data(self): storage = LogicStorage() storage.bundles_to_accounts[555] = set([1, 2]) storage.bundles_to_accounts[666] = set([3, 7, 9]) storage.bundles_to_accounts[777] = set([4, 5]) storage.accounts_to_heroes = { 1: mock.Mock(id=1), 2: mock.Mock(id=2), 3: mock.Mock(id=3), 4: mock.Mock(id=4), 5: mock.Mock(id=5), 7: mock.Mock(id=7), 9: mock.Mock(id=9) } with mock.patch( 'the_tale.game.logic_storage.LogicStorage._save_hero_data' ) as _save_hero_data: with mock.patch( 'the_tale.game.logic_storage.LogicStorage.process_cache_queue' ) as process_cache_queue: storage.save_bundle_data(666) self.assertEqual(_save_hero_data.call_count, 3) self.assertEqual(storage.cache_queue, set([3, 7, 9])) self.assertEqual(process_cache_queue.call_count, 1) self.assertEqual( set(call[0][0] for call in _save_hero_data.call_args_list), set([3, 7, 9]))
class ResurrectActionTest(testcase.TestCase): def setUp(self): super(ResurrectActionTest, self).setUp() create_test_map() self.account = self.accounts_factory.create_account(is_fast=True) self.storage = LogicStorage() self.storage.load_account_data(self.account) self.hero = self.storage.accounts_to_heroes[self.account.id] self.action_idl = self.hero.actions.current_action self.hero.kill() self.action_resurrect = ActionResurrectPrototype.create(hero=self.hero) def tearDown(self): pass def test_create(self): self.assertEqual(self.action_idl.leader, False) self.assertEqual(self.action_resurrect.leader, True) self.assertEqual(self.action_resurrect.bundle_id, self.action_idl.bundle_id) self.assertEqual(len(self.action_resurrect.HELP_CHOICES), 1) self.assertTrue( list(self.action_resurrect.HELP_CHOICES)[0].is_RESURRECT) self.storage._test_save() def test_processed(self): for i in range(c.TURNS_TO_RESURRECT - 1): self.storage.process_turn() turn.increment() self.assertEqual(len(self.hero.actions.actions_list), 2) self.assertEqual(self.hero.actions.current_action, self.action_resurrect) self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(len(self.hero.actions.actions_list), 1) self.assertEqual(self.hero.actions.current_action, self.action_idl) self.assertEqual(self.hero.health, self.hero.max_health) self.assertEqual(self.hero.is_alive, True) self.storage._test_save() def test_full(self): while len(self.hero.actions.actions_list) != 1: self.storage.process_turn(continue_steps_if_needed=False) turn.increment() self.assertTrue(self.action_idl.leader) self.assertEqual(self.hero.health, self.hero.max_health) self.assertEqual(self.hero.is_alive, True) self.storage._test_save() def test_fast_resurrect(self): self.action_resurrect.fast_resurrect() self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(len(self.hero.actions.actions_list), 1) self.assertEqual(self.hero.actions.current_action, self.action_idl) self.assertEqual(self.hero.health, self.hero.max_health) self.assertEqual(self.hero.is_alive, True) self.storage._test_save()
class RegenerateEnergyActionTest(testcase.TestCase): def setUp(self): super(RegenerateEnergyActionTest, self).setUp() create_test_map() result, account_id, bundle_id = register_user('test_user') self.storage = LogicStorage() self.storage.load_account_data(AccountPrototype.get_by_id(account_id)) self.hero = self.storage.accounts_to_heroes[account_id] self.action_idl = self.hero.actions.current_action self.action_regenerate = ActionRegenerateEnergyPrototype.create(hero=self.hero) def tearDown(self): pass def test_create(self): self.assertEqual(self.action_idl.leader, False) self.assertEqual(self.action_regenerate.leader, True) self.assertEqual(self.action_regenerate.bundle_id, self.action_idl.bundle_id) self.storage._test_save() def test_not_ready(self): self.storage.process_turn() self.assertEqual(len(self.hero.actions.actions_list), 2) self.assertEqual(self.hero.actions.current_action, self.action_regenerate) self.storage._test_save() @mock.patch('the_tale.game.heroes.objects.Hero.can_regenerate_double_energy', False) def test_full(self): self.hero.change_energy(-self.hero.energy) current_time = TimePrototype.get_current_time() while len(self.hero.actions.actions_list) != 1: self.storage.process_turn(continue_steps_if_needed=False) current_time.increment_turn() self.assertTrue(self.action_idl.leader) self.assertEqual(self.hero.energy, self.hero.preferences.energy_regeneration_type.amount) self.assertEqual(self.hero.need_regenerate_energy, False) self.assertEqual(self.hero.last_energy_regeneration_at_turn, TimePrototype.get_current_turn_number()-1) self.storage._test_save() @mock.patch('the_tale.game.heroes.objects.Hero.can_regenerate_double_energy', True) def test_full__double_energy(self): self.hero.change_energy(-self.hero.energy) current_time = TimePrototype.get_current_time() while len(self.hero.actions.actions_list) != 1: self.storage.process_turn(continue_steps_if_needed=False) current_time.increment_turn() self.assertTrue(self.action_idl.leader) self.assertEqual(self.hero.energy, self.hero.preferences.energy_regeneration_type.amount * 2) self.assertEqual(self.hero.need_regenerate_energy, False) self.assertEqual(self.hero.last_energy_regeneration_at_turn, TimePrototype.get_current_turn_number()-1) self.storage._test_save()
class CommonTests(testcase.TestCase): def setUp(self): super(CommonTests, self).setUp() create_test_map() self.account = self.accounts_factory.create_account() self.storage = LogicStorage() self.storage.load_account_data(self.account) self.hero = self.storage.accounts_to_heroes[self.account.id] def test_rarities_abilities(self): for rarity, rarity_abilities in helpers.RARITIES_ABILITIES.iteritems(): companion = logic.create_random_companion_record('%s companion' % rarity, abilities=rarity_abilities) self.assertEqual(companion.rarity, rarity) @mock.patch('the_tale.game.companions.objects.Companion.max_coherence', 100) @mock.patch('the_tale.game.heroes.habilities.companions.THOUGHTFUL.MULTIPLIER', [1, 1, 1, 1, 1]) @mock.patch('the_tale.game.heroes.habilities.companions._CompanionHealBase.PROBABILITY', [0, 0, 0, 0, 0]) def _test_companion_death_speed(self): current_time = game_prototypes.TimePrototype.get_current_time() companion_record = logic.create_random_companion_record('test companion', state=relations.STATE.ENABLED, dedication=relations.DEDICATION.BRAVE)#,#,; # abilities=abilities_container.Container(start=(effects.ABILITIES.BODYGUARD,)),# effects.ABILITIES.PUNY)), # dedication=relations.DEDICATION.HEROIC) # abilities=abilities_container.Container(common=(effects.ABILITIES.COWARDLY, )), # dedication=relations.DEDICATION.INDECISIVE) companion = logic.create_companion(companion_record) self.hero.set_companion(companion) # self.hero.preferences.set_companion_dedication(heroes_relations.COMPANION_DEDICATION.EGOISM) self.hero.preferences.set_companion_dedication(heroes_relations.COMPANION_DEDICATION.NORMAL) # self.hero.preferences.set_companion_dedication(heroes_relations.COMPANION_DEDICATION.ALTRUISM) # self.hero.preferences.set_companion_dedication(heroes_relations.COMPANION_DEDICATION.EVERY_MAN_FOR_HIMSELF) old_health = self.hero.companion.health print 'defend_probability: ', self.hero.companion.defend_in_battle_probability # for i in xrange(50): # self.hero.randomized_level_up(increment_level=True) while self.hero.companion: self.hero.companion.coherence = 50 self.storage.process_turn() current_time.increment_turn() self.hero.randomized_level_up() if not self.hero.is_alive: if hasattr(self.hero.actions.current_action, 'fast_resurrect'): self.hero.actions.current_action.fast_resurrect() print '!' if self.hero.companion: if old_health != self.hero.companion.health: print '%.2f:\t%s -> %s [%s] c%s' % ( (current_time.turn_number / c.TURNS_IN_HOUR / 24.0), self.hero.companion.health - self.hero.companion.max_health, self.hero.companion.health, self.hero.companion.health - old_health, self.hero.companion.coherence) old_health = self.hero.companion.health
class MoveToActionWithBreaksTest(testcase.TestCase): FIRST_BREAK_AT = 0.75 def setUp(self): super(MoveToActionWithBreaksTest, self).setUp() self.p1, self.p2, self.p3 = create_test_map() account = self.accounts_factory.create_account(is_fast=True) self.storage = LogicStorage() self.storage.load_account_data(account) self.hero = self.storage.accounts_to_heroes[account.id] self.action_idl = self.hero.actions.current_action self.hero.position.set_place(self.p1) self.action_move = prototypes.ActionMoveToPrototype.create(hero=self.hero, destination=self.p3, break_at=self.FIRST_BREAK_AT) def test_sequence_move(self): current_time = TimePrototype.get_current_time() while self.hero.actions.current_action != self.action_idl: self.storage.process_turn(continue_steps_if_needed=False) current_time.increment_turn() self.assertEqual(self.hero.position.road.point_1_id, self.p2.id) self.assertEqual(self.hero.position.road.point_2_id, self.p3.id) real_percents = None prototypes.ActionMoveToPrototype.create(hero=self.hero, destination=self.p1, break_at=0.9) while self.hero.actions.current_action != self.action_idl: real_percents = self.hero.actions.current_action.percents self.storage.process_turn(continue_steps_if_needed=False) current_time.increment_turn() self.assertEqual(round(real_percents, 1), 0.9) self.assertEqual(self.hero.position.road.point_1_id, self.p1.id) self.assertEqual(self.hero.position.road.point_2_id, self.p2.id) prototypes.ActionMoveToPrototype.create(hero=self.hero, destination=self.p2) while self.hero.position.place is None or self.hero.position.place.id != self.p2.id: self.storage.process_turn(continue_steps_if_needed=False) current_time.increment_turn() self.assertEqual(self.hero.actions.current_action.TYPE, prototypes.ActionInPlacePrototype.TYPE) self.storage._test_save() @mock.patch('the_tale.game.heroes.objects.Hero.is_battle_start_needed', lambda self: False) def test_teleport_to_place__break_at(self): self.storage.process_turn(continue_steps_if_needed=False) self.action_move.teleport_to_place(create_inplace_action=True) self.assertEqual(self.hero.position.place.id, self.p2.id) while not self.hero.actions.current_action.TYPE.is_MOVE_TO: self.storage.process_turn(continue_steps_if_needed=False) self.storage.process_turn(continue_steps_if_needed=False) self.assertTrue(self.action_move.teleport_to_place(create_inplace_action=True)) self.assertNotEqual(self.hero.position.road, None) self.assertTrue(self.hero.position.percents < 1) self.assertTrue(self.hero.actions.current_action.TYPE.is_MOVE_TO) self.assertEqual(self.hero.actions.current_action.percents, self.FIRST_BREAK_AT) self.assertTrue(self.action_move.leader) self.assertEqual(self.hero.position.place, None) self.storage._test_save() @mock.patch('the_tale.game.heroes.objects.Hero.is_battle_start_needed', lambda self: False) def test_teleport_to_end__break_at(self): self.storage.process_turn(continue_steps_if_needed=False) self.action_move.teleport_to_end() self.assertNotEqual(self.hero.position.road, None) self.assertTrue(self.hero.position.percents < 1.0) self.assertEqual(self.p2.id, self.hero.position.road.point_1_id) self.assertEqual(self.p3.id, self.hero.position.road.point_2_id) self.assertTrue(self.hero.actions.current_action.TYPE.is_MOVE_TO) self.assertEqual(self.hero.actions.current_action.percents, self.FIRST_BREAK_AT) self.assertTrue(self.action_move.leader) self.storage._test_save()
class GetArtifactMixin(CardsTestMixin): CARD = None RARITIES = None HAS_USELESS = False def setUp(self): super(GetArtifactMixin, self).setUp() create_test_map() self.account_1 = self.accounts_factory.create_account() self.storage = LogicStorage() self.storage.load_account_data(self.account_1) self.hero = self.storage.accounts_to_heroes[self.account_1.id] self.card = self.CARD() def test_use(self): rarities = set() has_useless = False for i in range(10000): result, step, postsave_actions = self.card.use( **self.use_attributes(storage=self.storage, hero=self.hero)) self.assertEqual((result, step, postsave_actions), (ComplexChangeTask.RESULT.SUCCESSED, ComplexChangeTask.STEP.SUCCESS, ())) artifact = list(self.hero.bag.values())[0] self.hero.bag.pop_artifact(artifact) rarities.add(artifact.rarity) has_useless = has_useless or artifact.type.is_USELESS self.assertEqual(has_useless, self.HAS_USELESS) self.assertEqual(rarities, self.RARITIES) def test_use__full_bag(self): with self.check_delta(lambda: self.hero.bag.occupation, 1000): for i in range(1000): result, step, postsave_actions = self.card.use( **self.use_attributes(storage=self.storage, hero=self.hero)) self.assertEqual((result, step, postsave_actions), (ComplexChangeTask.RESULT.SUCCESSED, ComplexChangeTask.STEP.SUCCESS, ())) def test_use_when_trading(self): from the_tale.game.actions.prototypes import ActionTradingPrototype action_idl = self.hero.actions.current_action action_trade = ActionTradingPrototype.create(hero=self.hero) result, step, postsave_actions = self.card.use( **self.use_attributes(storage=self.storage, hero=self.hero)) self.assertEqual((result, step, postsave_actions), (ComplexChangeTask.RESULT.SUCCESSED, ComplexChangeTask.STEP.SUCCESS, ())) self.assertEqual(self.hero.bag.occupation, 1) self.assertTrue(action_trade.replane_required) self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(self.hero.actions.current_action, action_idl) self.assertEqual(self.hero.bag.occupation, 1)
class QuestActionTests(testcase.TestCase): def setUp(self): super(QuestActionTests, self).setUp() create_test_map() result, account_id, bundle_id = register_user('test_user') self.storage = LogicStorage() self.storage.load_account_data(AccountPrototype.get_by_id(account_id)) self.hero = self.storage.accounts_to_heroes[account_id] self.action_idl = self.hero.actions.current_action self.action_quest = prototypes.ActionQuestPrototype.create(hero=self.hero) def test_create(self): self.assertEqual(self.action_idl.leader, False) self.assertEqual(self.action_quest.leader, True) self.assertEqual(self.action_quest.state, self.action_quest.STATE.SEARCHING) self.assertEqual(self.action_quest.bundle_id, self.action_idl.bundle_id) self.assertFalse(self.hero.quests.has_quests) self.storage._test_save() def test_setup_quest(self): quests_helpers.setup_quest(self.hero) self.assertEqual(self.action_quest.state, self.action_quest.STATE.PROCESSING) self.assertTrue(self.hero.quests.has_quests) self.storage._test_save() def test_one_step(self): self.storage.process_turn() # quest can create new action on first step self.assertTrue(2 <= len(self.hero.actions.actions_list) <= 3) self.storage._test_save() def test_step_with_no_quest(self): quests_helpers.setup_quest(self.hero) self.hero.quests.pop_quest() self.storage.process_turn() self.assertEqual(self.action_idl.leader, True) def test_need_equipping(self): with mock.patch('the_tale.game.heroes.objects.Hero.need_equipping', lambda hero: True): self.storage.process_turn() self.assertEqual(self.action_quest.state, self.action_quest.STATE.EQUIPPING) self.assertTrue(self.hero.actions.current_action.TYPE.is_EQUIPPING) self.storage.process_turn() self.assertEqual(self.action_quest.state, self.action_quest.STATE.EQUIPPING) self.assertTrue(self.hero.actions.current_action.TYPE.is_QUEST) self.storage.process_turn() self.assertEqual(self.action_quest.state, self.action_quest.STATE.PROCESSING) def test_full_quest(self): current_time = TimePrototype.get_current_time() # just test that quest will be ended while not self.action_idl.leader: self.storage.process_turn() current_time.increment_turn() self.storage._test_save() self.assertFalse(self.hero.quests.has_quests)
class InPlaceActionCompanionBuyMealTests(testcase.TestCase): def setUp(self): super(InPlaceActionCompanionBuyMealTests, self).setUp() self.place_1, self.place_2, self.place_3 = create_test_map() self.account = self.accounts_factory.create_account() self.storage = LogicStorage() self.storage.load_account_data(self.account) self.hero = self.storage.accounts_to_heroes[self.account.id] self.action_idl = self.hero.actions.current_action self.companion_record = companions_logic.create_random_companion_record('thief', state=companions_relations.STATE.ENABLED) self.hero.set_companion(companions_logic.create_companion(self.companion_record)) self.hero.money = f.expected_gold_in_day(self.hero.level) self.hero.position.set_place(self.place_1) self.hero.position.update_previous_place() self.hero.position.set_place(self.place_2) self.hero.position.move_out_place() @mock.patch('the_tale.game.heroes.objects.Hero.companion_money_for_food_multiplier', 1) @mock.patch('the_tale.game.heroes.objects.Hero.can_companion_eat', lambda hero: True) def test_buy_meal__from_moveto(self): prototypes.ActionMoveToPrototype.create(hero=self.hero, destination=self.place_3) current_time = TimePrototype.get_current_time() with contextlib.nested( self.check_decreased(lambda: self.hero.money), self.check_increased(lambda: self.hero.statistics.money_spend_for_companions)): while self.hero.actions.current_action.TYPE != prototypes.ActionInPlacePrototype.TYPE: current_time.increment_turn() self.storage.process_turn(continue_steps_if_needed=False) @mock.patch('the_tale.game.heroes.objects.Hero.companion_money_for_food_multiplier', 0.5) @mock.patch('the_tale.game.heroes.objects.Hero.can_companion_eat', lambda hero: True) def test_buy_meal(self): self.hero.position.last_place_visited_turn = TimePrototype.get_current_turn_number() - c.TURNS_IN_HOUR * 12 with contextlib.nested( self.check_decreased(lambda: self.hero.money), self.check_increased(lambda: self.hero.statistics.money_spend_for_companions) ): prototypes.ActionInPlacePrototype.create(hero=self.hero) def check_not_used(self): with contextlib.nested( self.check_not_changed(lambda: self.hero.money), self.check_not_changed(lambda: self.hero.statistics.money_spend_for_companions)): prototypes.ActionInPlacePrototype.create(hero=self.hero) @mock.patch('the_tale.game.heroes.objects.Hero.companion_money_for_food_multiplier', 0.5) @mock.patch('the_tale.game.heroes.objects.Hero.can_companion_eat', lambda hero: True) def test_no_turns_since_last_visit(self): self.hero.position.last_place_visited_turn = TimePrototype.get_current_turn_number() self.check_not_used() @mock.patch('the_tale.game.heroes.objects.Hero.companion_money_for_food_multiplier', 66666666) @mock.patch('the_tale.game.heroes.objects.Hero.can_companion_eat', lambda hero: True) def test_not_enough_money(self): self.hero.position.last_place_visited_turn = TimePrototype.get_current_turn_number() - 1000 with contextlib.nested( self.check_delta(lambda: self.hero.money, -self.hero.money), self.check_delta(lambda: self.hero.statistics.money_spend_for_companions, self.hero.money )): prototypes.ActionInPlacePrototype.create(hero=self.hero) @mock.patch('the_tale.game.heroes.objects.Hero.companion_money_for_food_multiplier', 66666666) @mock.patch('the_tale.game.heroes.objects.Hero.can_companion_eat', lambda hero: True) def test_no_money(self): self.hero.money = 0 self.check_not_used() @mock.patch('the_tale.game.heroes.objects.Hero.companion_money_for_food_multiplier', 0.5) @mock.patch('the_tale.game.heroes.objects.Hero.can_companion_eat', lambda hero: False) def test_companion_does_not_eat(self): self.check_not_used()
class InPlaceActionCompanionStealingTest(testcase.TestCase): def setUp(self): super(InPlaceActionCompanionStealingTest, self).setUp() create_test_map() self.account = self.accounts_factory.create_account() self.storage = LogicStorage() self.storage.load_account_data(self.account) self.hero = self.storage.accounts_to_heroes[self.account.id] self.hero.position.previous_place_id = None # test setting prevouse place in action constructor self.action_idl = self.hero.actions.current_action self.action_inplace = prototypes.ActionInPlacePrototype.create(hero=self.hero) self.action_inplace.state = self.action_inplace.STATE.PROCESSED self.companion_record = companions_logic.create_random_companion_record('thief', state=companions_relations.STATE.ENABLED) self.hero.set_companion(companions_logic.create_companion(self.companion_record)) @mock.patch('the_tale.game.heroes.objects.Hero.can_companion_steal_money', lambda self: True) @mock.patch('the_tale.game.heroes.objects.Hero.can_companion_steal_item', lambda self: True) def test_no_companion(self): self.hero.remove_companion() with contextlib.nested( self.check_not_changed(lambda: self.hero.bag.occupation), self.check_not_changed(lambda: self.hero.money), self.check_not_changed(lambda: self.hero.statistics.money_earned_from_companions), self.check_not_changed(lambda: self.hero.statistics.artifacts_had), self.check_not_changed(lambda: self.hero.statistics.loot_had), self.check_not_changed(lambda: len(self.hero.journal)) ): self.storage.process_turn(continue_steps_if_needed=False) @mock.patch('the_tale.game.heroes.objects.Hero.can_companion_steal_money', lambda self: True) @mock.patch('the_tale.game.heroes.objects.Hero.can_companion_steal_item', lambda self: True) def test_place_not_changed(self): self.hero.position.update_previous_place() with contextlib.nested( self.check_not_changed(lambda: self.hero.bag.occupation), self.check_not_changed(lambda: self.hero.money), self.check_not_changed(lambda: self.hero.statistics.money_earned_from_companions), self.check_not_changed(lambda: self.hero.statistics.artifacts_had), self.check_not_changed(lambda: self.hero.statistics.loot_had), self.check_not_changed(lambda: len(self.hero.journal)) ): self.storage.process_turn(continue_steps_if_needed=False) @mock.patch('the_tale.game.heroes.objects.Hero.can_companion_steal_money', lambda self: True) @mock.patch('the_tale.game.heroes.objects.Hero.can_companion_steal_item', lambda self: False) def test_steal_money(self): with contextlib.nested( self.check_not_changed(lambda: self.hero.bag.occupation), self.check_increased(lambda: self.hero.money), self.check_increased(lambda: self.hero.statistics.money_earned_from_companions), self.check_not_changed(lambda: self.hero.statistics.artifacts_had), self.check_not_changed(lambda: self.hero.statistics.loot_had), self.check_increased(lambda: len(self.hero.journal)) ): self.storage.process_turn(continue_steps_if_needed=False) @mock.patch('the_tale.game.heroes.objects.Hero.can_companion_steal_money', lambda self: False) @mock.patch('the_tale.game.heroes.objects.Hero.can_companion_steal_item', lambda self: True) @mock.patch('the_tale.game.heroes.objects.Hero.artifacts_probability', lambda self, mob: 0) def test_steal_item__loot(self): with contextlib.nested( self.check_increased(lambda: self.hero.bag.occupation), self.check_not_changed(lambda: self.hero.money), self.check_not_changed(lambda: self.hero.statistics.money_earned_from_companions), self.check_not_changed(lambda: self.hero.statistics.artifacts_had), self.check_delta(lambda: self.hero.statistics.loot_had, 1), self.check_increased(lambda: len(self.hero.journal)) ): self.storage.process_turn(continue_steps_if_needed=False) @mock.patch('the_tale.game.heroes.objects.Hero.can_companion_steal_money', lambda self: False) @mock.patch('the_tale.game.heroes.objects.Hero.can_companion_steal_item', lambda self: True) @mock.patch('the_tale.game.heroes.objects.Hero.artifacts_probability', lambda self, mob: 1) def test_steal_item__artifact(self): with contextlib.nested( self.check_increased(lambda: self.hero.bag.occupation), self.check_not_changed(lambda: self.hero.money), self.check_not_changed(lambda: self.hero.statistics.money_earned_from_companions), self.check_delta(lambda: self.hero.statistics.artifacts_had, 1), self.check_not_changed(lambda: self.hero.statistics.loot_had), self.check_increased(lambda: len(self.hero.journal)) ): self.storage.process_turn(continue_steps_if_needed=False) @mock.patch('the_tale.game.heroes.objects.Hero.can_companion_steal_money', lambda self: False) @mock.patch('the_tale.game.heroes.objects.Hero.can_companion_steal_item', lambda self: True) @mock.patch('the_tale.game.heroes.objects.Hero.bag_is_full', True) def test_steal_item__bag_is_full(self): with contextlib.nested( self.check_not_changed(lambda: self.hero.bag.occupation), self.check_not_changed(lambda: self.hero.money), self.check_not_changed(lambda: self.hero.statistics.money_earned_from_companions), self.check_not_changed(lambda: self.hero.statistics.artifacts_had), self.check_not_changed(lambda: self.hero.statistics.loot_had), self.check_not_changed(lambda: len(self.hero.journal)) ): self.storage.process_turn(continue_steps_if_needed=False) @mock.patch('the_tale.game.heroes.objects.Hero.can_companion_steal_money', lambda self: True) @mock.patch('the_tale.game.heroes.objects.Hero.can_companion_steal_item', lambda self: True) def test_steal_all(self): with contextlib.nested( self.check_increased(lambda: self.hero.bag.occupation), self.check_increased(lambda: self.hero.money), self.check_increased(lambda: self.hero.statistics.money_earned_from_companions), self.check_delta(lambda: self.hero.statistics.artifacts_had + self.hero.statistics.loot_had, 1), self.check_increased(lambda: len(self.hero.journal)) ): self.storage.process_turn(continue_steps_if_needed=False)
class InPlaceActionSpendMoneyTest(testcase.TestCase): def setUp(self): super(InPlaceActionSpendMoneyTest, self).setUp() create_test_map() result, account_id, bundle_id = register_user('test_user') self.storage = LogicStorage() self.storage.load_account_data(AccountPrototype.get_by_id(account_id)) self.hero = self.storage.accounts_to_heroes[account_id] self.action_idl = self.hero.actions.current_action self.action_inplace = prototypes.ActionInPlacePrototype.create(hero=self.hero) def test_no_money(self): self.hero.money = 1 self.storage.process_turn() self.assertEqual(self.hero.money, 1) self.assertEqual(self.hero.statistics.money_spend, 0) self.storage._test_save() @mock.patch('the_tale.game.heroes.objects.Hero.buy_price', lambda hero: -100) def test_buy_price_less_than_zero(self): money = self.hero.spend_amount self.hero.money = money self.assertEqual(self.action_inplace.try_to_spend_money(), 1) self.assertEqual(self.hero.statistics.money_spend, 1) def test_instant_heal(self): while not self.hero.next_spending.is_INSTANT_HEAL: self.hero.switch_spending() money = self.hero.spend_amount self.hero.money = money + 666 self.hero.health = 1 self.storage.process_turn() self.assertEqual(self.hero.money, 666) self.assertEqual(self.hero.health, self.hero.max_health) self.assertEqual(self.hero.statistics.money_spend, money) self.assertEqual(self.hero.statistics.money_spend_for_heal, money) self.storage._test_save() def test_instant_heal__switch_on_full_health(self): while not self.hero.next_spending.is_INSTANT_HEAL: self.hero.switch_spending() money = self.hero.spend_amount self.hero.money = money + 666 self.hero.health = self.hero.max_health with mock.patch('the_tale.game.heroes.objects.Hero.switch_spending') as switch_spending: self.storage.process_turn() self.assertEqual(switch_spending.call_count, 1) self.assertEqual(self.hero.money, money + 666) self.assertEqual(self.hero.health, self.hero.max_health) self.assertEqual(self.hero.statistics.money_spend, 0) self.assertEqual(self.hero.statistics.money_spend_for_heal, 0) self.storage._test_save() def test_instant_heal__too_much_health(self): while not self.hero.next_spending.is_INSTANT_HEAL: self.hero.switch_spending() money = self.hero.spend_amount health = (self.hero.max_health * c.SPEND_MONEY_FOR_HEAL_HEALTH_FRACTION) + 1 self.hero.money = money + 666 self.hero.health = health self.storage.process_turn() self.assertTrue(self.hero.money, money + 666) self.assertEqual(self.hero.health, health) self.assertEqual(self.hero.statistics.money_spend, 0) self.assertEqual(self.hero.statistics.money_spend_for_heal, 0) self.storage._test_save() def test_instant_heal__low_health(self): while not self.hero.next_spending.is_INSTANT_HEAL: self.hero.switch_spending() money = self.hero.spend_amount health = (self.hero.max_health * c.SPEND_MONEY_FOR_HEAL_HEALTH_FRACTION) - 1 self.hero.money = money self.hero.health = health self.storage.process_turn() self.assertEqual(self.hero.money, 0) self.assertEqual(self.hero.health, self.hero.max_health) self.assertEqual(self.hero.statistics.money_spend, money) self.assertEqual(self.hero.statistics.money_spend_for_heal, money) self.storage._test_save() def test_buying_artifact_with_hero_preferences(self): while not self.hero.next_spending.is_BUYING_ARTIFACT: self.hero.switch_spending() money = self.hero.spend_amount self.assertEqual(self.hero.statistics.money_spend, 0) self.assertEqual(self.hero.statistics.money_spend_for_artifacts, 0) self.assertEqual(self.hero.statistics.money_earned_from_artifacts, 0) #unequip all arefact self.hero.equipment._remove_all() # self.hero.preferences.set_equipment_slot(EQUIPMENT_SLOT.PLATE) heroes_logic.save_hero(self.hero) #buy artifact self.hero.money = money self.storage.process_turn() self.assertTrue(self.hero.money < 1) self.assertEqual(len(self.hero.bag.items()), 0) self.assertEqual(self.hero.statistics.money_spend, money - self.hero.money) self.assertEqual(self.hero.statistics.money_spend_for_artifacts, money - self.hero.money) self.assertEqual(self.hero.statistics.artifacts_had, 1) # # hero must not buy artifact in preferences slot, he has special quest for this # self.assertEqual(self.hero.equipment.get(EQUIPMENT_SLOT.PLATE), None) # self.storage._test_save() def test_buying_artifact_without_change(self): while not self.hero.next_spending.is_BUYING_ARTIFACT: self.hero.switch_spending() money = self.hero.spend_amount self.assertEqual(self.hero.statistics.money_spend, 0) self.assertEqual(self.hero.statistics.money_spend_for_artifacts, 0) self.assertEqual(self.hero.statistics.money_earned_from_artifacts, 0) #unequip all arefact self.hero.equipment._remove_all() heroes_logic.save_hero(self.hero) #buy artifact self.hero.money = money self.storage.process_turn() self.assertEqual(self.hero.money, 0) self.assertEqual(len(self.hero.bag.items()), 0) self.assertEqual(self.hero.statistics.money_spend, money - self.hero.money) self.assertEqual(self.hero.statistics.money_spend_for_artifacts, money - self.hero.money) self.assertEqual(self.hero.statistics.artifacts_had, 1) self.storage._test_save() def test_buying_artifact_with_change(self): while not self.hero.next_spending.is_BUYING_ARTIFACT: self.hero.switch_spending() # fill all slots with artifacts self.hero.equipment.test_equip_in_all_slots(artifacts_storage.generate_artifact_from_list(artifacts_storage.artifacts, self.hero.level, rarity=RARITY.NORMAL)) money = self.hero.spend_amount #buy artifact self.hero.money = money self.assertEqual(self.hero.statistics.money_spend, 0) self.assertEqual(self.hero.statistics.money_spend_for_artifacts, 0) self.assertEqual(self.hero.statistics.money_earned_from_artifacts, 0) self.storage.process_turn() self.assertTrue(self.hero.money > 0) self.assertEqual(len(self.hero.bag.items()), 0) self.assertTrue(self.hero.statistics.money_spend > money - self.hero.money) self.assertTrue(self.hero.statistics.money_spend_for_artifacts > money - self.hero.money) self.assertEqual(self.hero.statistics.artifacts_had, 1) self.assertTrue(self.hero.statistics.money_earned_from_artifacts > 0) self.storage._test_save() def test_not_bying_artifact__when_has_equip_candidates_in_bag(self): while not self.hero.next_spending.is_BUYING_ARTIFACT: self.hero.switch_spending() # fill all slots with artifacts self.hero.equipment.test_equip_in_all_slots(artifacts_storage.generate_artifact_from_list(artifacts_storage.artifacts, self.hero.level, rarity=RARITY.NORMAL)) money = self.hero.spend_amount self.hero.money = money self.hero.bag.put_artifact(artifacts_storage.generate_artifact_from_list(artifacts_storage.artifacts, 666, rarity=RARITY.EPIC)) with self.check_not_changed(lambda: self.hero.statistics.money_spend): with self.check_not_changed(lambda: self.hero.statistics.money_spend_for_artifacts): with self.check_not_changed(lambda: self.hero.statistics.money_earned_from_artifacts): with self.check_not_changed(lambda: self.hero.statistics.artifacts_had): with self.check_not_changed(lambda: self.hero.bag.occupation): self.storage.process_turn() def test_sharpening_artifact(self): while not self.hero.next_spending.is_SHARPENING_ARTIFACT: self.hero.switch_spending() money = self.hero.spend_amount old_power = self.hero.power.clone() self.hero.money = money self.storage.process_turn() self.assertEqual(self.hero.money, 0) self.assertEqual(old_power.total() + 1, self.hero.power.total()) self.assertEqual(self.hero.statistics.money_spend, money - self.hero.money) self.assertEqual(self.hero.statistics.money_spend_for_sharpening, money - self.hero.money) self.storage._test_save() @mock.patch('the_tale.game.heroes.objects.Hero.can_upgrade_prefered_slot', True) def test_sharpening_artifact_with_hero_preferences(self): while not self.hero.next_spending.is_SHARPENING_ARTIFACT: self.hero.switch_spending() self.hero.preferences.set_equipment_slot(heroes_relations.EQUIPMENT_SLOT.PLATE) self.hero.level = 666 # enshure that equipment power will be less than max allowerd _power heroes_logic.save_hero(self.hero) money = self.hero.spend_amount old_power = self.hero.power.clone() old_plate_power = self.hero.equipment.get(heroes_relations.EQUIPMENT_SLOT.PLATE).power.clone() self.hero.money = money self.storage.process_turn() self.assertEqual(self.hero.money, 0) self.assertEqual(old_power.total() + 1, self.hero.power.total()) self.assertEqual(old_plate_power.total() + 1, self.hero.equipment.get(heroes_relations.EQUIPMENT_SLOT.PLATE).power.total()) self.assertEqual(self.hero.statistics.money_spend, money - self.hero.money) self.assertEqual(self.hero.statistics.money_spend_for_sharpening, money - self.hero.money) self.storage._test_save() def test_repair_artifact(self): for artifact in self.hero.equipment.values(): artifact.integrity = artifact.max_integrity test_artifact = artifact test_artifact.integrity = 0 while not self.hero.next_spending.is_REPAIRING_ARTIFACT: self.hero.switch_spending() money = self.hero.spend_amount self.hero.money = money self.storage.process_turn() self.assertEqual(self.hero.money, 0) self.assertEqual(test_artifact.integrity, test_artifact.max_integrity) self.assertEqual(self.hero.statistics.money_spend, money - self.hero.money) self.assertEqual(self.hero.statistics.money_spend_for_repairing, money - self.hero.money) self.storage._test_save() def test_useless(self): while not self.hero.next_spending.is_USELESS: self.hero.switch_spending() money = self.hero.spend_amount self.hero.money = money self.storage.process_turn() self.assertEqual(self.hero.money, 0) self.assertEqual(self.hero.statistics.money_spend, money - self.hero.money) self.assertEqual(self.hero.statistics.money_spend_for_useless, money - self.hero.money) self.storage._test_save() def test_impact(self): while not self.hero.next_spending.is_IMPACT: self.hero.switch_spending() money = self.hero.spend_amount self.hero.money = money with mock.patch('the_tale.game.persons.objects.Person.cmd_change_power') as cmd_change_power: self.storage.process_turn() self.assertEqual(cmd_change_power.call_count, 0) self.assertEqual(self.hero.money, 0) self.assertEqual(self.hero.statistics.money_spend, money - self.hero.money) self.assertEqual(self.hero.statistics.money_spend_for_impact, money - self.hero.money) self.storage._test_save() def test_impact__can_change_power(self): while not self.hero.next_spending.is_IMPACT: self.hero.switch_spending() money = self.hero.spend_amount self.hero.money = money with mock.patch('the_tale.game.heroes.objects.Hero.can_change_person_power', lambda self, person: True): with mock.patch('the_tale.game.persons.objects.Person.cmd_change_power') as cmd_change_power: self.storage.process_turn() self.assertEqual(cmd_change_power.call_count, 1) self.assertEqual(self.hero.money, 0) self.assertEqual(self.hero.statistics.money_spend, money - self.hero.money) self.assertEqual(self.hero.statistics.money_spend_for_impact, money - self.hero.money) self.storage._test_save() def test_experience(self): while not self.hero.next_spending.is_EXPERIENCE: self.hero.switch_spending() money = self.hero.spend_amount self.hero.money = money self.storage.process_turn() self.assertEqual(self.hero.money, 0) self.assertEqual(self.hero.statistics.money_spend, money - self.hero.money) self.assertEqual(self.hero.statistics.money_spend_for_experience, money - self.hero.money) self.storage._test_save() def test_heal_companion(self): self.companion_record = companions_logic.create_random_companion_record('companion', state=companions_relations.STATE.ENABLED) self.hero.set_companion(companions_logic.create_companion(self.companion_record)) self.hero.companion.health = 1 while not self.hero.next_spending.is_HEAL_COMPANION: self.hero.switch_spending() money = self.hero.spend_amount self.hero.money = money + 666 with self.check_increased(lambda: self.hero.companion.health): with self.check_delta(lambda: self.hero.money, -money): self.storage.process_turn() self.assertEqual(self.hero.statistics.money_spend, money) self.assertEqual(self.hero.statistics.money_spend_for_companions, money) self.storage._test_save() def test_heal_companion__swich_spending_on_full_health(self): self.companion_record = companions_logic.create_random_companion_record('companion', state=companions_relations.STATE.ENABLED) self.hero.set_companion(companions_logic.create_companion(self.companion_record)) while not self.hero.next_spending.is_HEAL_COMPANION: self.hero.switch_spending() self.hero.companion.health = self.hero.companion.max_health money = self.hero.spend_amount self.hero.money = money + 666 with self.check_not_changed(lambda: self.hero.companion.health): with self.check_not_changed(lambda: self.hero.money): with mock.patch('the_tale.game.heroes.objects.Hero.switch_spending') as switch_spending: self.storage.process_turn() self.assertEqual(switch_spending.call_count, 1) self.assertEqual(self.hero.statistics.money_spend, 0) self.assertEqual(self.hero.statistics.money_spend_for_companions, 0) self.storage._test_save() def test_healed_companion(self): self.companion_record = companions_logic.create_random_companion_record('companion', state=companions_relations.STATE.ENABLED) self.hero.set_companion(companions_logic.create_companion(self.companion_record)) self.hero.companion.health = self.hero.companion.max_health while not self.hero.next_spending.is_HEAL_COMPANION: self.hero.switch_spending() money = self.hero.spend_amount self.hero.money = money + 666 with self.check_not_changed(lambda: self.hero.statistics.money_spend): with self.check_not_changed(lambda: self.hero.statistics.money_spend_for_companions): with self.check_not_changed(lambda: self.hero.money): self.storage.process_turn() self.storage._test_save() def test_heal_companion__no_companion(self): self.assertEqual(self.hero.companion, None) self.hero.next_spending = heroes_relations.ITEMS_OF_EXPENDITURE.HEAL_COMPANION money = self.hero.spend_amount self.hero.money = money + 666 with self.check_not_changed(lambda: self.hero.statistics.money_spend): with self.check_not_changed(lambda: self.hero.statistics.money_spend_for_companions): with self.check_not_changed(lambda: self.hero.money): self.storage.process_turn()
class InPlaceActionTest(testcase.TestCase, ActionEventsTestsMixin): def setUp(self): super(InPlaceActionTest, self).setUp() create_test_map() self.account = self.accounts_factory.create_account() self.storage = LogicStorage() self.storage.load_account_data(self.account) self.hero = self.storage.accounts_to_heroes[self.account.id] self.action_idl = self.hero.actions.current_action self.hero.position.previous_place_id = None # test setting prevouse place in action constructor self.action_inplace = prototypes.ActionInPlacePrototype.create(hero=self.hero) self.action_event = self.action_inplace def test_create(self): self.assertEqual(self.hero.position.previous_place, None) self.assertEqual(self.action_idl.leader, False) self.assertEqual(self.action_inplace.leader, True) self.assertEqual(self.action_inplace.bundle_id, self.action_idl.bundle_id) self.storage._test_save() @mock.patch('the_tale.game.places.objects.Place.is_modifier_active', lambda self: True) def test_instant_heal_in_resort(self): self.hero.health = 1 self.hero.position.place.set_modifier(places_modifiers.CITY_MODIFIERS.RESORT) old_messages_len = len (self.hero.journal.messages) prototypes.ActionInPlacePrototype.create(hero=self.hero) self.assertEqual(self.hero.health, self.hero.max_health) self.storage._test_save() @mock.patch('the_tale.game.places.objects.Place.is_modifier_active', lambda self: True) def test_no_instant_heal_in_resort(self): self.hero.health = self.hero.max_health self.hero.position.place.set_modifier(places_modifiers.CITY_MODIFIERS.RESORT) old_messages_len = len (self.hero.journal.messages) prototypes.ActionInPlacePrototype.create(hero=self.hero) self.assertEqual(self.hero.health, self.hero.max_health) self.storage._test_save() @mock.patch('the_tale.game.places.objects.Place.is_modifier_active', lambda self: True) def test_companion_heal_in_resort__no_companion(self): self.assertEqual(self.hero.companion, None) self.hero.position.place.set_modifier(places_modifiers.CITY_MODIFIERS.RESORT) prototypes.ActionInPlacePrototype.create(hero=self.hero) self.storage._test_save() @mock.patch('the_tale.game.places.objects.Place.is_modifier_active', lambda self: True) def test_companion_heal_in_resort__healed_companion(self): companion_record = companions_storage.companions.enabled_companions().next() self.hero.set_companion(companions_logic.create_companion(companion_record)) self.assertEqual(self.hero.companion.health, self.hero.companion.max_health) self.hero.position.place.set_modifier(places_modifiers.CITY_MODIFIERS.RESORT) prototypes.ActionInPlacePrototype.create(hero=self.hero) self.assertFalse(self.hero.journal.messages[-1].key.is_ACTION_INPLACE_COMPANION_HEAL) self.storage._test_save() @mock.patch('the_tale.game.places.objects.Place.is_modifier_active', lambda self: True) def test_companion_heal_in_resort__damaged_companion(self): companion_record = companions_storage.companions.enabled_companions().next() self.hero.set_companion(companions_logic.create_companion(companion_record)) self.hero.companion.health = 1 self.hero.position.place.set_modifier(places_modifiers.CITY_MODIFIERS.RESORT) with self.check_increased(lambda: self.hero.companion.health): prototypes.ActionInPlacePrototype.create(hero=self.hero) self.assertTrue(self.hero.journal.messages[-1].key.is_ACTION_INPLACE_COMPANION_HEAL) self.storage._test_save() @mock.patch('the_tale.game.places.objects.Place.is_modifier_active', lambda self: True) def test_instant_energy_regen_in_holy_city(self): self.hero.energy = 0 self.hero.position.previous_place_id = None self.hero.position.place.set_modifier(places_modifiers.CITY_MODIFIERS.HOLY_CITY) self.assertNotEqual(self.hero.position.place, self.hero.position.previous_place) prototypes.ActionInPlacePrototype.create(hero=self.hero) self.assertEqual(self.hero.energy, c.ANGEL_ENERGY_INSTANT_REGENERATION_IN_PLACE) self.storage._test_save() @mock.patch('the_tale.game.places.objects.Place.is_modifier_active', lambda self: True) def test_instant_energy_regen_in_holy_city__maximum_energy(self): self.hero.energy = self.hero.energy_maximum self.hero.position.previous_place_id = None self.hero.position.place.set_modifier(places_modifiers.CITY_MODIFIERS.HOLY_CITY) self.assertNotEqual(self.hero.position.place, self.hero.position.previous_place) prototypes.ActionInPlacePrototype.create(hero=self.hero) self.assertEqual(self.hero.energy, self.hero.energy_maximum) self.storage._test_save() @mock.patch('the_tale.game.places.objects.Place.is_modifier_active', lambda self: True) def test_instant_energy_regen_in_holy_city__no_regen(self): self.hero.energy = 0 self.hero.position.previous_place_id = None self.hero.position.place.set_modifier(places_modifiers.CITY_MODIFIERS.NONE) self.assertNotEqual(self.hero.position.place, self.hero.position.previous_place) prototypes.ActionInPlacePrototype.create(hero=self.hero) self.assertEqual(self.hero.energy, 0) self.storage._test_save() @mock.patch('the_tale.game.places.objects.Place.is_modifier_active', lambda self: True) def test_instant_energy_regen_in_holy_city__place_not_changed(self): self.hero.energy = 0 self.hero.position.place.set_modifier(places_modifiers.CITY_MODIFIERS.HOLY_CITY) self.hero.position.update_previous_place() self.assertEqual(self.hero.position.place, self.hero.position.previous_place) with self.check_not_changed(lambda: len(self.hero.journal.messages)): prototypes.ActionInPlacePrototype.create(hero=self.hero) self.assertEqual(self.hero.energy, 0) self.storage._test_save() def test_tax(self): for place in places_storage.places.all(): place.attrs.tax = 0.2 self.hero.money = 100 self.hero.position.previous_place_id = None self.assertNotEqual(self.hero.position.place, self.hero.position.previous_place) with self.check_delta(lambda: self.hero.statistics.money_spend, 20): with self.check_delta(lambda: self.hero.statistics.money_spend_for_tax, 20): prototypes.ActionInPlacePrototype.create(hero=self.hero) self.assertEqual(self.hero.money, 80) self.storage._test_save() def test_tax__no_money(self): for place in places_storage.places.all(): place.attrs.tax = 0.2 self.hero.money = 0 self.hero.position.previous_place_id = None self.assertNotEqual(self.hero.position.place, self.hero.position.previous_place) with self.check_delta(lambda: len(self.hero.journal.messages), 1): with self.check_delta(lambda: self.hero.statistics.money_spend, 0): with self.check_delta(lambda: self.hero.statistics.money_spend_for_tax, 0): prototypes.ActionInPlacePrototype.create(hero=self.hero) self.assertEqual(self.hero.money, 0) self.storage._test_save() def test_tax__no_tax(self): for place in places_storage.places.all(): place.attrs.tax = 0.0 self.hero.money = 100 self.hero.position.previous_place_id = None self.assertNotEqual(self.hero.position.place, self.hero.position.previous_place) with self.check_delta(lambda: self.hero.statistics.money_spend, 0): with self.check_delta(lambda: self.hero.statistics.money_spend_for_tax, 0): prototypes.ActionInPlacePrototype.create(hero=self.hero) self.assertEqual(self.hero.money, 100) self.storage._test_save() def test_tax__place_not_changed(self): for place in places_storage.places.all(): place.attrs.tax = 0.2 self.hero.money = 100 self.hero.position.update_previous_place() self.assertEqual(self.hero.position.place, self.hero.position.previous_place) with self.check_delta(lambda: len(self.hero.journal.messages), 0): with self.check_delta(lambda: self.hero.statistics.money_spend, 0): with self.check_delta(lambda: self.hero.statistics.money_spend_for_tax, 0): prototypes.ActionInPlacePrototype.create(hero=self.hero) self.assertEqual(self.hero.money, 100) self.storage._test_save() @mock.patch('the_tale.game.balance.constants.PLACE_HABITS_EVENT_PROBABILITY', 1.0) def test_habit_event(self): from the_tale.game.relations import HABIT_HONOR_INTERVAL, HABIT_PEACEFULNESS_INTERVAL for honor in HABIT_HONOR_INTERVAL.records: for peacefulness in HABIT_PEACEFULNESS_INTERVAL.records: self.hero.diary.clear() self.hero.position.previous_place_id = None self.assertNotEqual(self.hero.position.place, self.hero.position.previous_place) with mock.patch('the_tale.game.places.habits.Honor.interval', honor): with mock.patch('the_tale.game.places.habits.Peacefulness.interval', peacefulness): with self.check_delta(self.hero.diary.messages_number, 1): prototypes.ActionInPlacePrototype.create(hero=self.hero) self.storage._test_save() @mock.patch('the_tale.game.balance.constants.PLACE_HABITS_EVENT_PROBABILITY', 0.0) def test_habit_event__no_event(self): self.hero.position.previous_place_id = None self.assertNotEqual(self.hero.position.place, self.hero.position.previous_place) with self.check_not_changed(lambda: len(self.hero.diary.messages)): prototypes.ActionInPlacePrototype.create(hero=self.hero) self.storage._test_save() @mock.patch('the_tale.game.balance.constants.PLACE_HABITS_EVENT_PROBABILITY', 1.0) def test_habit_event__not_visit(self): self.hero.position.update_previous_place() self.assertEqual(self.hero.position.place, self.hero.position.previous_place) with self.check_not_changed(lambda: len(self.hero.diary.messages)): prototypes.ActionInPlacePrototype.create(hero=self.hero) self.storage._test_save() def test_processed(self): self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(len(self.hero.actions.actions_list), 1) self.assertEqual(self.hero.actions.current_action, self.action_idl) self.assertEqual(self.hero.position.previous_place, self.hero.position.place) self.storage._test_save() def test_regenerate_energy_action_create(self): self.hero.preferences.set_energy_regeneration_type(heroes_relations.ENERGY_REGENERATION.PRAY) self.hero.last_energy_regeneration_at_turn -= max(zip(*heroes_relations.ENERGY_REGENERATION.select('period'))[0]) self.storage.process_turn() self.assertEqual(len(self.hero.actions.actions_list), 3) self.assertEqual(self.hero.actions.current_action.TYPE, prototypes.ActionRegenerateEnergyPrototype.TYPE) self.storage._test_save() def test_regenerate_energy_action_not_create_for_sacrifice(self): self.hero.preferences.set_energy_regeneration_type(heroes_relations.ENERGY_REGENERATION.SACRIFICE) self.hero.last_energy_regeneration_at_turn -= max(zip(*heroes_relations.ENERGY_REGENERATION.select('period'))[0]) self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(len(self.hero.actions.actions_list), 1) self.assertEqual(self.hero.actions.current_action, self.action_idl) self.storage._test_save() def test_heal_action_create(self): self.hero.health = 1 self.storage.process_turn() self.assertEqual(len(self.hero.actions.actions_list), 3) self.assertEqual(self.hero.actions.current_action.TYPE, prototypes.ActionRestPrototype.TYPE) self.storage._test_save() @mock.patch('the_tale.game.companions.objects.Companion.need_heal', True) def test_heal_companion_action_create(self): companion_record = companions_storage.companions.enabled_companions().next() self.hero.set_companion(companions_logic.create_companion(companion_record)) self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(len(self.hero.actions.actions_list), 3) self.assertEqual(self.hero.actions.current_action.TYPE, prototypes.ActionHealCompanionPrototype.TYPE) self.assertEqual(self.action_inplace.state, prototypes.ActionInPlacePrototype.STATE.HEALING_COMPANION) self.storage._test_save() self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(len(self.hero.actions.actions_list), 2) self.assertEqual(self.hero.actions.current_action.TYPE, prototypes.ActionInPlacePrototype.TYPE) self.storage._test_save() @mock.patch('the_tale.game.companions.objects.Companion.need_heal', True) def test_heal_companion_action_create__no_companion(self): self.assertEqual(self.hero.companion, None) self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(len(self.hero.actions.actions_list), 1) self.assertEqual(self.hero.actions.current_action.TYPE, prototypes.ActionIdlenessPrototype.TYPE) self.storage._test_save() def test_trade_action_create(self): for i in xrange(int(c.MAX_BAG_SIZE * c.BAG_SIZE_TO_SELL_LOOT_FRACTION) + 1): artifact = artifacts_storage.generate_artifact_from_list(artifacts_storage.loot, 1, rarity=RARITY.NORMAL) self.hero.bag.put_artifact(artifact) self.storage.process_turn() self.assertEqual(len(self.hero.actions.actions_list), 3) self.assertEqual(self.hero.actions.current_action.TYPE, prototypes.ActionTradingPrototype.TYPE) self.storage._test_save() def test_equip_action_create(self): artifact = artifacts_storage.generate_artifact_from_list(artifacts_storage.artifacts, 1, rarity=RARITY.NORMAL) artifact.power = Power(666, 666) self.hero.bag.put_artifact(artifact) self.storage.process_turn() self.assertEqual(len(self.hero.actions.actions_list), 3) self.assertEqual(self.hero.actions.current_action.TYPE, prototypes.ActionEquippingPrototype.TYPE) self.storage._test_save() def test_full(self): current_time = TimePrototype.get_current_time() while len(self.hero.actions.actions_list) != 1: self.storage.process_turn() current_time.increment_turn() self.assertTrue(self.action_idl.leader) self.storage._test_save()
class IdlenessActionTest(testcase.TestCase): def setUp(self): super(IdlenessActionTest, self).setUp() create_test_map() result, account_id, bundle_id = register_user('test_user') self.account = AccountPrototype.get_by_id(account_id) self.storage = LogicStorage() self.storage.load_account_data(self.account) self.hero = self.storage.accounts_to_heroes[self.account.id] self.action_idl = self.hero.actions.current_action def tearDown(self): pass def test_create(self): self.assertEqual(self.action_idl.leader, True) self.assertEqual(self.action_idl.percents, 1.0) self.storage._test_save() def test_first_quest(self): self.storage.process_turn() self.assertEqual(len(self.hero.actions.actions_list), 2) self.assertEqual(self.hero.actions.current_action.TYPE, prototypes.ActionQuestPrototype.TYPE) self.assertEqual(self.action_idl.state, prototypes.ActionIdlenessPrototype.STATE.QUEST) self.assertEqual(self.action_idl.bundle_id, self.hero.account_id) self.storage._test_save() def test_reset_percents_on_quest_end(self): self.action_idl.percents = 1.0 self.action_idl.state = prototypes.ActionIdlenessPrototype.STATE.QUEST self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(self.action_idl.percents, 0.0) def test_inplace(self): self.action_idl.percents = 1.0 self.action_idl.state = prototypes.ActionIdlenessPrototype.STATE.QUEST self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(len(self.hero.actions.actions_list), 2) self.assertEqual(self.hero.actions.current_action.TYPE, prototypes.ActionInPlacePrototype.TYPE) self.assertEqual(self.action_idl.state, prototypes.ActionIdlenessPrototype.STATE.IN_PLACE) self.storage._test_save() def test_waiting(self): self.action_idl.percents = 0.0 self.action_idl.state = prototypes.ActionIdlenessPrototype.STATE.IN_PLACE self.storage.process_turn() self.assertEqual(len(self.hero.actions.actions_list), 1) self.assertEqual(self.hero.actions.current_action, self.action_idl) self.assertEqual(self.action_idl.state, prototypes.ActionIdlenessPrototype.STATE.WAITING) self.storage._test_save() def test_regenerate_energy_action_create(self): self.hero.preferences.set_energy_regeneration_type(heroes_relations.ENERGY_REGENERATION.PRAY) self.hero.last_energy_regeneration_at_turn -= max(zip(*heroes_relations.ENERGY_REGENERATION.select('period'))[0]) self.action_idl.percents = 0.0 self.storage.process_turn() self.assertEqual(len(self.hero.actions.actions_list), 2) self.assertEqual(self.hero.actions.current_action.TYPE, prototypes.ActionRegenerateEnergyPrototype.TYPE) self.storage._test_save() def test_regenerate_energy_action_not_create_for_sacrifice(self): self.action_idl.percents = 0 self.hero.preferences.set_energy_regeneration_type(heroes_relations.ENERGY_REGENERATION.SACRIFICE) self.hero.last_energy_regeneration_at_turn -= max(zip(*heroes_relations.ENERGY_REGENERATION.select('period'))[0]) self.storage.process_turn() self.assertEqual(len(self.hero.actions.actions_list), 1) self.assertEqual(self.hero.actions.current_action, self.action_idl) self.storage._test_save() def test_full_waiting(self): self.action_idl.state = prototypes.ActionIdlenessPrototype.STATE.WAITING self.action_idl.percents = 0 current_time = TimePrototype.get_current_time() for i in xrange(c.TURNS_TO_IDLE*self.hero.level): self.storage.process_turn() current_time.increment_turn() self.assertEqual(len(self.hero.actions.actions_list), 1) self.assertEqual(self.hero.actions.current_action, self.action_idl) self.storage.process_turn() self.assertEqual(len(self.hero.actions.actions_list), 2) self.assertEqual(self.hero.actions.current_action.TYPE, prototypes.ActionQuestPrototype.TYPE) self.assertEqual(self.action_idl.state, prototypes.ActionIdlenessPrototype.STATE.QUEST) self.storage._test_save() def test_initiate_quest(self): self.action_idl.state = prototypes.ActionIdlenessPrototype.STATE.WAITING self.action_idl.percents = 0 self.action_idl.init_quest() self.storage.process_turn() self.assertEqual(len(self.hero.actions.actions_list), 2) self.assertEqual(self.hero.actions.current_action.TYPE, prototypes.ActionQuestPrototype.TYPE) self.assertEqual(self.action_idl.state, prototypes.ActionIdlenessPrototype.STATE.QUEST) self.storage._test_save() def test_initiate_quest_just_after_quest(self): self.action_idl.state = prototypes.ActionIdlenessPrototype.STATE.QUEST self.action_idl.percents = 0 self.action_idl.init_quest() self.storage.process_turn() self.assertEqual(len(self.hero.actions.actions_list), 2) self.assertEqual(self.hero.actions.current_action.TYPE, prototypes.ActionQuestPrototype.TYPE) self.assertEqual(self.action_idl.state, prototypes.ActionIdlenessPrototype.STATE.QUEST) self.storage._test_save() def test_help_choices__contain_start_quest(self): self.action_idl.percents = 0.0 self.assertTrue(HELP_CHOICES.START_QUEST in self.action_idl.HELP_CHOICES) def test_help_choices__start_quest_removed(self): self.action_idl.percents = 1.0 self.assertFalse(HELP_CHOICES.START_QUEST in self.action_idl.HELP_CHOICES) @mock.patch('the_tale.game.heroes.prototypes.HeroPositionPrototype.is_battle_start_needed', lambda self: False) def test_return_from_road__after_quest(self): self.action_idl.state = prototypes.ActionIdlenessPrototype.STATE.QUEST self.hero.position.set_road(list(roads_storage.all())[0], percents=0.5) self.storage.process_turn() self.assertEqual(self.hero.actions.number, 2) self.assertEqual(self.hero.actions.current_action.TYPE, prototypes.ActionMoveToPrototype.TYPE) @mock.patch('the_tale.game.heroes.prototypes.HeroPositionPrototype.is_battle_start_needed', lambda self: False) def test_return_from_wild_terrain__after_quest(self): self.action_idl.state = prototypes.ActionIdlenessPrototype.STATE.QUEST self.hero.position.set_coordinates(0, 0, 5, 5, percents=0) self.storage.process_turn() self.assertEqual(self.hero.actions.number, 2) self.assertEqual(self.hero.actions.current_action.TYPE, prototypes.ActionMoveNearPlacePrototype.TYPE) @mock.patch('the_tale.game.heroes.prototypes.HeroPositionPrototype.is_battle_start_needed', lambda self: False) def test_return_from_road__after_resurrect(self): self.action_idl.state = prototypes.ActionIdlenessPrototype.STATE.RESURRECT self.hero.position.set_road(list(roads_storage.all())[0], percents=0.5) self.storage.process_turn() self.assertEqual(self.hero.actions.number, 2) self.assertEqual(self.hero.actions.current_action.TYPE, prototypes.ActionMoveToPrototype.TYPE) @mock.patch('the_tale.game.heroes.prototypes.HeroPositionPrototype.is_battle_start_needed', lambda self: False) def test_return_from_wild_terrain__after_resurrect(self): self.action_idl.state = prototypes.ActionIdlenessPrototype.STATE.RESURRECT self.hero.position.set_coordinates(0, 0, 5, 5, percents=0) self.storage.process_turn() self.assertEqual(self.hero.actions.number, 2) self.assertEqual(self.hero.actions.current_action.TYPE, prototypes.ActionMoveNearPlacePrototype.TYPE) def test_resurrect(self): self.hero.is_alive = False self.storage.process_turn() self.assertEqual(self.hero.actions.number, 2) self.assertEqual(self.hero.actions.current_action.TYPE, prototypes.ActionResurrectPrototype.TYPE)
class MoveToActionWithBreaksTest(testcase.TestCase): FIRST_BREAK_AT = 0.75 def setUp(self): super(MoveToActionWithBreaksTest, self).setUp() self.p1, self.p2, self.p3 = create_test_map() result, account_id, bundle_id = register_user('test_user') self.storage = LogicStorage() self.storage.load_account_data(AccountPrototype.get_by_id(account_id)) self.hero = self.storage.accounts_to_heroes[account_id] self.action_idl = self.hero.actions.current_action self.hero.position.set_place(self.p1) self.action_move = prototypes.ActionMoveToPrototype.create( hero=self.hero, destination=self.p3, break_at=self.FIRST_BREAK_AT) def test_sequence_move(self): current_time = TimePrototype.get_current_time() while self.hero.actions.current_action != self.action_idl: self.storage.process_turn(continue_steps_if_needed=False) current_time.increment_turn() self.assertEqual(self.hero.position.road.point_1_id, self.p2.id) self.assertEqual(self.hero.position.road.point_2_id, self.p3.id) real_percents = None prototypes.ActionMoveToPrototype.create(hero=self.hero, destination=self.p1, break_at=0.9) while self.hero.actions.current_action != self.action_idl: real_percents = self.hero.actions.current_action.percents self.storage.process_turn(continue_steps_if_needed=False) current_time.increment_turn() self.assertEqual(round(real_percents, 1), 0.9) self.assertEqual(self.hero.position.road.point_1_id, self.p1.id) self.assertEqual(self.hero.position.road.point_2_id, self.p2.id) prototypes.ActionMoveToPrototype.create(hero=self.hero, destination=self.p2) while self.hero.position.place is None or self.hero.position.place.id != self.p2.id: self.storage.process_turn(continue_steps_if_needed=False) current_time.increment_turn() self.assertEqual(self.hero.actions.current_action.TYPE, prototypes.ActionInPlacePrototype.TYPE) self.storage._test_save() @mock.patch( 'the_tale.game.heroes.prototypes.HeroPositionPrototype.is_battle_start_needed', lambda self: False) def test_teleport_to_place__break_at(self): self.storage.process_turn(continue_steps_if_needed=False) self.action_move.teleport_to_place(create_inplace_action=True) self.assertEqual(self.hero.position.place.id, self.p2.id) while not self.hero.actions.current_action.TYPE.is_MOVE_TO: self.storage.process_turn(continue_steps_if_needed=False) self.storage.process_turn(continue_steps_if_needed=False) self.assertTrue( self.action_move.teleport_to_place(create_inplace_action=True)) self.assertNotEqual(self.hero.position.road, None) self.assertTrue(self.hero.position.percents < 1) self.assertTrue(self.hero.actions.current_action.TYPE.is_MOVE_TO) self.assertEqual(self.hero.actions.current_action.percents, self.FIRST_BREAK_AT) self.assertTrue(self.action_move.leader) self.assertEqual(self.hero.position.place, None) self.storage._test_save() @mock.patch( 'the_tale.game.heroes.prototypes.HeroPositionPrototype.is_battle_start_needed', lambda self: False) def test_teleport_to_end__break_at(self): self.storage.process_turn(continue_steps_if_needed=False) self.action_move.teleport_to_end() self.assertNotEqual(self.hero.position.road, None) self.assertTrue(self.hero.position.percents < 1.0) self.assertEqual(self.p2.id, self.hero.position.road.point_1_id) self.assertEqual(self.p3.id, self.hero.position.road.point_2_id) self.assertTrue(self.hero.actions.current_action.TYPE.is_MOVE_TO) self.assertEqual(self.hero.actions.current_action.percents, self.FIRST_BREAK_AT) self.assertTrue(self.action_move.leader) self.storage._test_save()
class LogicStorageTests(testcase.TestCase): def setUp(self): super(LogicStorageTests, self).setUp() self.p1, self.p2, self.p3 = create_test_map() self.storage = LogicStorage() self.account_1 = self.accounts_factory.create_account() self.account_2 = self.accounts_factory.create_account() self.storage.load_account_data(self.account_1) self.storage.load_account_data(self.account_2) self.hero_1 = self.storage.accounts_to_heroes[self.account_1.id] self.hero_2 = self.storage.accounts_to_heroes[self.account_2.id] self.action_idl_1 = self.hero_1.actions.current_action self.action_idl_2 = self.hero_2.actions.current_action self.bundle_1_id = self.action_idl_1.bundle_id self.bundle_2_id = self.action_idl_2.bundle_id def test_load_account_data(self): self.assertEqual(len(self.storage.heroes), 2) self.assertEqual(len(self.storage.accounts_to_heroes), 2) self.assertEqual(self.storage.bundles_to_accounts, {self.hero_1.actions.current_action.bundle_id: set([self.account_1.id]), self.hero_2.actions.current_action.bundle_id: set([self.account_2.id])}) action_regenerate = actions_prototypes.ActionRegenerateEnergyPrototype.create(hero=self.hero_1) self.assertEqual(self.action_idl_1.storage, self.storage) self.assertEqual(action_regenerate.storage, self.storage) storage = LogicStorage() storage.load_account_data(AccountPrototype.get_by_id(self.account_1.id)) storage.load_account_data(AccountPrototype.get_by_id(self.account_2.id)) self.assertEqual(len(storage.heroes), 2) self.assertEqual(len(storage.accounts_to_heroes), 2) self.assertEqual(storage.bundles_to_accounts, {self.hero_1.actions.current_action.bundle_id: set([self.account_1.id]), self.hero_2.actions.current_action.bundle_id: set([self.account_2.id])}) def test_load_account_data_with_meta_action(self): bundle_id = 666 meta_action_battle = meta_actions.ArenaPvP1x1.create(self.storage, self.hero_1, self.hero_2) proxy_action_1 = actions_prototypes.ActionMetaProxyPrototype.create(hero=self.hero_1, _bundle_id=bundle_id, meta_action=meta_action_battle) proxy_action_2 = actions_prototypes.ActionMetaProxyPrototype.create(hero=self.hero_2, _bundle_id=bundle_id, meta_action=meta_action_battle) self.assertEqual(len(self.storage.meta_actions), 1) self.assertEqual(len(self.storage.meta_actions_to_actions), 1) self.assertEqual(self.storage.meta_actions_to_actions[meta_action_battle.uid], set([LogicStorage.get_action_uid(proxy_action_1), LogicStorage.get_action_uid(proxy_action_2)])) self.storage.save_changed_data() self.assertIs(self.hero_1.actions.current_action.meta_action, self.hero_2.actions.current_action.meta_action) self.assertIs(self.hero_1.actions.current_action.saved_meta_action, self.hero_2.actions.current_action.saved_meta_action) storage = LogicStorage() storage.load_account_data(AccountPrototype.get_by_id(self.account_1.id)) storage.load_account_data(AccountPrototype.get_by_id(self.account_2.id)) self.assertEqual(len(storage.meta_actions), 1) self.assertEqual(len(storage.meta_actions_to_actions), 1) self.assertEqual(storage.meta_actions_to_actions[meta_action_battle.uid], set([LogicStorage.get_action_uid(proxy_action_1), LogicStorage.get_action_uid(proxy_action_2)])) self.assertEqual(storage.bundles_to_accounts, {self.hero_1.actions.current_action.bundle_id: set([self.account_1.id, self.account_2.id])}) hero_1 = storage.accounts_to_heroes[self.account_1.id] hero_2 = storage.accounts_to_heroes[self.account_2.id] self.assertIs(hero_1.actions.current_action.meta_action, hero_2.actions.current_action.meta_action) self.assertIsNot(hero_1.actions.current_action.saved_meta_action, hero_2.actions.current_action.saved_meta_action) self.assertEqual(hero_1.actions.current_action.saved_meta_action.serialize(), hero_2.actions.current_action.saved_meta_action.serialize()) def test_add_duplicate_hero(self): self.assertRaises(exceptions.HeroAlreadyRegisteredError, self.storage._add_hero, self.hero_1) def test_action_release_account_data(self): actions_prototypes.ActionRegenerateEnergyPrototype.create(hero=self.hero_1) self.storage.skipped_heroes.add(self.hero_1.id) self.storage.release_account_data(self.account_1.id) self.assertEqual(len(self.storage.heroes), 1) self.assertEqual(len(self.storage.accounts_to_heroes), 1) self.assertEqual(self.storage.bundles_to_accounts, {self.hero_2.actions.current_action.bundle_id: set([self.account_2.id])}) self.assertEqual(self.storage.heroes.values()[0].id, self.hero_2.id) self.assertFalse(self.storage.skipped_heroes) def test_save_hero_data(self): self.hero_1.health = 1 self.hero_2.health = 1 self.hero_1.actions.updated = True self.storage._save_hero_data(self.hero_1.id) self.assertEqual(self.hero_1.health, HeroPrototype.get_by_id(self.hero_1.id).health) self.assertNotEqual(self.hero_2.health, HeroPrototype.get_by_id(self.hero_2.id).health) self.assertFalse(self.hero_1.actions.updated) def test_save_all(self): self.hero_1.health = 1 self.hero_2.health = 1 self.hero_1.actions.updated = True self.storage.save_all() self.assertEqual(self.hero_1.health, HeroPrototype.get_by_id(self.hero_1.id).health) self.assertEqual(self.hero_2.health, HeroPrototype.get_by_id(self.hero_2.id).health) self.assertFalse(self.hero_1.actions.updated) def test_save_hero_data_with_meta_action(self): bundle_id = 666 meta_action_battle = meta_actions.ArenaPvP1x1.create(self.storage, self.hero_1, self.hero_2) actions_prototypes.ActionMetaProxyPrototype.create(hero=self.hero_1, _bundle_id=bundle_id, meta_action=meta_action_battle) actions_prototypes.ActionMetaProxyPrototype.create(hero=self.hero_2, _bundle_id=bundle_id, meta_action=meta_action_battle) self.storage._save_hero_data(self.hero_1.id) self.storage._save_hero_data(self.hero_2.id) self.hero_1.reload() self.hero_2.reload() self.assertEqual(meta_action_battle.serialize(), self.hero_1.actions.current_action.saved_meta_action.serialize()) self.assertEqual(meta_action_battle.serialize(), self.hero_2.actions.current_action.saved_meta_action.serialize()) def test_switch_caches(self): self.assertEqual(self.storage.previous_cache, {}) self.assertEqual(self.storage.current_cache, {}) self.storage.previous_cache[1] = 2 self.storage.current_cache[3] = 4 self.storage.switch_caches() self.assertEqual(self.storage.previous_cache, {3: 4}) self.assertEqual(self.storage.current_cache, {}) self.storage.current_cache[5] = 6 self.storage.switch_caches() self.assertEqual(self.storage.previous_cache, {5: 6}) self.assertEqual(self.storage.current_cache, {}) def test_process_cache_queue__with_update(self): self.assertEqual(self.storage.cache_queue, set()) self.storage.cache_queue.add(self.hero_2.id) self.storage.cache_queue.add(self.hero_1.id) self.storage.process_cache_queue(update_cache=True) self.assertItemsEqual(self.storage.current_cache.keys(), (self.hero_1.cached_ui_info_key, self.hero_2.cached_ui_info_key)) self.assertEqual(self.storage.cache_queue, set()) def test_process_cache_queue__without_update(self): self.assertEqual(self.storage.cache_queue, set()) self.storage.cache_queue.add(self.hero_2.id) self.storage.cache_queue.add(self.hero_1.id) self.storage.process_cache_queue(update_cache=False) self.assertItemsEqual(self.storage.current_cache.keys(), ()) self.assertEqual(self.storage.cache_queue, set()) def test_process_cache_queue__update_cache__with_update(self): self.assertEqual(self.storage.cache_queue, set()) self.storage.current_cache[self.hero_1.cached_ui_info_key] = 1 self.storage.current_cache[self.hero_2.cached_ui_info_key] = 2 self.storage.cache_queue.add(self.hero_2.id) self.storage.process_cache_queue(update_cache=True) self.assertEqual(self.storage.current_cache[self.hero_1.cached_ui_info_key], 1) self.assertNotEqual(self.storage.current_cache[self.hero_2.cached_ui_info_key], 2) def test_process_cache_queue__update_cache__without_update(self): self.assertEqual(self.storage.cache_queue, set()) self.storage.current_cache[self.hero_1.cached_ui_info_key] = 1 self.storage.current_cache[self.hero_2.cached_ui_info_key] = 2 self.storage.cache_queue.add(self.hero_2.id) self.storage.process_cache_queue(update_cache=False) self.assertEqual(self.storage.current_cache[self.hero_1.cached_ui_info_key], 1) self.assertEqual(self.storage.current_cache[self.hero_2.cached_ui_info_key], 2) @mock.patch('the_tale.game.heroes.conf.heroes_settings.DUMP_CACHED_HEROES', True) def test_process_turn(self): self.assertEqual(self.storage.skipped_heroes, set()) self.storage.process_turn() self.assertEqual(self.storage.skipped_heroes, set()) with mock.patch('the_tale.game.logic_storage.LogicStorage._save_hero_data') as save_hero_data: self.storage.save_changed_data() self.assertEqual(save_hero_data.call_count, 2) @mock.patch('the_tale.game.heroes.conf.heroes_settings.DUMP_CACHED_HEROES', True) def test_process_turn__switch_caches(self): self.assertEqual(self.storage.previous_cache, {}) self.assertEqual(self.storage.current_cache, {}) self.storage.process_turn() self.storage.save_changed_data() self.assertEqual(self.storage.previous_cache, {}) self.assertNotEqual(self.storage.current_cache, {}) old_cache = self.storage.current_cache self.storage.process_turn() self.storage.save_changed_data() self.assertEqual(self.storage.previous_cache, old_cache) self.assertNotEqual(self.storage.current_cache, old_cache) def test_process_turn_single_hero__runned_outside_storage(self): action_1 = actions_prototypes.ActionRegenerateEnergyPrototype.create(hero=self.hero_1) action_1.state = action_1.STATE.PROCESSED action_2 = actions_prototypes.ActionMoveToPrototype.create(hero=self.hero_1, destination=self.p1) action_2.state = action_2.STATE.PROCESSED action_3 = actions_prototypes.ActionInPlacePrototype.create(hero=self.hero_1) action_3.state = action_3.STATE.PROCESSED self.assertEqual(self.hero_1.actions.number, 4) self.storage.process_turn__single_hero(hero=self.hero_1, logger=None, continue_steps_if_needed=True) self.assertEqual(self.hero_1.actions.number, 2) self.assertEqual(self.hero_1.actions.current_action.TYPE, actions_prototypes.ActionQuestPrototype.TYPE) self.storage.process_turn() # just nothing was broken def test_process_turn__process_action_chain(self): action_1 = actions_prototypes.ActionRegenerateEnergyPrototype.create(hero=self.hero_1) action_1.state = action_1.STATE.PROCESSED action_2 = actions_prototypes.ActionMoveToPrototype.create(hero=self.hero_1, destination=self.p1) action_2.state = action_2.STATE.PROCESSED action_3 = actions_prototypes.ActionInPlacePrototype.create(hero=self.hero_1) action_3.state = action_3.STATE.PROCESSED self.assertEqual(self.hero_1.actions.number, 4) self.storage.process_turn() self.assertEqual(self.hero_1.actions.number, 2) self.assertEqual(self.hero_1.actions.current_action.TYPE, actions_prototypes.ActionQuestPrototype.TYPE) @mock.patch('the_tale.game.heroes.conf.heroes_settings.DUMP_CACHED_HEROES', False) def test_process_turn__without_dump(self): self.assertEqual(self.storage.skipped_heroes, set()) self.storage.process_turn() self.assertEqual(self.storage.skipped_heroes, set()) with mock.patch('the_tale.game.logic_storage.LogicStorage._save_hero_data') as save_hero_data: self.storage.save_changed_data() self.assertEqual(save_hero_data.call_count, 1) # save only game_settings.SAVED_UNCACHED_HEROES_FRACTION bundles number def test_process_turn__process_created_action(self): from the_tale.game.actions.prototypes import ActionMoveToPrototype place = self.p1 def process_action(self): ActionMoveToPrototype.create(hero=self.hero, destination=place) with mock.patch('the_tale.game.actions.prototypes.ActionIdlenessPrototype.process', process_action): with mock.patch('the_tale.game.actions.prototypes.ActionMoveToPrototype.process') as move_to_process: self.storage.process_turn() self.assertEqual(move_to_process.call_count, 2) @mock.patch('the_tale.game.heroes.conf.heroes_settings.DUMP_CACHED_HEROES', True) def test_process_turn_with_skipped_hero(self): # skipped heroes saved, but not processed self.storage.skipped_heroes.add(self.hero_1.id) with mock.patch('the_tale.game.actions.prototypes.ActionBase.process_turn') as action_process_turn: self.storage.process_turn() self.assertEqual(action_process_turn.call_count, 1) with mock.patch('the_tale.game.logic_storage.LogicStorage._save_hero_data') as save_hero_data: self.storage.save_changed_data() self.assertEqual(save_hero_data.call_count, 2) @mock.patch('the_tale.game.heroes.conf.heroes_settings.DUMP_CACHED_HEROES', False) def test_process_turn_with_skipped_hero__without_cache_dump(self): # skipped heroes saved, but not processed self.storage.skipped_heroes.add(self.hero_1.id) with mock.patch('the_tale.game.actions.prototypes.ActionBase.process_turn') as action_process_turn: self.storage.process_turn() self.assertEqual(action_process_turn.call_count, 1) with mock.patch('the_tale.game.logic_storage.LogicStorage._save_hero_data') as save_hero_data: self.storage.save_changed_data() self.assertEqual(save_hero_data.call_count, 1) @mock.patch('the_tale.game.heroes.prototypes.HeroPrototype.can_process_turn', lambda self, turn: True) def test_process_turn__can_process_turn(self): with mock.patch('the_tale.game.actions.prototypes.ActionBase.process_turn') as action_process_turn: self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(action_process_turn.call_count, 2) @mock.patch('the_tale.game.heroes.prototypes.HeroPrototype.can_process_turn', lambda self, turn: False) def test_process_turn__can_not_process_turn(self): with mock.patch('the_tale.game.actions.prototypes.ActionBase.process_turn') as action_process_turn: self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(action_process_turn.call_count, 0) def test_process_turn___exception_raises(self): def process_turn_raise_exception(action): if action.hero.id == self.hero_2.id: raise Exception('error') with mock.patch('the_tale.game.actions.prototypes.ActionBase.process_turn', process_turn_raise_exception): with mock.patch('the_tale.game.logic_storage.LogicStorage._save_on_exception') as _save_on_exception: with mock.patch('django.conf.settings.TESTS_RUNNING', False): self.storage.process_turn() self.assertIn(self.hero_2.actions.current_action.bundle_id, self.storage.ignored_bundles) self.assertEqual(_save_on_exception.call_count, 1) self.assertEqual(_save_on_exception.call_args, mock.call()) @mock.patch('the_tale.game.conf.game_settings.SAVE_ON_EXCEPTION_TIMEOUT', 0) def test_save_on_exception(self): # hero 1 not saved due to one bundle with hero 3 # hero 2 saved # hero 3 not saved # hero 4 saved result, account_3_id, bundle_3_id = register_user('test_user_3', '*****@*****.**', '111111') self.storage.load_account_data(AccountPrototype.get_by_id(account_3_id)) hero_3 = self.storage.accounts_to_heroes[account_3_id] result, account_4_id, bundle_4_id = register_user('test_user_4', '*****@*****.**', '111111') self.storage.load_account_data(AccountPrototype.get_by_id(account_4_id)) hero_4 = self.storage.accounts_to_heroes[account_4_id] self.hero_1.actions.current_action.bundle_id = hero_3.actions.current_action.bundle_id saved_heroes = set() def save_hero_data(storage, hero_id, **kwargs): saved_heroes.add(hero_id) self.storage.ignored_bundles.add(hero_3.actions.current_action.bundle_id) with mock.patch('the_tale.game.logic_storage.LogicStorage._save_hero_data', save_hero_data): self.storage._save_on_exception() self.assertEqual(saved_heroes, set([self.hero_2.id, hero_4.id])) def test_save_on_exception__time_border(self): # hero 1 not saved due to one bundle with hero 3 # hero 2 saved # hero 3 not saved # hero 4 saved result, account_3_id, bundle_3_id = register_user('test_user_3', '*****@*****.**', '111111') self.storage.load_account_data(AccountPrototype.get_by_id(account_3_id)) hero_3 = self.storage.accounts_to_heroes[account_3_id] result, account_4_id, bundle_4_id = register_user('test_user_4', '*****@*****.**', '111111') self.storage.load_account_data(AccountPrototype.get_by_id(account_4_id)) hero_4 = self.storage.accounts_to_heroes[account_4_id] self.hero_1.actions.current_action.bundle_id = hero_3.actions.current_action.bundle_id saved_heroes = set() self.hero_2._model.saved_at = datetime.datetime.now() - datetime.timedelta(seconds=conf.game_settings.SAVE_ON_EXCEPTION_TIMEOUT+1) def save_hero_data(storage, hero_id, **kwargs): saved_heroes.add(hero_id) self.storage.ignored_bundles.add(hero_3.actions.current_action.bundle_id) with mock.patch('the_tale.game.logic_storage.LogicStorage._save_hero_data', save_hero_data): self.storage._save_on_exception() self.assertEqual(saved_heroes, set([self.hero_2.id])) def test_save_changed_data(self): self.storage.process_turn() with mock.patch('dext.common.utils.cache.set_many') as set_many: with mock.patch('the_tale.game.heroes.prototypes.HeroPrototype.ui_info') as ui_info: self.storage.save_changed_data() self.assertEqual(set_many.call_count, 1) self.assertEqual(ui_info.call_count, 2) self.assertEqual(ui_info.call_args_list, [mock.call(actual_guaranteed=True, old_info=None), mock.call(actual_guaranteed=True, old_info=None)]) def test_old_info(self): self.storage.process_turn() calls = [] def ui_info(hero, **kwargs): calls.append(kwargs) return {'hero': hero.id} with mock.patch('the_tale.game.heroes.prototypes.HeroPrototype.ui_info', ui_info): self.storage.save_changed_data() self.storage.process_turn() self.storage.save_changed_data() self.assertEqual(calls, [{'actual_guaranteed': True, 'old_info': None}, {'actual_guaranteed': True, 'old_info': None}, {'actual_guaranteed': True, 'old_info': {'hero': self.hero_1.id}}, {'actual_guaranteed': True, 'old_info': {'hero': self.hero_2.id}}]) def test_save_changed_data__old_info(self): self.storage.process_turn() self.storage.save_changed_data() self.storage.process_turn() with mock.patch('dext.common.utils.cache.set_many') as set_many: with mock.patch('the_tale.game.heroes.prototypes.HeroPrototype.ui_info') as ui_info: self.storage.save_changed_data() self.assertEqual(set_many.call_count, 1) self.assertEqual(ui_info.call_count, 2) self.assertNotEqual(ui_info.call_args_list[0][1]['old_info'], None) self.assertNotEqual(ui_info.call_args_list[1][1]['old_info'], None) @mock.patch('the_tale.game.heroes.conf.heroes_settings.DUMP_CACHED_HEROES', True) def test_save_changed_data__with_unsaved_bundles(self): self.storage.process_turn() self.assertEqual(len(self.storage.heroes), 2) with mock.patch('the_tale.game.logic_storage.LogicStorage._get_bundles_to_save', lambda x: [self.bundle_2_id]): with mock.patch('the_tale.game.logic_storage.LogicStorage._save_hero_data') as save_hero_data: with mock.patch('the_tale.game.heroes.prototypes.HeroPrototype.ui_info') as ui_info: self.storage.save_changed_data() self.assertEqual(ui_info.call_count, 2) # cache all heroes, since they are new self.assertEqual(ui_info.call_args_list, [mock.call(actual_guaranteed=True, old_info=None), mock.call(actual_guaranteed=True, old_info=None)]) self.assertEqual(save_hero_data.call_args, mock.call(self.hero_2.id)) def test_save_changed_data__with_unsaved_bundles__without_dump(self): self.storage.process_turn() self.assertEqual(len(self.storage.heroes), 2) self.hero_2.ui_caching_started_at = datetime.datetime.fromtimestamp(0) with mock.patch('the_tale.game.logic_storage.LogicStorage._get_bundles_to_save', lambda x: [self.bundle_2_id]): with mock.patch('the_tale.game.logic_storage.LogicStorage._save_hero_data') as save_hero_data: with mock.patch('the_tale.game.heroes.prototypes.HeroPrototype.ui_info') as ui_info: self.storage.save_changed_data() self.assertEqual(ui_info.call_count, 1) # cache only first hero self.assertEqual(ui_info.call_args, mock.call(actual_guaranteed=True, old_info=None)) self.assertEqual(save_hero_data.call_args, mock.call(self.hero_2.id)) def test_remove_action__from_middle(self): actions_prototypes.ActionRegenerateEnergyPrototype.create(hero=self.hero_1) self.assertRaises(exceptions.RemoveActionFromMiddleError, self.storage.remove_action, self.action_idl_1) def test_remove_action__metaaction(self): bundle_id = 666 meta_action_battle = meta_actions.ArenaPvP1x1.create(self.storage, self.hero_1, self.hero_2) proxy_action_1 = actions_prototypes.ActionMetaProxyPrototype.create(hero=self.hero_1, _bundle_id=bundle_id, meta_action=meta_action_battle) proxy_action_2 = actions_prototypes.ActionMetaProxyPrototype.create(hero=self.hero_2, _bundle_id=bundle_id, meta_action=meta_action_battle) self.assertEqual(len(self.storage.meta_actions), 1) self.assertEqual(len(self.storage.meta_actions_to_actions), 1) self.assertEqual(self.storage.meta_actions_to_actions[meta_action_battle.uid], set([LogicStorage.get_action_uid(proxy_action_1), LogicStorage.get_action_uid(proxy_action_2)])) self.storage.remove_action(proxy_action_2) self.assertEqual(len(self.storage.meta_actions), 1) self.assertEqual(len(self.storage.meta_actions_to_actions), 1) self.assertEqual(self.storage.meta_actions_to_actions[meta_action_battle.uid], set([LogicStorage.get_action_uid(proxy_action_1)])) self.storage.remove_action(proxy_action_1) self.assertEqual(len(self.storage.meta_actions), 0) self.assertEqual(len(self.storage.meta_actions_to_actions), 0) @mock.patch('the_tale.game.heroes.conf.heroes_settings.DUMP_CACHED_HEROES', True) @mock.patch('the_tale.game.conf.game_settings.SAVED_UNCACHED_HEROES_FRACTION', 0) def test_get_bundles_to_save(self): # hero 1 not saved # hero 2 saved by quota # hero 3 saved by caching # hero 4 not saved result, account_3_id, bundle_3_id = register_user('test_user_3', '*****@*****.**', '111111') result, account_4_id, bundle_4_id = register_user('test_user_4', '*****@*****.**', '111111') result, account_5_id, bundle_5_id = register_user('test_user_5', '*****@*****.**', '111111') self.storage.load_account_data(AccountPrototype.get_by_id(account_3_id)) self.storage.load_account_data(AccountPrototype.get_by_id(account_4_id)) self.storage.load_account_data(AccountPrototype.get_by_id(account_5_id)) hero_3 = self.storage.accounts_to_heroes[account_3_id] hero_4 = self.storage.accounts_to_heroes[account_4_id] self.hero_1._model.saved_at = datetime.datetime.now() self.hero_1.ui_caching_started_at = datetime.datetime.fromtimestamp(0) self.hero_2.ui_caching_started_at = datetime.datetime.fromtimestamp(0) hero_4.ui_caching_started_at = datetime.datetime.fromtimestamp(0) self.assertTrue(self.hero_1.saved_at > self.hero_2.saved_at) self.assertFalse(self.hero_1.is_ui_caching_required) self.assertFalse(self.hero_2.is_ui_caching_required) self.assertTrue(hero_3.is_ui_caching_required) self.assertFalse(hero_4.is_ui_caching_required) self.assertEqual(self.storage._get_bundles_to_save(), set([self.bundle_2_id, bundle_3_id, bundle_5_id])) @mock.patch('the_tale.game.heroes.conf.heroes_settings.DUMP_CACHED_HEROES', True) @mock.patch('the_tale.game.conf.game_settings.SAVED_UNCACHED_HEROES_FRACTION', 0) def test_get_bundles_to_save__force_save_required(self): # hero 1 not saved # hero 2 saved by quota # hero 3 saved by caching # hero 4 saved by force result, account_3_id, bundle_3_id = register_user('test_user_3', '*****@*****.**', '111111') result, account_4_id, bundle_4_id = register_user('test_user_4', '*****@*****.**', '111111') result, account_5_id, bundle_5_id = register_user('test_user_5', '*****@*****.**', '111111') self.storage.load_account_data(AccountPrototype.get_by_id(account_3_id)) self.storage.load_account_data(AccountPrototype.get_by_id(account_4_id)) self.storage.load_account_data(AccountPrototype.get_by_id(account_5_id)) hero_3 = self.storage.accounts_to_heroes[account_3_id] hero_4 = self.storage.accounts_to_heroes[account_4_id] hero_4.force_save_required = True self.hero_1._model.saved_at = datetime.datetime.now() self.hero_1.ui_caching_started_at = datetime.datetime.fromtimestamp(0) self.hero_2.ui_caching_started_at = datetime.datetime.fromtimestamp(0) hero_4.ui_caching_started_at = datetime.datetime.fromtimestamp(0) self.assertTrue(self.hero_1.saved_at > self.hero_2.saved_at) self.assertFalse(self.hero_1.is_ui_caching_required) self.assertFalse(self.hero_2.is_ui_caching_required) self.assertTrue(hero_3.is_ui_caching_required) self.assertFalse(hero_4.is_ui_caching_required) self.assertEqual(self.storage._get_bundles_to_save(), set([self.bundle_2_id, bundle_3_id, bundle_4_id, bundle_5_id])) self.assertFalse(hero_4.force_save_required) @mock.patch('the_tale.game.heroes.conf.heroes_settings.DUMP_CACHED_HEROES', False) @mock.patch('the_tale.game.conf.game_settings.SAVED_UNCACHED_HEROES_FRACTION', 0) def test_get_bundles_to_save__without_cache_dump(self): # hero 1 not saved # hero 2 saved by quota # hero 3 does not saved by caching # hero 4 not saved result, account_3_id, bundle_3_id = register_user('test_user_3', '*****@*****.**', '111111') result, account_4_id, bundle_4_id = register_user('test_user_4', '*****@*****.**', '111111') self.storage.load_account_data(AccountPrototype.get_by_id(account_3_id)) self.storage.load_account_data(AccountPrototype.get_by_id(account_4_id)) hero_3 = self.storage.accounts_to_heroes[account_3_id] hero_4 = self.storage.accounts_to_heroes[account_4_id] self.hero_1._model.saved_at = datetime.datetime.now() self.hero_1.ui_caching_started_at = datetime.datetime.fromtimestamp(0) self.hero_2.ui_caching_started_at = datetime.datetime.fromtimestamp(0) hero_4.ui_caching_started_at = datetime.datetime.fromtimestamp(0) self.assertTrue(self.hero_1.saved_at > self.hero_2.saved_at) self.assertFalse(self.hero_1.is_ui_caching_required) self.assertFalse(self.hero_2.is_ui_caching_required) self.assertTrue(hero_3.is_ui_caching_required) self.assertFalse(hero_4.is_ui_caching_required) self.assertEqual(self.storage._get_bundles_to_save(), set([self.bundle_2_id])) @mock.patch('the_tale.game.heroes.conf.heroes_settings.DUMP_CACHED_HEROES', True) @mock.patch('the_tale.game.conf.game_settings.SAVED_UNCACHED_HEROES_FRACTION', 0) def test_save_changed_data__with_multiple_heroes_to_bundle(self): # hero 1 saved by bundle from hero 3 # hero 2 saved by quota # hero 3 saved by caching result, account_3_id, bundle_3_id = register_user('test_user_3', '*****@*****.**', '111111') self.storage.load_account_data(AccountPrototype.get_by_id(account_3_id)) hero_3 = self.storage.accounts_to_heroes[account_3_id] self.hero_1._model.saved_at = datetime.datetime.now() self.hero_1.ui_caching_started_at = datetime.datetime.fromtimestamp(0) self.hero_1.actions.current_action.bundle_id = hero_3.actions.current_action.bundle_id self.hero_2.ui_caching_started_at = datetime.datetime.fromtimestamp(0) self.assertTrue(self.hero_1.saved_at > self.hero_2.saved_at) self.assertFalse(self.hero_1.is_ui_caching_required) self.assertFalse(self.hero_2.is_ui_caching_required) self.assertTrue(hero_3.is_ui_caching_required) self.assertEqual(self.storage._get_bundles_to_save(), set([self.bundle_2_id, bundle_3_id])) self.storage.process_turn() with mock.patch('dext.common.utils.cache.set_many') as set_many: with mock.patch('the_tale.game.logic_storage.LogicStorage._save_hero_data') as save_hero_data: with mock.patch('the_tale.game.heroes.prototypes.HeroPrototype.ui_info') as ui_info: self.storage.save_changed_data() self.assertEqual(set_many.call_count, 1) self.assertEqual(save_hero_data.call_count, 3) self.assertEqual(ui_info.call_count, 2) self.assertEqual(ui_info.call_args_list, [mock.call(actual_guaranteed=True, old_info=None), mock.call(actual_guaranteed=True, old_info=None)]) @mock.patch('the_tale.game.heroes.conf.heroes_settings.DUMP_CACHED_HEROES', False) @mock.patch('the_tale.game.conf.game_settings.SAVED_UNCACHED_HEROES_FRACTION', 0) def test_save_changed_data__with_multiple_heroes_to_bundle__without_cache_dump(self): # hero 1 saved by bundle from hero 2 # hero 2 saved by quota # hero 3 does not saved by caching result, account_3_id, bundle_3_id = register_user('test_user_3', '*****@*****.**', '111111') self.storage.load_account_data(AccountPrototype.get_by_id(account_3_id)) hero_3 = self.storage.accounts_to_heroes[account_3_id] self.hero_1._model.saved_at = datetime.datetime.now() self.hero_1.ui_caching_started_at = datetime.datetime.fromtimestamp(0) self.hero_1.actions.current_action.bundle_id = self.hero_2.actions.current_action.bundle_id self.hero_2.ui_caching_started_at = datetime.datetime.fromtimestamp(0) self.assertTrue(self.hero_1.saved_at > self.hero_2.saved_at) self.assertFalse(self.hero_1.is_ui_caching_required) self.assertFalse(self.hero_2.is_ui_caching_required) self.assertTrue(hero_3.is_ui_caching_required) self.assertEqual(self.storage._get_bundles_to_save(), set([self.bundle_2_id])) self.storage.process_turn() with mock.patch('dext.common.utils.cache.set_many') as set_many: with mock.patch('the_tale.game.logic_storage.LogicStorage._save_hero_data') as save_hero_data: with mock.patch('the_tale.game.heroes.prototypes.HeroPrototype.ui_info') as ui_info: self.storage.save_changed_data() self.assertEqual(set_many.call_count, 1) self.assertEqual(save_hero_data.call_count, 2) self.assertEqual(ui_info.call_count, 1) self.assertEqual(ui_info.call_args, mock.call(actual_guaranteed=True, old_info=None)) def test_merge_bundles(self): storage = LogicStorage() storage.bundles_to_accounts[555] = set([1, 2]) storage.bundles_to_accounts[666] = set([3]) storage.merge_bundles([555, 666], 777) self.assertEqual(storage.bundles_to_accounts, {777: set([1, 2, 3])}) def test_merge_bundles__in_existed_bundle(self): storage = LogicStorage() storage.bundles_to_accounts[555] = set([1, 2]) storage.bundles_to_accounts[666] = set([3]) storage.bundles_to_accounts[777] = set([4, 5]) storage.merge_bundles([555, 666], 777) self.assertEqual(storage.bundles_to_accounts, {777: set([1, 2, 3, 4, 5])}) def test_unmerge_bundles__in_existed_bundle(self): storage = LogicStorage() storage.bundles_to_accounts[555] = set([1, 2]) storage.bundles_to_accounts[666] = set([3]) storage.bundles_to_accounts[777] = set([4, 5]) storage.unmerge_bundles(4, 777, 666) self.assertEqual(storage.bundles_to_accounts, {555: set([1, 2]), 666: set([3, 4]), 777: set([5])}) def test_unmerge_bundles__last_account_in_bundle(self): storage = LogicStorage() storage.bundles_to_accounts[555] = set([1, 2]) storage.bundles_to_accounts[666] = set([3]) storage.bundles_to_accounts[777] = set([4, 5]) storage.unmerge_bundles(3, 666, 555) self.assertEqual(storage.bundles_to_accounts, {555: set([1, 2, 3]), 777: set([4, 5])}) def test_unmerge_bundles(self): storage = LogicStorage() storage.bundles_to_accounts[555] = set([1, 2]) storage.bundles_to_accounts[666] = set([3]) storage.bundles_to_accounts[777] = set([4, 5]) storage.unmerge_bundles(4, 777, 888) self.assertEqual(storage.bundles_to_accounts, {555: set([1, 2]), 666: set([3]), 777: set([5]), 888: set([4])}) def test_save_bundle_data(self): storage = LogicStorage() storage.bundles_to_accounts[555] = set([1, 2]) storage.bundles_to_accounts[666] = set([3, 7, 9]) storage.bundles_to_accounts[777] = set([4, 5]) storage.accounts_to_heroes = {1: mock.Mock(id=1), 2: mock.Mock(id=2), 3: mock.Mock(id=3), 4: mock.Mock(id=4), 5: mock.Mock(id=5), 7: mock.Mock(id=7), 9: mock.Mock(id=9)} with mock.patch('the_tale.game.logic_storage.LogicStorage._save_hero_data') as _save_hero_data: with mock.patch('the_tale.game.logic_storage.LogicStorage.process_cache_queue') as process_cache_queue: storage.save_bundle_data(666) self.assertEqual(_save_hero_data.call_count, 3) self.assertEqual(storage.cache_queue, set([3, 7, 9])) self.assertEqual(process_cache_queue.call_count, 1) self.assertEqual(set(call[0][0] for call in _save_hero_data.call_args_list), set([3, 7, 9]))
class TradingActionTest(testcase.TestCase): def setUp(self): super(TradingActionTest, self).setUp() create_test_map() result, account_id, bundle_id = register_user('test_user') self.storage = LogicStorage() self.storage.load_account_data(AccountPrototype.get_by_id(account_id)) self.hero = self.storage.accounts_to_heroes[account_id] self.action_idl = self.hero.actions.current_action self.action_trade = ActionTradingPrototype.create(hero=self.hero) def tearDown(self): pass def test_create(self): self.assertEqual(self.action_idl.leader, False) self.assertEqual(self.action_trade.leader, True) self.assertEqual(self.action_trade.bundle_id, self.action_idl.bundle_id) self.storage._test_save() def test_processed(self): self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(len(self.hero.actions.actions_list), 1) self.assertEqual(self.hero.actions.current_action, self.action_idl) self.storage._test_save() def test_sell_and_finish(self): old_money_statistics = self.hero.statistics.money_earned old_money = self.hero.money artifact = artifacts_storage.generate_artifact_from_list( artifacts_storage.artifacts, self.hero.level, rarity=RARITY.NORMAL) self.hero.bag.put_artifact(artifact) self.action_trade.percents_barier = 1 self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(len(self.hero.actions.actions_list), 1) self.assertEqual(self.hero.actions.current_action, self.action_idl) self.assertTrue(self.hero.money > old_money) self.assertTrue( self.hero.statistics.money_earned > old_money_statistics) self.storage._test_save() def test_sell_and_continue(self): old_money = self.hero.money artifact = artifacts_storage.generate_artifact_from_list( artifacts_storage.artifacts, self.hero.level, rarity=RARITY.NORMAL) self.hero.bag.put_artifact(artifact) artifact = artifacts_storage.generate_artifact_from_list( artifacts_storage.artifacts, self.hero.level, rarity=RARITY.NORMAL) self.hero.bag.put_artifact(artifact) self.assertEqual(self.hero.bag.occupation, 2) self.action_trade.percents_barier = 2 current_time = TimePrototype.get_current_time() self.storage.process_turn() self.assertEqual(len(self.hero.actions.actions_list), 2) self.assertEqual(self.hero.actions.current_action, self.action_trade) self.assertEqual(self.hero.bag.occupation, 1) self.assertTrue(self.hero.money > old_money) old_money = self.hero.money current_time.increment_turn() self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(len(self.hero.actions.actions_list), 1) self.assertEqual(self.hero.actions.current_action, self.action_idl) self.assertEqual(self.hero.bag.occupation, 0) self.assertTrue(self.hero.money > old_money) self.storage._test_save() def test_stop_when_quest_required_replane(self): self.action_idl.percents = 0.0 self.assertFalse(self.action_trade.replane_required) artifact = artifacts_storage.generate_artifact_from_list( artifacts_storage.artifacts, self.hero.level, rarity=RARITY.NORMAL) self.hero.bag.put_artifact(artifact) artifact = artifacts_storage.generate_artifact_from_list( artifacts_storage.artifacts, self.hero.level, rarity=RARITY.NORMAL) self.hero.bag.put_artifact(artifact) self.action_trade.percents_barier = 2 self.assertEqual(self.hero.bag.occupation, 2) current_time = TimePrototype.get_current_time() self.storage.process_turn() self.assertEqual(self.hero.bag.occupation, 1) self.assertEqual(len(self.hero.actions.actions_list), 2) self.assertEqual(self.hero.actions.current_action, self.action_trade) self.action_trade.replane_required = True current_time.increment_turn() self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(self.hero.bag.occupation, 1) self.assertEqual(len(self.hero.actions.actions_list), 1) self.assertEqual(self.action_trade.state, self.action_trade.STATE.PROCESSED) self.assertEqual(self.hero.actions.current_action, self.action_idl)
class MoveNearActionTest(testcase.TestCase): def setUp(self): super(MoveNearActionTest, self).setUp() self.p1, self.p2, self.p3 = create_test_map() account = self.accounts_factory.create_account(is_fast=True) self.storage = LogicStorage() self.storage.load_account_data(account) self.hero = self.storage.accounts_to_heroes[account.id] self.action_idl = self.hero.actions.current_action self.hero.position.set_place(self.p1) self.action_move = prototypes.ActionMoveNearPlacePrototype.create(hero=self.hero, place=self.p1, back=False) def tearDown(self): pass def test_create(self): self.assertEqual(self.action_idl.leader, False) self.assertEqual(self.action_move.leader, True) self.assertEqual(self.action_move.bundle_id, self.action_idl.bundle_id) self.storage._test_save() def test_get_destination_coordinates(self): self.assertTrue(len(self.p1.nearest_cells) > 3) # two coordinates will be in coordinates set, other will not x_1, y_1 = self.p1.nearest_cells[0] map_info_storage.item.terrain[y_1][x_1] = TERRAIN.WATER_DEEP x_2, y_2 = self.p1.nearest_cells[1] map_info_storage.item.terrain[y_2][x_2] = TERRAIN.WATER_DEEP coordinates = set() for i in range(100): coordinates.add(prototypes.ActionMoveNearPlacePrototype._get_destination_coordinates(back=False, place=self.p1, terrains=(TERRAIN.WATER_DEEP,))) self.assertEqual(coordinates, set([(x_1, y_1), (x_2, y_2)])) def test_get_destination_coordinates__no_terrains(self): self.assertTrue(len(self.p1.nearest_cells) > 3) # two coordinates will be in coordinates set, other will not coordinates = set() for i in range(100): coordinates.add(prototypes.ActionMoveNearPlacePrototype._get_destination_coordinates(back=False, place=self.p1, terrains=(TERRAIN.WATER_DEEP,))) self.assertEqual(coordinates, set(self.p1.nearest_cells)) def test_get_destination_coordinates__back(self): self.assertTrue(len(self.p1.nearest_cells) > 3) # two coordinates will be in coordinates set, other will not x_1, y_1 = self.p1.nearest_cells[0] map_info_storage.item.terrain[y_1][x_1] = TERRAIN.WATER_DEEP x_2, y_2 = self.p1.nearest_cells[1] map_info_storage.item.terrain[y_2][x_2] = TERRAIN.WATER_DEEP coordinates = set() for i in range(100): coordinates.add(prototypes.ActionMoveNearPlacePrototype._get_destination_coordinates(back=True, place=self.p1, terrains=(TERRAIN.WATER_DEEP,))) self.assertEqual(coordinates, set([(self.p1.x, self.p1.y)])) @mock.patch('the_tale.game.heroes.objects.Hero.is_battle_start_needed', lambda self: False) def test_processed(self): current_time = TimePrototype.get_current_time() self.storage.process_turn(continue_steps_if_needed=False) x, y = self.action_move.get_destination() self.hero.position.set_coordinates(x, y, x, y, percents=1) current_time.increment_turn() self.storage.process_turn(continue_steps_if_needed=False) # can end in field or in start place self.assertTrue(self.hero.actions.current_action.TYPE in [prototypes.ActionIdlenessPrototype.TYPE, prototypes.ActionInPlacePrototype.TYPE]) self.assertTrue(self.hero.position.is_walking or self.hero.position.place) self.storage._test_save() @mock.patch('the_tale.game.heroes.objects.Hero.is_battle_start_needed', lambda self: False) def test_not_ready(self): self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(len(self.hero.actions.actions_list), 2) self.assertEqual(self.hero.actions.current_action, self.action_move) self.assertTrue(self.hero.position.is_walking or self.hero.position.place) # can end in start place self.storage._test_save() @mock.patch('the_tale.game.heroes.objects.Hero.is_battle_start_needed', lambda self: False) @mock.patch('the_tale.game.heroes.position.Position.subroad_len', lambda self: 1) def test_modify_speed(self): with mock.patch('the_tale.game.heroes.objects.Hero.modify_move_speed', mock.Mock(return_value=self.hero.move_speed)) as speed_modifier_call_counter: self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(speed_modifier_call_counter.call_count, 1) def test_full_move_and_back(self): current_time = TimePrototype.get_current_time() while len(self.hero.actions.actions_list) != 1: self.storage.process_turn(continue_steps_if_needed=False) current_time.increment_turn() self.assertEqual(self.hero.actions.current_action.TYPE, prototypes.ActionIdlenessPrototype.TYPE) self.assertTrue(self.hero.position.is_walking or self.hero.position.place) # can end in start place prototypes.ActionMoveNearPlacePrototype.create(hero=self.hero, place=self.p1, back=True) while self.hero.position.place is None or self.hero.position.place.id != self.p1.id: self.storage.process_turn(continue_steps_if_needed=False) current_time.increment_turn() self.assertEqual(self.hero.actions.current_action.TYPE, prototypes.ActionInPlacePrototype.TYPE) self.assertTrue(not self.hero.position.is_walking) self.storage._test_save() def test_move_change_place_coordinates_and_back(self): current_time = TimePrototype.get_current_time() while len(self.hero.actions.actions_list) != 1: self.storage.process_turn(continue_steps_if_needed=False) current_time.increment_turn() self.assertEqual(self.hero.actions.current_action.TYPE, prototypes.ActionIdlenessPrototype.TYPE) self.assertTrue(self.hero.position.is_walking or self.hero.position.place) # can end in start place prototypes.ActionMoveNearPlacePrototype.create(hero=self.hero, place=self.p1, back=True) self.p1.x = self.p1.x + 1 self.p1.y = self.p1.y + 1 places_logic.save_place(self.p1) while self.hero.position.place is None or self.hero.position.place.id != self.p1.id: self.storage.process_turn(continue_steps_if_needed=False) current_time.increment_turn() self.assertEqual(self.hero.actions.current_action.TYPE, prototypes.ActionInPlacePrototype.TYPE) self.assertTrue(not self.hero.position.is_walking) self.storage._test_save() def test_full(self): current_time = TimePrototype.get_current_time() while len(self.hero.actions.actions_list) != 1: self.storage.process_turn(continue_steps_if_needed=False) current_time.increment_turn() self.assertTrue(self.action_idl.leader) self.storage._test_save() def test_full__start_equal_to_finish(self): self.action_move.destination_x = self.p1.x self.action_move.destination_y = self.p1.y current_time = TimePrototype.get_current_time() while len(self.hero.actions.actions_list) != 1: self.storage.process_turn(continue_steps_if_needed=False) current_time.increment_turn() self.assertTrue(self.action_idl.leader) self.storage._test_save() @mock.patch('the_tale.game.heroes.objects.Hero.is_battle_start_needed', lambda self: True) def test_battle(self): self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(self.hero.actions.current_action.TYPE, prototypes.ActionBattlePvE1x1Prototype.TYPE) self.storage._test_save() def test_regenerate_energy_on_move(self): self.hero.preferences.set_energy_regeneration_type(heroes_relations.ENERGY_REGENERATION.PRAY) self.hero.last_energy_regeneration_at_turn -= max(next(zip(*heroes_relations.ENERGY_REGENERATION.select('period')))) self.action_move.state = self.action_move.STATE.MOVING self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(self.hero.actions.current_action.TYPE, prototypes.ActionRegenerateEnergyPrototype.TYPE) self.storage._test_save() def test_not_regenerate_energy_on_move_for_sacrifice(self): self.hero.preferences.set_energy_regeneration_type(heroes_relations.ENERGY_REGENERATION.SACRIFICE) self.hero.last_energy_regeneration_at_turn -= max(next(zip(*heroes_relations.ENERGY_REGENERATION.select('period')))) self.action_move.state = self.action_move.STATE.MOVING self.storage.process_turn(continue_steps_if_needed=False) self.assertNotEqual(self.hero.actions.current_action.TYPE, prototypes.ActionRegenerateEnergyPrototype.TYPE) self.storage._test_save() def test_regenerate_energy_after_battle_for_sacrifice(self): self.hero.preferences.set_energy_regeneration_type(heroes_relations.ENERGY_REGENERATION.SACRIFICE) self.hero.last_energy_regeneration_at_turn -= max(next(zip(*heroes_relations.ENERGY_REGENERATION.select('period')))) self.action_move.state = self.action_move.STATE.BATTLE self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(self.hero.actions.current_action.TYPE, prototypes.ActionRegenerateEnergyPrototype.TYPE) self.storage._test_save() def test_rest(self): self.hero.health = 1 self.action_move.state = self.action_move.STATE.BATTLE self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(self.hero.actions.current_action.TYPE, prototypes.ActionRestPrototype.TYPE) self.storage._test_save() @mock.patch('the_tale.game.companions.objects.Companion.need_heal', True) def test_heal_companion(self): self.action_move.state = self.action_move.STATE.BATTLE companion_record = next(companions_storage.companions.enabled_companions()) self.hero.set_companion(companions_logic.create_companion(companion_record)) self.assertEqual(self.hero.actions.current_action.TYPE, prototypes.ActionMoveNearPlacePrototype.TYPE) self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(self.hero.actions.current_action.TYPE, prototypes.ActionHealCompanionPrototype.TYPE) self.assertEqual(self.action_move.state, prototypes.ActionMoveNearPlacePrototype.STATE.HEALING_COMPANION) self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(self.hero.actions.current_action.TYPE, prototypes.ActionMoveNearPlacePrototype.TYPE) self.assertEqual(self.hero.actions.current_action.state, prototypes.ActionMoveNearPlacePrototype.STATE.HEALING_COMPANION) @mock.patch('the_tale.game.heroes.objects.Hero.is_battle_start_needed', lambda self: False) @mock.patch('the_tale.game.heroes.objects.Hero.can_companion_say_wisdom', lambda hero: True) @mock.patch('the_tale.game.balance.constants.COMPANIONS_EXP_PER_MOVE_PROBABILITY', 1.0) def test_companion_say_wisdom(self): companion_record = next(companions_storage.companions.enabled_companions()) self.hero.set_companion(companions_logic.create_companion(companion_record)) self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(self.action_move.state, self.action_move.STATE.MOVING) with self.check_delta(lambda: self.hero.experience, c.COMPANIONS_EXP_PER_MOVE_GET_EXP): self.storage.process_turn(continue_steps_if_needed=False) self.assertTrue(self.hero.journal.messages[-1].key.is_COMPANIONS_SAY_WISDOM) self.storage._test_save() def test_resurrect(self): self.hero.kill() self.action_move.state = self.action_move.STATE.BATTLE self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(self.hero.actions.current_action.TYPE, prototypes.ActionResurrectPrototype.TYPE) self.storage._test_save() @mock.patch('the_tale.game.heroes.objects.Hero.is_battle_start_needed', lambda self: False) def test_stop_when_quest_required_replane(self): while self.action_move.state != prototypes.ActionMoveNearPlacePrototype.STATE.MOVING: self.storage.process_turn(continue_steps_if_needed=False) self.assertFalse(self.action_move.replane_required) self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(self.action_move.state, prototypes.ActionMoveNearPlacePrototype.STATE.MOVING) self.action_move.replane_required = True self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(self.action_move.state, prototypes.ActionMoveNearPlacePrototype.STATE.PROCESSED)
class Worker(workers.BaseWorker): STOP_SIGNAL_REQUIRED = False def initialize(self): # worker initialized by supervisor pass def cmd_initialize(self, turn_number, worker_id): self.send_cmd('initialize', {'turn_number': turn_number, 'worker_id': worker_id}) def process_initialize(self, turn_number, worker_id): if self.initialized: self.logger.warn('WARNING: game already initialized, do reinitialization') self.storage = LogicStorage() self.initialized = True self.turn_number = turn_number self.queue = [] self.worker_id = worker_id self.logger.info('GAME INITIALIZED') environment.workers.supervisor.cmd_answer('initialize', self.worker_id) def cmd_next_turn(self, turn_number): return self.send_cmd('next_turn', data={'turn_number': turn_number}) # @profile.profile_decorator('/home/tie/repos/mine/the-tale/profile.info') def process_next_turn(self, turn_number): self.turn_number += 1 if turn_number != self.turn_number: raise LogicException('dessinchonization: workers turn number (%d) not equal to command turn number (%d)' % (self.turn_number, turn_number)) if TimePrototype.get_current_turn_number() != self.turn_number: raise LogicException('dessinchonization: workers turn number (%d) not equal to saved turn number (%d)' % (self.turn_number, TimePrototype.get_current_turn_number())) self.storage.process_turn(logger=self.logger) self.storage.save_changed_data(logger=self.logger) for hero_id in self.storage.skipped_heroes: hero = self.storage.heroes[hero_id] if hero.actions.current_action.bundle_id in self.storage.ignored_bundles: continue environment.workers.supervisor.cmd_account_release_required(hero.account_id) environment.workers.supervisor.cmd_answer('next_turn', self.worker_id) if game_settings.COLLECT_GARBAGE and self.turn_number % game_settings.COLLECT_GARBAGE_PERIOD == 0: self.logger.info('GC: start') gc.collect() self.logger.info('GC: end') def release_account(self, account_id): if account_id not in self.storage.accounts_to_heroes: environment.workers.supervisor.cmd_account_released(account_id) return hero = self.storage.accounts_to_heroes[account_id] bundle_id = hero.actions.current_action.bundle_id if bundle_id in self.storage.ignored_bundles: return with self.storage.on_exception(self.logger, message='LogicWorker.process_release_account catch exception, while processing hero %d, try to save all bundles except %d', data=(hero.id, bundle_id), excluded_bundle_id=bundle_id): self.storage.release_account_data(account_id) environment.workers.supervisor.cmd_account_released(account_id) def cmd_stop(self): return self.send_cmd('stop') def process_stop(self): # no need to save data, since they automaticaly saved on every turn self.initialized = False self.storage.save_all(logger=self.logger) environment.workers.supervisor.cmd_answer('stop', self.worker_id) self.stop_required = True self.logger.info('LOGIC STOPPED') def cmd_register_account(self, account_id): return self.send_cmd('register_account', {'account_id': account_id}) def process_register_account(self, account_id): from the_tale.accounts.prototypes import AccountPrototype account = AccountPrototype.get_by_id(account_id) if account is None: raise LogicException('can not get account with id "%d"' % (account_id,)) self.storage.load_account_data(account) def cmd_release_account(self, account_id): return self.send_cmd('release_account', {'account_id': account_id}) def process_release_account(self, account_id): self.release_account(account_id) def cmd_logic_task(self, account_id, task_id): return self.send_cmd('logic_task', {'task_id': task_id, 'account_id': account_id}) def process_logic_task(self, account_id, task_id): # pylint: disable=W0613 hero = self.storage.accounts_to_heroes[account_id] bundle_id = hero.actions.current_action.bundle_id if bundle_id in self.storage.ignored_bundles: return with self.storage.on_exception(self.logger, message='LogicWorker.process_logic_task catch exception, while processing hero %d, try to save all bundles except %d', data=(hero.id, bundle_id), excluded_bundle_id=bundle_id): task = postponed_tasks.PostponedTaskPrototype.get_by_id(task_id) task.process(self.logger, storage=self.storage) task.do_postsave_actions() self.storage.recache_bundle(bundle_id) def cmd_force_save(self, account_id): return self.send_cmd('force_save', {'account_id': account_id}) def process_force_save(self, account_id): # pylint: disable=W0613 hero = self.storage.accounts_to_heroes[account_id] bundle_id = hero.actions.current_action.bundle_id if bundle_id in self.storage.ignored_bundles: return self.storage.save_bundle_data(bundle_id=bundle_id) def cmd_start_hero_caching(self, account_id): self.send_cmd('start_hero_caching', {'account_id': account_id}) def process_start_hero_caching(self, account_id): hero = self.storage.accounts_to_heroes[account_id] if hero.actions.current_action.bundle_id in self.storage.ignored_bundles: return hero.ui_caching_started_at = datetime.datetime.now() self.storage.recache_bundle(hero.actions.current_action.bundle_id) def cmd_update_hero_with_account_data(self, account_id, is_fast, premium_end_at, active_end_at, ban_end_at, might, actual_bills): self.send_cmd('update_hero_with_account_data', {'account_id': account_id, 'is_fast': is_fast, 'premium_end_at': premium_end_at, 'active_end_at': active_end_at, 'ban_end_at': ban_end_at, 'might': might, 'actual_bills': actual_bills}) def process_update_hero_with_account_data(self, account_id, is_fast, premium_end_at, active_end_at, ban_end_at, might, actual_bills): hero = self.storage.accounts_to_heroes[account_id] if hero.actions.current_action.bundle_id in self.storage.ignored_bundles: return hero.update_with_account_data(is_fast=is_fast, premium_end_at=datetime.datetime.fromtimestamp(premium_end_at), active_end_at=datetime.datetime.fromtimestamp(active_end_at), ban_end_at=datetime.datetime.fromtimestamp(ban_end_at), might=might, actual_bills=actual_bills) self.storage.save_bundle_data(hero.actions.current_action.bundle_id) def cmd_highlevel_data_updated(self): self.send_cmd('highlevel_data_updated') def process_highlevel_data_updated(self): self.storage.on_highlevel_data_updated() def cmd_setup_quest(self, account_id, knowledge_base): return self.send_cmd('setup_quest', {'account_id': account_id, 'knowledge_base': knowledge_base}) def process_setup_quest(self, account_id, knowledge_base): hero = self.storage.accounts_to_heroes[account_id] bundle_id = hero.actions.current_action.bundle_id if bundle_id in self.storage.ignored_bundles: return with self.storage.on_exception(self.logger, message='LogicWorker.process_logic_task catch exception, while processing hero %d, try to save all bundles except %d', data=(hero.id, bundle_id), excluded_bundle_id=bundle_id): quests_logic.setup_quest_for_hero(hero, knowledge_base) self.storage.recache_bundle(bundle_id)
class MetaProxyActionForArenaPvP1x1Tests(testcase.TestCase, PvPTestsMixin): @mock.patch('the_tale.game.actions.prototypes.ActionBase.get_description', lambda self: 'abrakadabra') def setUp(self): super(MetaProxyActionForArenaPvP1x1Tests, self).setUp() create_test_map() self.account_1 = self.accounts_factory.create_account() self.account_2 = self.accounts_factory.create_account() self.storage = LogicStorage() self.storage.load_account_data(AccountPrototype.get_by_id(self.account_1.id)) self.storage.load_account_data(AccountPrototype.get_by_id(self.account_2.id)) self.hero_1 = self.storage.accounts_to_heroes[self.account_1.id] self.hero_2 = self.storage.accounts_to_heroes[self.account_2.id] self.action_idl_1 = self.hero_1.actions.current_action self.action_idl_2 = self.hero_2.actions.current_action self.pvp_create_battle(self.account_1, self.account_2, BATTLE_1X1_STATE.PROCESSING) self.pvp_create_battle(self.account_2, self.account_1, BATTLE_1X1_STATE.PROCESSING) self.bundle_id = 666 meta_action_battle = meta_actions.ArenaPvP1x1.create(self.storage, self.hero_1, self.hero_2) self.action_proxy_1 = ActionMetaProxyPrototype.create(hero=self.hero_1, _bundle_id=self.bundle_id, meta_action=meta_action_battle) self.action_proxy_2 = ActionMetaProxyPrototype.create(hero=self.hero_2, _bundle_id=self.bundle_id, meta_action=meta_action_battle) self.storage.merge_bundles([self.action_idl_1.bundle_id, self.action_idl_2.bundle_id], self.bundle_id) self.meta_action_battle = self.storage.meta_actions.values()[0] def tearDown(self): pass # renamed to fix segmentation fault def test_z_create(self): self.assertFalse(self.action_idl_1.leader) self.assertFalse(self.action_idl_2.leader) self.assertTrue(self.action_proxy_1.leader) self.assertTrue(self.action_proxy_2.leader) self.assertEqual(self.hero_1.actions.number, 2) self.assertEqual(self.hero_2.actions.number, 2) self.assertNotEqual(self.action_proxy_1.bundle_id, self.action_idl_1.bundle_id) self.assertNotEqual(self.action_proxy_2.bundle_id, self.action_idl_2.bundle_id) self.assertEqual(self.action_proxy_1.bundle_id, self.action_proxy_2.bundle_id) self.assertEqual(self.action_proxy_1.meta_action, self.action_proxy_2.meta_action) self.assertEqual(self.action_proxy_1.meta_action, self.meta_action_battle) self.assertEqual(len(self.storage.meta_actions), 1) def test_one_action_step_one_meta_step(self): with mock.patch('the_tale.game.actions.meta_actions.ArenaPvP1x1._process') as meta_action_process_counter: self.action_proxy_1.process() self.assertEqual(meta_action_process_counter.call_count, 1) def test_two_actions_step_one_meta_step(self): with mock.patch('the_tale.game.actions.meta_actions.ArenaPvP1x1._process') as meta_action_process_counter: self.action_proxy_1.process() self.action_proxy_2.process() self.assertEqual(meta_action_process_counter.call_count, 1) def test_two_actions_step_one_meta_step_from_storage(self): with mock.patch('the_tale.game.actions.meta_actions.ArenaPvP1x1._process') as meta_action_process_counter: self.storage.process_turn() self.assertEqual(meta_action_process_counter.call_count, 1) def test_success_processing(self): self.action_proxy_1.process() self.assertEqual(self.action_proxy_1.percents, self.meta_action_battle.percents) self.assertNotEqual(self.action_proxy_2.percents, self.meta_action_battle.percents) def test_full_battle(self): current_time = TimePrototype.get_current_time() while self.action_proxy_1.state != meta_actions.ArenaPvP1x1.STATE.PROCESSED: self.storage.process_turn(continue_steps_if_needed=False) current_time.increment_turn() self.assertEqual(self.meta_action_battle.state, meta_actions.ArenaPvP1x1.STATE.PROCESSED) self.assertTrue(self.hero_1.is_alive and self.hero_2.is_alive) self.assertTrue(self.action_idl_1.leader) self.assertTrue(self.action_idl_2.leader) def test_get_meta_action__without_storage(self): self.action_proxy_1.storage = None self.assertNotEqual(self.action_proxy_1.meta_action, None) def test_get_meta_action__no_meta_action(self): self.storage.meta_actions = {} self.assertEqual(self.action_proxy_1.meta_action, None) def test_get_meta_action(self): self.assertEqual(self.action_proxy_1.meta_action.uid, self.meta_action_battle.uid) self.assertEqual(self.action_proxy_2.meta_action.uid, self.meta_action_battle.uid) def test_get_ui_type__without_storage(self): self.action_proxy_1.storage = None self.assertEqual(self.action_proxy_1.ui_type, relations.ACTION_TYPE.ARENA_PVP_1X1) self.assertEqual(self.action_proxy_1.description_text_name, 'meta_action_arena_pvp_1x1_description') def test_get_ui_type__with_metaaction(self): self.assertEqual(self.action_proxy_1.ui_type, relations.ACTION_TYPE.ARENA_PVP_1X1) self.assertEqual(self.action_proxy_1.description_text_name, 'meta_action_arena_pvp_1x1_description')
class MetaProxyActionForArenaPvP1x1Tests(testcase.TestCase, PvPTestsMixin): @mock.patch('the_tale.game.actions.prototypes.ActionBase.get_description', lambda self: 'abrakadabra') def setUp(self): super(MetaProxyActionForArenaPvP1x1Tests, self).setUp() create_test_map() self.account_1 = self.accounts_factory.create_account() self.account_2 = self.accounts_factory.create_account() self.storage = LogicStorage() self.storage.load_account_data( AccountPrototype.get_by_id(self.account_1.id)) self.storage.load_account_data( AccountPrototype.get_by_id(self.account_2.id)) self.hero_1 = self.storage.accounts_to_heroes[self.account_1.id] self.hero_2 = self.storage.accounts_to_heroes[self.account_2.id] self.action_idl_1 = self.hero_1.actions.current_action self.action_idl_2 = self.hero_2.actions.current_action self.pvp_create_battle(self.account_1, self.account_2, BATTLE_1X1_STATE.PROCESSING) self.pvp_create_battle(self.account_2, self.account_1, BATTLE_1X1_STATE.PROCESSING) self.bundle_id = 666 meta_action_battle = meta_actions.ArenaPvP1x1.create( self.storage, self.hero_1, self.hero_2) self.action_proxy_1 = ActionMetaProxyPrototype.create( hero=self.hero_1, _bundle_id=self.bundle_id, meta_action=meta_action_battle) self.action_proxy_2 = ActionMetaProxyPrototype.create( hero=self.hero_2, _bundle_id=self.bundle_id, meta_action=meta_action_battle) self.storage.merge_bundles( [self.action_idl_1.bundle_id, self.action_idl_2.bundle_id], self.bundle_id) self.meta_action_battle = self.storage.meta_actions.values()[0] def tearDown(self): pass # renamed to fix segmentation fault def test_z_create(self): self.assertFalse(self.action_idl_1.leader) self.assertFalse(self.action_idl_2.leader) self.assertTrue(self.action_proxy_1.leader) self.assertTrue(self.action_proxy_2.leader) self.assertEqual(self.hero_1.actions.number, 2) self.assertEqual(self.hero_2.actions.number, 2) self.assertNotEqual(self.action_proxy_1.bundle_id, self.action_idl_1.bundle_id) self.assertNotEqual(self.action_proxy_2.bundle_id, self.action_idl_2.bundle_id) self.assertEqual(self.action_proxy_1.bundle_id, self.action_proxy_2.bundle_id) self.assertEqual(self.action_proxy_1.meta_action, self.action_proxy_2.meta_action) self.assertEqual(self.action_proxy_1.meta_action, self.meta_action_battle) self.assertEqual(len(self.storage.meta_actions), 1) def test_one_action_step_one_meta_step(self): with mock.patch( 'the_tale.game.actions.meta_actions.ArenaPvP1x1._process' ) as meta_action_process_counter: self.action_proxy_1.process() self.assertEqual(meta_action_process_counter.call_count, 1) def test_two_actions_step_one_meta_step(self): with mock.patch( 'the_tale.game.actions.meta_actions.ArenaPvP1x1._process' ) as meta_action_process_counter: self.action_proxy_1.process() self.action_proxy_2.process() self.assertEqual(meta_action_process_counter.call_count, 1) def test_two_actions_step_one_meta_step_from_storage(self): with mock.patch( 'the_tale.game.actions.meta_actions.ArenaPvP1x1._process' ) as meta_action_process_counter: self.storage.process_turn() self.assertEqual(meta_action_process_counter.call_count, 1) def test_success_processing(self): self.action_proxy_1.process() self.assertEqual(self.action_proxy_1.percents, self.meta_action_battle.percents) self.assertNotEqual(self.action_proxy_2.percents, self.meta_action_battle.percents) def test_full_battle(self): current_time = TimePrototype.get_current_time() while self.action_proxy_1.state != meta_actions.ArenaPvP1x1.STATE.PROCESSED: self.storage.process_turn(continue_steps_if_needed=False) current_time.increment_turn() self.assertEqual(self.meta_action_battle.state, meta_actions.ArenaPvP1x1.STATE.PROCESSED) self.assertTrue(self.hero_1.is_alive and self.hero_2.is_alive) self.assertTrue(self.action_idl_1.leader) self.assertTrue(self.action_idl_2.leader) def test_get_meta_action__without_storage(self): self.action_proxy_1.storage = None self.assertNotEqual(self.action_proxy_1.meta_action, None) def test_get_meta_action__no_meta_action(self): self.storage.meta_actions = {} self.assertEqual(self.action_proxy_1.meta_action, None) def test_get_meta_action(self): self.assertEqual(self.action_proxy_1.meta_action.uid, self.meta_action_battle.uid) self.assertEqual(self.action_proxy_2.meta_action.uid, self.meta_action_battle.uid) def test_get_ui_type__without_storage(self): self.action_proxy_1.storage = None self.assertEqual(self.action_proxy_1.ui_type, relations.ACTION_TYPE.ARENA_PVP_1X1) self.assertEqual(self.action_proxy_1.description_text_name, 'meta_action_arena_pvp_1x1_description') def test_get_ui_type__with_metaaction(self): self.assertEqual(self.action_proxy_1.ui_type, relations.ACTION_TYPE.ARENA_PVP_1X1) self.assertEqual(self.action_proxy_1.description_text_name, 'meta_action_arena_pvp_1x1_description')
class MakeChoiceTaskTest(testcase.TestCase, QuestTestsMixin): def setUp(self): super(MakeChoiceTaskTest, self).setUp() create_test_map() result, account_id, bundle_id = register_user("test_user", "*****@*****.**", "111111") self.account_id = account_id self.storage = LogicStorage() self.storage.load_account_data(AccountPrototype.get_by_id(account_id)) self.hero = self.storage.accounts_to_heroes[account_id] self.choice_1_uid = "[ns-0]choice_1" self.choice_2_uid = "[ns-0]choice_2" self.option_1_1_uid = "#option([ns-0]choice_1, [ns-0]choice_2, opt_2)" self.option_1_2_uid = "#option([ns-0]choice_1, [ns-0]finish_2, opt_1)" self.option_2_1_uid = "#option([ns-0]choice_2, [ns-0]finish_1_1, opt_2_1)" self.option_2_2_uid = "#option([ns-0]choice_2, [ns-0]finish_1_2, opt_2_2)" def create_task(self, option_uid, account_id=None): if account_id is None: account_id = self.account_id quest = self.turn_to_quest(self.storage, self.hero.id) self.assertTrue(self.choice_1_uid in quest.knowledge_base) self.assertTrue(self.choice_2_uid in quest.knowledge_base) self.assertTrue(self.option_1_1_uid in quest.knowledge_base) self.assertTrue(self.option_1_2_uid in quest.knowledge_base) self.assertTrue(self.option_2_1_uid in quest.knowledge_base) self.assertTrue(self.option_2_2_uid in quest.knowledge_base) task = MakeChoiceTask(account_id=account_id, option_uid=option_uid) return task @mock.patch( "questgen.quests.quests_base.QuestsBase._available_quests", lambda *argv, **kwargs: [QuestWith2ChoicePoints] ) def test_create(self): task = self.create_task(option_uid=self.option_1_1_uid) self.assertTrue(task.state.is_UNPROCESSED) @mock.patch( "questgen.quests.quests_base.QuestsBase._available_quests", lambda *argv, **kwargs: [QuestWith2ChoicePoints] ) def test_serialization(self): task = self.create_task(option_uid=self.option_1_1_uid) self.assertEqual(task.serialize(), MakeChoiceTask.deserialize(task.serialize()).serialize()) @mock.patch( "questgen.quests.quests_base.QuestsBase._available_quests", lambda *argv, **kwargs: [QuestWith2ChoicePoints] ) def test_unknown_choice(self): task = self.create_task(option_uid="unknown_choice") self.assertEqual(task.process(FakePostpondTaskPrototype(), self.storage), POSTPONED_TASK_LOGIC_RESULT.ERROR) self.assertTrue(task.state.is_WRONG_POINT) @mock.patch( "questgen.quests.quests_base.QuestsBase._available_quests", lambda *argv, **kwargs: [QuestWith2ChoicePoints] ) def test_no_quests(self): self.turn_to_quest(self.storage, self.hero.id) result, account_id, bundle_id = register_user("test_user_2", "*****@*****.**", "111111") self.storage.load_account_data(AccountPrototype.get_by_id(account_id)) task = self.create_task(option_uid=self.option_1_1_uid, account_id=account_id) self.assertEqual(task.process(FakePostpondTaskPrototype(), self.storage), POSTPONED_TASK_LOGIC_RESULT.ERROR) self.assertTrue(task.state.is_QUEST_NOT_FOUND) @mock.patch( "questgen.quests.quests_base.QuestsBase._available_quests", lambda *argv, **kwargs: [QuestWith2ChoicePoints] ) def test_wrong_point(self): task = self.create_task(option_uid=self.option_2_1_uid) self.assertEqual(task.process(FakePostpondTaskPrototype(), self.storage), POSTPONED_TASK_LOGIC_RESULT.ERROR) self.assertTrue(task.state.is_WRONG_POINT) @mock.patch( "questgen.quests.quests_base.QuestsBase._available_quests", lambda *argv, **kwargs: [QuestWith2ChoicePoints] ) def test_already_chosen(self): task = self.create_task(option_uid=self.option_1_1_uid) self.assertEqual(task.process(FakePostpondTaskPrototype(), self.storage), POSTPONED_TASK_LOGIC_RESULT.SUCCESS) self.assertTrue(task.state.is_PROCESSED) task = self.create_task(option_uid=self.option_1_2_uid) self.assertEqual(task.process(FakePostpondTaskPrototype(), self.storage), POSTPONED_TASK_LOGIC_RESULT.ERROR) self.assertTrue(task.state.is_ALREADY_CHOSEN) @mock.patch( "questgen.quests.quests_base.QuestsBase._available_quests", lambda *argv, **kwargs: [QuestWith2ChoicePoints] ) def test_success(self): self.hero.quests.updated = False self.assertTrue(all(not action.replane_required for action in self.hero.actions.actions_list)) task = self.create_task(option_uid=self.option_1_1_uid) self.assertEqual(task.process(FakePostpondTaskPrototype(), self.storage), POSTPONED_TASK_LOGIC_RESULT.SUCCESS) self.assertTrue(task.state.is_PROCESSED) self.assertTrue(all(action.replane_required for action in self.hero.actions.actions_list)) self.assertTrue(self.hero.quests.updated) @mock.patch( "questgen.quests.quests_base.QuestsBase._available_quests", lambda *argv, **kwargs: [QuestWith2ChoicePoints] ) def test_choose_second_choice_before_first_completed(self): task = self.create_task(option_uid=self.option_1_2_uid) self.assertEqual(task.process(FakePostpondTaskPrototype(), self.storage), POSTPONED_TASK_LOGIC_RESULT.SUCCESS) self.assertTrue(task.state.is_PROCESSED) task = self.create_task(option_uid=self.option_2_1_uid) self.assertEqual(task.process(FakePostpondTaskPrototype(), self.storage), POSTPONED_TASK_LOGIC_RESULT.ERROR) self.assertTrue(task.state.is_WRONG_POINT) @mock.patch( "questgen.quests.quests_base.QuestsBase._available_quests", lambda *argv, **kwargs: [QuestWith2ChoicePoints] ) def test_choose_second_choice_after_first_completed(self): task = self.create_task(option_uid=self.option_1_2_uid) self.assertEqual(task.process(FakePostpondTaskPrototype(), self.storage), POSTPONED_TASK_LOGIC_RESULT.SUCCESS) self.assertTrue(task.state.is_PROCESSED) current_time = TimePrototype.get_current_time() while True: self.assertNotEqual(self.hero.actions.current_action, ActionIdlenessPrototype.TYPE) task = self.create_task(option_uid=self.option_2_1_uid) if task.process(FakePostpondTaskPrototype(), self.storage) == POSTPONED_TASK_LOGIC_RESULT.ERROR: break self.storage.process_turn() self.storage.save_changed_data() current_time.increment_turn() @mock.patch( "questgen.quests.quests_base.QuestsBase._available_quests", lambda *argv, **kwargs: [QuestWith2ChoicePoints] ) def test_no_choices(self): task = self.create_task(option_uid=self.option_1_1_uid) knowledge_base = self.hero.quests.current_quest.knowledge_base finish_state = knowledge_base.filter(facts.Finish).next() self.hero.quests.current_quest.machine.pointer.change_in_knowlege_base( knowledge_base, state=finish_state.uid, jump=None ) self.assertEqual(task.process(FakePostpondTaskPrototype(), self.storage), POSTPONED_TASK_LOGIC_RESULT.ERROR) self.assertTrue(task.state.is_NO_CHOICES_IN_QUEST)
class Command(BaseCommand): help = 'test how hero move in levels corridor on real map' @mock.patch('the_tale.game.balance.constants.EXP_PER_QUEST_FRACTION', 0.0) def handle(self, *args, **options): try: self.test_corridor() except KeyboardInterrupt: pass except Exception: traceback.print_exc() def set_hero_companion(self): from the_tale.game.companions import storage from the_tale.game.companions import models from the_tale.game.companions import logic COMPANION_NAME = 'test_hero_level_companion' for companion in storage.companions.all(): if companion.name.startswith(COMPANION_NAME): models.CompanionRecord.objects.filter(id=companion.id).delete() storage.companions.refresh() break companion_record = logic.create_random_companion_record(COMPANION_NAME) self.hero.set_companion(logic.create_companion(companion_record)) @mock.patch('dext.settings.conf.dext_settings_settings.UPDATE_DATABASE', False) @mock.patch( 'the_tale.game.heroes.habilities.AbilitiesPrototype.modify_attribute', fake_modify_attribute) @mock.patch( 'the_tale.game.quests.conf.quests_settings.INTERFERED_PERSONS_LIVE_TIME', 0) def test_corridor(self): # fill_empty_keys_with_fake_phrases(u'test_hero_level_companion') result, account_id, bundle_id = register_user(uuid.uuid4().hex) # pylint: disable=W0612 self.storage = LogicStorage() self.storage.load_account_data(AccountPrototype.get_by_id(account_id)) self.hero = self.storage.accounts_to_heroes[account_id] self.set_hero_companion() for level in range(1, 100): print() print( '-----------------------------------------------------------------------' ) print('process level %d\texpected turns: %d\texpected days: %.2f' % (level, f.turns_on_lvl(level), f.time_on_lvl(level) / 24)) for i in range(f.turns_on_lvl(level)): # pylint: disable=W0612 self.storage.process_turn() turn.increment() # simulate user behaviour on healing companion if self.hero.companion.health < self.hero.companion.max_health / 2: self.hero.companion.health = self.hero.companion.max_health self.hero.randomized_level_up() exp_to_next_level = float(self.hero.experience) / f.exp_on_lvl( self.hero.level) * 100 exp_from_expected = float( f.total_exp_to_lvl(self.hero.level) + self.hero.experience) / f.total_exp_to_lvl(level + 1) * 100 exp_untaken = f.total_exp_to_lvl(level + 1) - f.total_exp_to_lvl( self.hero.level) - self.hero.experience quests_untaken = float(exp_untaken) / f.experience_for_quest( c.QUEST_AREA_RADIUS) print( 'hero level: %d\texp: %.2f%%\texp from expected: %.2f%% (%d exp, %.2f quests)\ttotal quests %d' % (self.hero.level, exp_to_next_level, exp_from_expected, exp_untaken, quests_untaken, self.hero.statistics.quests_done)) print('abilities: %s' % ' '.join('%s-%d' % (ability_id, ability.level) for ability_id, ability in list( self.hero.abilities.abilities.items()))) print('deaths: %d' % self.hero.statistics.pve_deaths) total_gold = f.total_gold_at_lvl(self.hero.level) print('total money: %d from expected %d (x%.2f)' % (self.hero.statistics.money_earned, total_gold, float(self.hero.statistics.money_earned) / total_gold if total_gold > 0 else 0)) total_artifacts = int( f.total_time_for_lvl(self.hero.level) / 24 * c.ARTIFACTS_LOOT_PER_DAY) print('total artifacts: %d from expected %d (x%.2f)' % (self.hero.statistics.artifacts_had, total_artifacts, float(self.hero.statistics.artifacts_had) / total_artifacts if total_artifacts > 0 else 0)) print('power: %r from expected %r' % (self.hero.power, Power.power_to_level( self.hero.preferences.archetype.power_distribution, self.hero.level))) print('power total: %d from expected %r (x%.2f)' % (self.hero.power.total(), Power.power_to_level( self.hero.preferences.archetype.power_distribution, self.hero.level).total(), float(self.hero.power.total()) / Power.power_to_level( self.hero.preferences.archetype.power_distribution, self.hero.level).total()))
class MakeChoiceTaskTest(testcase.TestCase, QuestTestsMixin): def setUp(self): super(MakeChoiceTaskTest, self).setUp() create_test_map() account = self.accounts_factory.create_account() self.account_id = account.id self.storage = LogicStorage() self.storage.load_account_data(account) self.hero = self.storage.accounts_to_heroes[account.id] self.choice_1_uid = '[ns-0]choice_1' self.choice_2_uid = '[ns-0]choice_2' self.option_1_1_uid = '#option([ns-0]choice_1, [ns-0]choice_2, opt_2)' self.option_1_2_uid = '#option([ns-0]choice_1, [ns-0]finish_2, opt_1)' self.option_2_1_uid = '#option([ns-0]choice_2, [ns-0]finish_1_1, opt_2_1)' self.option_2_2_uid = '#option([ns-0]choice_2, [ns-0]finish_1_2, opt_2_2)' def create_task(self, option_uid, account_id=None): if account_id is None: account_id = self.account_id quest = self.turn_to_quest(self.storage, self.hero.id) self.assertTrue(self.choice_1_uid in quest.knowledge_base) self.assertTrue(self.choice_2_uid in quest.knowledge_base) self.assertTrue(self.option_1_1_uid in quest.knowledge_base) self.assertTrue(self.option_1_2_uid in quest.knowledge_base) self.assertTrue(self.option_2_1_uid in quest.knowledge_base) self.assertTrue(self.option_2_2_uid in quest.knowledge_base) task = MakeChoiceTask(account_id=account_id, option_uid=option_uid) return task @mock.patch('questgen.quests.quests_base.QuestsBase._available_quests', lambda *argv, **kwargs: [QuestWith2ChoicePoints]) def test_create(self): task = self.create_task(option_uid=self.option_1_1_uid) self.assertTrue(task.state.is_UNPROCESSED) @mock.patch('questgen.quests.quests_base.QuestsBase._available_quests', lambda *argv, **kwargs: [QuestWith2ChoicePoints]) def test_serialization(self): task = self.create_task(option_uid=self.option_1_1_uid) self.assertEqual( task.serialize(), MakeChoiceTask.deserialize(task.serialize()).serialize()) @mock.patch('questgen.quests.quests_base.QuestsBase._available_quests', lambda *argv, **kwargs: [QuestWith2ChoicePoints]) def test_unknown_choice(self): task = self.create_task(option_uid='unknown_choice') self.assertEqual( task.process(FakePostpondTaskPrototype(), self.storage), POSTPONED_TASK_LOGIC_RESULT.ERROR) self.assertTrue(task.state.is_WRONG_POINT) @mock.patch('questgen.quests.quests_base.QuestsBase._available_quests', lambda *argv, **kwargs: [QuestWith2ChoicePoints]) def test_no_quests(self): self.turn_to_quest(self.storage, self.hero.id) account = self.accounts_factory.create_account() self.storage.load_account_data(account) task = self.create_task(option_uid=self.option_1_1_uid, account_id=account.id) self.assertEqual( task.process(FakePostpondTaskPrototype(), self.storage), POSTPONED_TASK_LOGIC_RESULT.ERROR) self.assertTrue(task.state.is_QUEST_NOT_FOUND) @mock.patch('questgen.quests.quests_base.QuestsBase._available_quests', lambda *argv, **kwargs: [QuestWith2ChoicePoints]) def test_wrong_point(self): task = self.create_task(option_uid=self.option_2_1_uid) self.assertEqual( task.process(FakePostpondTaskPrototype(), self.storage), POSTPONED_TASK_LOGIC_RESULT.ERROR) self.assertTrue(task.state.is_WRONG_POINT) @mock.patch('questgen.quests.quests_base.QuestsBase._available_quests', lambda *argv, **kwargs: [QuestWith2ChoicePoints]) def test_already_chosen(self): task = self.create_task(option_uid=self.option_1_1_uid) self.assertEqual( task.process(FakePostpondTaskPrototype(), self.storage), POSTPONED_TASK_LOGIC_RESULT.SUCCESS) self.assertTrue(task.state.is_PROCESSED) task = self.create_task(option_uid=self.option_1_2_uid) self.assertEqual( task.process(FakePostpondTaskPrototype(), self.storage), POSTPONED_TASK_LOGIC_RESULT.ERROR) self.assertTrue(task.state.is_ALREADY_CHOSEN) @mock.patch('questgen.quests.quests_base.QuestsBase._available_quests', lambda *argv, **kwargs: [QuestWith2ChoicePoints]) def test_success(self): self.hero.quests.updated = False self.assertTrue( all(not action.replane_required for action in self.hero.actions.actions_list)) task = self.create_task(option_uid=self.option_1_1_uid) self.assertEqual( task.process(FakePostpondTaskPrototype(), self.storage), POSTPONED_TASK_LOGIC_RESULT.SUCCESS) self.assertTrue(task.state.is_PROCESSED) self.assertTrue( all(action.replane_required for action in self.hero.actions.actions_list)) self.assertTrue(self.hero.quests.updated) @mock.patch('questgen.quests.quests_base.QuestsBase._available_quests', lambda *argv, **kwargs: [QuestWith2ChoicePoints]) def test_choose_second_choice_before_first_completed(self): task = self.create_task(option_uid=self.option_1_2_uid) self.assertEqual( task.process(FakePostpondTaskPrototype(), self.storage), POSTPONED_TASK_LOGIC_RESULT.SUCCESS) self.assertTrue(task.state.is_PROCESSED) task = self.create_task(option_uid=self.option_2_1_uid) self.assertEqual( task.process(FakePostpondTaskPrototype(), self.storage), POSTPONED_TASK_LOGIC_RESULT.ERROR) self.assertTrue(task.state.is_WRONG_POINT) @mock.patch('questgen.quests.quests_base.QuestsBase._available_quests', lambda *argv, **kwargs: [QuestWith2ChoicePoints]) def test_choose_second_choice_after_first_completed(self): task = self.create_task(option_uid=self.option_1_2_uid) self.assertEqual( task.process(FakePostpondTaskPrototype(), self.storage), POSTPONED_TASK_LOGIC_RESULT.SUCCESS) self.assertTrue(task.state.is_PROCESSED) current_time = TimePrototype.get_current_time() while True: self.assertNotEqual(self.hero.actions.current_action.TYPE, ActionIdlenessPrototype.TYPE) task = self.create_task(option_uid=self.option_2_1_uid) if task.process(FakePostpondTaskPrototype(), self.storage) == POSTPONED_TASK_LOGIC_RESULT.ERROR: break self.storage.process_turn() self.storage.save_changed_data() current_time.increment_turn() @mock.patch('questgen.quests.quests_base.QuestsBase._available_quests', lambda *argv, **kwargs: [QuestWith2ChoicePoints]) def test_no_choices(self): task = self.create_task(option_uid=self.option_1_1_uid) knowledge_base = self.hero.quests.current_quest.knowledge_base finish_state = next(knowledge_base.filter(facts.Finish)) self.hero.quests.current_quest.machine.pointer.change_in_knowlege_base( knowledge_base, state=finish_state.uid, jump=None) self.assertEqual( task.process(FakePostpondTaskPrototype(), self.storage), POSTPONED_TASK_LOGIC_RESULT.ERROR) self.assertTrue(task.state.is_NO_CHOICES_IN_QUEST)
class HealCompanionActionTest(UseAbilityTaskMixin, testcase.TestCase): PROCESSOR = Help def setUp(self): super(HealCompanionActionTest, self).setUp() create_test_map() self.account = self.accounts_factory.create_account() self.storage = LogicStorage() self.storage.load_account_data(self.account) self.hero = self.storage.accounts_to_heroes[self.account.id] self.companion_record = companions_storage.companions.enabled_companions( ).next() self.hero.set_companion( companions_logic.create_companion(self.companion_record)) self.action_idl = self.hero.actions.current_action self.hero.companion.healed_at_turn = -1 with self.check_increased(lambda: self.hero.companion.healed_at_turn): self.action_heal_companion = prototypes.ActionHealCompanionPrototype.create( hero=self.hero) def test_create(self): self.assertEqual(self.action_idl.leader, False) self.assertEqual(self.action_heal_companion.leader, True) self.assertEqual(self.action_heal_companion.bundle_id, self.action_heal_companion.bundle_id) self.assertEqual(self.action_heal_companion.percents, 0) self.storage._test_save() def test_processed__no_companion(self): self.hero.remove_companion() self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(len(self.hero.actions.actions_list), 1) self.assertEqual(self.hero.actions.current_action, self.action_idl) self.storage._test_save() def test_processed__max_health(self): self.assertEqual(self.hero.companion.health, self.hero.companion.max_health) self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(len(self.hero.actions.actions_list), 1) self.assertEqual(self.hero.actions.current_action, self.action_idl) self.storage._test_save() def test_not_ready(self): self.hero.companion.health = 1 self.storage.process_turn() self.assertEqual(len(self.hero.actions.actions_list), 2) self.assertEqual(self.hero.actions.current_action, self.action_heal_companion) self.assertTrue(self.hero.companion.health, 1) self.assertTrue(self.action_heal_companion.percents > 0) self.storage._test_save() def test_ability_heal_companion(self): self.hero.companion.health = 1 with self.check_increased(lambda: self.action_heal_companion.percents): with self.check_increased(lambda: self.hero.companion.health): ability = self.PROCESSOR() with mock.patch( 'the_tale.game.actions.prototypes.ActionBase.get_help_choice', lambda x: HELP_CHOICES.HEAL_COMPANION): self.assertTrue( ability.use(**self.use_attributes( hero=self.hero, storage=self.storage))) def test_ability_heal_companion__processed_when_healed(self): self.hero.companion.health -= 1 with self.check_increased(lambda: self.action_heal_companion.percents): with self.check_increased(lambda: self.hero.companion.health): ability = self.PROCESSOR() with mock.patch( 'the_tale.game.actions.prototypes.ActionBase.get_help_choice', lambda x: HELP_CHOICES.HEAL_COMPANION): self.assertTrue( ability.use(**self.use_attributes( hero=self.hero, storage=self.storage))) self.assertTrue(self.action_heal_companion.percents, 1) self.assertEqual(self.action_heal_companion.state, self.action_heal_companion.STATE.PROCESSED) @mock.patch( 'the_tale.game.heroes.prototypes.HeroPrototype.can_companion_exp_per_heal', lambda hero: True) def test_ability_heal_companion__processed_when_healed__exp_per_heal(self): self.hero.companion.health -= 1 with self.check_delta(lambda: self.hero.experience, c.COMPANIONS_EXP_PER_HEAL): with self.check_increased(lambda: self.hero.companion.health): ability = self.PROCESSOR() with mock.patch( 'the_tale.game.actions.prototypes.ActionBase.get_help_choice', lambda x: HELP_CHOICES.HEAL_COMPANION): self.assertTrue( ability.use(**self.use_attributes( hero=self.hero, storage=self.storage))) self.assertTrue(self.action_heal_companion.percents, 1) self.assertEqual(self.action_heal_companion.state, self.action_heal_companion.STATE.PROCESSED) def test_ability_heal_companion__full_action(self): self.hero.companion.health = 1 current_time = TimePrototype.get_current_time() with self.check_increased(lambda: self.action_heal_companion.percents): with self.check_increased(lambda: self.hero.companion.health): while self.action_heal_companion.state != self.action_heal_companion.STATE.PROCESSED: current_time.increment_turn() ability = self.PROCESSOR() with mock.patch( 'the_tale.game.actions.prototypes.ActionBase.get_help_choice', lambda x: HELP_CHOICES.HEAL_COMPANION): self.assertTrue( ability.use(**self.use_attributes( hero=self.hero, storage=self.storage))) def test_full(self): self.hero.companion.health = 1 current_time = TimePrototype.get_current_time() with self.check_delta(lambda: self.hero.companion.health, c.COMPANIONS_HEALTH_PER_HEAL): while len(self.hero.actions.actions_list) != 1: self.storage.process_turn(continue_steps_if_needed=False) current_time.increment_turn() self.assertTrue(self.action_idl.leader) self.storage._test_save() @mock.patch( 'the_tale.game.heroes.prototypes.HeroPrototype.can_companion_exp_per_heal', lambda hero: True) def test_full__exp_per_heal(self): self.hero.companion.health = 1 with self.check_delta(lambda: self.hero.experience, c.COMPANIONS_EXP_PER_HEAL): current_time = TimePrototype.get_current_time() while len(self.hero.actions.actions_list) != 1: self.storage.process_turn(continue_steps_if_needed=False) current_time.increment_turn() self.assertTrue(self.action_idl.leader) self.storage._test_save() @mock.patch('the_tale.common.utils.logic.randint_from_1', lambda v: v) @mock.patch( 'the_tale.game.balance.constants.COMPANIONS_REGEN_ON_HEAL_PER_HEAL', 1.0) @mock.patch( 'the_tale.game.heroes.prototypes.HeroPrototype.can_companion_regenerate', lambda hero: True) def test_full__regeneration(self): self.hero.companion.health = 1 current_time = TimePrototype.get_current_time() while len(self.hero.actions.actions_list) != 1: self.storage.process_turn(continue_steps_if_needed=False) current_time.increment_turn() self.assertTrue(self.action_idl.leader) self.assertEqual( self.hero.companion.health, 1 + c.COMPANIONS_HEALTH_PER_HEAL + c.COMPANIONS_REGEN_ON_HEAL_AMOUNT) self.storage._test_save() @mock.patch( 'the_tale.game.heroes.prototypes.HeroPrototype.companion_heal_probability', 1) @mock.patch('the_tale.common.utils.logic.randint_from_1', lambda v: v) def test_companion_healing_by_hero(self): current_time = TimePrototype.get_current_time() self.hero.companion.health = 1 with self.check_delta( lambda: self.hero.companion.health, c.COMPANIONS_HEALTH_PER_HEAL + c.COMPANIONS_REGEN_BY_HERO): self.action_heal_companion.state = self.action_heal_companion.STATE.PROCESSED self.storage.process_turn(continue_steps_if_needed=False) current_time.increment_turn() @mock.patch( 'the_tale.game.heroes.prototypes.HeroPrototype.companion_heal_probability', 0) @mock.patch('the_tale.common.utils.logic.randint_from_1', lambda v: v) def test_companion_healing_by_hero__not_healed(self): current_time = TimePrototype.get_current_time() self.hero.companion.health = 1 with self.check_delta(lambda: self.hero.companion.health, c.COMPANIONS_HEALTH_PER_HEAL): self.action_heal_companion.state = self.action_heal_companion.STATE.PROCESSED self.storage.process_turn(continue_steps_if_needed=False) current_time.increment_turn()
class ShortTeleportTests(CardsTestMixin, testcase.TestCase): CARD = effects.ShortTeleport def setUp(self): super(ShortTeleportTests, self).setUp() self.place_1, self.place_2, self.place_3 = create_test_map() self.account_1 = self.accounts_factory.create_account() self.storage = LogicStorage() self.storage.load_account_data(self.account_1) self.hero = self.storage.accounts_to_heroes[self.account_1.id] self.hero.position.set_place(self.place_1) self.card = self.CARD() @mock.patch('the_tale.game.heroes.objects.Hero.is_battle_start_needed', lambda self: False) def test_moving(self): self.assertFalse(self.hero.actions.current_action.TYPE.is_MOVE_TO) result, step, postsave_actions = self.card.use(**self.use_attributes(storage=self.storage, hero=self.hero)) self.assertEqual((result, step, postsave_actions), (ComplexChangeTask.RESULT.FAILED, ComplexChangeTask.STEP.ERROR, ())) @mock.patch('the_tale.game.heroes.objects.Hero.is_battle_start_needed', lambda self: False) def test_use(self): action_move = actions_prototypes.ActionMoveToPrototype.create(hero=self.hero, destination=self.place_3) self.storage.process_turn(continue_steps_if_needed=False) self.assertTrue(self.hero.actions.current_action.state == actions_prototypes.ActionMoveToPrototype.STATE.MOVING) self.assertTrue(self.hero.position.percents < 1) result, step, postsave_actions = self.card.use(**self.use_attributes(storage=self.storage, hero=self.hero)) self.assertEqual((result, step, postsave_actions), (ComplexChangeTask.RESULT.SUCCESSED, ComplexChangeTask.STEP.SUCCESS, ())) self.assertEqual(action_move.state, action_move.STATE.IN_CITY) self.assertEqual(self.hero.actions.current_action.TYPE, actions_prototypes.ActionInPlacePrototype.TYPE) self.assertTrue(self.hero.position.place.id, self.place_2.id) result, step, postsave_actions = self.card.use(**self.use_attributes(storage=self.storage, hero=self.hero)) self.assertEqual((result, step, postsave_actions), (ComplexChangeTask.RESULT.FAILED, ComplexChangeTask.STEP.ERROR, ())) while not action_move.leader: self.storage.process_turn(continue_steps_if_needed=False) self.storage.process_turn(continue_steps_if_needed=False) result, step, postsave_actions = self.card.use(**self.use_attributes(storage=self.storage, hero=self.hero)) self.assertEqual((result, step, postsave_actions), (ComplexChangeTask.RESULT.SUCCESSED, ComplexChangeTask.STEP.SUCCESS, ())) self.assertTrue(self.hero.position.place.id, self.place_3.id) self.assertEqual(action_move.state, action_move.STATE.PROCESSED) @mock.patch('the_tale.game.heroes.objects.Hero.is_battle_start_needed', lambda self: False) def test_use__wrong_state(self): actions_prototypes.ActionMoveToPrototype.create(hero=self.hero, destination=self.place_3) self.assertTrue(self.hero.actions.current_action.state != actions_prototypes.ActionMoveToPrototype.STATE.MOVING) with self.check_not_changed(lambda: self.hero.actions.current_action.percents): result, step, postsave_actions = self.card.use(**self.use_attributes(storage=self.storage, hero=self.hero)) self.assertEqual((result, step, postsave_actions), (ComplexChangeTask.RESULT.FAILED, ComplexChangeTask.STEP.ERROR, ())) self.assertTrue(self.hero.position.place.id, self.place_1.id)
class RegenerateEnergyActionTest(testcase.TestCase): def setUp(self): super(RegenerateEnergyActionTest, self).setUp() create_test_map() account = self.accounts_factory.create_account(is_fast=True) self.storage = LogicStorage() self.storage.load_account_data(account) self.hero = self.storage.accounts_to_heroes[account.id] self.action_idl = self.hero.actions.current_action self.action_regenerate = ActionRegenerateEnergyPrototype.create(hero=self.hero) def tearDown(self): pass def test_create(self): self.assertEqual(self.action_idl.leader, False) self.assertEqual(self.action_regenerate.leader, True) self.assertEqual(self.action_regenerate.bundle_id, self.action_idl.bundle_id) self.storage._test_save() def test_not_ready(self): self.storage.process_turn() self.assertEqual(len(self.hero.actions.actions_list), 2) self.assertEqual(self.hero.actions.current_action, self.action_regenerate) self.storage._test_save() @mock.patch('the_tale.game.heroes.objects.Hero.can_regenerate_double_energy', False) def test_full(self): with self.check_delta(lambda: tt_api_energy.energy_balance(self.hero.account_id), self.hero.preferences.energy_regeneration_type.amount): while len(self.hero.actions.actions_list) != 1: self.storage.process_turn(continue_steps_if_needed=False) turn.increment() time.sleep(0.1) self.assertTrue(self.action_idl.leader) self.assertEqual(self.hero.need_regenerate_energy, False) self.assertEqual(self.hero.last_energy_regeneration_at_turn, turn.number()-1) self.storage._test_save() @mock.patch('the_tale.game.heroes.objects.Hero.can_regenerate_double_energy', False) @mock.patch('the_tale.game.heroes.objects.Hero.can_regenerate_energy', False) def test_full__regeneration_restricted(self): with self.check_not_changed(lambda: tt_api_energy.energy_balance(self.hero.account_id)): while len(self.hero.actions.actions_list) != 1: self.storage.process_turn(continue_steps_if_needed=False) turn.increment() time.sleep(0.1) self.assertTrue(self.action_idl.leader) self.assertEqual(self.hero.need_regenerate_energy, False) self.assertEqual(self.hero.last_energy_regeneration_at_turn, turn.number()-1) self.storage._test_save() @mock.patch('the_tale.game.heroes.objects.Hero.can_regenerate_double_energy', True) def test_full__double_energy(self): with self.check_delta(lambda: tt_api_energy.energy_balance(self.hero.account_id), self.hero.preferences.energy_regeneration_type.amount * 2): while len(self.hero.actions.actions_list) != 1: self.storage.process_turn(continue_steps_if_needed=False) turn.increment() time.sleep(0.1) self.assertTrue(self.action_idl.leader) self.assertEqual(self.hero.need_regenerate_energy, False) self.assertEqual(self.hero.last_energy_regeneration_at_turn, turn.number()-1) self.storage._test_save()
class HelpAbilityTest(UseAbilityTaskMixin, testcase.TestCase): ABILITY = Help def setUp(self): super(HelpAbilityTest, self).setUp() self.p1, self.p2, self.p3 = create_test_map() result, account_id, bundle_id = register_user('test_user_1', '*****@*****.**', '111111') self.account = AccountPrototype.get_by_id(account_id) self.storage = LogicStorage() self.storage.load_account_data(self.account) self.hero = self.storage.accounts_to_heroes[self.account.id] self.action_idl = self.hero.actions.current_action self.ability = self.ABILITY() @property def use_attributes(self): return super(HelpAbilityTest, self).use_attributes(hero=self.hero, storage=self.storage) def test_none(self): with mock.patch( 'the_tale.game.actions.prototypes.ActionBase.get_help_choice', lambda x: None): with self.check_not_changed( lambda: self.hero.statistics.help_count): with self.check_not_changed( lambda: self.hero.cards.help_count): self.assertEqual(self.ability.use(**self.use_attributes), (ComplexChangeTask.RESULT.FAILED, ComplexChangeTask.STEP.ERROR, ())) def test_success(self): with mock.patch( 'the_tale.game.heroes.objects.Hero.on_help') as on_help: with self.check_delta(lambda: self.hero.statistics.help_count, 1): with self.check_delta(lambda: self.hero.cards.help_count, 1): self.assertEqual(self.ability.use(**self.use_attributes), (ComplexChangeTask.RESULT.SUCCESSED, ComplexChangeTask.STEP.SUCCESS, ())) self.assertEqual(on_help.call_count, 1) @mock.patch('the_tale.game.heroes.objects.Hero.can_be_helped', lambda hero: False) def test_help_restricted(self): with self.check_not_changed(lambda: self.hero.statistics.help_count): with self.check_not_changed(lambda: self.hero.cards.help_count): self.assertEqual(self.ability.use(**self.use_attributes), (ComplexChangeTask.RESULT.FAILED, ComplexChangeTask.STEP.ERROR, ())) def test_help_when_battle_waiting(self): battle = Battle1x1Prototype.create(self.account) self.assertTrue(battle.state.is_WAITING) with self.check_delta(lambda: self.hero.statistics.help_count, 1): self.assertEqual(self.ability.use(**self.use_attributes), (ComplexChangeTask.RESULT.SUCCESSED, ComplexChangeTask.STEP.SUCCESS, ())) def test_help_when_battle_not_waiting(self): battle = Battle1x1Prototype.create(self.account) battle.state = BATTLE_1X1_STATE.PREPAIRING battle.save() self.assertFalse(battle.state.is_WAITING) with self.check_not_changed(lambda: self.hero.statistics.help_count): self.assertEqual( self.ability.use(**self.use_attributes), (ComplexChangeTask.RESULT.FAILED, ComplexChangeTask.STEP.ERROR, ())) def test_heal(self): self.hero.health = 1 with mock.patch( 'the_tale.game.actions.prototypes.ActionBase.get_help_choice', lambda x: HELP_CHOICES.HEAL): with self.check_delta(lambda: self.hero.statistics.help_count, 1): self.assertEqual(self.ability.use(**self.use_attributes), (ComplexChangeTask.RESULT.SUCCESSED, ComplexChangeTask.STEP.SUCCESS, ())) self.assertTrue(self.hero.health > 1) def test_start_quest(self): with mock.patch( 'the_tale.game.actions.prototypes.ActionBase.get_help_choice', lambda x: HELP_CHOICES.START_QUEST): with self.check_delta(lambda: self.hero.statistics.help_count, 1): self.assertEqual(self.ability.use(**self.use_attributes), (ComplexChangeTask.RESULT.SUCCESSED, ComplexChangeTask.STEP.SUCCESS, ())) self.assertTrue(self.action_idl.percents >= 1) def test_experience(self): old_experience = self.hero.experience with mock.patch( 'the_tale.game.actions.prototypes.ActionBase.get_help_choice', lambda x: HELP_CHOICES.EXPERIENCE): with self.check_delta(lambda: self.hero.statistics.help_count, 1): self.assertEqual(self.ability.use(**self.use_attributes), (ComplexChangeTask.RESULT.SUCCESSED, ComplexChangeTask.STEP.SUCCESS, ())) self.assertTrue(old_experience < self.hero.experience) def test_stock_up_energy(self): with self.check_changed(lambda: self.hero.energy_bonus): with mock.patch( 'the_tale.game.actions.prototypes.ActionBase.get_help_choice', lambda x: HELP_CHOICES.STOCK_UP_ENERGY): with self.check_delta(lambda: self.hero.statistics.help_count, 1): self.assertEqual(self.ability.use(**self.use_attributes), (ComplexChangeTask.RESULT.SUCCESSED, ComplexChangeTask.STEP.SUCCESS, ())) def test_money(self): old_hero_money = self.hero.money with mock.patch( 'the_tale.game.actions.prototypes.ActionBase.get_help_choice', lambda x: HELP_CHOICES.MONEY): with self.check_delta(lambda: self.hero.statistics.help_count, 1): self.assertEqual(self.ability.use(**self.use_attributes), (ComplexChangeTask.RESULT.SUCCESSED, ComplexChangeTask.STEP.SUCCESS, ())) self.assertTrue(self.hero.money > old_hero_money) @mock.patch('the_tale.game.heroes.objects.Hero.is_battle_start_needed', lambda self: False) def test_teleport(self): move_place = self.p3 if move_place.id == self.hero.position.place.id: move_place = self.p1 current_time = TimePrototype.get_current_time() action_move = actions_prototypes.ActionMoveToPrototype.create( hero=self.hero, destination=move_place) current_time.increment_turn() self.storage.process_turn() old_road_percents = self.hero.position.percents old_percents = action_move.percents with mock.patch( 'the_tale.game.actions.prototypes.ActionBase.get_help_choice', lambda x: HELP_CHOICES.TELEPORT): with self.check_delta(lambda: self.hero.statistics.help_count, 1): self.assertEqual(self.ability.use(**self.use_attributes), (ComplexChangeTask.RESULT.SUCCESSED, ComplexChangeTask.STEP.SUCCESS, ())) self.assertTrue(old_road_percents < self.hero.position.percents) self.assertTrue(old_percents < action_move.percents) self.assertEqual(self.hero.actions.current_action.percents, action_move.percents) @mock.patch( 'the_tale.game.balance.constants.ANGEL_HELP_CRIT_TELEPORT_DISTANCE', 9999999999) @mock.patch('the_tale.game.balance.constants.ANGEL_HELP_TELEPORT_DISTANCE', 9999999999) @mock.patch('the_tale.game.heroes.objects.Hero.is_battle_start_needed', lambda self: False) def test_teleport__inplace_action_created(self): move_place = self.p3 if move_place.id == self.hero.position.place.id: move_place = self.p1 current_time = TimePrototype.get_current_time() actions_prototypes.ActionMoveToPrototype.create(hero=self.hero, destination=move_place) current_time.increment_turn() self.storage.process_turn() with mock.patch( 'the_tale.game.actions.prototypes.ActionBase.get_help_choice', lambda x: HELP_CHOICES.TELEPORT): with self.check_delta(lambda: self.hero.statistics.help_count, 1): self.assertEqual(self.ability.use(**self.use_attributes), (ComplexChangeTask.RESULT.SUCCESSED, ComplexChangeTask.STEP.SUCCESS, ())) self.assertEqual(self.hero.actions.current_action.TYPE, actions_prototypes.ActionInPlacePrototype.TYPE) def test_lighting(self): current_time = TimePrototype.get_current_time() action_battle = actions_prototypes.ActionBattlePvE1x1Prototype.create( hero=self.hero, mob=mobs_storage.create_mob_for_hero(self.hero)) current_time.increment_turn() self.storage.process_turn() old_mob_health = action_battle.mob.health old_percents = action_battle.percents self.assertTrue(HELP_CHOICES.LIGHTING in action_battle.help_choices) with mock.patch( 'the_tale.game.actions.prototypes.ActionBase.get_help_choice', lambda x: HELP_CHOICES.LIGHTING): with self.check_delta(lambda: self.hero.statistics.help_count, 1): self.assertEqual(self.ability.use(**self.use_attributes), (ComplexChangeTask.RESULT.SUCCESSED, ComplexChangeTask.STEP.SUCCESS, ())) self.assertTrue(old_mob_health > action_battle.mob.health) self.assertEqual(self.hero.actions.current_action.percents, action_battle.percents) self.assertTrue(old_percents < action_battle.percents) def test_lighting_when_mob_killed(self): current_time = TimePrototype.get_current_time() action_battle = actions_prototypes.ActionBattlePvE1x1Prototype.create( hero=self.hero, mob=mobs_storage.create_mob_for_hero(self.hero)) current_time.increment_turn() self.storage.process_turn() action_battle.mob.health = 0 self.assertFalse(HELP_CHOICES.LIGHTING in action_battle.help_choices) def test_resurrect(self): current_time = TimePrototype.get_current_time() self.hero.kill() action_resurrect = actions_prototypes.ActionResurrectPrototype.create( hero=self.hero) with mock.patch( 'the_tale.game.actions.prototypes.ActionBase.get_help_choice', lambda x: HELP_CHOICES.RESURRECT): with self.check_delta(lambda: self.hero.statistics.help_count, 1): current_time.increment_turn() self.assertEqual(self.ability.use(**self.use_attributes), (ComplexChangeTask.RESULT.SUCCESSED, ComplexChangeTask.STEP.SUCCESS, ())) self.assertEqual(self.hero.health, self.hero.max_health) self.assertEqual(self.hero.is_alive, True) self.assertEqual(action_resurrect.state, action_resurrect.STATE.PROCESSED) def test_process_turn_called_if_current_action_processed(self): current_time = TimePrototype.get_current_time() self.hero.kill() actions_prototypes.ActionResurrectPrototype.create(hero=self.hero) with mock.patch( 'the_tale.game.logic_storage.LogicStorage.process_turn__single_hero' ) as process_turn__single_hero: with mock.patch( 'the_tale.game.actions.prototypes.ActionBase.get_help_choice', lambda x: HELP_CHOICES.RESURRECT): with self.check_delta(lambda: self.hero.statistics.help_count, 1): current_time.increment_turn() self.assertEqual(self.ability.use(**self.use_attributes), (ComplexChangeTask.RESULT.SUCCESSED, ComplexChangeTask.STEP.SUCCESS, ())) self.assertEqual(process_turn__single_hero.call_args_list, [ mock.call( hero=self.hero, logger=None, continue_steps_if_needed=True) ]) def test_resurrect__two_times(self): current_time = TimePrototype.get_current_time() self.hero.kill() actions_prototypes.ActionResurrectPrototype.create(hero=self.hero) with mock.patch( 'the_tale.game.actions.prototypes.ActionBase.get_help_choice', lambda x: HELP_CHOICES.RESURRECT): with self.check_delta(lambda: self.hero.statistics.help_count, 1): current_time.increment_turn() self.assertEqual(self.ability.use(**self.use_attributes), (ComplexChangeTask.RESULT.SUCCESSED, ComplexChangeTask.STEP.SUCCESS, ())) with mock.patch( 'the_tale.game.actions.prototypes.ActionBase.get_help_choice', lambda x: HELP_CHOICES.RESURRECT): with self.check_not_changed( lambda: self.hero.statistics.help_count): current_time.increment_turn() self.assertEqual(self.ability.use(**self.use_attributes), (ComplexChangeTask.RESULT.IGNORE, ComplexChangeTask.STEP.SUCCESS, ())) @mock.patch( 'the_tale.game.actions.prototypes.ActionIdlenessPrototype.HABIT_MODE', actions_relations.ACTION_HABIT_MODE.AGGRESSIVE) def test_update_habits__aggressive_action(self): with mock.patch('the_tale.game.heroes.objects.Hero.update_habits' ) as update_habits: with self.check_delta(lambda: self.hero.statistics.help_count, 1): self.assertEqual(self.ability.use(**self.use_attributes), (ComplexChangeTask.RESULT.SUCCESSED, ComplexChangeTask.STEP.SUCCESS, ())) self.assertEqual(update_habits.call_args_list, [mock.call(HABIT_CHANGE_SOURCE.HELP_AGGRESSIVE)]) @mock.patch( 'the_tale.game.actions.prototypes.ActionIdlenessPrototype.HABIT_MODE', actions_relations.ACTION_HABIT_MODE.PEACEFUL) def test_update_habits__unaggressive_action(self): with mock.patch('the_tale.game.heroes.objects.Hero.update_habits' ) as update_habits: with self.check_delta(lambda: self.hero.statistics.help_count, 1): self.assertEqual(self.ability.use(**self.use_attributes), (ComplexChangeTask.RESULT.SUCCESSED, ComplexChangeTask.STEP.SUCCESS, ())) self.assertEqual(update_habits.call_args_list, [mock.call(HABIT_CHANGE_SOURCE.HELP_UNAGGRESSIVE)]) @mock.patch('the_tale.game.artifacts.effects.Health.REMOVE_ON_HELP', True) def test_return_child_gifts(self): not_child_gift, child_gift, removed_artifact = artifacts_storage.all( )[:3] child_gift.special_effect = artifacts_relations.ARTIFACT_EFFECT.CHILD_GIFT removed_artifact.rare_effect = artifacts_relations.ARTIFACT_EFFECT.HEALTH self.hero.bag.put_artifact( not_child_gift.create_artifact(level=1, power=0)) self.hero.bag.put_artifact( not_child_gift.create_artifact(level=1, power=0)) self.hero.bag.put_artifact( not_child_gift.create_artifact(level=1, power=0)) self.hero.bag.put_artifact(child_gift.create_artifact(level=1, power=0)) self.hero.bag.put_artifact(child_gift.create_artifact(level=1, power=0)) self.hero.bag.put_artifact( removed_artifact.create_artifact(level=1, power=0)) self.hero.bag.put_artifact( removed_artifact.create_artifact( level=1, power=0, rarity=artifacts_relations.RARITY.RARE)) with self.check_delta(lambda: self.hero.statistics.gifts_returned, 2): with self.check_delta(lambda: self.hero.bag.occupation, -3): self.ability.use(**self.use_attributes) @mock.patch('the_tale.game.actions.prototypes.ActionBase.get_help_choice', lambda x: HELP_CHOICES.HEAL_COMPANION) def test_heal_companion__no_companion(self): self.assertEqual(self.hero.companion, None) with self.check_not_changed(lambda: self.hero.statistics.help_count): self.assertEqual( self.ability.use(**self.use_attributes), (ComplexChangeTask.RESULT.FAILED, ComplexChangeTask.STEP.ERROR, ())) @mock.patch('the_tale.game.actions.prototypes.ActionBase.get_help_choice', lambda x: HELP_CHOICES.HEAL_COMPANION) def test_heal_companion(self): companion_record = companions_storage.companions.enabled_companions( ).next() self.hero.set_companion( companions_logic.create_companion(companion_record)) self.hero.companion.health = 1 with self.check_delta(lambda: self.hero.companion.health, c.COMPANIONS_HEAL_AMOUNT): with self.check_delta(lambda: self.hero.statistics.help_count, 1): self.assertEqual(self.ability.use(**self.use_attributes), (ComplexChangeTask.RESULT.SUCCESSED, ComplexChangeTask.STEP.SUCCESS, ())) self.assertTrue( self.hero.journal.messages[-1].key.is_ANGEL_ABILITY_HEAL_COMPANION) self.assertFalse(self.hero.journal.messages[-1].key. is_ANGEL_ABILITY_HEAL_COMPANION_CRIT) @mock.patch('the_tale.game.actions.prototypes.ActionBase.get_help_choice', lambda x: HELP_CHOICES.HEAL_COMPANION) def test_heal_companion__full_health(self): companion_record = companions_storage.companions.enabled_companions( ).next() self.hero.set_companion( companions_logic.create_companion(companion_record)) self.assertEqual(self.hero.companion.health, self.hero.companion.max_health) with self.check_not_changed(lambda: self.hero.companion.health): with self.check_not_changed( lambda: self.hero.statistics.help_count): self.assertEqual(self.ability.use(**self.use_attributes), (ComplexChangeTask.RESULT.FAILED, ComplexChangeTask.STEP.ERROR, ())) self.assertFalse( self.hero.journal.messages[-1].key.is_ANGEL_ABILITY_HEAL_COMPANION) self.assertFalse(self.hero.journal.messages[-1].key. is_ANGEL_ABILITY_HEAL_COMPANION_CRIT) @mock.patch('the_tale.game.heroes.objects.Hero.might_crit_chance', 1) @mock.patch('the_tale.game.actions.prototypes.ActionBase.get_help_choice', lambda x: HELP_CHOICES.HEAL_COMPANION) def test_heal_companion__crit(self): companion_record = companions_storage.companions.enabled_companions( ).next() self.hero.set_companion( companions_logic.create_companion(companion_record)) self.hero.companion.health = 1 with self.check_delta(lambda: self.hero.companion.health, c.COMPANIONS_HEAL_CRIT_AMOUNT): with self.check_delta(lambda: self.hero.statistics.help_count, 1): self.assertEqual(self.ability.use(**self.use_attributes), (ComplexChangeTask.RESULT.SUCCESSED, ComplexChangeTask.STEP.SUCCESS, ())) self.assertFalse( self.hero.journal.messages[-1].key.is_ANGEL_ABILITY_HEAL_COMPANION) self.assertTrue(self.hero.journal.messages[-1].key. is_ANGEL_ABILITY_HEAL_COMPANION_CRIT) @mock.patch('the_tale.game.actions.prototypes.ActionBase.get_help_choice', lambda x: HELP_CHOICES.HEAL_COMPANION) def test_heal_companion__on_heal_called(self): companion_record = companions_storage.companions.enabled_companions( ).next() self.hero.set_companion( companions_logic.create_companion(companion_record)) self.hero.companion.health = 1 with mock.patch( 'the_tale.game.actions.prototypes.ActionBase.on_heal_companion' ) as on_heal_companion: self.assertEqual(self.ability.use(**self.use_attributes), (ComplexChangeTask.RESULT.SUCCESSED, ComplexChangeTask.STEP.SUCCESS, ())) self.assertEqual(on_heal_companion.call_count, 1) @mock.patch( 'the_tale.game.actions.prototypes.ActionIdlenessPrototype.HABIT_MODE', actions_relations.ACTION_HABIT_MODE.COMPANION) @mock.patch('the_tale.game.actions.prototypes.ActionBase.get_help_choice', lambda x: HELP_CHOICES.HEAL_COMPANION) def test_heal_companion__on_heal_action_habits_changed(self): habit_effect = random.choice([ ability for ability in companions_effects.ABILITIES.records if isinstance(ability.effect, companions_effects.ChangeHabits) ]) companion_record = companions_storage.companions.enabled_companions( ).next() companion_record.abilities = companions_container.Container( start=[habit_effect]) self.hero.set_companion( companions_logic.create_companion(companion_record)) self.hero.habit_honor.change(100) self.hero.habit_peacefulness.change(-100) self.hero.companion.health = 1 with self.check_changed(lambda: self.hero.habit_honor.raw_value + self. hero.habit_peacefulness.raw_value): self.assertEqual(self.ability.use(**self.use_attributes), (ComplexChangeTask.RESULT.SUCCESSED, ComplexChangeTask.STEP.SUCCESS, ())) @mock.patch( 'the_tale.game.actions.prototypes.ActionIdlenessPrototype.HABIT_MODE', actions_relations.ACTION_HABIT_MODE.COMPANION) @mock.patch('the_tale.game.actions.prototypes.ActionBase.get_help_choice', lambda x: HELP_CHOICES.HEAL_COMPANION) def test_heal_companion__on_heal_action_habits_not_changed(self): habit_effect = random.choice([ ability for ability in companions_effects.ABILITIES.records if not isinstance(ability.effect, companions_effects.ChangeHabits) ]) companion_record = companions_storage.companions.enabled_companions( ).next() companion_record.abilities = companions_container.Container( start=[habit_effect]) self.hero.set_companion( companions_logic.create_companion(companion_record)) self.hero.habit_honor.change(100) self.hero.habit_peacefulness.change(-100) self.hero.companion.health = 1 with self.check_not_changed(lambda: self.hero.habit_honor.raw_value + self.hero.habit_peacefulness.raw_value): self.assertEqual(self.ability.use(**self.use_attributes), (ComplexChangeTask.RESULT.SUCCESSED, ComplexChangeTask.STEP.SUCCESS, ()))
class HelpAbilityTest(UseAbilityTaskMixin, testcase.TestCase): ABILITY = Help def setUp(self): super(HelpAbilityTest, self).setUp() self.p1, self.p2, self.p3 = create_test_map() result, account_id, bundle_id = register_user('test_user_1', '*****@*****.**', '111111') self.account = AccountPrototype.get_by_id(account_id) self.storage = LogicStorage() self.storage.load_account_data(self.account) self.hero = self.storage.accounts_to_heroes[self.account.id] self.action_idl = self.hero.actions.current_action self.ability = self.ABILITY() @property def use_attributes(self): return super(HelpAbilityTest, self).use_attributes(hero=self.hero, storage=self.storage) def test_none(self): with mock.patch('the_tale.game.actions.prototypes.ActionBase.get_help_choice', lambda x: None): with self.check_not_changed(lambda: self.hero.statistics.help_count): with self.check_not_changed(lambda: self.hero.cards.help_count): self.assertEqual(self.ability.use(**self.use_attributes), (ComplexChangeTask.RESULT.FAILED, ComplexChangeTask.STEP.ERROR, ())) def test_success(self): with mock.patch('the_tale.game.heroes.objects.Hero.on_help') as on_help: with self.check_delta(lambda: self.hero.statistics.help_count, 1): with self.check_delta(lambda: self.hero.cards.help_count, 1): self.assertEqual(self.ability.use(**self.use_attributes), (ComplexChangeTask.RESULT.SUCCESSED, ComplexChangeTask.STEP.SUCCESS, ())) self.assertEqual(on_help.call_count, 1) @mock.patch('the_tale.game.heroes.objects.Hero.can_be_helped', lambda hero: False) def test_help_restricted(self): with self.check_not_changed(lambda: self.hero.statistics.help_count): with self.check_not_changed(lambda: self.hero.cards.help_count): self.assertEqual(self.ability.use(**self.use_attributes), (ComplexChangeTask.RESULT.FAILED, ComplexChangeTask.STEP.ERROR, ())) def test_help_when_battle_waiting(self): battle = Battle1x1Prototype.create(self.account) self.assertTrue(battle.state.is_WAITING) with self.check_delta(lambda: self.hero.statistics.help_count, 1): self.assertEqual(self.ability.use(**self.use_attributes), (ComplexChangeTask.RESULT.SUCCESSED, ComplexChangeTask.STEP.SUCCESS, ())) def test_help_when_battle_not_waiting(self): battle = Battle1x1Prototype.create(self.account) battle.state = BATTLE_1X1_STATE.PREPAIRING battle.save() self.assertFalse(battle.state.is_WAITING) with self.check_not_changed(lambda: self.hero.statistics.help_count): self.assertEqual(self.ability.use(**self.use_attributes), (ComplexChangeTask.RESULT.FAILED, ComplexChangeTask.STEP.ERROR, ())) def test_heal(self): self.hero.health = 1 with mock.patch('the_tale.game.actions.prototypes.ActionBase.get_help_choice', lambda x: HELP_CHOICES.HEAL): with self.check_delta(lambda: self.hero.statistics.help_count, 1): self.assertEqual(self.ability.use(**self.use_attributes), (ComplexChangeTask.RESULT.SUCCESSED, ComplexChangeTask.STEP.SUCCESS, ())) self.assertTrue(self.hero.health > 1) def test_start_quest(self): with mock.patch('the_tale.game.actions.prototypes.ActionBase.get_help_choice', lambda x: HELP_CHOICES.START_QUEST): with self.check_delta(lambda: self.hero.statistics.help_count, 1): self.assertEqual(self.ability.use(**self.use_attributes), (ComplexChangeTask.RESULT.SUCCESSED, ComplexChangeTask.STEP.SUCCESS, ())) self.assertTrue(self.action_idl.percents >= 1) def test_experience(self): old_experience = self.hero.experience with mock.patch('the_tale.game.actions.prototypes.ActionBase.get_help_choice', lambda x: HELP_CHOICES.EXPERIENCE): with self.check_delta(lambda: self.hero.statistics.help_count, 1): self.assertEqual(self.ability.use(**self.use_attributes), (ComplexChangeTask.RESULT.SUCCESSED, ComplexChangeTask.STEP.SUCCESS, ())) self.assertTrue(old_experience < self.hero.experience) def test_stock_up_energy(self): with self.check_changed(lambda: self.hero.energy_bonus): with mock.patch('the_tale.game.actions.prototypes.ActionBase.get_help_choice', lambda x: HELP_CHOICES.STOCK_UP_ENERGY): with self.check_delta(lambda: self.hero.statistics.help_count, 1): self.assertEqual(self.ability.use(**self.use_attributes), (ComplexChangeTask.RESULT.SUCCESSED, ComplexChangeTask.STEP.SUCCESS, ())) def test_money(self): old_hero_money = self.hero.money with mock.patch('the_tale.game.actions.prototypes.ActionBase.get_help_choice', lambda x: HELP_CHOICES.MONEY): with self.check_delta(lambda: self.hero.statistics.help_count, 1): self.assertEqual(self.ability.use(**self.use_attributes), (ComplexChangeTask.RESULT.SUCCESSED, ComplexChangeTask.STEP.SUCCESS, ())) self.assertTrue(self.hero.money > old_hero_money) @mock.patch('the_tale.game.heroes.objects.Hero.is_battle_start_needed', lambda self: False) def test_teleport(self): move_place = self.p3 if move_place.id == self.hero.position.place.id: move_place = self.p1 current_time = TimePrototype.get_current_time() action_move = actions_prototypes.ActionMoveToPrototype.create(hero=self.hero, destination=move_place) current_time.increment_turn() self.storage.process_turn() old_road_percents = self.hero.position.percents old_percents = action_move.percents with mock.patch('the_tale.game.actions.prototypes.ActionBase.get_help_choice', lambda x: HELP_CHOICES.TELEPORT): with self.check_delta(lambda: self.hero.statistics.help_count, 1): self.assertEqual(self.ability.use(**self.use_attributes), (ComplexChangeTask.RESULT.SUCCESSED, ComplexChangeTask.STEP.SUCCESS, ())) self.assertTrue(old_road_percents < self.hero.position.percents) self.assertTrue(old_percents < action_move.percents) self.assertEqual(self.hero.actions.current_action.percents, action_move.percents) @mock.patch('the_tale.game.balance.constants.ANGEL_HELP_CRIT_TELEPORT_DISTANCE', 9999999999) @mock.patch('the_tale.game.balance.constants.ANGEL_HELP_TELEPORT_DISTANCE', 9999999999) @mock.patch('the_tale.game.heroes.objects.Hero.is_battle_start_needed', lambda self: False) def test_teleport__inplace_action_created(self): move_place = self.p3 if move_place.id == self.hero.position.place.id: move_place = self.p1 current_time = TimePrototype.get_current_time() actions_prototypes.ActionMoveToPrototype.create(hero=self.hero, destination=move_place) current_time.increment_turn() self.storage.process_turn() with mock.patch('the_tale.game.actions.prototypes.ActionBase.get_help_choice', lambda x: HELP_CHOICES.TELEPORT): with self.check_delta(lambda: self.hero.statistics.help_count, 1): self.assertEqual(self.ability.use(**self.use_attributes), (ComplexChangeTask.RESULT.SUCCESSED, ComplexChangeTask.STEP.SUCCESS, ())) self.assertEqual(self.hero.actions.current_action.TYPE, actions_prototypes.ActionInPlacePrototype.TYPE) def test_lighting(self): current_time = TimePrototype.get_current_time() action_battle = actions_prototypes.ActionBattlePvE1x1Prototype.create(hero=self.hero, mob=mobs_storage.create_mob_for_hero(self.hero)) current_time.increment_turn() self.storage.process_turn() old_mob_health = action_battle.mob.health old_percents = action_battle.percents self.assertTrue(HELP_CHOICES.LIGHTING in action_battle.help_choices) with mock.patch('the_tale.game.actions.prototypes.ActionBase.get_help_choice', lambda x: HELP_CHOICES.LIGHTING): with self.check_delta(lambda: self.hero.statistics.help_count, 1): self.assertEqual(self.ability.use(**self.use_attributes), (ComplexChangeTask.RESULT.SUCCESSED, ComplexChangeTask.STEP.SUCCESS, ())) self.assertTrue(old_mob_health > action_battle.mob.health) self.assertEqual(self.hero.actions.current_action.percents, action_battle.percents) self.assertTrue(old_percents < action_battle.percents) def test_lighting_when_mob_killed(self): current_time = TimePrototype.get_current_time() action_battle = actions_prototypes.ActionBattlePvE1x1Prototype.create(hero=self.hero, mob=mobs_storage.create_mob_for_hero(self.hero)) current_time.increment_turn() self.storage.process_turn() action_battle.mob.health = 0 self.assertFalse(HELP_CHOICES.LIGHTING in action_battle.help_choices) def test_resurrect(self): current_time = TimePrototype.get_current_time() self.hero.kill() action_resurrect = actions_prototypes.ActionResurrectPrototype.create(hero=self.hero) with mock.patch('the_tale.game.actions.prototypes.ActionBase.get_help_choice', lambda x: HELP_CHOICES.RESURRECT): with self.check_delta(lambda: self.hero.statistics.help_count, 1): current_time.increment_turn() self.assertEqual(self.ability.use(**self.use_attributes), (ComplexChangeTask.RESULT.SUCCESSED, ComplexChangeTask.STEP.SUCCESS, ())) self.assertEqual(self.hero.health, self.hero.max_health) self.assertEqual(self.hero.is_alive, True) self.assertEqual(action_resurrect.state, action_resurrect.STATE.PROCESSED) def test_process_turn_called_if_current_action_processed(self): current_time = TimePrototype.get_current_time() self.hero.kill() actions_prototypes.ActionResurrectPrototype.create(hero=self.hero) with mock.patch('the_tale.game.logic_storage.LogicStorage.process_turn__single_hero') as process_turn__single_hero: with mock.patch('the_tale.game.actions.prototypes.ActionBase.get_help_choice', lambda x: HELP_CHOICES.RESURRECT): with self.check_delta(lambda: self.hero.statistics.help_count, 1): current_time.increment_turn() self.assertEqual(self.ability.use(**self.use_attributes), (ComplexChangeTask.RESULT.SUCCESSED, ComplexChangeTask.STEP.SUCCESS, ())) self.assertEqual(process_turn__single_hero.call_args_list, [mock.call(hero=self.hero, logger=None, continue_steps_if_needed=True)]) def test_resurrect__two_times(self): current_time = TimePrototype.get_current_time() self.hero.kill() actions_prototypes.ActionResurrectPrototype.create(hero=self.hero) with mock.patch('the_tale.game.actions.prototypes.ActionBase.get_help_choice', lambda x: HELP_CHOICES.RESURRECT): with self.check_delta(lambda: self.hero.statistics.help_count, 1): current_time.increment_turn() self.assertEqual(self.ability.use(**self.use_attributes), (ComplexChangeTask.RESULT.SUCCESSED, ComplexChangeTask.STEP.SUCCESS, ())) with mock.patch('the_tale.game.actions.prototypes.ActionBase.get_help_choice', lambda x: HELP_CHOICES.RESURRECT): with self.check_not_changed(lambda: self.hero.statistics.help_count): current_time.increment_turn() self.assertEqual(self.ability.use(**self.use_attributes), (ComplexChangeTask.RESULT.IGNORE, ComplexChangeTask.STEP.SUCCESS, ())) @mock.patch('the_tale.game.actions.prototypes.ActionIdlenessPrototype.HABIT_MODE', actions_relations.ACTION_HABIT_MODE.AGGRESSIVE) def test_update_habits__aggressive_action(self): with mock.patch('the_tale.game.heroes.objects.Hero.update_habits') as update_habits: with self.check_delta(lambda: self.hero.statistics.help_count, 1): self.assertEqual(self.ability.use(**self.use_attributes), (ComplexChangeTask.RESULT.SUCCESSED, ComplexChangeTask.STEP.SUCCESS, ())) self.assertEqual(update_habits.call_args_list, [mock.call(HABIT_CHANGE_SOURCE.HELP_AGGRESSIVE)]) @mock.patch('the_tale.game.actions.prototypes.ActionIdlenessPrototype.HABIT_MODE', actions_relations.ACTION_HABIT_MODE.PEACEFUL) def test_update_habits__unaggressive_action(self): with mock.patch('the_tale.game.heroes.objects.Hero.update_habits') as update_habits: with self.check_delta(lambda: self.hero.statistics.help_count, 1): self.assertEqual(self.ability.use(**self.use_attributes), (ComplexChangeTask.RESULT.SUCCESSED, ComplexChangeTask.STEP.SUCCESS, ())) self.assertEqual(update_habits.call_args_list, [mock.call(HABIT_CHANGE_SOURCE.HELP_UNAGGRESSIVE)]) @mock.patch('the_tale.game.artifacts.effects.Health.REMOVE_ON_HELP', True) def test_return_child_gifts(self): not_child_gift, child_gift, removed_artifact = artifacts_storage.all()[:3] child_gift.special_effect = artifacts_relations.ARTIFACT_EFFECT.CHILD_GIFT removed_artifact.rare_effect = artifacts_relations.ARTIFACT_EFFECT.HEALTH self.hero.bag.put_artifact(not_child_gift.create_artifact(level=1, power=0)) self.hero.bag.put_artifact(not_child_gift.create_artifact(level=1, power=0)) self.hero.bag.put_artifact(not_child_gift.create_artifact(level=1, power=0)) self.hero.bag.put_artifact(child_gift.create_artifact(level=1, power=0)) self.hero.bag.put_artifact(child_gift.create_artifact(level=1, power=0)) self.hero.bag.put_artifact(removed_artifact.create_artifact(level=1, power=0)) self.hero.bag.put_artifact(removed_artifact.create_artifact(level=1, power=0, rarity=artifacts_relations.RARITY.RARE)) with self.check_delta(lambda: self.hero.statistics.gifts_returned, 2): with self.check_delta(lambda: self.hero.bag.occupation, -3): self.ability.use(**self.use_attributes) @mock.patch('the_tale.game.actions.prototypes.ActionBase.get_help_choice', lambda x: HELP_CHOICES.HEAL_COMPANION) def test_heal_companion__no_companion(self): self.assertEqual(self.hero.companion, None) with self.check_not_changed(lambda: self.hero.statistics.help_count): self.assertEqual(self.ability.use(**self.use_attributes), (ComplexChangeTask.RESULT.FAILED, ComplexChangeTask.STEP.ERROR, ())) @mock.patch('the_tale.game.actions.prototypes.ActionBase.get_help_choice', lambda x: HELP_CHOICES.HEAL_COMPANION) def test_heal_companion(self): companion_record = companions_storage.companions.enabled_companions().next() self.hero.set_companion(companions_logic.create_companion(companion_record)) self.hero.companion.health = 1 with self.check_delta(lambda: self.hero.companion.health, c.COMPANIONS_HEAL_AMOUNT): with self.check_delta(lambda: self.hero.statistics.help_count, 1): self.assertEqual(self.ability.use(**self.use_attributes), (ComplexChangeTask.RESULT.SUCCESSED, ComplexChangeTask.STEP.SUCCESS, ())) self.assertTrue(self.hero.journal.messages[-1].key.is_ANGEL_ABILITY_HEAL_COMPANION) self.assertFalse(self.hero.journal.messages[-1].key.is_ANGEL_ABILITY_HEAL_COMPANION_CRIT) @mock.patch('the_tale.game.actions.prototypes.ActionBase.get_help_choice', lambda x: HELP_CHOICES.HEAL_COMPANION) def test_heal_companion__full_health(self): companion_record = companions_storage.companions.enabled_companions().next() self.hero.set_companion(companions_logic.create_companion(companion_record)) self.assertEqual(self.hero.companion.health, self.hero.companion.max_health) with self.check_not_changed(lambda: self.hero.companion.health): with self.check_not_changed(lambda: self.hero.statistics.help_count): self.assertEqual(self.ability.use(**self.use_attributes), (ComplexChangeTask.RESULT.FAILED, ComplexChangeTask.STEP.ERROR, ())) self.assertFalse(self.hero.journal.messages[-1].key.is_ANGEL_ABILITY_HEAL_COMPANION) self.assertFalse(self.hero.journal.messages[-1].key.is_ANGEL_ABILITY_HEAL_COMPANION_CRIT) @mock.patch('the_tale.game.heroes.objects.Hero.might_crit_chance', 1) @mock.patch('the_tale.game.actions.prototypes.ActionBase.get_help_choice', lambda x: HELP_CHOICES.HEAL_COMPANION) def test_heal_companion__crit(self): companion_record = companions_storage.companions.enabled_companions().next() self.hero.set_companion(companions_logic.create_companion(companion_record)) self.hero.companion.health = 1 with self.check_delta(lambda: self.hero.companion.health, c.COMPANIONS_HEAL_CRIT_AMOUNT): with self.check_delta(lambda: self.hero.statistics.help_count, 1): self.assertEqual(self.ability.use(**self.use_attributes), (ComplexChangeTask.RESULT.SUCCESSED, ComplexChangeTask.STEP.SUCCESS, ())) self.assertFalse(self.hero.journal.messages[-1].key.is_ANGEL_ABILITY_HEAL_COMPANION) self.assertTrue(self.hero.journal.messages[-1].key.is_ANGEL_ABILITY_HEAL_COMPANION_CRIT) @mock.patch('the_tale.game.actions.prototypes.ActionBase.get_help_choice', lambda x: HELP_CHOICES.HEAL_COMPANION) def test_heal_companion__on_heal_called(self): companion_record = companions_storage.companions.enabled_companions().next() self.hero.set_companion(companions_logic.create_companion(companion_record)) self.hero.companion.health = 1 with mock.patch('the_tale.game.actions.prototypes.ActionBase.on_heal_companion') as on_heal_companion: self.assertEqual(self.ability.use(**self.use_attributes), (ComplexChangeTask.RESULT.SUCCESSED, ComplexChangeTask.STEP.SUCCESS, ())) self.assertEqual(on_heal_companion.call_count, 1) @mock.patch('the_tale.game.actions.prototypes.ActionIdlenessPrototype.HABIT_MODE', actions_relations.ACTION_HABIT_MODE.COMPANION) @mock.patch('the_tale.game.actions.prototypes.ActionBase.get_help_choice', lambda x: HELP_CHOICES.HEAL_COMPANION) def test_heal_companion__on_heal_action_habits_changed(self): habit_effect = random.choice([ability for ability in companions_effects.ABILITIES.records if isinstance(ability.effect, companions_effects.ChangeHabits)]) companion_record = companions_storage.companions.enabled_companions().next() companion_record.abilities = companions_container.Container(start=[habit_effect]) self.hero.set_companion(companions_logic.create_companion(companion_record)) self.hero.habit_honor.change(100) self.hero.habit_peacefulness.change(-100) self.hero.companion.health = 1 with self.check_changed(lambda: self.hero.habit_honor.raw_value + self.hero.habit_peacefulness.raw_value): self.assertEqual(self.ability.use(**self.use_attributes), (ComplexChangeTask.RESULT.SUCCESSED, ComplexChangeTask.STEP.SUCCESS, ())) @mock.patch('the_tale.game.actions.prototypes.ActionIdlenessPrototype.HABIT_MODE', actions_relations.ACTION_HABIT_MODE.COMPANION) @mock.patch('the_tale.game.actions.prototypes.ActionBase.get_help_choice', lambda x: HELP_CHOICES.HEAL_COMPANION) def test_heal_companion__on_heal_action_habits_not_changed(self): habit_effect = random.choice([ability for ability in companions_effects.ABILITIES.records if not isinstance(ability.effect, companions_effects.ChangeHabits)]) companion_record = companions_storage.companions.enabled_companions().next() companion_record.abilities = companions_container.Container(start=[habit_effect]) self.hero.set_companion(companions_logic.create_companion(companion_record)) self.hero.habit_honor.change(100) self.hero.habit_peacefulness.change(-100) self.hero.companion.health = 1 with self.check_not_changed(lambda: self.hero.habit_honor.raw_value + self.hero.habit_peacefulness.raw_value): self.assertEqual(self.ability.use(**self.use_attributes), (ComplexChangeTask.RESULT.SUCCESSED, ComplexChangeTask.STEP.SUCCESS, ()))
class ResurrectActionTest(testcase.TestCase): def setUp(self): super(ResurrectActionTest, self).setUp() create_test_map() self.account = self.accounts_factory.create_account(is_fast=True) self.storage = LogicStorage() self.storage.load_account_data(self.account) self.hero = self.storage.accounts_to_heroes[self.account.id] self.action_idl = self.hero.actions.current_action self.hero.kill() self.action_resurrect = ActionResurrectPrototype.create(hero=self.hero) def tearDown(self): pass def test_create(self): self.assertEqual(self.action_idl.leader, False) self.assertEqual(self.action_resurrect.leader, True) self.assertEqual(self.action_resurrect.bundle_id, self.action_idl.bundle_id) self.assertEqual(len(self.action_resurrect.HELP_CHOICES), 1) self.assertTrue(list(self.action_resurrect.HELP_CHOICES)[0].is_RESURRECT) self.storage._test_save() def test_processed(self): current_time = TimePrototype.get_current_time() for i in range(c.TURNS_TO_RESURRECT-1): self.storage.process_turn() current_time.increment_turn() self.assertEqual(len(self.hero.actions.actions_list), 2) self.assertEqual(self.hero.actions.current_action, self.action_resurrect) self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(len(self.hero.actions.actions_list), 1) self.assertEqual(self.hero.actions.current_action, self.action_idl) self.assertEqual(self.hero.health, self.hero.max_health) self.assertEqual(self.hero.is_alive, True) self.storage._test_save() def test_full(self): current_time = TimePrototype.get_current_time() while len(self.hero.actions.actions_list) != 1: self.storage.process_turn(continue_steps_if_needed=False) current_time.increment_turn() self.assertTrue(self.action_idl.leader) self.assertEqual(self.hero.health, self.hero.max_health) self.assertEqual(self.hero.is_alive, True) self.storage._test_save() def test_fast_resurrect(self): self.action_resurrect.fast_resurrect() self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(len(self.hero.actions.actions_list), 1) self.assertEqual(self.hero.actions.current_action, self.action_idl) self.assertEqual(self.hero.health, self.hero.max_health) self.assertEqual(self.hero.is_alive, True) self.storage._test_save()
class Worker(workers.BaseWorker): STOP_SIGNAL_REQUIRED = False def initialize(self): # worker initialized by supervisor pass def cmd_initialize(self, turn_number, worker_id): self.send_cmd('initialize', { 'turn_number': turn_number, 'worker_id': worker_id }) def process_initialize(self, turn_number, worker_id): if self.initialized: self.logger.warn( 'WARNING: game already initialized, do reinitialization') self.storage = LogicStorage() self.initialized = True self.turn_number = turn_number self.queue = [] self.worker_id = worker_id self.logger.info('GAME INITIALIZED') environment.workers.supervisor.cmd_answer('initialize', self.worker_id) def cmd_next_turn(self, turn_number): return self.send_cmd('next_turn', data={'turn_number': turn_number}) # @profile.profile_decorator('/home/tie/repos/mine/the-tale/profile.info') def process_next_turn(self, turn_number): self.turn_number += 1 if turn_number != self.turn_number: raise LogicException( 'dessinchonization: workers turn number (%d) not equal to command turn number (%d)' % (self.turn_number, turn_number)) if TimePrototype.get_current_turn_number() != self.turn_number: raise LogicException( 'dessinchonization: workers turn number (%d) not equal to saved turn number (%d)' % (self.turn_number, TimePrototype.get_current_turn_number())) self.storage.process_turn(logger=self.logger) self.storage.save_changed_data(logger=self.logger) for hero_id in self.storage.skipped_heroes: hero = self.storage.heroes[hero_id] if hero.actions.current_action.bundle_id in self.storage.ignored_bundles: continue environment.workers.supervisor.cmd_account_release_required( hero.account_id) environment.workers.supervisor.cmd_answer('next_turn', self.worker_id) if game_settings.COLLECT_GARBAGE and self.turn_number % game_settings.COLLECT_GARBAGE_PERIOD == 0: self.logger.info('GC: start') gc.collect() self.logger.info('GC: end') def release_account(self, account_id): if account_id not in self.storage.accounts_to_heroes: environment.workers.supervisor.cmd_account_released(account_id) return hero = self.storage.accounts_to_heroes[account_id] bundle_id = hero.actions.current_action.bundle_id if bundle_id in self.storage.ignored_bundles: return with self.storage.on_exception( self.logger, message= 'LogicWorker.process_release_account catch exception, while processing hero %d, try to save all bundles except %d', data=(hero.id, bundle_id), excluded_bundle_id=bundle_id): self.storage.release_account_data(account_id) environment.workers.supervisor.cmd_account_released(account_id) def cmd_stop(self): return self.send_cmd('stop') def process_stop(self): # no need to save data, since they automaticaly saved on every turn self.initialized = False self.storage.save_all(logger=self.logger) environment.workers.supervisor.cmd_answer('stop', self.worker_id) self.stop_required = True self.logger.info('LOGIC STOPPED') def cmd_register_account(self, account_id): return self.send_cmd('register_account', {'account_id': account_id}) def process_register_account(self, account_id): from the_tale.accounts.prototypes import AccountPrototype account = AccountPrototype.get_by_id(account_id) if account is None: raise LogicException('can not get account with id "%d"' % (account_id, )) self.storage.load_account_data(account) def cmd_release_account(self, account_id): return self.send_cmd('release_account', {'account_id': account_id}) def process_release_account(self, account_id): self.release_account(account_id) def cmd_logic_task(self, account_id, task_id): return self.send_cmd('logic_task', { 'task_id': task_id, 'account_id': account_id }) def process_logic_task(self, account_id, task_id): # pylint: disable=W0613 hero = self.storage.accounts_to_heroes[account_id] bundle_id = hero.actions.current_action.bundle_id if bundle_id in self.storage.ignored_bundles: return with self.storage.on_exception( self.logger, message= 'LogicWorker.process_logic_task catch exception, while processing hero %d, try to save all bundles except %d', data=(hero.id, bundle_id), excluded_bundle_id=bundle_id): task = postponed_tasks.PostponedTaskPrototype.get_by_id(task_id) task.process(self.logger, storage=self.storage) task.do_postsave_actions() self.storage.recache_bundle(bundle_id) def cmd_force_save(self, account_id): return self.send_cmd('force_save', {'account_id': account_id}) def process_force_save(self, account_id): # pylint: disable=W0613 hero = self.storage.accounts_to_heroes[account_id] bundle_id = hero.actions.current_action.bundle_id if bundle_id in self.storage.ignored_bundles: return self.storage.save_bundle_data(bundle_id=bundle_id) def cmd_start_hero_caching(self, account_id): self.send_cmd('start_hero_caching', {'account_id': account_id}) def process_start_hero_caching(self, account_id): hero = self.storage.accounts_to_heroes[account_id] if hero.actions.current_action.bundle_id in self.storage.ignored_bundles: return hero.ui_caching_started_at = datetime.datetime.now() self.storage.recache_bundle(hero.actions.current_action.bundle_id) def cmd_update_hero_with_account_data(self, account_id, is_fast, premium_end_at, active_end_at, ban_end_at, might, actual_bills): self.send_cmd( 'update_hero_with_account_data', { 'account_id': account_id, 'is_fast': is_fast, 'premium_end_at': premium_end_at, 'active_end_at': active_end_at, 'ban_end_at': ban_end_at, 'might': might, 'actual_bills': actual_bills }) def process_update_hero_with_account_data(self, account_id, is_fast, premium_end_at, active_end_at, ban_end_at, might, actual_bills): hero = self.storage.accounts_to_heroes[account_id] if hero.actions.current_action.bundle_id in self.storage.ignored_bundles: return hero.update_with_account_data( is_fast=is_fast, premium_end_at=datetime.datetime.fromtimestamp(premium_end_at), active_end_at=datetime.datetime.fromtimestamp(active_end_at), ban_end_at=datetime.datetime.fromtimestamp(ban_end_at), might=might, actual_bills=actual_bills) self.storage.save_bundle_data(hero.actions.current_action.bundle_id) def cmd_highlevel_data_updated(self): self.send_cmd('highlevel_data_updated') def process_highlevel_data_updated(self): self.storage.on_highlevel_data_updated() def cmd_setup_quest(self, account_id, knowledge_base): return self.send_cmd('setup_quest', { 'account_id': account_id, 'knowledge_base': knowledge_base }) def process_setup_quest(self, account_id, knowledge_base): hero = self.storage.accounts_to_heroes[account_id] bundle_id = hero.actions.current_action.bundle_id if bundle_id in self.storage.ignored_bundles: return with self.storage.on_exception( self.logger, message= 'LogicWorker.process_logic_task catch exception, while processing hero %d, try to save all bundles except %d', data=(hero.id, bundle_id), excluded_bundle_id=bundle_id): quests_logic.setup_quest_for_hero(hero, knowledge_base) self.storage.recache_bundle(bundle_id)
class ShortTeleportTests(CardsTestMixin, testcase.TestCase): CARD = cards.CARD.SHORT_TELEPORT def setUp(self): super(ShortTeleportTests, self).setUp() self.place_1, self.place_2, self.place_3 = create_test_map() self.account_1 = self.accounts_factory.create_account() self.storage = LogicStorage() self.storage.load_account_data(self.account_1) self.hero = self.storage.accounts_to_heroes[self.account_1.id] self.hero.position.set_place(self.place_1) @mock.patch('the_tale.game.heroes.objects.Hero.is_battle_start_needed', lambda self: False) def test_moving(self): self.assertFalse(self.hero.actions.current_action.TYPE.is_MOVE_TO) result, step, postsave_actions = self.CARD.effect.use( **self.use_attributes(storage=self.storage, hero=self.hero)) self.assertEqual( (result, step, postsave_actions), (ComplexChangeTask.RESULT.FAILED, ComplexChangeTask.STEP.ERROR, ())) @mock.patch('the_tale.game.heroes.objects.Hero.is_battle_start_needed', lambda self: False) def test_use(self): action_move = actions_prototypes.ActionMoveToPrototype.create( hero=self.hero, destination=self.place_3) self.storage.process_turn(continue_steps_if_needed=False) self.assertTrue(self.hero.actions.current_action.state == actions_prototypes.ActionMoveToPrototype.STATE.MOVING) self.assertTrue(self.hero.position.percents < 1) result, step, postsave_actions = self.CARD.effect.use( **self.use_attributes(storage=self.storage, hero=self.hero)) self.assertEqual((result, step, postsave_actions), (ComplexChangeTask.RESULT.SUCCESSED, ComplexChangeTask.STEP.SUCCESS, ())) self.assertEqual(action_move.state, action_move.STATE.IN_CITY) self.assertEqual(self.hero.actions.current_action.TYPE, actions_prototypes.ActionInPlacePrototype.TYPE) self.assertTrue(self.hero.position.place.id, self.place_2.id) result, step, postsave_actions = self.CARD.effect.use( **self.use_attributes(storage=self.storage, hero=self.hero)) self.assertEqual( (result, step, postsave_actions), (ComplexChangeTask.RESULT.FAILED, ComplexChangeTask.STEP.ERROR, ())) while not action_move.leader: self.storage.process_turn(continue_steps_if_needed=False) self.storage.process_turn(continue_steps_if_needed=False) result, step, postsave_actions = self.CARD.effect.use( **self.use_attributes(storage=self.storage, hero=self.hero)) self.assertEqual((result, step, postsave_actions), (ComplexChangeTask.RESULT.SUCCESSED, ComplexChangeTask.STEP.SUCCESS, ())) self.assertTrue(self.hero.position.place.id, self.place_3.id) self.assertEqual(action_move.state, action_move.STATE.PROCESSED) @mock.patch('the_tale.game.heroes.objects.Hero.is_battle_start_needed', lambda self: False) def test_use__wrong_state(self): actions_prototypes.ActionMoveToPrototype.create( hero=self.hero, destination=self.place_3) self.assertTrue(self.hero.actions.current_action.state != actions_prototypes.ActionMoveToPrototype.STATE.MOVING) with self.check_not_changed( lambda: self.hero.actions.current_action.percents): result, step, postsave_actions = self.CARD.effect.use( **self.use_attributes(storage=self.storage, hero=self.hero)) self.assertEqual( (result, step, postsave_actions), (ComplexChangeTask.RESULT.FAILED, ComplexChangeTask.STEP.ERROR, ())) self.assertTrue(self.hero.position.place.id, self.place_1.id)
class HealCompanionActionTest(UseAbilityTaskMixin, testcase.TestCase): PROCESSOR = Help def setUp(self): super(HealCompanionActionTest, self).setUp() create_test_map() self.account = self.accounts_factory.create_account() self.storage = LogicStorage() self.storage.load_account_data(self.account) self.hero = self.storage.accounts_to_heroes[self.account.id] self.companion_record = companions_storage.companions.enabled_companions().next() self.hero.set_companion(companions_logic.create_companion(self.companion_record)) self.action_idl = self.hero.actions.current_action self.hero.companion.healed_at_turn = -1 with self.check_increased(lambda: self.hero.companion.healed_at_turn): self.action_heal_companion = prototypes.ActionHealCompanionPrototype.create(hero=self.hero) def test_create(self): self.assertEqual(self.action_idl.leader, False) self.assertEqual(self.action_heal_companion.leader, True) self.assertEqual(self.action_heal_companion.bundle_id, self.action_heal_companion.bundle_id) self.assertEqual(self.action_heal_companion.percents, 0) self.storage._test_save() def test_processed__no_companion(self): self.hero.remove_companion() self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(len(self.hero.actions.actions_list), 1) self.assertEqual(self.hero.actions.current_action, self.action_idl) self.storage._test_save() def test_processed__max_health(self): self.assertEqual(self.hero.companion.health, self.hero.companion.max_health) self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(len(self.hero.actions.actions_list), 1) self.assertEqual(self.hero.actions.current_action, self.action_idl) self.storage._test_save() def test_not_ready(self): self.hero.companion.health = 1 self.storage.process_turn() self.assertEqual(len(self.hero.actions.actions_list), 2) self.assertEqual(self.hero.actions.current_action, self.action_heal_companion) self.assertTrue(self.hero.companion.health, 1) self.assertTrue(self.action_heal_companion.percents > 0) self.storage._test_save() def test_ability_heal_companion(self): self.hero.companion.health = 1 with self.check_increased(lambda: self.action_heal_companion.percents): with self.check_increased(lambda: self.hero.companion.health): ability = self.PROCESSOR() with mock.patch('the_tale.game.actions.prototypes.ActionBase.get_help_choice', lambda x: HELP_CHOICES.HEAL_COMPANION): self.assertTrue(ability.use(**self.use_attributes(hero=self.hero, storage=self.storage))) def test_ability_heal_companion__processed_when_healed(self): self.hero.companion.health -= 1 with self.check_increased(lambda: self.action_heal_companion.percents): with self.check_increased(lambda: self.hero.companion.health): ability = self.PROCESSOR() with mock.patch('the_tale.game.actions.prototypes.ActionBase.get_help_choice', lambda x: HELP_CHOICES.HEAL_COMPANION): self.assertTrue(ability.use(**self.use_attributes(hero=self.hero, storage=self.storage))) self.assertTrue(self.action_heal_companion.percents, 1) self.assertEqual(self.action_heal_companion.state, self.action_heal_companion.STATE.PROCESSED) @mock.patch('the_tale.game.heroes.prototypes.HeroPrototype.can_companion_exp_per_heal', lambda hero: True) def test_ability_heal_companion__processed_when_healed__exp_per_heal(self): self.hero.companion.health -= 1 with self.check_delta(lambda: self.hero.experience, c.COMPANIONS_EXP_PER_HEAL): with self.check_increased(lambda: self.hero.companion.health): ability = self.PROCESSOR() with mock.patch('the_tale.game.actions.prototypes.ActionBase.get_help_choice', lambda x: HELP_CHOICES.HEAL_COMPANION): self.assertTrue(ability.use(**self.use_attributes(hero=self.hero, storage=self.storage))) self.assertTrue(self.action_heal_companion.percents, 1) self.assertEqual(self.action_heal_companion.state, self.action_heal_companion.STATE.PROCESSED) def test_ability_heal_companion__full_action(self): self.hero.companion.health = 1 current_time = TimePrototype.get_current_time() with self.check_increased(lambda: self.action_heal_companion.percents): with self.check_increased(lambda: self.hero.companion.health): while self.action_heal_companion.state != self.action_heal_companion.STATE.PROCESSED: current_time.increment_turn() ability = self.PROCESSOR() with mock.patch('the_tale.game.actions.prototypes.ActionBase.get_help_choice', lambda x: HELP_CHOICES.HEAL_COMPANION): self.assertTrue(ability.use(**self.use_attributes(hero=self.hero, storage=self.storage))) def test_full(self): self.hero.companion.health = 1 current_time = TimePrototype.get_current_time() with self.check_delta(lambda: self.hero.companion.health, c.COMPANIONS_HEALTH_PER_HEAL): while len(self.hero.actions.actions_list) != 1: self.storage.process_turn(continue_steps_if_needed=False) current_time.increment_turn() self.assertTrue(self.action_idl.leader) self.storage._test_save() @mock.patch('the_tale.game.heroes.prototypes.HeroPrototype.can_companion_exp_per_heal', lambda hero: True) def test_full__exp_per_heal(self): self.hero.companion.health = 1 with self.check_delta(lambda: self.hero.experience, c.COMPANIONS_EXP_PER_HEAL): current_time = TimePrototype.get_current_time() while len(self.hero.actions.actions_list) != 1: self.storage.process_turn(continue_steps_if_needed=False) current_time.increment_turn() self.assertTrue(self.action_idl.leader) self.storage._test_save() @mock.patch('the_tale.common.utils.logic.randint_from_1', lambda v: v) @mock.patch('the_tale.game.balance.constants.COMPANIONS_REGEN_ON_HEAL_PER_HEAL', 1.0) @mock.patch('the_tale.game.heroes.prototypes.HeroPrototype.can_companion_regenerate', lambda hero: True) def test_full__regeneration(self): self.hero.companion.health = 1 current_time = TimePrototype.get_current_time() while len(self.hero.actions.actions_list) != 1: self.storage.process_turn(continue_steps_if_needed=False) current_time.increment_turn() self.assertTrue(self.action_idl.leader) self.assertEqual(self.hero.companion.health, 1+c.COMPANIONS_HEALTH_PER_HEAL+c.COMPANIONS_REGEN_ON_HEAL_AMOUNT) self.storage._test_save() @mock.patch('the_tale.game.heroes.prototypes.HeroPrototype.companion_heal_probability', 1) @mock.patch('the_tale.common.utils.logic.randint_from_1', lambda v: v) def test_companion_healing_by_hero(self): current_time = TimePrototype.get_current_time() self.hero.companion.health = 1 with self.check_delta(lambda: self.hero.companion.health, c.COMPANIONS_HEALTH_PER_HEAL+c.COMPANIONS_REGEN_BY_HERO): self.action_heal_companion.state = self.action_heal_companion.STATE.PROCESSED self.storage.process_turn(continue_steps_if_needed=False) current_time.increment_turn() @mock.patch('the_tale.game.heroes.prototypes.HeroPrototype.companion_heal_probability', 0) @mock.patch('the_tale.common.utils.logic.randint_from_1', lambda v: v) def test_companion_healing_by_hero__not_healed(self): current_time = TimePrototype.get_current_time() self.hero.companion.health = 1 with self.check_delta(lambda: self.hero.companion.health, c.COMPANIONS_HEALTH_PER_HEAL): self.action_heal_companion.state = self.action_heal_companion.STATE.PROCESSED self.storage.process_turn(continue_steps_if_needed=False) current_time.increment_turn()
class RestActionTest(UseAbilityTaskMixin, testcase.TestCase): PROCESSOR = Help def setUp(self): super(RestActionTest, self).setUp() create_test_map() account = self.accounts_factory.create_account(is_fast=True) self.storage = LogicStorage() self.storage.load_account_data(account) self.hero = self.storage.accounts_to_heroes[account.id] self.action_idl = self.hero.actions.current_action self.action_rest = ActionRestPrototype.create(hero=self.hero) def tearDown(self): pass def test_create(self): self.assertEqual(self.action_idl.leader, False) self.assertEqual(self.action_rest.leader, True) self.assertEqual(self.action_rest.bundle_id, self.action_idl.bundle_id) self.storage._test_save() def test_processed(self): self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(len(self.hero.actions.actions_list), 1) self.assertEqual(self.hero.actions.current_action, self.action_idl) self.storage._test_save() def test_not_ready(self): self.hero.health = 1 self.storage.process_turn() self.assertEqual(len(self.hero.actions.actions_list), 2) self.assertEqual(self.hero.actions.current_action, self.action_rest) self.assertTrue(self.hero.health > 1) self.storage._test_save() def test_ability_heal(self): self.hero.health = 1 old_percents = self.action_rest.percents ability = self.PROCESSOR() with mock.patch('the_tale.game.actions.prototypes.ActionBase.get_help_choice', lambda x: HELP_CHOICES.HEAL): self.assertTrue(ability.use(**self.use_attributes(hero=self.hero, storage=self.storage))) self.assertTrue(self.hero.health > 1) self.assertTrue(old_percents < self.action_rest.percents) self.assertEqual(self.hero.actions.current_action.percents, self.action_rest.percents) def test_ability_heal__healed(self): self.hero.health = self.hero.max_health - 1 ability = self.PROCESSOR() with mock.patch('the_tale.game.actions.prototypes.ActionBase.get_help_choice', lambda x: HELP_CHOICES.HEAL): self.assertTrue(ability.use(**self.use_attributes(hero=self.hero, storage=self.storage))) self.assertEqual(self.hero.health, self.hero.max_health) self.assertTrue(self.action_rest.percents, 1) self.assertEqual(self.action_rest.state, self.action_rest.STATE.PROCESSED) def test_full(self): self.hero.health = 1 current_time = TimePrototype.get_current_time() while len(self.hero.actions.actions_list) != 1: self.storage.process_turn(continue_steps_if_needed=False) current_time.increment_turn() self.assertTrue(self.action_idl.leader) self.assertEqual(self.hero.health, self.hero.max_health) self.storage._test_save()
class BattlePvE1x1ActionTest(testcase.TestCase): def setUp(self): super(BattlePvE1x1ActionTest, self).setUp() create_test_map() account = self.accounts_factory.create_account() self.storage = LogicStorage() self.storage.load_account_data(account) self.hero = self.storage.accounts_to_heroes[account.id] self.hero.level = 6 self.hero.health = self.hero.max_health # do half of tests with companion if random.random() < 0.5: companion_record = next(companions_storage.companions.enabled_companions()) companion = companions_logic.create_companion(companion_record) self.hero.set_companion(companion) self.hero.companion.health = 1 self.action_idl = self.hero.actions.current_action with mock.patch('the_tale.game.balance.constants.KILL_BEFORE_BATTLE_PROBABILITY', 0): self.action_battle = ActionBattlePvE1x1Prototype.create(hero=self.hero, mob=mobs_storage.create_mob_for_hero(self.hero)) def tearDown(self): pass def test_create(self): self.assertEqual(self.action_idl.leader, False) self.assertEqual(self.action_battle.leader, True) self.assertEqual(self.action_battle.bundle_id, self.action_idl.bundle_id) self.assertEqual(self.action_battle.percents, 0.0) self.assertEqual(self.action_battle.state, self.action_battle.STATE.BATTLE_RUNNING) self.storage._test_save() @mock.patch('the_tale.game.actions.prototypes.battle.make_turn', lambda a, b, c: None) def test_mob_killed(self): self.assertEqual(self.hero.statistics.pve_kills, 0) self.action_battle.mob.health = 0 self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(len(self.hero.actions.actions_list), 1) self.assertEqual(self.hero.actions.current_action, self.action_idl) self.assertEqual(self.hero.statistics.pve_kills, 1) self.storage._test_save() @mock.patch('the_tale.game.balance.constants.ARTIFACTS_PER_BATTLE', 0) @mock.patch('the_tale.game.balance.constants.GET_LOOT_PROBABILITY', 1) @mock.patch('the_tale.game.actions.prototypes.battle.make_turn', lambda a, b, c: None) def test_loot(self): self.assertEqual(self.hero.statistics.loot_had, 0) self.assertEqual(len(list(self.hero.bag.items())), 0) self.action_battle.mob.health = 0 self.storage.process_turn() self.assertEqual(self.hero.statistics.loot_had, 1) self.assertEqual(len(list(self.hero.bag.items())), 1) self.storage._test_save() @mock.patch('the_tale.game.balance.constants.ARTIFACTS_PER_BATTLE', 1) @mock.patch('the_tale.game.actions.prototypes.battle.make_turn', lambda a, b, c: None) def test_artifacts(self): self.assertEqual(self.hero.statistics.artifacts_had, 0) self.assertEqual(len(list(self.hero.bag.items())), 0) self.action_battle.mob.health = 0 self.storage.process_turn() self.assertEqual(self.hero.statistics.artifacts_had, 1) self.assertEqual(len(list(self.hero.bag.items())), 1) self.storage._test_save() @mock.patch('the_tale.game.actions.prototypes.battle.make_turn', lambda a, b, c: None) def test_hero_killed(self): self.assertEqual(self.hero.statistics.pve_deaths, 0) self.hero.health = 0 self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(len(self.hero.actions.actions_list), 1) self.assertEqual(self.hero.actions.current_action, self.action_idl) self.assertTrue(not self.hero.is_alive) self.assertEqual(self.hero.statistics.pve_deaths, 1) self.storage._test_save() @mock.patch('the_tale.game.balance.constants.ARTIFACTS_PER_BATTLE', 1) @mock.patch('the_tale.game.actions.prototypes.battle.make_turn', lambda a, b, c: None) def test_hero_and_mob_killed(self): self.hero.health = 0 self.action_battle.mob.health = 0 with self.check_not_changed(lambda: self.hero.statistics.artifacts_had): with self.check_not_changed(lambda: self.hero.bag.occupation): self.storage.process_turn(continue_steps_if_needed=False) self.assertTrue(self.hero.journal.messages[-1].key.is_ACTION_BATTLEPVE1X1_JOURNAL_HERO_AND_MOB_KILLED) self.assertEqual(len(self.hero.actions.actions_list), 1) self.assertEqual(self.hero.actions.current_action, self.action_idl) self.assertTrue(not self.hero.is_alive) self.assertEqual(self.hero.statistics.pve_deaths, 1) self.storage._test_save() def test_full_battle(self): current_time = TimePrototype.get_current_time() while len(self.hero.actions.actions_list) != 1: self.storage.process_turn() current_time.increment_turn() self.assertTrue(self.action_idl.leader) self.storage._test_save() def test_full_battle__with_companion(self): battle_ability = random.choice([ability for ability in effects.ABILITIES.records if isinstance(ability.effect, effects.BaseBattleAbility)]) companion_record = next(companions_storage.companions.enabled_companions()) companion_record.abilities = container.Container(start=(battle_ability,)) companion = companions_logic.create_companion(companion_record) self.hero.set_companion(companion) self.hero.reset_accessors_cache() current_time = TimePrototype.get_current_time() while len(self.hero.actions.actions_list) != 1: self.storage.process_turn() current_time.increment_turn() self.assertTrue(self.action_idl.leader) self.storage._test_save() @mock.patch('the_tale.game.heroes.objects.Hero.companion_damage_probability', 0.0) @mock.patch('the_tale.game.balance.constants.COMPANIONS_WOUNDS_IN_HOUR_FROM_HEAL', 0.0) @mock.patch('the_tale.game.balance.constants.COMPANIONS_EATEN_CORPSES_PER_BATTLE', 1.0) @mock.patch('the_tale.game.mobs.prototypes.MobPrototype.mob_type', game_relations.BEING_TYPE.ANIMAL) @mock.patch('the_tale.game.heroes.objects.Hero.can_companion_eat_corpses', lambda hero: True) def test_full_battle__with_companion__eat_corpse(self): companion_record = next(companions_storage.companions.enabled_companions()) self.hero.set_companion(companions_logic.create_companion(companion_record)) self.hero.reset_accessors_cache() self.hero.companion.health = 10 current_time = TimePrototype.get_current_time() while self.hero.actions.current_action.TYPE == ActionBattlePvE1x1Prototype.TYPE: self.storage.process_turn(continue_steps_if_needed=False) current_time.increment_turn() self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(self.hero.companion.health, 10 + c.COMPANIONS_EATEN_CORPSES_HEAL_AMOUNT) self.storage._test_save() def test_bit_mob(self): old_mob_health = self.action_battle.mob.health old_action_percents = self.action_battle.percents self.action_battle.bit_mob(0.33) self.assertTrue(self.action_battle.mob.health < old_mob_health) self.assertTrue(self.action_battle.percents > old_action_percents) self.assertTrue(self.action_battle.updated) def test_bit_mob_and_kill(self): self.action_battle.bit_mob(1) self.assertEqual(self.action_battle.mob.health, self.action_battle.mob.max_health - 1) self.assertTrue(0 < self.action_battle.percents < 1) self.assertTrue(self.action_battle.updated) self.assertEqual(self.action_battle.state, self.action_battle.STATE.BATTLE_RUNNING) self.action_battle.bit_mob(self.action_battle.mob.max_health) self.assertEqual(self.action_battle.mob.health, 0) self.assertEqual(self.action_battle.percents, 1) self.assertTrue(self.action_battle.updated) self.assertEqual(self.action_battle.state, self.action_battle.STATE.PROCESSED) def test_fast_resurrect__not_processed(self): self.action_battle.hero.kill() self.assertFalse(self.action_battle.fast_resurrect()) self.assertFalse(self.action_battle.hero.is_alive) def test_fast_resurrect__hero_is_alive(self): self.action_battle.state = self.action_battle.STATE.PROCESSED self.assertFalse(self.action_battle.fast_resurrect()) self.assertTrue(self.action_battle.hero.is_alive) def test_fast_resurrect__success(self): self.action_battle.hero.kill() self.action_battle.state = self.action_battle.STATE.PROCESSED self.assertTrue(self.action_battle.fast_resurrect()) self.assertTrue(self.action_battle.hero.is_alive) def test_help_choices(self): self.assertTrue(HELP_CHOICES.LIGHTING in self.action_battle.HELP_CHOICES) self.action_battle.mob.health = 0 self.assertFalse(HELP_CHOICES.LIGHTING in self.action_battle.HELP_CHOICES) self.action_battle.mob.health = 1 self.action_battle.hero.kill() self.assertEqual(self.action_battle.HELP_CHOICES, set((HELP_CHOICES.RESURRECT,))) @mock.patch('the_tale.game.balance.constants.KILL_BEFORE_BATTLE_PROBABILITY', 1.01) @mock.patch('the_tale.game.heroes.habits.Honor.interval', game_relations.HABIT_HONOR_INTERVAL.LEFT_3) def test_kill_before_start(self): self.hero.actions.pop_action() with self.check_delta(lambda: self.hero.statistics.pve_kills, 1): action_battle = ActionBattlePvE1x1Prototype.create(hero=self.hero, mob=mobs_storage.create_mob_for_hero(self.hero)) self.assertEqual(action_battle.percents, 1.0) self.assertEqual(action_battle.state, self.action_battle.STATE.PROCESSED) self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(self.hero.actions.current_action, self.action_idl) @mock.patch('the_tale.game.heroes.objects.Hero.can_companion_do_exorcism', lambda hero: True) def test_companion_exorcims__demon(self): self.companion_record = companions_logic.create_random_companion_record('exorcist', state=companions_relations.STATE.ENABLED) self.hero.set_companion(companions_logic.create_companion(self.companion_record)) demon_record = mobs_prototypes.MobRecordPrototype.create_random('demon', type=game_relations.BEING_TYPE.DEMON) demon = mobs_prototypes.MobPrototype(record_id=demon_record.id, level=self.hero.level, is_boss=False) self.hero.actions.pop_action() with self.check_delta(lambda: self.hero.statistics.pve_kills, 1): action_battle = ActionBattlePvE1x1Prototype.create(hero=self.hero, mob=demon) self.assertEqual(action_battle.percents, 1.0) self.assertEqual(action_battle.state, self.action_battle.STATE.PROCESSED) self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(self.hero.actions.current_action, self.action_idl) @mock.patch('the_tale.game.heroes.objects.Hero.can_companion_do_exorcism', lambda hero: True) def test_companion_exorcims__not_demon(self): self.companion_record = companions_logic.create_random_companion_record('exorcist', state=companions_relations.STATE.ENABLED) self.hero.set_companion(companions_logic.create_companion(self.companion_record)) not_demon_record = mobs_prototypes.MobRecordPrototype.create_random('demon', type=game_relations.BEING_TYPE.random(exclude=(game_relations.BEING_TYPE.DEMON, ))) not_demon = mobs_prototypes.MobPrototype(record_id=not_demon_record.id, level=self.hero.level, is_boss=False) self.hero.actions.pop_action() with self.check_delta(lambda: self.hero.statistics.pve_kills, 0): action_battle = ActionBattlePvE1x1Prototype.create(hero=self.hero, mob=not_demon) self.assertEqual(action_battle.percents, 0.0) self.assertEqual(action_battle.state, self.action_battle.STATE.BATTLE_RUNNING) self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(self.hero.actions.current_action, action_battle) @mock.patch('the_tale.game.balance.constants.PEACEFULL_BATTLE_PROBABILITY', 1.01) @mock.patch('the_tale.game.heroes.habits.Peacefulness.interval', game_relations.HABIT_PEACEFULNESS_INTERVAL.RIGHT_3) def test_peacefull_battle(self): self.hero.actions.pop_action() with self.check_delta(lambda: self.hero.statistics.pve_kills, 0): action_battle = ActionBattlePvE1x1Prototype.create(hero=self.hero, mob=mobs_storage.create_mob_for_hero(self.hero)) self.assertEqual(action_battle.percents, 1.0) self.assertEqual(action_battle.state, self.action_battle.STATE.PROCESSED) self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(self.hero.actions.current_action, self.action_idl) @mock.patch('the_tale.game.heroes.objects.Hero.can_leave_battle_in_fear', lambda self: True) def test_fear_battle(self): self.hero.actions.pop_action() mob = next((m for m in mobs_storage.all() if m.type.is_CIVILIZED)) with self.check_delta(lambda: self.hero.statistics.pve_kills, 0): action_battle = ActionBattlePvE1x1Prototype.create(hero=self.hero, mob=mob.create_mob(self.hero, is_boss=False)) self.assertEqual(action_battle.percents, 1.0) self.assertEqual(action_battle.state, self.action_battle.STATE.PROCESSED) self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(self.hero.actions.current_action, self.action_idl) @mock.patch('the_tale.game.balance.constants.EXP_FOR_KILL_PROBABILITY', 1.01) @mock.patch('the_tale.game.balance.constants.EXP_FOR_KILL_DELTA', 0) @mock.patch('the_tale.game.heroes.habits.Peacefulness.interval', game_relations.HABIT_PEACEFULNESS_INTERVAL.LEFT_3) def test_experience_for_kill(self): mob = mobs_storage.create_mob_for_hero(self.hero) mob.health = 0 with self.check_delta(lambda: self.hero.statistics.pve_kills, 1): with mock.patch('the_tale.game.heroes.objects.Hero.add_experience') as add_experience: with mock.patch('the_tale.game.actions.prototypes.ActionBattlePvE1x1Prototype.process_artifact_breaking') as process_artifact_breaking: action_battle = ActionBattlePvE1x1Prototype.create(hero=self.hero, mob=mob) self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(add_experience.call_args_list, [mock.call(c.EXP_FOR_KILL)]) self.assertEqual(process_artifact_breaking.call_count, 1) self.assertEqual(action_battle.state, self.action_battle.STATE.PROCESSED) @mock.patch('the_tale.game.balance.constants.ARTIFACTS_BREAKS_PER_BATTLE', 1.0) @mock.patch('the_tale.game.artifacts.prototypes.ArtifactPrototype.can_be_broken', lambda self: True) def test_process_artifact_breaking__no_equipment(self): self.hero.equipment._remove_all() old_power = self.hero.power.clone() self.action_battle.process_artifact_breaking() self.assertEqual(old_power, self.hero.power) @mock.patch('the_tale.game.balance.constants.ARTIFACTS_BREAKS_PER_BATTLE', 1.0) @mock.patch('the_tale.game.artifacts.prototypes.ArtifactPrototype.can_be_broken', lambda self: True) def test_process_artifact_breaking__broken(self): for artifact in list(self.hero.equipment.values()): artifact.power = Power(100, 100) old_power = self.hero.power.total() self.action_battle.process_artifact_breaking() self.assertTrue(old_power > self.hero.power.total()) @mock.patch('the_tale.game.balance.constants.ARTIFACTS_BREAKS_PER_BATTLE', 1.0) @mock.patch('the_tale.game.artifacts.prototypes.ArtifactPrototype.can_be_broken', lambda self: False) def test_process_artifact_breaking__not_broken(self): for artifact in list(self.hero.equipment.values()): artifact.power = Power(100, 100) old_power = self.hero.power.total() self.action_battle.process_artifact_breaking() self.assertEqual(old_power, self.hero.power.total()) @mock.patch('the_tale.game.balance.constants.ARTIFACTS_BREAKS_PER_BATTLE', 1.0) @mock.patch('the_tale.game.artifacts.prototypes.ArtifactPrototype.can_be_broken', lambda self: False) def test_process_artifact_breaking__break_only_mostly_damaged(self): for artifact in list(self.hero.equipment.values()): artifact.power = Power(100, 100) artifact.integrity = 0 artifact.integrity = artifact.max_integrity for i in range(100): self.action_battle.process_artifact_breaking() self.assertEqual(artifact.power, Power(100, 100)) def test_process_artifact_breaking__integrity_damage(self): for artifact in list(self.hero.equipment.values()): artifact.integrity = artifact.max_integrity self.action_battle.process_artifact_breaking() for artifact in list(self.hero.equipment.values()): self.assertTrue(artifact.integrity < artifact.max_integrity)
class GetArtifactMixin(CardsTestMixin): CARD = None RARITIES = None HAS_USELESS = False def setUp(self): super(GetArtifactMixin, self).setUp() create_test_map() result, account_1_id, bundle_id = register_user('test_user', '*****@*****.**', '111111') self.account_1 = AccountPrototype.get_by_id(account_1_id) self.storage = LogicStorage() self.storage.load_account_data(self.account_1) self.hero = self.storage.accounts_to_heroes[self.account_1.id] self.card = self.CARD() def test_use(self): rarities = set() has_useless = False for i in xrange(10000): result, step, postsave_actions = self.card.use(**self.use_attributes(storage=self.storage, hero=self.hero)) self.assertEqual((result, step, postsave_actions), (ComplexChangeTask.RESULT.SUCCESSED, ComplexChangeTask.STEP.SUCCESS, ())) artifact = self.hero.bag.values()[0] self.hero.bag.pop_artifact(artifact) rarities.add(artifact.rarity) has_useless = has_useless or artifact.type.is_USELESS self.assertEqual(has_useless, self.HAS_USELESS) self.assertEqual(rarities, self.RARITIES) def test_use__full_bag(self): with self.check_delta(lambda: self.hero.bag.occupation, 1000): for i in xrange(1000): result, step, postsave_actions = self.card.use(**self.use_attributes(storage=self.storage, hero=self.hero)) self.assertEqual((result, step, postsave_actions), (ComplexChangeTask.RESULT.SUCCESSED, ComplexChangeTask.STEP.SUCCESS, ())) def test_use_when_trading(self): from the_tale.game.actions.prototypes import ActionTradingPrototype action_idl = self.hero.actions.current_action action_trade = ActionTradingPrototype.create(hero=self.hero) result, step, postsave_actions = self.card.use(**self.use_attributes(storage=self.storage, hero=self.hero)) self.assertEqual((result, step, postsave_actions), (ComplexChangeTask.RESULT.SUCCESSED, ComplexChangeTask.STEP.SUCCESS, ())) self.assertEqual(self.hero.bag.occupation, 1) self.assertTrue(action_trade.replane_required) self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(self.hero.actions.current_action, action_idl) self.assertEqual(self.hero.bag.occupation, 1)
class Command(BaseCommand): help = 'test how hero move in levels corridor on real map' option_list = BaseCommand.option_list @mock.patch('the_tale.game.balance.constants.EXP_PER_QUEST_FRACTION', 0.0) def handle(self, *args, **options): try: self.test_corridor() except KeyboardInterrupt: pass except Exception: traceback.print_exc() def set_hero_companion(self): from the_tale.game.companions import storage from the_tale.game.companions import models from the_tale.game.companions import logic COMPANION_NAME = u'test_hero_level_companion' for companion in storage.companions.all(): if companion.name.startswith(COMPANION_NAME): models.CompanionRecord.objects.filter(id=companion.id).delete() storage.companions.refresh() break companion_record = logic.create_random_companion_record(COMPANION_NAME) self.hero.set_companion(logic.create_companion(companion_record)) @mock.patch('dext.settings.conf.dext_settings_settings.UPDATE_DATABASE', False) @mock.patch('the_tale.game.heroes.habilities.AbilitiesPrototype.modify_attribute', fake_modify_attribute) @mock.patch('the_tale.game.quests.conf.quests_settings.INTERFERED_PERSONS_LIVE_TIME', 0) def test_corridor(self): # fill_empty_keys_with_fake_phrases(u'test_hero_level_companion') result, account_id, bundle_id = register_user(uuid.uuid4().hex) # pylint: disable=W0612 self.storage = LogicStorage() self.storage.load_account_data(AccountPrototype.get_by_id(account_id)) self.hero = self.storage.accounts_to_heroes[account_id] self.set_hero_companion() current_time = TimePrototype.get_current_time() for level in xrange(1, 100): print print '-----------------------------------------------------------------------' print 'process level %d\texpected turns: %d\texpected days: %.2f' % (level, f.turns_on_lvl(level), f.time_on_lvl(level)/24) for i in xrange(f.turns_on_lvl(level)): # pylint: disable=W0612 self.storage.process_turn() current_time.increment_turn() # simulate user behaviour on healing companion if self.hero.companion.health < self.hero.companion.max_health / 2: self.hero.companion.health = self.hero.companion.max_health self.hero.randomized_level_up() exp_to_next_level = float(self.hero.experience) / f.exp_on_lvl(self.hero.level) * 100 exp_from_expected = float(f.total_exp_to_lvl(self.hero.level)+self.hero.experience)/f.total_exp_to_lvl(level+1)*100 exp_untaken = f.total_exp_to_lvl(level+1) - f.total_exp_to_lvl(self.hero.level) - self.hero.experience quests_untaken = float(exp_untaken) / f.experience_for_quest(c.QUEST_AREA_RADIUS) print u'hero level: %d\texp: %.2f%%\texp from expected: %.2f%% (%d exp, %.2f quests)\ttotal quests %d' % (self.hero.level, exp_to_next_level, exp_from_expected, exp_untaken, quests_untaken, self.hero.statistics.quests_done) print u'abilities: %s' % ' '.join(u'%s-%d' % (ability_id, ability.level) for ability_id, ability in self.hero.abilities.abilities.items()) print u'deaths: %d' % self.hero.statistics.pve_deaths total_gold = f.total_gold_at_lvl(self.hero.level) print u'total money: %d from expected %d (x%.2f)' % (self.hero.statistics.money_earned, total_gold, float(self.hero.statistics.money_earned) / total_gold if total_gold > 0 else 0) total_artifacts = int(f.total_time_for_lvl(self.hero.level) / 24 * c.ARTIFACTS_LOOT_PER_DAY ) print u'total artifacts: %d from expected %d (x%.2f)' % (self.hero.statistics.artifacts_had, total_artifacts, float(self.hero.statistics.artifacts_had) / total_artifacts if total_artifacts > 0 else 0) print u'power: %r from expected %r' % (self.hero.power, Power.power_to_level(self.hero.preferences.archetype.power_distribution, self.hero.level)) print u'power total: %d from expected %r (x%.2f)' % (self.hero.power.total(), Power.power_to_level(self.hero.preferences.archetype.power_distribution, self.hero.level).total(), float(self.hero.power.total()) / Power.power_to_level(self.hero.preferences.archetype.power_distribution, self.hero.level).total())
class QuestActionTests(testcase.TestCase): def setUp(self): super(QuestActionTests, self).setUp() create_test_map() account = self.accounts_factory.create_account(is_fast=True) self.storage = LogicStorage() self.storage.load_account_data(account) self.hero = self.storage.accounts_to_heroes[account.id] self.action_idl = self.hero.actions.current_action self.action_quest = prototypes.ActionQuestPrototype.create( hero=self.hero) def test_create(self): self.assertEqual(self.action_idl.leader, False) self.assertEqual(self.action_quest.leader, True) self.assertEqual(self.action_quest.state, self.action_quest.STATE.SEARCHING) self.assertEqual(self.action_quest.bundle_id, self.action_idl.bundle_id) self.assertFalse(self.hero.quests.has_quests) self.storage._test_save() def test_setup_quest(self): quests_helpers.setup_quest(self.hero) self.assertEqual(self.action_quest.state, self.action_quest.STATE.PROCESSING) self.assertTrue(self.hero.quests.has_quests) self.storage._test_save() def test_one_step(self): self.storage.process_turn() # quest can create new action on first step self.assertTrue(2 <= len(self.hero.actions.actions_list) <= 3) self.storage._test_save() def test_step_with_no_quest(self): quests_helpers.setup_quest(self.hero) self.hero.quests.pop_quest() self.storage.process_turn() self.assertEqual(self.action_idl.leader, True) def test_need_equipping(self): with mock.patch('the_tale.game.heroes.objects.Hero.need_equipping', lambda hero: True): self.storage.process_turn() self.assertEqual(self.action_quest.state, self.action_quest.STATE.EQUIPPING) self.assertTrue(self.hero.actions.current_action.TYPE.is_EQUIPPING) self.storage.process_turn() self.assertEqual(self.action_quest.state, self.action_quest.STATE.EQUIPPING) self.assertTrue(self.hero.actions.current_action.TYPE.is_QUEST) self.storage.process_turn() self.assertEqual(self.action_quest.state, self.action_quest.STATE.PROCESSING) def test_full_quest(self): # just test that quest will be ended while not self.action_idl.leader: self.storage.process_turn() turn.increment() self.storage._test_save() self.assertFalse(self.hero.quests.has_quests)
class MoveNearActionTest(testcase.TestCase): def setUp(self): super(MoveNearActionTest, self).setUp() self.p1, self.p2, self.p3 = create_test_map() account = self.accounts_factory.create_account(is_fast=True) self.storage = LogicStorage() self.storage.load_account_data(account) self.hero = self.storage.accounts_to_heroes[account.id] self.action_idl = self.hero.actions.current_action self.hero.position.set_place(self.p1) self.action_move = prototypes.ActionMoveNearPlacePrototype.create( hero=self.hero, place=self.p1, back=False) def tearDown(self): pass def test_create(self): self.assertEqual(self.action_idl.leader, False) self.assertEqual(self.action_move.leader, True) self.assertEqual(self.action_move.bundle_id, self.action_idl.bundle_id) self.storage._test_save() def test_get_destination_coordinates(self): self.assertTrue( len(self.p1.nearest_cells) > 3) # two coordinates will be in coordinates set, other will not x_1, y_1 = self.p1.nearest_cells[0] map_info_storage.item.terrain[y_1][x_1] = TERRAIN.WATER_DEEP x_2, y_2 = self.p1.nearest_cells[1] map_info_storage.item.terrain[y_2][x_2] = TERRAIN.WATER_DEEP coordinates = set() for i in range(100): coordinates.add( prototypes.ActionMoveNearPlacePrototype. _get_destination_coordinates(back=False, place=self.p1, terrains=(TERRAIN.WATER_DEEP, ))) self.assertEqual(coordinates, set([(x_1, y_1), (x_2, y_2)])) def test_get_destination_coordinates__no_terrains(self): self.assertTrue( len(self.p1.nearest_cells) > 3) # two coordinates will be in coordinates set, other will not coordinates = set() for i in range(100): coordinates.add( prototypes.ActionMoveNearPlacePrototype. _get_destination_coordinates(back=False, place=self.p1, terrains=(TERRAIN.WATER_DEEP, ))) self.assertEqual(coordinates, set(self.p1.nearest_cells)) def test_get_destination_coordinates__back(self): self.assertTrue( len(self.p1.nearest_cells) > 3) # two coordinates will be in coordinates set, other will not x_1, y_1 = self.p1.nearest_cells[0] map_info_storage.item.terrain[y_1][x_1] = TERRAIN.WATER_DEEP x_2, y_2 = self.p1.nearest_cells[1] map_info_storage.item.terrain[y_2][x_2] = TERRAIN.WATER_DEEP coordinates = set() for i in range(100): coordinates.add( prototypes.ActionMoveNearPlacePrototype. _get_destination_coordinates(back=True, place=self.p1, terrains=(TERRAIN.WATER_DEEP, ))) self.assertEqual(coordinates, set([(self.p1.x, self.p1.y)])) @mock.patch('the_tale.game.heroes.objects.Hero.is_battle_start_needed', lambda self: False) def test_processed(self): current_time = TimePrototype.get_current_time() self.storage.process_turn(continue_steps_if_needed=False) x, y = self.action_move.get_destination() self.hero.position.set_coordinates(x, y, x, y, percents=1) current_time.increment_turn() self.storage.process_turn(continue_steps_if_needed=False) # can end in field or in start place self.assertTrue(self.hero.actions.current_action.TYPE in [ prototypes.ActionIdlenessPrototype.TYPE, prototypes.ActionInPlacePrototype.TYPE ]) self.assertTrue(self.hero.position.is_walking or self.hero.position.place) self.storage._test_save() @mock.patch('the_tale.game.heroes.objects.Hero.is_battle_start_needed', lambda self: False) def test_not_ready(self): self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(len(self.hero.actions.actions_list), 2) self.assertEqual(self.hero.actions.current_action, self.action_move) self.assertTrue(self.hero.position.is_walking or self.hero.position.place) # can end in start place self.storage._test_save() @mock.patch('the_tale.game.heroes.objects.Hero.is_battle_start_needed', lambda self: False) @mock.patch('the_tale.game.heroes.position.Position.subroad_len', lambda self: 1) def test_modify_speed(self): with mock.patch('the_tale.game.heroes.objects.Hero.modify_move_speed', mock.Mock(return_value=self.hero.move_speed) ) as speed_modifier_call_counter: self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(speed_modifier_call_counter.call_count, 1) def test_full_move_and_back(self): current_time = TimePrototype.get_current_time() while len(self.hero.actions.actions_list) != 1: self.storage.process_turn(continue_steps_if_needed=False) current_time.increment_turn() self.assertEqual(self.hero.actions.current_action.TYPE, prototypes.ActionIdlenessPrototype.TYPE) self.assertTrue(self.hero.position.is_walking or self.hero.position.place) # can end in start place prototypes.ActionMoveNearPlacePrototype.create(hero=self.hero, place=self.p1, back=True) while self.hero.position.place is None or self.hero.position.place.id != self.p1.id: self.storage.process_turn(continue_steps_if_needed=False) current_time.increment_turn() self.assertEqual(self.hero.actions.current_action.TYPE, prototypes.ActionInPlacePrototype.TYPE) self.assertTrue(not self.hero.position.is_walking) self.storage._test_save() def test_move_change_place_coordinates_and_back(self): current_time = TimePrototype.get_current_time() while len(self.hero.actions.actions_list) != 1: self.storage.process_turn(continue_steps_if_needed=False) current_time.increment_turn() self.assertEqual(self.hero.actions.current_action.TYPE, prototypes.ActionIdlenessPrototype.TYPE) self.assertTrue(self.hero.position.is_walking or self.hero.position.place) # can end in start place prototypes.ActionMoveNearPlacePrototype.create(hero=self.hero, place=self.p1, back=True) self.p1.x = self.p1.x + 1 self.p1.y = self.p1.y + 1 places_logic.save_place(self.p1) while self.hero.position.place is None or self.hero.position.place.id != self.p1.id: self.storage.process_turn(continue_steps_if_needed=False) current_time.increment_turn() self.assertEqual(self.hero.actions.current_action.TYPE, prototypes.ActionInPlacePrototype.TYPE) self.assertTrue(not self.hero.position.is_walking) self.storage._test_save() def test_full(self): current_time = TimePrototype.get_current_time() while len(self.hero.actions.actions_list) != 1: self.storage.process_turn(continue_steps_if_needed=False) current_time.increment_turn() self.assertTrue(self.action_idl.leader) self.storage._test_save() def test_full__start_equal_to_finish(self): self.action_move.destination_x = self.p1.x self.action_move.destination_y = self.p1.y current_time = TimePrototype.get_current_time() while len(self.hero.actions.actions_list) != 1: self.storage.process_turn(continue_steps_if_needed=False) current_time.increment_turn() self.assertTrue(self.action_idl.leader) self.storage._test_save() @mock.patch('the_tale.game.heroes.objects.Hero.is_battle_start_needed', lambda self: True) def test_battle(self): self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(self.hero.actions.current_action.TYPE, prototypes.ActionBattlePvE1x1Prototype.TYPE) self.storage._test_save() def test_regenerate_energy_on_move(self): self.hero.preferences.set_energy_regeneration_type( heroes_relations.ENERGY_REGENERATION.PRAY) self.hero.last_energy_regeneration_at_turn -= max( next(zip(*heroes_relations.ENERGY_REGENERATION.select('period')))) self.action_move.state = self.action_move.STATE.MOVING self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(self.hero.actions.current_action.TYPE, prototypes.ActionRegenerateEnergyPrototype.TYPE) self.storage._test_save() def test_not_regenerate_energy_on_move_for_sacrifice(self): self.hero.preferences.set_energy_regeneration_type( heroes_relations.ENERGY_REGENERATION.SACRIFICE) self.hero.last_energy_regeneration_at_turn -= max( next(zip(*heroes_relations.ENERGY_REGENERATION.select('period')))) self.action_move.state = self.action_move.STATE.MOVING self.storage.process_turn(continue_steps_if_needed=False) self.assertNotEqual(self.hero.actions.current_action.TYPE, prototypes.ActionRegenerateEnergyPrototype.TYPE) self.storage._test_save() def test_regenerate_energy_after_battle_for_sacrifice(self): self.hero.preferences.set_energy_regeneration_type( heroes_relations.ENERGY_REGENERATION.SACRIFICE) self.hero.last_energy_regeneration_at_turn -= max( next(zip(*heroes_relations.ENERGY_REGENERATION.select('period')))) self.action_move.state = self.action_move.STATE.BATTLE self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(self.hero.actions.current_action.TYPE, prototypes.ActionRegenerateEnergyPrototype.TYPE) self.storage._test_save() def test_rest(self): self.hero.health = 1 self.action_move.state = self.action_move.STATE.BATTLE self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(self.hero.actions.current_action.TYPE, prototypes.ActionRestPrototype.TYPE) self.storage._test_save() @mock.patch('the_tale.game.companions.objects.Companion.need_heal', True) def test_heal_companion(self): self.action_move.state = self.action_move.STATE.BATTLE companion_record = next( companions_storage.companions.enabled_companions()) self.hero.set_companion( companions_logic.create_companion(companion_record)) self.assertEqual(self.hero.actions.current_action.TYPE, prototypes.ActionMoveNearPlacePrototype.TYPE) self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(self.hero.actions.current_action.TYPE, prototypes.ActionHealCompanionPrototype.TYPE) self.assertEqual( self.action_move.state, prototypes.ActionMoveNearPlacePrototype.STATE.HEALING_COMPANION) self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(self.hero.actions.current_action.TYPE, prototypes.ActionMoveNearPlacePrototype.TYPE) self.assertEqual( self.hero.actions.current_action.state, prototypes.ActionMoveNearPlacePrototype.STATE.HEALING_COMPANION) @mock.patch('the_tale.game.heroes.objects.Hero.is_battle_start_needed', lambda self: False) @mock.patch('the_tale.game.heroes.objects.Hero.can_companion_say_wisdom', lambda hero: True) @mock.patch( 'the_tale.game.balance.constants.COMPANIONS_EXP_PER_MOVE_PROBABILITY', 1.0) def test_companion_say_wisdom(self): companion_record = next( companions_storage.companions.enabled_companions()) self.hero.set_companion( companions_logic.create_companion(companion_record)) self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(self.action_move.state, self.action_move.STATE.MOVING) with self.check_delta(lambda: self.hero.experience, c.COMPANIONS_EXP_PER_MOVE_GET_EXP): self.storage.process_turn(continue_steps_if_needed=False) self.assertTrue( self.hero.journal.messages[-1].key.is_COMPANIONS_SAY_WISDOM) self.storage._test_save() def test_resurrect(self): self.hero.kill() self.action_move.state = self.action_move.STATE.BATTLE self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(self.hero.actions.current_action.TYPE, prototypes.ActionResurrectPrototype.TYPE) self.storage._test_save() @mock.patch('the_tale.game.heroes.objects.Hero.is_battle_start_needed', lambda self: False) def test_stop_when_quest_required_replane(self): while self.action_move.state != prototypes.ActionMoveNearPlacePrototype.STATE.MOVING: self.storage.process_turn(continue_steps_if_needed=False) self.assertFalse(self.action_move.replane_required) self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(self.action_move.state, prototypes.ActionMoveNearPlacePrototype.STATE.MOVING) self.action_move.replane_required = True self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual( self.action_move.state, prototypes.ActionMoveNearPlacePrototype.STATE.PROCESSED)
class GeneralTest(testcase.TestCase): def setUp(self): super(GeneralTest, self).setUp() create_test_map() result, account_id, bundle_id = register_user('test_user') self.bundle_id = bundle_id self.storage = LogicStorage() self.storage.load_account_data(AccountPrototype.get_by_id(account_id)) self.hero = self.storage.accounts_to_heroes[account_id] self.action_idl = self.hero.actions.current_action def tearDown(self): pass def test_HELP_CHOICES(self): for action_class in ACTION_TYPES.values(): self.assertTrue('HELP_CHOICES' in action_class.__dict__) def test_TEXTGEN_TYPE(self): for action_class in ACTION_TYPES.values(): self.assertTrue('TEXTGEN_TYPE' in action_class.__dict__) def test_get_help_choice_has_heal(self): self.hero.health = 1 heal_found = False for i in xrange(100): heal_found = heal_found or (self.action_idl.get_help_choice() == HELP_CHOICES.HEAL) self.assertTrue(heal_found) def check_heal_in_choices(self, result): heal_found = False for i in xrange(100): heal_found = heal_found or (self.action_idl.get_help_choice() == HELP_CHOICES.HEAL) self.assertEqual(heal_found, result) def check_heal_companion_in_choices(self, result): heal_found = False for i in xrange(100): heal_found = heal_found or (self.action_idl.get_help_choice() == HELP_CHOICES.HEAL_COMPANION) self.assertEqual(heal_found, result) @mock.patch('the_tale.game.actions.prototypes.ActionIdlenessPrototype.HELP_CHOICES', set((HELP_CHOICES.HEAL,))) def test_help_choice_has_heal__for_full_health_without_alternative(self): self.check_heal_in_choices(False) @mock.patch('the_tale.game.actions.prototypes.ActionIdlenessPrototype.HELP_CHOICES', set((HELP_CHOICES.HEAL, HELP_CHOICES.MONEY))) def test_help_choice_has_heal__for_full_health_with_alternative(self): self.check_heal_in_choices(False) @mock.patch('the_tale.game.actions.prototypes.ActionIdlenessPrototype.HELP_CHOICES', set((HELP_CHOICES.HEAL,))) def test_help_choice_has_heal__for_large_health_without_alternative(self): self.hero.health = self.hero.max_health - 1 self.check_heal_in_choices(True) @mock.patch('the_tale.game.actions.prototypes.ActionIdlenessPrototype.HELP_CHOICES', set((HELP_CHOICES.HEAL, HELP_CHOICES.MONEY))) def test_help_choice_has_heal__for_large_health_with_alternative(self): self.hero.health = self.hero.max_health - 1 self.check_heal_in_choices(False) @mock.patch('the_tale.game.actions.prototypes.ActionIdlenessPrototype.HELP_CHOICES', set((HELP_CHOICES.HEAL,))) def test_help_choice_has_heal__for_low_health_without_alternative(self): self.hero.health = 1 self.check_heal_in_choices(True) @mock.patch('the_tale.game.actions.prototypes.ActionIdlenessPrototype.HELP_CHOICES', set((HELP_CHOICES.HEAL, HELP_CHOICES.MONEY))) def test_help_choice_has_heal__for_low_health_with_alternative(self): self.hero.health = 1 self.check_heal_in_choices(True) @mock.patch('the_tale.game.actions.prototypes.ActionIdlenessPrototype.HELP_CHOICES', set((HELP_CHOICES.HEAL_COMPANION,))) def test_help_choice_has_heal_companion__for_no_companion(self): self.check_heal_companion_in_choices(False) @mock.patch('the_tale.game.actions.prototypes.ActionIdlenessPrototype.HELP_CHOICES', set((HELP_CHOICES.HEAL_COMPANION, HELP_CHOICES.MONEY))) def test_help_choice_has_heal_companion__for_no_companion_with_alternative(self): self.check_heal_companion_in_choices(False) @mock.patch('the_tale.game.actions.prototypes.ActionIdlenessPrototype.HELP_CHOICES', set((HELP_CHOICES.HEAL_COMPANION,))) def test_help_choice_has_heal_companion__for_full_health_without_alternative(self): companion_record = companions_storage.companions.enabled_companions().next() self.hero.set_companion(companions_logic.create_companion(companion_record)) self.check_heal_companion_in_choices(False) @mock.patch('the_tale.game.actions.prototypes.ActionIdlenessPrototype.HELP_CHOICES', set((HELP_CHOICES.HEAL_COMPANION,))) @mock.patch('the_tale.game.heroes.prototypes.HeroPrototype.companion_heal_disabled', lambda hero: True) def test_help_choice_has_heal_companion__for_companion_heal_disabled(self): companion_record = companions_storage.companions.enabled_companions().next() self.hero.set_companion(companions_logic.create_companion(companion_record)) self.check_heal_companion_in_choices(False) @mock.patch('the_tale.game.actions.prototypes.ActionIdlenessPrototype.HELP_CHOICES', set((HELP_CHOICES.HEAL_COMPANION, HELP_CHOICES.MONEY))) def test_help_choice_has_heal_companion__for_full_health_with_alternative(self): companion_record = companions_storage.companions.enabled_companions().next() self.hero.set_companion(companions_logic.create_companion(companion_record)) self.check_heal_companion_in_choices(False) @mock.patch('the_tale.game.actions.prototypes.ActionIdlenessPrototype.HELP_CHOICES', set((HELP_CHOICES.HEAL_COMPANION,))) def test_help_choice_has_heal_companion__for_low_health_without_alternative(self): companion_record = companions_storage.companions.enabled_companions().next() self.hero.set_companion(companions_logic.create_companion(companion_record)) self.hero.companion.health = 1 self.check_heal_companion_in_choices(True) @mock.patch('the_tale.game.actions.prototypes.ActionIdlenessPrototype.HELP_CHOICES', set((HELP_CHOICES.HEAL_COMPANION, HELP_CHOICES.MONEY))) def test_help_choice_has_heal_companion__for_low_health_with_alternative(self): companion_record = companions_storage.companions.enabled_companions().next() self.hero.set_companion(companions_logic.create_companion(companion_record)) self.hero.companion.health = 1 self.check_heal_companion_in_choices(True) def check_stock_up_energy_in_choices(self, result): stock_found = False for i in xrange(1000): stock_found = stock_found or (self.action_idl.get_help_choice() == HELP_CHOICES.STOCK_UP_ENERGY) self.assertEqual(stock_found, result) @mock.patch('the_tale.game.actions.prototypes.ActionIdlenessPrototype.HELP_CHOICES', set((HELP_CHOICES.STOCK_UP_ENERGY, HELP_CHOICES.MONEY))) def test_help_choice_has_stock_up_energy__can_stock(self): self.hero.energy_charges = 0 self.check_stock_up_energy_in_choices(True) @mock.patch('the_tale.game.actions.prototypes.ActionIdlenessPrototype.HELP_CHOICES', set((HELP_CHOICES.STOCK_UP_ENERGY, HELP_CHOICES.MONEY))) def test_help_choice_has_stock_up_energy__can_not_stock(self): self.hero.energy_bonus = c.ANGEL_FREE_ENERGY_MAXIMUM self.check_stock_up_energy_in_choices(False) def test_percents_consistency(self): current_time = TimePrototype.get_current_time() # just test that quest will be ended while not self.action_idl.leader: self.storage.process_turn() current_time.increment_turn() self.assertEqual(self.storage.tests_get_last_action().percents, self.hero.last_action_percents) def test_help_choice_heal_not_in_choices_for_dead_hero(self): self.hero.health = 1 self.hero.save() self.assertTrue(HELP_CHOICES.HEAL in self.action_idl.help_choices) self.hero.kill() self.hero.save() self.assertFalse(HELP_CHOICES.HEAL in self.action_idl.help_choices) def test_action_default_serialization(self): # class TestAction(ActionBase): # TYPE = 'test-action' default_action = TestAction( hero=self.hero, bundle_id=self.bundle_id, state=TestAction.STATE.UNINITIALIZED) self.assertEqual(default_action.serialize(), {'bundle_id': self.bundle_id, 'state': TestAction.STATE.UNINITIALIZED, 'percents': 0.0, 'description': None, 'type': TestAction.TYPE.value, 'created_at_turn': TimePrototype.get_current_turn_number()}) self.assertEqual(default_action, TestAction.deserialize(self.hero, default_action.serialize())) def test_action_full_serialization(self): mob = mobs_storage.create_mob_for_hero(self.hero) default_action = TestAction( hero=self.hero, bundle_id=self.bundle_id, state=TestAction.STATE.UNINITIALIZED, created_at_turn=666, context=TestAction.CONTEXT_MANAGER(), description=u'description', place_id=2, mob=mob, data={'xxx': 'yyy'}, break_at=0.75, length=777, destination_x=20, destination_y=30, percents_barier=77, extra_probability=0.6, mob_context=TestAction.CONTEXT_MANAGER(), textgen_id='textgen_id', back=True, info_link='/bla-bla', meta_action_id=7, replane_required=True) self.assertEqual(default_action.serialize(), {'bundle_id': self.bundle_id, 'state': TestAction.STATE.UNINITIALIZED, 'context': TestAction.CONTEXT_MANAGER().serialize(), 'mob_context': TestAction.CONTEXT_MANAGER().serialize(), 'mob': mob.serialize(), 'meta_action_id': 7, 'length': 777, 'back': True, 'textgen_id': 'textgen_id', 'extra_probability': 0.6, 'percents_barier': 77, 'destination_x': 20, 'destination_y': 30, 'percents': 0.0, 'description': u'description', 'type': TestAction.TYPE.value, 'created_at_turn': 666, 'place_id': 2, 'data': {'xxx': 'yyy'}, 'info_link': '/bla-bla', 'break_at': 0.75, 'replane_required': True}) self.assertEqual(default_action, TestAction.deserialize(self.hero, default_action.serialize()))
class BattlePvE1x1ActionTest(testcase.TestCase): def setUp(self): super(BattlePvE1x1ActionTest, self).setUp() create_test_map() result, account_id, bundle_id = register_user('test_user') self.storage = LogicStorage() self.storage.load_account_data(AccountPrototype.get_by_id(account_id)) self.hero = self.storage.accounts_to_heroes[account_id] self.hero._model.level = 6 self.hero.health = self.hero.max_health # do half of tests with companion if random.random() < 0.5: companion_record = companions_storage.companions.enabled_companions( ).next() companion = companions_logic.create_companion(companion_record) self.hero.set_companion(companion) self.hero.companion.health = 1 self.action_idl = self.hero.actions.current_action with mock.patch( 'the_tale.game.balance.constants.KILL_BEFORE_BATTLE_PROBABILITY', 0): self.action_battle = ActionBattlePvE1x1Prototype.create( hero=self.hero, mob=mobs_storage.create_mob_for_hero(self.hero)) def tearDown(self): pass def test_create(self): self.assertEqual(self.action_idl.leader, False) self.assertEqual(self.action_battle.leader, True) self.assertEqual(self.action_battle.bundle_id, self.action_idl.bundle_id) self.assertEqual(self.action_battle.percents, 0.0) self.assertEqual(self.action_battle.state, self.action_battle.STATE.BATTLE_RUNNING) self.storage._test_save() @mock.patch('the_tale.game.actions.prototypes.battle.make_turn', lambda a, b, c: None) def test_mob_killed(self): self.assertEqual(self.hero.statistics.pve_kills, 0) self.action_battle.mob.health = 0 self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(len(self.hero.actions.actions_list), 1) self.assertEqual(self.hero.actions.current_action, self.action_idl) self.assertEqual(self.hero.statistics.pve_kills, 1) self.storage._test_save() @mock.patch('the_tale.game.balance.constants.ARTIFACTS_PER_BATTLE', 0) @mock.patch('the_tale.game.balance.constants.GET_LOOT_PROBABILITY', 1) @mock.patch('the_tale.game.actions.prototypes.battle.make_turn', lambda a, b, c: None) def test_loot(self): self.assertEqual(self.hero.statistics.loot_had, 0) self.assertEqual(len(self.hero.bag.items()), 0) self.action_battle.mob.health = 0 self.storage.process_turn() self.assertEqual(self.hero.statistics.loot_had, 1) self.assertEqual(len(self.hero.bag.items()), 1) self.storage._test_save() @mock.patch('the_tale.game.balance.constants.ARTIFACTS_PER_BATTLE', 1) @mock.patch('the_tale.game.actions.prototypes.battle.make_turn', lambda a, b, c: None) def test_artifacts(self): self.assertEqual(self.hero.statistics.artifacts_had, 0) self.assertEqual(len(self.hero.bag.items()), 0) self.action_battle.mob.health = 0 self.storage.process_turn() self.assertEqual(self.hero.statistics.artifacts_had, 1) self.assertEqual(len(self.hero.bag.items()), 1) self.storage._test_save() @mock.patch('the_tale.game.actions.prototypes.battle.make_turn', lambda a, b, c: None) def test_hero_killed(self): self.assertEqual(self.hero.statistics.pve_deaths, 0) self.hero.health = 0 self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(len(self.hero.actions.actions_list), 1) self.assertEqual(self.hero.actions.current_action, self.action_idl) self.assertTrue(not self.hero.is_alive) self.assertEqual(self.hero.statistics.pve_deaths, 1) self.storage._test_save() @mock.patch('the_tale.game.balance.constants.ARTIFACTS_PER_BATTLE', 1) @mock.patch('the_tale.game.actions.prototypes.battle.make_turn', lambda a, b, c: None) def test_hero_and_mob_killed(self): self.hero.health = 0 self.action_battle.mob.health = 0 with self.check_not_changed( lambda: self.hero.statistics.artifacts_had): with self.check_not_changed(lambda: self.hero.bag.occupation): self.storage.process_turn(continue_steps_if_needed=False) self.assertTrue(self.hero.messages.messages[-1].key. is_ACTION_BATTLEPVE1X1_JOURNAL_HERO_AND_MOB_KILLED) self.assertTrue(self.hero.diary.messages[-1].key. is_ACTION_BATTLEPVE1X1_DIARY_HERO_AND_MOB_KILLED) self.assertEqual(len(self.hero.actions.actions_list), 1) self.assertEqual(self.hero.actions.current_action, self.action_idl) self.assertTrue(not self.hero.is_alive) self.assertEqual(self.hero.statistics.pve_deaths, 1) self.storage._test_save() def test_full_battle(self): current_time = TimePrototype.get_current_time() while len(self.hero.actions.actions_list) != 1: self.storage.process_turn() current_time.increment_turn() self.assertTrue(self.action_idl.leader) self.storage._test_save() def test_full_battle__with_companion(self): battle_ability = random.choice([ ability for ability in effects.ABILITIES.records if isinstance(ability.effect, effects.BaseBattleAbility) ]) companion_record = companions_storage.companions.enabled_companions( ).next() companion_record.abilities = container.Container( start=(battle_ability, )) companion = companions_logic.create_companion(companion_record) self.hero.set_companion(companion) self.hero.reset_accessors_cache() current_time = TimePrototype.get_current_time() while len(self.hero.actions.actions_list) != 1: self.storage.process_turn() current_time.increment_turn() self.assertTrue(self.action_idl.leader) self.storage._test_save() @mock.patch( 'the_tale.game.heroes.prototypes.HeroPrototype.companion_damage_probability', 0.0) @mock.patch( 'the_tale.game.balance.constants.COMPANIONS_WOUNDS_IN_HOUR_FROM_HEAL', 0.0) @mock.patch( 'the_tale.game.balance.constants.COMPANIONS_EATEN_CORPSES_PER_BATTLE', 1.0) @mock.patch('the_tale.game.mobs.prototypes.MobPrototype.mob_type', mobs_relations.MOB_TYPE.ANIMAL) @mock.patch( 'the_tale.game.heroes.prototypes.HeroPrototype.can_companion_eat_corpses', lambda hero: True) def test_full_battle__with_companion__eat_corpse(self): companion_record = companions_storage.companions.enabled_companions( ).next() self.hero.set_companion( companions_logic.create_companion(companion_record)) self.hero.reset_accessors_cache() self.hero.companion.health = 10 current_time = TimePrototype.get_current_time() while self.hero.actions.current_action.TYPE == ActionBattlePvE1x1Prototype.TYPE: self.storage.process_turn(continue_steps_if_needed=False) current_time.increment_turn() self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(self.hero.companion.health, 10 + c.COMPANIONS_EATEN_CORPSES_HEAL_AMOUNT) self.storage._test_save() def test_bit_mob(self): old_mob_health = self.action_battle.mob.health old_action_percents = self.action_battle.percents self.action_battle.bit_mob(0.33) self.assertTrue(self.action_battle.mob.health < old_mob_health) self.assertTrue(self.action_battle.percents > old_action_percents) self.assertTrue(self.action_battle.updated) def test_bit_mob_and_kill(self): self.action_battle.bit_mob(1) self.assertEqual(self.action_battle.mob.health, 0) self.assertEqual(self.action_battle.percents, 1) self.assertTrue(self.action_battle.updated) self.assertEqual(self.action_battle.state, self.action_battle.STATE.PROCESSED) def test_fast_resurrect__not_processed(self): self.action_battle.hero.kill() self.assertFalse(self.action_battle.fast_resurrect()) self.assertFalse(self.action_battle.hero.is_alive) def test_fast_resurrect__hero_is_alive(self): self.action_battle.state = self.action_battle.STATE.PROCESSED self.assertFalse(self.action_battle.fast_resurrect()) self.assertTrue(self.action_battle.hero.is_alive) def test_fast_resurrect__success(self): self.action_battle.hero.kill() self.action_battle.state = self.action_battle.STATE.PROCESSED self.assertTrue(self.action_battle.fast_resurrect()) self.assertTrue(self.action_battle.hero.is_alive) def test_help_choices(self): self.assertTrue( HELP_CHOICES.LIGHTING in self.action_battle.HELP_CHOICES) self.action_battle.mob.health = 0 self.assertFalse( HELP_CHOICES.LIGHTING in self.action_battle.HELP_CHOICES) self.action_battle.mob.health = 1 self.action_battle.hero.kill() self.assertEqual(self.action_battle.HELP_CHOICES, set((HELP_CHOICES.RESURRECT, ))) @mock.patch( 'the_tale.game.balance.constants.KILL_BEFORE_BATTLE_PROBABILITY', 1.01) @mock.patch('the_tale.game.heroes.habits.Honor.interval', HABIT_HONOR_INTERVAL.LEFT_3) def test_kill_before_start(self): self.hero.actions.pop_action() with self.check_delta(lambda: self.hero.statistics.pve_kills, 1): action_battle = ActionBattlePvE1x1Prototype.create( hero=self.hero, mob=mobs_storage.create_mob_for_hero(self.hero)) self.assertEqual(action_battle.percents, 1.0) self.assertEqual(action_battle.state, self.action_battle.STATE.PROCESSED) self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(self.hero.actions.current_action, self.action_idl) @mock.patch( 'the_tale.game.heroes.prototypes.HeroPrototype.can_companion_do_exorcism', lambda hero: True) def test_companion_exorcims__demon(self): self.companion_record = companions_logic.create_random_companion_record( 'exorcist', state=companions_relations.STATE.ENABLED) self.hero.set_companion( companions_logic.create_companion(self.companion_record)) demon_record = mobs_prototypes.MobRecordPrototype.create_random( 'demon', type=mobs_relations.MOB_TYPE.DEMON) demon = mobs_prototypes.MobPrototype(record_id=demon_record.id, level=self.hero.level, is_boss=False) self.hero.actions.pop_action() with self.check_delta(lambda: self.hero.statistics.pve_kills, 1): action_battle = ActionBattlePvE1x1Prototype.create(hero=self.hero, mob=demon) self.assertEqual(action_battle.percents, 1.0) self.assertEqual(action_battle.state, self.action_battle.STATE.PROCESSED) self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(self.hero.actions.current_action, self.action_idl) @mock.patch( 'the_tale.game.heroes.prototypes.HeroPrototype.can_companion_do_exorcism', lambda hero: True) def test_companion_exorcims__not_demon(self): self.companion_record = companions_logic.create_random_companion_record( 'exorcist', state=companions_relations.STATE.ENABLED) self.hero.set_companion( companions_logic.create_companion(self.companion_record)) not_demon_record = mobs_prototypes.MobRecordPrototype.create_random( 'demon', type=mobs_relations.MOB_TYPE.random( exclude=(mobs_relations.MOB_TYPE.DEMON, ))) not_demon = mobs_prototypes.MobPrototype(record_id=not_demon_record.id, level=self.hero.level, is_boss=False) self.hero.actions.pop_action() with self.check_delta(lambda: self.hero.statistics.pve_kills, 0): action_battle = ActionBattlePvE1x1Prototype.create(hero=self.hero, mob=not_demon) self.assertEqual(action_battle.percents, 0.0) self.assertEqual(action_battle.state, self.action_battle.STATE.BATTLE_RUNNING) self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(self.hero.actions.current_action, action_battle) @mock.patch('the_tale.game.balance.constants.PEACEFULL_BATTLE_PROBABILITY', 1.01) @mock.patch('the_tale.game.heroes.habits.Peacefulness.interval', HABIT_PEACEFULNESS_INTERVAL.RIGHT_3) def test_peacefull_battle(self): self.hero.actions.pop_action() with self.check_delta(lambda: self.hero.statistics.pve_kills, 0): action_battle = ActionBattlePvE1x1Prototype.create( hero=self.hero, mob=mobs_storage.create_mob_for_hero(self.hero)) self.assertEqual(action_battle.percents, 1.0) self.assertEqual(action_battle.state, self.action_battle.STATE.PROCESSED) self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(self.hero.actions.current_action, self.action_idl) @mock.patch( 'the_tale.game.heroes.prototypes.HeroPrototype.can_leave_battle_in_fear', lambda self: True) def test_fear_battle(self): self.hero.actions.pop_action() mob = (m for m in mobs_storage.all() if m.type.is_CIVILIZED).next() with self.check_delta(lambda: self.hero.statistics.pve_kills, 0): action_battle = ActionBattlePvE1x1Prototype.create( hero=self.hero, mob=mob.create_mob(self.hero, is_boss=False)) self.assertEqual(action_battle.percents, 1.0) self.assertEqual(action_battle.state, self.action_battle.STATE.PROCESSED) self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(self.hero.actions.current_action, self.action_idl) @mock.patch('the_tale.game.balance.constants.EXP_FOR_KILL_PROBABILITY', 1.01) @mock.patch('the_tale.game.balance.constants.EXP_FOR_KILL_DELTA', 0) @mock.patch('the_tale.game.heroes.habits.Peacefulness.interval', HABIT_PEACEFULNESS_INTERVAL.LEFT_3) def test_experience_for_kill(self): mob = mobs_storage.create_mob_for_hero(self.hero) mob.health = 0 with self.check_delta(lambda: self.hero.statistics.pve_kills, 1): with mock.patch( 'the_tale.game.heroes.prototypes.HeroPrototype.add_experience' ) as add_experience: with mock.patch( 'the_tale.game.actions.prototypes.ActionBattlePvE1x1Prototype.process_artifact_breaking' ) as process_artifact_breaking: action_battle = ActionBattlePvE1x1Prototype.create( hero=self.hero, mob=mob) self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(add_experience.call_args_list, [mock.call(c.EXP_FOR_KILL)]) self.assertEqual(process_artifact_breaking.call_count, 1) self.assertEqual(action_battle.state, self.action_battle.STATE.PROCESSED) @mock.patch('the_tale.game.balance.constants.ARTIFACTS_BREAKS_PER_BATTLE', 1.0) @mock.patch( 'the_tale.game.artifacts.prototypes.ArtifactPrototype.can_be_broken', lambda self: True) def test_process_artifact_breaking__no_equipment(self): self.hero.equipment._remove_all() old_power = self.hero.power.clone() self.action_battle.process_artifact_breaking() self.assertEqual(old_power, self.hero.power) @mock.patch('the_tale.game.balance.constants.ARTIFACTS_BREAKS_PER_BATTLE', 1.0) @mock.patch( 'the_tale.game.artifacts.prototypes.ArtifactPrototype.can_be_broken', lambda self: True) def test_process_artifact_breaking__broken(self): for artifact in self.hero.equipment.values(): artifact.power = Power(100, 100) old_power = self.hero.power.total() self.action_battle.process_artifact_breaking() self.assertTrue(old_power > self.hero.power.total()) @mock.patch('the_tale.game.balance.constants.ARTIFACTS_BREAKS_PER_BATTLE', 1.0) @mock.patch( 'the_tale.game.artifacts.prototypes.ArtifactPrototype.can_be_broken', lambda self: False) def test_process_artifact_breaking__not_broken(self): for artifact in self.hero.equipment.values(): artifact.power = Power(100, 100) old_power = self.hero.power.total() self.action_battle.process_artifact_breaking() self.assertEqual(old_power, self.hero.power.total()) @mock.patch('the_tale.game.balance.constants.ARTIFACTS_BREAKS_PER_BATTLE', 1.0) @mock.patch( 'the_tale.game.artifacts.prototypes.ArtifactPrototype.can_be_broken', lambda self: False) def test_process_artifact_breaking__break_only_mostly_damaged(self): for artifact in self.hero.equipment.values(): artifact.power = Power(100, 100) artifact.integrity = 0 artifact.integrity = artifact.max_integrity for i in xrange(100): self.action_battle.process_artifact_breaking() self.assertEqual(artifact.power, Power(100, 100)) def test_process_artifact_breaking__integrity_damage(self): for artifact in self.hero.equipment.values(): artifact.integrity = artifact.max_integrity self.action_battle.process_artifact_breaking() for artifact in self.hero.equipment.values(): self.assertEqual(artifact.integrity, artifact.max_integrity - 1)
class TradingActionTest(testcase.TestCase): def setUp(self): super(TradingActionTest, self).setUp() create_test_map() account = self.accounts_factory.create_account(is_fast=True) self.storage = LogicStorage() self.storage.load_account_data(account) self.hero = self.storage.accounts_to_heroes[account.id] self.action_idl = self.hero.actions.current_action self.action_trade = ActionTradingPrototype.create(hero=self.hero) def tearDown(self): pass def test_create(self): self.assertEqual(self.action_idl.leader, False) self.assertEqual(self.action_trade.leader, True) self.assertEqual(self.action_trade.bundle_id, self.action_idl.bundle_id) self.storage._test_save() def test_processed(self): self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(len(self.hero.actions.actions_list), 1) self.assertEqual(self.hero.actions.current_action, self.action_idl) self.storage._test_save() def test_sell_and_finish(self): old_money_statistics = self.hero.statistics.money_earned old_money = self.hero.money artifact = artifacts_storage.generate_artifact_from_list(artifacts_storage.artifacts, self.hero.level, rarity=RARITY.NORMAL) self.hero.bag.put_artifact(artifact) self.action_trade.percents_barier = 1 self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(len(self.hero.actions.actions_list), 1) self.assertEqual(self.hero.actions.current_action, self.action_idl) self.assertTrue(self.hero.money > old_money) self.assertTrue(self.hero.statistics.money_earned > old_money_statistics) self.storage._test_save() def test_sell_and_continue(self): old_money = self.hero.money artifact = artifacts_storage.generate_artifact_from_list(artifacts_storage.artifacts, self.hero.level, rarity=RARITY.NORMAL) self.hero.bag.put_artifact(artifact) artifact = artifacts_storage.generate_artifact_from_list(artifacts_storage.artifacts, self.hero.level, rarity=RARITY.NORMAL) self.hero.bag.put_artifact(artifact) self.assertEqual(self.hero.bag.occupation, 2) self.action_trade.percents_barier = 2 current_time = TimePrototype.get_current_time() self.storage.process_turn() self.assertEqual(len(self.hero.actions.actions_list), 2) self.assertEqual(self.hero.actions.current_action, self.action_trade) self.assertEqual(self.hero.bag.occupation, 1) self.assertTrue(self.hero.money > old_money) old_money = self.hero.money current_time.increment_turn() self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(len(self.hero.actions.actions_list), 1) self.assertEqual(self.hero.actions.current_action, self.action_idl) self.assertEqual(self.hero.bag.occupation, 0) self.assertTrue(self.hero.money > old_money) self.storage._test_save() def test_stop_when_quest_required_replane(self): self.action_idl.percents = 0.0 self.assertFalse(self.action_trade.replane_required) artifact = artifacts_storage.generate_artifact_from_list(artifacts_storage.artifacts, self.hero.level, rarity=RARITY.NORMAL) self.hero.bag.put_artifact(artifact) artifact = artifacts_storage.generate_artifact_from_list(artifacts_storage.artifacts, self.hero.level, rarity=RARITY.NORMAL) self.hero.bag.put_artifact(artifact) self.action_trade.percents_barier = 2 self.assertEqual(self.hero.bag.occupation, 2) current_time = TimePrototype.get_current_time() self.storage.process_turn() self.assertEqual(self.hero.bag.occupation, 1) self.assertEqual(len(self.hero.actions.actions_list), 2) self.assertEqual(self.hero.actions.current_action, self.action_trade) self.action_trade.replane_required = True current_time.increment_turn() self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(self.hero.bag.occupation, 1) self.assertEqual(len(self.hero.actions.actions_list), 1) self.assertEqual(self.action_trade.state, self.action_trade.STATE.PROCESSED) self.assertEqual(self.hero.actions.current_action, self.action_idl)