def process_battle_ending(self): battle_1 = Battle1x1Prototype.get_by_account_id(self.hero_1.account_id) battle_2 = Battle1x1Prototype.get_by_account_id(self.hero_2.account_id) participant_1 = AccountPrototype.get_by_id(self.hero_1.account_id) participant_2 = AccountPrototype.get_by_id(self.hero_2.account_id) if self.hero_1.health <= 0: if self.hero_2.health <= 0: Battle1x1ResultPrototype.create(participant_1=participant_1, participant_2=participant_2, result =BATTLE_1X1_RESULT.DRAW) if battle_1.calculate_rating and battle_2.calculate_rating: self.hero_1.statistics.change_pvp_battles_1x1_draws(1) self.hero_2.statistics.change_pvp_battles_1x1_draws(1) else: Battle1x1ResultPrototype.create(participant_1=participant_1, participant_2=participant_2, result =BATTLE_1X1_RESULT.DEFEAT) if battle_1.calculate_rating and battle_2.calculate_rating: self.hero_2.statistics.change_pvp_battles_1x1_victories(1) self.hero_1.statistics.change_pvp_battles_1x1_defeats(1) else: Battle1x1ResultPrototype.create(participant_1=participant_1, participant_2=participant_2, result =BATTLE_1X1_RESULT.VICTORY) if battle_1.calculate_rating and battle_2.calculate_rating: self.hero_1.statistics.change_pvp_battles_1x1_victories(1) self.hero_2.statistics.change_pvp_battles_1x1_defeats(1) battle_1.remove() battle_2.remove() self.hero_1.health = self.hero_1_old_health self.hero_2.health = self.hero_2_old_health self.state = self.STATE.PROCESSED
def accept_battle(cls, pvp_balancer, battle_id, hero_id): accepted_battle = Battle1x1Prototype.get_by_id(battle_id) if accepted_battle is None: return ACCEPT_BATTLE_RESULT.BATTLE_NOT_FOUND if not accepted_battle.state.is_WAITING: return ACCEPT_BATTLE_RESULT.WRONG_ACCEPTED_BATTLE_STATE if not accepted_battle.account_id in pvp_balancer.arena_queue: return ACCEPT_BATTLE_RESULT.NOT_IN_QUEUE initiator_id = heroes_logic.load_hero(hero_id=hero_id).account_id initiator_battle = Battle1x1Prototype.get_by_account_id(initiator_id) if initiator_battle is not None and not initiator_battle.state.is_WAITING: return ACCEPT_BATTLE_RESULT.WRONG_INITIATOR_BATTLE_STATE if initiator_id not in pvp_balancer.arena_queue: pvp_balancer.add_to_arena_queue(hero_id) pvp_balancer.force_battle(accepted_battle.account_id, initiator_id) return ACCEPT_BATTLE_RESULT.PROCESSED
def test_process_add_to_arena_queue_two_requests_from_one_account(self): battle_1 = Battle1x1Prototype.create( AccountPrototype.get_by_id(self.account_1.id)) battle_2 = Battle1x1Prototype.create( AccountPrototype.get_by_id(self.account_1.id)) self.assertEqual(Battle1x1.objects.all().count(), 1) self.assertEqual(battle_1.id, battle_2.id)
def test_initiate_battle_with_bot__create_battle(self): self.hero_1.level = 50 heroes_logic.save_hero(self.hero_1) bot_account = self.accounts_factory.create_account(is_bot=True) records_to_remove, records_to_exclude = self.worker._initiate_battle_with_bot( self.battle_1_record()) bot_battle = Battle1x1Prototype.get_by_id( records_to_exclude[1].battle_id) bot_record = QueueRecord(account_id=bot_account.id, battle_id=bot_battle.id, created_at=bot_battle.created_at + datetime.timedelta(seconds=0), hero_level=1) self.assertEqual(records_to_remove, []) self.assertEqual(records_to_exclude, [self.battle_1_record(), bot_record]) self.assertEqual(SupervisorTask.objects.all().count(), 1) battle_player = Battle1x1Prototype.get_by_account_id(self.account_1.id) battle_bot = Battle1x1Prototype.get_by_account_id(bot_account.id) self.assertEqual(battle_player.enemy_id, bot_account.id) self.assertFalse(battle_player.calculate_rating) self.assertEqual(battle_bot.enemy_id, self.account_1.id) self.assertFalse(battle_bot.calculate_rating)
def test_initiate_battle_with_bot__create_battle(self): self.hero_1._model.level = 50 self.hero_1.save() result, bot_account_id, bundle_id = register_user('bot_user', '*****@*****.**', '111111', is_bot=True) records_to_remove, records_to_exclude = self.worker._initiate_battle_with_bot(self.battle_1_record()) bot_battle = Battle1x1Prototype.get_by_id(records_to_exclude[1].battle_id) bot_record = QueueRecord(account_id=bot_account_id, battle_id=bot_battle.id, created_at=bot_battle.created_at + datetime.timedelta(seconds=0), hero_level=1) self.assertEqual(records_to_remove, []) self.assertEqual(records_to_exclude, [self.battle_1_record(), bot_record]) self.assertEqual(SupervisorTask.objects.all().count(), 1) battle_player = Battle1x1Prototype.get_by_account_id(self.account_1.id) battle_bot = Battle1x1Prototype.get_by_account_id(bot_account_id) self.assertEqual(battle_player.enemy_id, bot_account_id) self.assertFalse(battle_player.calculate_rating) self.assertEqual(battle_bot.enemy_id, self.account_1.id) self.assertFalse(battle_bot.calculate_rating)
def test_initiate_battle_with_bot__create_battle(self): self.hero_1.level = 50 heroes_logic.save_hero(self.hero_1) bot_account = self.accounts_factory.create_account(is_bot=True) records_to_remove, records_to_exclude = self.worker._initiate_battle_with_bot(self.battle_1_record()) bot_battle = Battle1x1Prototype.get_by_id(records_to_exclude[1].battle_id) bot_record = QueueRecord(account_id=bot_account.id, battle_id=bot_battle.id, created_at=bot_battle.created_at + datetime.timedelta(seconds=0), hero_level=1) self.assertEqual(records_to_remove, []) self.assertEqual(records_to_exclude, [self.battle_1_record(), bot_record]) self.assertEqual(SupervisorTask.objects.all().count(), 1) battle_player = Battle1x1Prototype.get_by_account_id(self.account_1.id) battle_bot = Battle1x1Prototype.get_by_account_id(bot_account.id) self.assertEqual(battle_player.enemy_id, bot_account.id) self.assertFalse(battle_player.calculate_rating) self.assertEqual(battle_bot.enemy_id, self.account_1.id) self.assertFalse(battle_bot.calculate_rating)
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_initiate_battle_with_bot__create_battle(self): self.hero_1._model.level = 50 self.hero_1.save() result, bot_account_id, bundle_id = register_user('bot_user', '*****@*****.**', '111111', is_bot=True) records_to_remove, records_to_exclude = self.worker._initiate_battle_with_bot( self.battle_1_record()) bot_battle = Battle1x1Prototype.get_by_id( records_to_exclude[1].battle_id) bot_record = QueueRecord(account_id=bot_account_id, battle_id=bot_battle.id, created_at=bot_battle.created_at + datetime.timedelta(seconds=0), hero_level=1) self.assertEqual(records_to_remove, []) self.assertEqual(records_to_exclude, [self.battle_1_record(), bot_record]) self.assertEqual(SupervisorTask.objects.all().count(), 1) battle_player = Battle1x1Prototype.get_by_account_id(self.account_1.id) battle_bot = Battle1x1Prototype.get_by_account_id(bot_account_id) self.assertEqual(battle_player.enemy_id, bot_account_id) self.assertFalse(battle_player.calculate_rating) self.assertEqual(battle_bot.enemy_id, self.account_1.id) self.assertFalse(battle_bot.calculate_rating)
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_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 _clean_queue(self, records_to_remove, records_to_exclude): for record in itertools.chain(records_to_remove, records_to_exclude): del self.arena_queue[record.account_id] if records_to_remove: for record in records_to_remove: Battle1x1Prototype.get_by_id(record.battle_id).remove() self.logger.info("remove from queue request from the_tale.accounts %r" % (records_to_remove,))
def _clean_queue(self, records_to_remove, records_to_exclude): for record in itertools.chain(records_to_remove, records_to_exclude): del self.arena_queue[record.account_id] if records_to_remove: for record in records_to_remove: Battle1x1Prototype.get_by_id(record.battle_id).remove() self.logger.info('remove from queue request from the_tale.accounts %r' % (records_to_remove, ))
def test_1_process_success(self): self.assertEqual(SupervisorTask.objects.all().count(), 0) self.assertTrue(self.accept_battle().is_PROCESSED) self.assertEqual(SupervisorTask.objects.all().count(), 1) self.assertEqual(Battle1x1Prototype.get_by_id(self.battle.id).enemy_id, self.account_2.id) self.assertEqual(Battle1x1Prototype.get_by_enemy_id(self.account_2.id).account_id, self.account_1.id)
def test_1_process_success(self): self.assertEqual(SupervisorTask.objects.all().count(), 0) self.process_ability() self.assertEqual(SupervisorTask.objects.all().count(), 1) self.assertEqual(Battle1x1Prototype.get_by_id(self.battle.id).enemy_id, self.account_2.id) self.assertEqual(Battle1x1Prototype.get_by_enemy_id(self.account_2.id).account_id, self.account_1.id)
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_process_leave_queue_not_waiting_state(self): battle_1 = Battle1x1Prototype.create(self.account_1) battle_1.set_enemy(self.account_2) battle_1.save() self.assertTrue(Battle1x1Prototype.get_by_id(battle_1.id).state.is_PREPAIRING) self.worker.leave_arena_queue(self.hero_1.id) self.assertTrue(Battle1x1Prototype.get_by_id(battle_1.id).state.is_PREPAIRING)
def test_process_success_when_initiator_already_has_battle_object(self): self.pvp_balancer.add_to_arena_queue(self.hero_2.id) self.assertEqual(SupervisorTask.objects.all().count(), 0) self.assertTrue(self.accept_battle().is_PROCESSED) self.assertEqual(SupervisorTask.objects.all().count(), 1) self.assertEqual(Battle1x1Prototype.get_by_id(self.battle.id).enemy_id, self.account_2.id) self.assertEqual(Battle1x1Prototype.get_by_enemy_id(self.account_2.id).account_id, self.account_1.id)
def calls(self): battles = [ Battle1x1Prototype(battle_model) for battle_model in Battle1x1.objects.filter( state=BATTLE_1X1_STATE.WAITING) ] accounts_ids = [battle.account_id for battle in battles] current_battles = [ Battle1x1Prototype(battle_model) for battle_model in Battle1x1.objects.filter( state=BATTLE_1X1_STATE.PROCESSING) ] current_battle_pairs = set() for battle in current_battles: if battle.account_id < battle.enemy_id: battle_pair = (battle.account_id, battle.enemy_id) else: battle_pair = (battle.enemy_id, battle.account_id) current_battle_pairs.add(battle_pair) accounts_ids.append(battle.account_id) accounts_ids.append(battle.enemy_id) heroes = [ HeroPrototype(model=hero_model) for hero_model in Hero.objects.filter(account_id__in=accounts_ids) ] heroes = dict((hero.account_id, hero) for hero in heroes) ACCEPTED_LEVEL_MIN, ACCEPTED_LEVEL_MAX = None, None if self.own_hero is not None: ACCEPTED_LEVEL_MIN, ACCEPTED_LEVEL_MAX = accept_call_valid_levels( self.own_hero.level) return self.template( 'pvp/calls.html', { 'battles': battles, 'current_battle_pairs': current_battle_pairs, 'heroes': heroes, 'own_hero': self.own_hero, 'ACCEPTED_LEVEL_MAX': ACCEPTED_LEVEL_MAX, 'ACCEPTED_LEVEL_MIN': ACCEPTED_LEVEL_MIN, 'ABILITY_TYPE': ABILITY_TYPE })
def process_initialize(self, worker_id): if self.initialized: self.logger.warn('WARNING: pvp balancer already initialized, do reinitialization') self.initialized = True self.worker_id = worker_id Battle1x1Prototype.reset_waiting_battles() self.arena_queue = {} self.logger.info('PVP BALANCER INITIALIZED') environment.workers.supervisor.cmd_answer('initialize', self.worker_id)
def test_initiate_battle_without_rating_by_option(self): self.assertEqual(SupervisorTask.objects.all().count(), 0) self.worker._initiate_battle(self.battle_1_record(), self.battle_2_record(), calculate_ratings=False) battle_1 = Battle1x1Prototype.get_by_id(self.battle_1.id) battle_2 = Battle1x1Prototype.get_by_id(self.battle_2.id) self.assertEqual(battle_1.enemy_id, self.account_2.id) self.assertFalse(battle_1.calculate_rating) self.assertEqual(battle_2.enemy_id, self.account_1.id) self.assertFalse(battle_2.calculate_rating) self.assertEqual(SupervisorTask.objects.all().count(), 1)
def process_initialize(self, worker_id): if self.initialized: self.logger.warn("WARNING: pvp balancer already initialized, do reinitialization") self.initialized = True self.worker_id = worker_id Battle1x1Prototype.reset_waiting_battles() self.arena_queue = {} self.logger.info("PVP BALANCER INITIALIZED") environment.workers.supervisor.cmd_answer("initialize", self.worker_id)
def setUp(self): super(UsePvPAbilityTests, self).setUp() self.p1, self.p2, self.p3 = create_test_map() result, account_1_id, bundle_id = register_user('test_user', '*****@*****.**', '111111') result, account_2_id, bundle_id = register_user('test_user_2', '*****@*****.**', '111111') self.account_1 = AccountPrototype.get_by_id(account_1_id) self.account_2 = AccountPrototype.get_by_id(account_2_id) self.storage = LogicStorage() 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.battle = Battle1x1Prototype.create(self.account_1) self.battle.set_enemy(self.account_2) self.battle.save() self.ability = random.choice(ABILITIES.values()) self.task = UsePvPAbilityTask(battle_id=self.battle.id, account_id=self.account_1.id, ability_id=self.ability.TYPE)
def setUp(self): super(SayInBattleLogTests, self).setUp() self.p1, self.p2, self.p3 = create_test_map() result, account_1_id, bundle_id = register_user( 'test_user', '*****@*****.**', '111111') result, account_2_id, bundle_id = register_user( 'test_user_2', '*****@*****.**', '111111') self.account_1 = AccountPrototype.get_by_id(account_1_id) self.account_2 = AccountPrototype.get_by_id(account_2_id) self.storage = LogicStorage() 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.battle = Battle1x1Prototype.create(self.account_1) self.battle.set_enemy(self.account_2) self.battle.save() self.task = SayInBattleLogTask(battle_id=self.battle.id, text=u'some pvp message')
def setUp(self): super(UsePvPAbilityTests, self).setUp() self.p1, self.p2, self.p3 = 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(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.battle = Battle1x1Prototype.create(self.account_1) self.battle.set_enemy(self.account_2) self.battle.save() self.ability = random.choice(list(ABILITIES.values())) self.task = UsePvPAbilityTask(battle_id=self.battle.id, account_id=self.account_1.id, ability_id=self.ability.TYPE) self.meta_action_battle = meta_actions.ArenaPvP1x1.create(self.storage, self.hero_1, self.hero_2) self.meta_action_battle.set_storage(self.storage) actions_prototypes.ActionMetaProxyPrototype.create(hero=self.hero_1, _bundle_id=self.hero_1.actions.current_action.bundle_id, meta_action=self.meta_action_battle) actions_prototypes.ActionMetaProxyPrototype.create(hero=self.hero_2, _bundle_id=self.hero_1.actions.current_action.bundle_id, meta_action=self.meta_action_battle)
def process(self, main_task, storage): battle = Battle1x1Prototype.get_by_id(self.battle_id) if battle is None: # battle ended self.state = USE_PVP_ABILITY_TASK_STATE.BATTLE_FINISHED main_task.comment = 'battle finished' return POSTPONED_TASK_LOGIC_RESULT.ERROR hero = storage.accounts_to_heroes.get(self.account_id) enemy_hero = storage.accounts_to_heroes.get(battle.enemy_id) if hero is None: self.state = USE_PVP_ABILITY_TASK_STATE.HERO_NOT_FOUND main_task.comment = 'hero for account %d not found' % self.account_id return POSTPONED_TASK_LOGIC_RESULT.ERROR pvp_ability_class = ABILITIES.get(self.ability_id) if pvp_ability_class is None: self.state = USE_PVP_ABILITY_TASK_STATE.WRONG_ABILITY_ID main_task.comment = 'unknown ability id "%s"' % self.ability_id return POSTPONED_TASK_LOGIC_RESULT.ERROR pvp_ability = pvp_ability_class(hero=hero, enemy=enemy_hero) if not pvp_ability.has_resources: self.state = USE_PVP_ABILITY_TASK_STATE.NO_ENERGY main_task.comment = 'no resources for ability %s' % self.ability_id return POSTPONED_TASK_LOGIC_RESULT.ERROR pvp_ability.use() self.state = USE_PVP_ABILITY_TASK_STATE.PROCESSED return POSTPONED_TASK_LOGIC_RESULT.SUCCESS
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 setUp(self): super(UsePvPAbilityTests, self).setUp() self.p1, self.p2, self.p3 = create_test_map() result, account_1_id, bundle_id = register_user( 'test_user', '*****@*****.**', '111111') result, account_2_id, bundle_id = register_user( 'test_user_2', '*****@*****.**', '111111') self.account_1 = AccountPrototype.get_by_id(account_1_id) self.account_2 = AccountPrototype.get_by_id(account_2_id) self.storage = LogicStorage() 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.battle = Battle1x1Prototype.create(self.account_1) self.battle.set_enemy(self.account_2) self.battle.save() self.ability = random.choice(ABILITIES.values()) self.task = UsePvPAbilityTask(battle_id=self.battle.id, account_id=self.account_1.id, ability_id=self.ability.TYPE)
def form_game_info(account=None, is_own=False, client_turns=None): from the_tale.accounts.prototypes import AccountPrototype from the_tale.game.prototypes import GameState from the_tale.game.pvp.prototypes import Battle1x1Prototype game_time = TimePrototype.get_current_time() data = {'mode': 'pve', 'turn': game_time.ui_info(), 'game_state': GameState.state().value, 'map_version': map_info_storage.version, 'account': None, 'enemy': None } if account: battle = Battle1x1Prototype.get_by_account_id(account.id) data['account'] = _form_game_account_info(game_time, account, in_pvp_queue=False if battle is None else battle.state.is_WAITING, is_own=is_own, client_turns=client_turns) if battle and battle.state.is_PROCESSING: data['mode'] = 'pvp' data['enemy'] = _form_game_account_info(game_time, AccountPrototype.get_by_id(battle.enemy_id), in_pvp_queue=False, is_own=False, client_turns=client_turns) return data
def add_to_arena_queue(self, hero_id): hero = HeroPrototype.get_by_id(hero_id) if hero.account_id in self.arena_queue: return None battle = Battle1x1Prototype.create( AccountPrototype.get_by_id(hero.account_id)) if not battle.state.is_WAITING: raise PvPBalancerException( 'account %d already has battle not in waiting state' % hero.account_id) record = QueueRecord(account_id=battle.account_id, created_at=battle.created_at, battle_id=battle.id, hero_level=hero.level) if record.account_id in self.arena_queue: raise PvPBalancerException( 'account %d already added in balancer queue' % record.account_id) self.arena_queue[record.account_id] = record return battle
def game_page(context): battle = Battle1x1Prototype.get_by_account_id(context.account.id) if battle and battle.state.is_PROCESSING: return dext_views.Redirect(url('game:pvp:')) clan = None if context.account.clan_id is not None: clan = ClanPrototype.get_by_id(context.account.clan_id) cards = sorted(CARD.records, key=lambda r: (r.rarity.value, r.text)) return dext_views.Page('game/game_page.html', content={ 'map_settings': map_settings, 'game_settings': game_settings, 'EQUIPMENT_SLOT': EQUIPMENT_SLOT, 'current_map_version': map_info_storage.version, 'clan': clan, 'CARDS': cards, 'resource': context.resource, 'hero': context.account_hero, 'ABILITY_TYPE': ABILITY_TYPE })
def use(self, task, storage, pvp_balancer, **kwargs): if task.step.is_LOGIC: if not task.hero.can_participate_in_pvp: return task.logic_result( next_step=ComplexChangeTask.STEP.ERROR) task.hero.add_message('angel_ability_arena_pvp_1x1', hero=task.hero, energy=self.TYPE.cost) task.hero.update_habits(HABIT_CHANGE_SOURCE.ARENA_SEND) task.hero.process_removed_artifacts() return task.logic_result( next_step=ComplexChangeTask.STEP.PVP_BALANCER) elif task.step.is_PVP_BALANCER: battle = Battle1x1Prototype.get_by_account_id( task.data['account_id']) if battle is None: pvp_balancer.add_to_arena_queue(task.data['hero_id']) return task.logic_result()
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_1_force_battle(self): self.assertEqual(SupervisorTask.objects.all().count(), 0) self.assertEqual(len(self.worker.arena_queue), 2) self.worker.force_battle(self.account_1.id, self.account_2.id) battle_1 = Battle1x1Prototype.get_by_id(self.battle_1.id) battle_2 = Battle1x1Prototype.get_by_id(self.battle_2.id) self.assertEqual(battle_1.enemy_id, self.account_2.id) self.assertFalse(battle_1.calculate_rating) self.assertEqual(battle_2.enemy_id, self.account_1.id) self.assertFalse(battle_2.calculate_rating) self.assertEqual(self.worker.arena_queue, {}) self.assertEqual(SupervisorTask.objects.all().count(), 1)
def process_arena_pvp_1x1(self, bundle_id): # pylint: disable=R0914 from the_tale.accounts.prototypes import AccountPrototype from the_tale.game.actions.prototypes import ActionMetaProxyPrototype from the_tale.game.actions import meta_actions from the_tale.game.logic_storage import LogicStorage from the_tale.game.pvp.prototypes import Battle1x1Prototype from the_tale.game.pvp.models import BATTLE_1X1_STATE storage = LogicStorage() account_1_id, account_2_id = list(self.members) account_1 = AccountPrototype.get_by_id(account_1_id) account_2 = AccountPrototype.get_by_id(account_2_id) storage.load_account_data(account_1) storage.load_account_data(account_2) hero_1 = storage.accounts_to_heroes[account_1_id] hero_2 = storage.accounts_to_heroes[account_2_id] old_bundle_1_id = hero_1.actions.current_action.bundle_id old_bundle_2_id = hero_2.actions.current_action.bundle_id meta_action_battle = meta_actions.ArenaPvP1x1.create( storage, hero_1, hero_2) ActionMetaProxyPrototype.create(hero=hero_1, _bundle_id=bundle_id, meta_action=meta_action_battle) ActionMetaProxyPrototype.create(hero=hero_2, _bundle_id=bundle_id, meta_action=meta_action_battle) storage.merge_bundles([old_bundle_1_id, old_bundle_2_id], bundle_id) storage.save_bundle_data(bundle_id) battle_1 = Battle1x1Prototype.get_by_account_id(account_1_id) battle_1.state = BATTLE_1X1_STATE.PROCESSING battle_1.save() battle_2 = Battle1x1Prototype.get_by_account_id(account_2_id) battle_2.state = BATTLE_1X1_STATE.PROCESSING battle_2.save()
def setUp(self): super(BalancerTestsBase, self).setUp() self.p1, self.p2, self.p3 = create_test_map() self.account_1 = self.accounts_factory.create_account() self.account_2 = self.accounts_factory.create_account() self.hero_1 = heroes_logic.load_hero(account_id=self.account_1.id) self.hero_2 = heroes_logic.load_hero(account_id=self.account_2.id) environment.deinitialize() environment.initialize() Battle1x1Prototype.create(self.account_1) self.worker = environment.workers.pvp_balancer self.worker.process_initialize('pvp_balancer')
def pvp_create_battle(self, account, enemy, state=None, calculate_rating=False): battle = Battle1x1Prototype.create(account) if enemy: battle.set_enemy(enemy) if state is not None: battle.state = state battle.calculate_rating = calculate_rating battle.save() return battle
def test_initiate_battle_without_rating_by_level(self): self.assertEqual(SupervisorTask.objects.all().count(), 0) self.hero_1.level = 100 heroes_logic.save_hero(self.hero_1) self.worker._initiate_battle(self.battle_1_record(), self.battle_2_record()) battle_1 = Battle1x1Prototype.get_by_id(self.battle_1.id) battle_2 = Battle1x1Prototype.get_by_id(self.battle_2.id) self.assertEqual(battle_1.enemy_id, self.account_2.id) self.assertFalse(battle_1.calculate_rating) self.assertEqual(battle_2.enemy_id, self.account_1.id) self.assertFalse(battle_2.calculate_rating) self.assertEqual(SupervisorTask.objects.all().count(), 1)
def test_initiate_battle_without_rating_by_level(self): self.assertEqual(SupervisorTask.objects.all().count(), 0) self.hero_1._model.level = 100 self.hero_1.save() self.worker._initiate_battle(self.battle_1_record(), self.battle_2_record()) battle_1 = Battle1x1Prototype.get_by_id(self.battle_1.id) battle_2 = Battle1x1Prototype.get_by_id(self.battle_2.id) self.assertEqual(battle_1.enemy_id, self.account_2.id) self.assertFalse(battle_1.calculate_rating) self.assertEqual(battle_2.enemy_id, self.account_1.id) self.assertFalse(battle_2.calculate_rating) self.assertEqual(SupervisorTask.objects.all().count(), 1)
def process_battle_ending(self): battle_1 = Battle1x1Prototype.get_by_account_id(self.hero_1.account_id) battle_2 = Battle1x1Prototype.get_by_account_id(self.hero_2.account_id) participant_1 = AccountPrototype.get_by_id(self.hero_1.account_id) participant_2 = AccountPrototype.get_by_id(self.hero_2.account_id) if self.hero_1.health <= 0: if self.hero_2.health <= 0: Battle1x1ResultPrototype.create(participant_1=participant_1, participant_2=participant_2, result=BATTLE_1X1_RESULT.DRAW) if battle_1.calculate_rating and battle_2.calculate_rating: self.hero_1.statistics.change_pvp_battles_1x1_draws(1) self.hero_2.statistics.change_pvp_battles_1x1_draws(1) else: Battle1x1ResultPrototype.create( participant_1=participant_1, participant_2=participant_2, result=BATTLE_1X1_RESULT.DEFEAT) if battle_1.calculate_rating and battle_2.calculate_rating: self.hero_2.statistics.change_pvp_battles_1x1_victories(1) self.hero_1.statistics.change_pvp_battles_1x1_defeats(1) else: Battle1x1ResultPrototype.create(participant_1=participant_1, participant_2=participant_2, result=BATTLE_1X1_RESULT.VICTORY) if battle_1.calculate_rating and battle_2.calculate_rating: self.hero_1.statistics.change_pvp_battles_1x1_victories(1) self.hero_2.statistics.change_pvp_battles_1x1_defeats(1) battle_1.remove() battle_2.remove() self.hero_1.health = self.hero_1_old_health self.hero_2.health = self.hero_2_old_health self.reset_hero_info(self.hero_1) self.reset_hero_info(self.hero_2) self.state = self.STATE.PROCESSED
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_process_leave_queue_waiting_state(self): battle = self.worker.add_to_arena_queue(self.hero_1.id) self.assertEqual(len(self.worker.arena_queue), 1) self.worker.leave_arena_queue(self.hero_1.id) self.assertEqual(len(self.worker.arena_queue), 0) self.assertEqual(Battle1x1Prototype.get_by_id(battle.id), None)
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_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 setUp(self): super(BalancerTestsBase, self).setUp() self.p1, self.p2, self.p3 = create_test_map() result, account_1_id, bundle_id = register_user('test_user', '*****@*****.**', '111111') result, account_2_id, bundle_id = register_user('test_user_2', '*****@*****.**', '111111') self.account_1 = AccountPrototype.get_by_id(account_1_id) self.account_2 = AccountPrototype.get_by_id(account_2_id) self.hero_1 = HeroPrototype.get_by_account_id(account_1_id) self.hero_2 = HeroPrototype.get_by_account_id(account_2_id) environment.deinitialize() environment.initialize() Battle1x1Prototype.create(self.account_1) self.worker = environment.workers.pvp_balancer self.worker.process_initialize('pvp_balancer')
def process_arena_pvp_1x1(self, bundle_id): # pylint: disable=R0914 from the_tale.accounts.prototypes import AccountPrototype from the_tale.game.actions.prototypes import ActionMetaProxyPrototype from the_tale.game.actions import meta_actions from the_tale.game.logic_storage import LogicStorage from the_tale.game.pvp.prototypes import Battle1x1Prototype from the_tale.game.pvp.models import BATTLE_1X1_STATE storage = LogicStorage() account_1_id, account_2_id = list(self.members) account_1 = AccountPrototype.get_by_id(account_1_id) account_2 = AccountPrototype.get_by_id(account_2_id) storage.load_account_data(account_1) storage.load_account_data(account_2) hero_1 = storage.accounts_to_heroes[account_1_id] hero_2 = storage.accounts_to_heroes[account_2_id] old_bundle_1_id = hero_1.actions.current_action.bundle_id old_bundle_2_id = hero_2.actions.current_action.bundle_id meta_action_battle = meta_actions.ArenaPvP1x1.create(storage, hero_1, hero_2) ActionMetaProxyPrototype.create(hero=hero_1, _bundle_id=bundle_id, meta_action=meta_action_battle) ActionMetaProxyPrototype.create(hero=hero_2, _bundle_id=bundle_id, meta_action=meta_action_battle) storage.merge_bundles([old_bundle_1_id, old_bundle_2_id], bundle_id) storage.save_bundle_data(bundle_id) battle_1 = Battle1x1Prototype.get_by_account_id(account_1_id) battle_1.state = BATTLE_1X1_STATE.PROCESSING battle_1.save() battle_2 = Battle1x1Prototype.get_by_account_id(account_2_id) battle_2.state = BATTLE_1X1_STATE.PROCESSING battle_2.save()
def use_ability(self, ability): battle = Battle1x1Prototype.get_by_account_id(self.account.id) if battle is None or not battle.state.is_PROCESSING: return self.json_error("pvp.use_ability.no_battle", u"Бой не идёт, вы не можете использовать способность") use_ability_task = UsePvPAbilityTask(battle_id=battle.id, account_id=self.account.id, ability_id=ability.TYPE) task = PostponedTaskPrototype.create(use_ability_task) environment.workers.supervisor.cmd_logic_task(self.account.id, task.id) return self.json_processing(task.status_url)
def leave_arena_queue(self, hero_id): hero = heroes_logic.load_hero(hero_id=hero_id) battle = Battle1x1Prototype.get_by_account_id(hero.account_id) if not battle.state.is_WAITING: return if battle.account_id not in self.arena_queue: self.logger.error("can not leave queue: battle %d in waiting state but no in arena queue" % battle.id) return del self.arena_queue[battle.account_id] battle.remove()
def use(self, task, storage, **kwargs): # pylint: disable=R0911 battle = Battle1x1Prototype.get_by_account_id(task.hero.account_id) if battle and not battle.state.is_WAITING: return task.logic_result(next_step=ComplexChangeTask.STEP.ERROR) if not task.hero.can_be_helped(): return task.logic_result(next_step=ComplexChangeTask.STEP.ERROR) task.hero.on_help() action = task.hero.actions.current_action choice = action.get_help_choice() if choice is None: return task.logic_result(next_step=ComplexChangeTask.STEP.ERROR) if action.HABIT_MODE.is_AGGRESSIVE: task.hero.update_habits(HABIT_CHANGE_SOURCE.HELP_AGGRESSIVE) elif action.HABIT_MODE.is_PEACEFUL: task.hero.update_habits(HABIT_CHANGE_SOURCE.HELP_UNAGGRESSIVE) elif action.HABIT_MODE.is_COMPANION: if task.hero.companion: for habit_source in task.hero.companion.modify_attribute(heroes_relations.MODIFIERS.HABITS_SOURCES, set()): task.hero.update_habits(habit_source, multuplier=task.hero.companion_habits_multiplier) else: raise exceptions.UnknownHabitModeError(mode=action.HABIT_MODE) critical = random.uniform(0, 1) < task.hero.might_crit_chance result = self._use(task, choice, action, task.hero, critical) if result[0].is_SUCCESSED: task.hero.statistics.change_help_count(1) if task.hero.actions.current_action.state == task.hero.actions.current_action.STATE.PROCESSED: storage.process_turn__single_hero(hero=task.hero, logger=None, continue_steps_if_needed=True) task.hero.cards.change_help_count(1) self.process_removed_artifacts(task.hero) return result