def test_register_two_tasks_requested_one_account(self): self.worker.initialize() task = SupervisorTaskPrototype.create_arena_pvp_1x1(self.account_1, self.account_2) task_2 = SupervisorTaskPrototype.create_arena_pvp_1x1(self.account_1, self.account_2) self.worker.register_task(task) self.assertRaises(SupervisorException, self.worker.register_task, task_2)
def test_register_two_tasks_requested_one_account(self): self.worker.process_initialize() task = SupervisorTaskPrototype.create_arena_pvp_1x1(self.account_1, self.account_2) task_2 = SupervisorTaskPrototype.create_arena_pvp_1x1(self.account_1, self.account_2) self.worker.register_task(task) self.assertRaises(SupervisorException, self.worker.register_task, task_2) self.assertTrue(self.worker.logger.calls_count > 0)
def _initiate_battle(self, record_1, record_2, calculate_ratings=False): from the_tale.accounts.prototypes import AccountPrototype account_1 = AccountPrototype.get_by_id(record_1.account_id) account_2 = AccountPrototype.get_by_id(record_2.account_id) self.logger.info("start battle between accounts %d and %d" % (account_1.id, account_2.id)) with transaction.atomic(): battle_1 = Battle1x1Prototype.get_by_id(record_1.battle_id) battle_2 = Battle1x1Prototype.get_by_id(record_2.battle_id) battle_1.set_enemy(account_2) battle_2.set_enemy(account_1) if ( calculate_ratings and abs(record_1.hero_level - record_2.hero_level) <= pvp_settings.BALANCING_MIN_LEVEL_DELTA ): battle_1.calculate_rating = True battle_2.calculate_rating = True battle_1.save() battle_2.save() task = SupervisorTaskPrototype.create_arena_pvp_1x1(account_1, account_2) environment.workers.supervisor.cmd_add_task(task.id)
def _initiate_battle(self, record_1, record_2, calculate_ratings=False): from the_tale.accounts.prototypes import AccountPrototype account_1 = AccountPrototype.get_by_id(record_1.account_id) account_2 = AccountPrototype.get_by_id(record_2.account_id) self.logger.info('start battle between accounts %d and %d' % (account_1.id, account_2.id)) with transaction.atomic(): battle_1 = Battle1x1Prototype.get_by_id(record_1.battle_id) battle_2 = Battle1x1Prototype.get_by_id(record_2.battle_id) battle_1.set_enemy(account_2) battle_2.set_enemy(account_1) if calculate_ratings and abs( record_1.hero_level - record_2.hero_level ) <= pvp_settings.BALANCING_MIN_LEVEL_DELTA: battle_1.calculate_rating = True battle_2.calculate_rating = True battle_1.save() battle_2.save() task = SupervisorTaskPrototype.create_arena_pvp_1x1( account_1, account_2) environment.workers.supervisor.cmd_add_task(task.id)
def test_register_task_second_time(self): self.worker.process_initialize() task = SupervisorTaskPrototype.create_arena_pvp_1x1(self.account_1, self.account_2) self.worker.register_task(task) self.assertRaises(SupervisorException, self.worker.register_task, task)
def test_register_account_last_in_task(self): self.worker.process_initialize() Battle1x1Prototype.create(self.account_1).set_enemy(self.account_2) Battle1x1Prototype.create(self.account_2).set_enemy(self.account_1) task = SupervisorTaskPrototype.create_arena_pvp_1x1( self.account_1, self.account_2) self.worker.register_task(task) with mock.patch( 'the_tale.game.workers.logic.Worker.cmd_register_account' ) as register_account_counter: self.worker.register_account(self.account_1.id) self.worker.register_account(self.account_2.id) self.assertEqual(register_account_counter.call_count, 2) self.assertEqual(set(self.worker.accounts_for_tasks.keys()), set()) self.assertEqual(self.worker.tasks.values(), []) self.assertEqual(SupervisorTask.objects.all().count(), 0) self.assertEqual(self.worker.accounts_owners, { self.account_1.id: 'game_logic_1', self.account_2.id: 'game_logic_1' })
def test_register_task_release_account(self): self.worker.process_initialize() task = SupervisorTaskPrototype.create_arena_pvp_1x1(self.account_1, self.account_2) with mock.patch('the_tale.game.workers.logic.Worker.cmd_release_account') as release_accounts_counter: self.worker.register_task(task) self.assertEqual(release_accounts_counter.call_count, 2)
def test_process_arena_pvp_1x1(self): task = SupervisorTaskPrototype.create_arena_pvp_1x1( self.account_1, self.account_2) task.capture_member(self.account_1.id) task.capture_member(self.account_2.id) battle_1 = Battle1x1Prototype.create(self.account_1) battle_1.set_enemy(self.account_2) battle_1.save() battle_2 = Battle1x1Prototype.create(self.account_2) battle_2.set_enemy(self.account_1) battle_2.save() self.assertEqual( Battle1x1.objects.filter( state=BATTLE_1X1_STATE.PREPAIRING).count(), 2) self.assertEqual( Battle1x1.objects.filter( state=BATTLE_1X1_STATE.PROCESSING).count(), 0) old_hero = heroes_logic.load_hero(account_id=self.account_1.id) old_hero.health = 1 heroes_logic.save_hero(old_hero) task.process(bundle_id=666) new_hero = heroes_logic.load_hero(account_id=self.account_1.id) new_hero_2 = heroes_logic.load_hero(account_id=self.account_2.id) self.assertEqual(new_hero.actions.current_action.bundle_id, new_hero_2.actions.current_action.bundle_id) self.assertNotEqual(new_hero.actions.actions_list[0].bundle_id, new_hero.actions.actions_list[1].bundle_id) self.assertNotEqual(new_hero_2.actions.actions_list[0].bundle_id, new_hero_2.actions.actions_list[1].bundle_id) self.assertNotEqual(old_hero, new_hero) self.assertTrue(old_hero.actions.number < new_hero.actions.number) self.assertEqual(new_hero.health, new_hero.max_health) self.assertEqual(new_hero.actions.number, 2) self.assertEqual(new_hero_2.actions.number, 2) self.assertEqual( new_hero.actions.current_action.meta_action.serialize(), new_hero_2.actions.current_action.meta_action.serialize()) self.assertEqual( Battle1x1.objects.filter( state=BATTLE_1X1_STATE.PREPAIRING).count(), 0) self.assertEqual( Battle1x1.objects.filter( state=BATTLE_1X1_STATE.PROCESSING).count(), 2)
def test_register_account_in_task(self): self.worker.process_initialize() task = SupervisorTaskPrototype.create_arena_pvp_1x1(self.account_1, self.account_2) self.worker.register_task(task) with mock.patch('the_tale.game.workers.logic.Worker.cmd_register_account') as register_account_counter: self.worker.register_account(self.account_1.id) self.assertEqual(register_account_counter.call_count, 0) self.assertEqual(set(self.worker.accounts_for_tasks.keys()), set([self.account_1.id, self.account_2.id])) self.assertEqual(self.worker.tasks.values()[0].captured_members, set([self.account_1.id])) self.assertEqual(self.worker.accounts_owners, {self.account_1.id: 'game_supervisor', self.account_2.id: None})
def test_register_account_last_in_task(self): self.worker.process_initialize() Battle1x1Prototype.create(self.account_1).set_enemy(self.account_2) Battle1x1Prototype.create(self.account_2).set_enemy(self.account_1) task = SupervisorTaskPrototype.create_arena_pvp_1x1(self.account_1, self.account_2) self.worker.register_task(task) with mock.patch('the_tale.game.workers.logic.Worker.cmd_register_account') as register_account_counter: self.worker.register_account(self.account_1.id) self.worker.register_account(self.account_2.id) self.assertEqual(register_account_counter.call_count, 2) self.assertEqual(set(self.worker.accounts_for_tasks.keys()), set()) self.assertEqual(self.worker.tasks.values(), []) self.assertEqual(SupervisorTask.objects.all().count(), 0) self.assertEqual(self.worker.accounts_owners, {self.account_1.id: 'game_logic_1', self.account_2.id: 'game_logic_1'})
def test_register_account_not_in_task(self): self.worker.process_initialize() result, account_id, bundle_id = register_user('test_user_3', '*****@*****.**', '111111') task = SupervisorTaskPrototype.create_arena_pvp_1x1( self.account_1, self.account_2) self.worker.register_task(task) # for test pending account cmd queue self.worker.accounts_queues[account_id] = [('logic_task', { 'account_id': self.account_1.id, 'task_id': 1 }), ('logic_task', { 'account_id': self.account_1.id, 'task_id': 2 }), ('logic_task', { 'account_id': self.account_1.id, 'task_id': 4 })] with mock.patch( 'the_tale.game.workers.logic.Worker.cmd_register_account' ) as register_account_counter: with mock.patch('the_tale.game.workers.logic.Worker.cmd_logic_task' ) as cmd_logic_task: self.worker.register_account(account_id) self.assertEqual(cmd_logic_task.call_count, 3) self.assertEqual(register_account_counter.call_count, 1) self.assertEqual(set(self.worker.accounts_for_tasks.keys()), set([self.account_1.id, self.account_2.id])) self.assertEqual(self.worker.tasks.values()[0].captured_members, set()) self.assertEqual( self.worker.accounts_owners, { self.account_1.id: None, self.account_2.id: None, account_id: 'game_logic_1' })
def test_1_register_task(self): self.worker.process_initialize() task = SupervisorTaskPrototype.create_arena_pvp_1x1(self.account_1, self.account_2) battle_1 = Battle1x1Prototype.create(self.account_1) battle_1.set_enemy(self.account_2) battle_1.save() battle_2 = Battle1x1Prototype.create(self.account_2) battle_2.set_enemy(self.account_1) battle_2.save() self.assertEqual(len(self.worker.tasks), 0) self.assertEqual(len(self.worker.accounts_for_tasks), 0) with mock.patch('the_tale.game.workers.logic.Worker.cmd_release_account') as release_accounts_counter: self.worker.register_task(task, release_accounts=True) self.assertEqual(len(self.worker.tasks), 1) self.assertEqual(len(self.worker.accounts_for_tasks), 2) self.assertFalse(self.worker.tasks.values()[0].all_members_captured) self.assertEqual(self.worker.accounts_owners, {self.account_1.id: None, self.account_2.id: None}) self.assertEqual(self.worker.accounts_queues, {}) self.worker.process_account_released(self.account_1.id) self.assertEqual(self.worker.accounts_owners, {self.account_1.id: 'game_supervisor', self.account_2.id: None}) #test commands queue self.worker.process_start_hero_caching(self.account_1.id) self.worker.process_start_hero_caching(self.account_2.id) self.worker.process_logic_task(self.account_1.id, 666) self.assertEqual(self.worker.accounts_queues, { self.account_1.id: [('start_hero_caching', {'account_id': self.account_1.id}), ('logic_task', {'account_id': self.account_1.id, 'task_id': 666}),], self.account_2.id: [('start_hero_caching', {'account_id': self.account_2.id})]}) self.worker.process_account_released(self.account_2.id) self.assertEqual(self.worker.accounts_owners, {self.account_1.id: 'game_logic_1', self.account_2.id: 'game_logic_1'}) self.assertEqual(len(self.worker.tasks), 0) self.assertEqual(release_accounts_counter.call_count, 2)
def test_process_arena_pvp_1x1(self): task = SupervisorTaskPrototype.create_arena_pvp_1x1(self.account_1, self.account_2) task.capture_member(self.account_1.id) task.capture_member(self.account_2.id) battle_1 = Battle1x1Prototype.create(self.account_1) battle_1.set_enemy(self.account_2) battle_1.save() battle_2 = Battle1x1Prototype.create(self.account_2) battle_2.set_enemy(self.account_1) battle_2.save() self.assertEqual(Battle1x1.objects.filter(state=BATTLE_1X1_STATE.PREPAIRING).count(), 2) self.assertEqual(Battle1x1.objects.filter(state=BATTLE_1X1_STATE.PROCESSING).count(), 0) old_hero = HeroPrototype.get_by_account_id(self.account_1.id) old_hero.health = 1 old_hero.save() task.process(bundle_id=666) new_hero = HeroPrototype.get_by_account_id(self.account_1.id) new_hero_2 = HeroPrototype.get_by_account_id(self.account_2.id) self.assertEqual(new_hero.actions.current_action.bundle_id, new_hero_2.actions.current_action.bundle_id) self.assertNotEqual(new_hero.actions.actions_list[0].bundle_id, new_hero.actions.actions_list[1].bundle_id) self.assertNotEqual(new_hero_2.actions.actions_list[0].bundle_id, new_hero_2.actions.actions_list[1].bundle_id) self.assertNotEqual(old_hero, new_hero) self.assertTrue(old_hero.actions.number < new_hero.actions.number) self.assertEqual(new_hero.health, new_hero.max_health) self.assertEqual(new_hero.actions.number, 2) self.assertEqual(new_hero_2.actions.number, 2) self.assertEqual(new_hero.actions.current_action.meta_action.serialize(), new_hero_2.actions.current_action.meta_action.serialize()) self.assertEqual(Battle1x1.objects.filter(state=BATTLE_1X1_STATE.PREPAIRING).count(), 0) self.assertEqual(Battle1x1.objects.filter(state=BATTLE_1X1_STATE.PROCESSING).count(), 2)
def test_register_account_not_in_task(self): self.worker.process_initialize() result, account_id, bundle_id = register_user('test_user_3', '*****@*****.**', '111111') task = SupervisorTaskPrototype.create_arena_pvp_1x1(self.account_1, self.account_2) self.worker.register_task(task) # for test pending account cmd queue self.worker.accounts_queues[account_id] = [('logic_task', {'account_id': self.account_1.id, 'task_id': 1}), ('logic_task', {'account_id': self.account_1.id, 'task_id': 2}), ('logic_task', {'account_id': self.account_1.id, 'task_id': 4}) ] with mock.patch('the_tale.game.workers.logic.Worker.cmd_register_account') as register_account_counter: with mock.patch('the_tale.game.workers.logic.Worker.cmd_logic_task') as cmd_logic_task: self.worker.register_account(account_id) self.assertEqual(cmd_logic_task.call_count, 3) self.assertEqual(register_account_counter.call_count, 1) self.assertEqual(set(self.worker.accounts_for_tasks.keys()), set([self.account_1.id, self.account_2.id])) self.assertEqual(self.worker.tasks.values()[0].captured_members, set()) self.assertEqual(self.worker.accounts_owners, {self.account_1.id: None, self.account_2.id: None, account_id: 'game_logic_1'})
def test_process_when_not_all_members_captured(self): task = SupervisorTaskPrototype.create_arena_pvp_1x1(self.account_1, self.account_2) self.assertRaises(exceptions.GameError, task.process, bundle_id=666)
def test_process_when_not_all_members_captured(self): task = SupervisorTaskPrototype.create_arena_pvp_1x1( self.account_1, self.account_2) self.assertRaises(exceptions.GameError, task.process, bundle_id=666)