示例#1
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)
示例#2
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)
示例#3
0
    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, ))
示例#4
0
    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,))
示例#5
0
    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)
示例#6
0
    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)
示例#7
0
    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)
示例#8
0
    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)
示例#9
0
    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)
示例#10
0
    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)
示例#11
0
    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)
示例#12
0
    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
示例#13
0
    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)
示例#14
0
    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
示例#15
0
    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
示例#16
0
    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)
示例#17
0
    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)
示例#18
0
    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_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_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)
示例#21
0
    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)
示例#22
0
    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)
示例#23
0
    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)
示例#24
0
    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)
示例#25
0
    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_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)
示例#27
0
    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)
示例#28
0
    def process(self, main_task, storage):

        battle = Battle1x1Prototype.get_by_id(self.battle_id)

        if battle is None: # battle ended, for example
            self.state = SAY_IN_HERO_LOG_TASK_STATE.BATTLE_NOT_FOUND
            main_task.comment = 'battle %d not found' % self.battle_id
            return POSTPONED_TASK_LOGIC_RESULT.ERROR

        account_hero = storage.accounts_to_heroes.get(battle.account_id)
        enemy_hero = storage.accounts_to_heroes.get(battle.enemy_id)

        if account_hero is None:
            self.state = SAY_IN_HERO_LOG_TASK_STATE.ACCOUNT_HERO_NOT_FOUND
            main_task.comment = 'hero for account %d not found' % battle.account_id
            return POSTPONED_TASK_LOGIC_RESULT.ERROR

        account_hero.add_message('pvp_say', text=self.text)

        if enemy_hero is not None:
            enemy_hero.add_message('pvp_say', text=self.text)

        self.state = SAY_IN_HERO_LOG_TASK_STATE.PROCESSED
        return POSTPONED_TASK_LOGIC_RESULT.SUCCESS