Пример #1
0
 def use(self, task, storage, **kwargs):  # pylint: disable=R0911,W0613
     tt_api_energy.change_energy_balance(account_id=task.hero.account_id,
                                         type='card',
                                         energy=int(self.modificator),
                                         async=True,
                                         autocommit=True)
     return task.logic_result()
Пример #2
0
def register_user(nick,
                  email=None,
                  password=None,
                  referer=None,
                  referral_of_id=None,
                  action_id=None,
                  is_bot=False,
                  gender=game_relations.GENDER.MALE,
                  full_create=True):
    from the_tale.game import tt_api_energy
    from the_tale.game.heroes import logic as heroes_logic
    from the_tale.game.balance import constants as c

    if Account.objects.filter(nick=nick).exists():
        return REGISTER_USER_RESULT.DUPLICATE_USERNAME, None, None

    if email and Account.objects.filter(email=email).exists():
        return REGISTER_USER_RESULT.DUPLICATE_EMAIL, None, None

    try:
        referral_of = AccountPrototype.get_by_id(referral_of_id)
    except ValueError:
        referral_of = None

    if (email and not password) or (not email and password):
        raise exceptions.EmailAndPasswordError()

    is_fast = not (email and password)

    if is_fast and is_bot:
        raise exceptions.BotIsFastError()

    if password is None:
        password = accounts_settings.FAST_REGISTRATION_USER_PASSWORD

    account = AccountPrototype.create(nick=nick,
                                      email=email,
                                      is_fast=is_fast,
                                      is_bot=is_bot,
                                      password=password,
                                      referer=referer,
                                      referral_of=referral_of,
                                      action_id=action_id,
                                      gender=gender)

    AccountAchievementsPrototype.create(account)
    AccountItemsPrototype.create(account)

    hero = heroes_logic.create_hero(account=account, full_create=full_create)

    if full_create:
        tt_api_energy.change_energy_balance(account_id=account.id,
                                            type='initial_contribution',
                                            energy=c.INITIAL_ENERGY_AMOUNT,
                                            async=False,
                                            autocommit=True)

        tt_api.create_cards_timer(account_id=account.id)

    return REGISTER_USER_RESULT.OK, account.id, hero.actions.current_action.bundle_id
Пример #3
0
    def test_activate__no_energy(self):
        energy = tt_api_energy.energy_balance(self.account.id)
        tt_api_energy.change_energy_balance(account_id=self.account.id,
                                            type='test',
                                            energy=-energy,
                                            autocommit=True)

        task = self.ability.activate(self.hero, self.task_data)
        self.assertEqual(task, None)
Пример #4
0
    def job_energy(self, place_id, person_id, message_type, job_power):
        energy = max(1, int(math.ceil(c.ANGEL_ENERGY_IN_DAY * job_power * c.NORMAL_JOB_LENGTH * c.JOB_HERO_REWARD_FRACTION)))

        tt_api_energy.change_energy_balance(account_id=self.account_id,
                                            type='job_energy',
                                            energy=energy,
                                            async=True,
                                            autocommit=True)

        self.add_message(message_type, diary=True, hero=self, energy=energy, **self.get_job_variables(place_id, person_id))
Пример #5
0
    def test_activate_and_complete__zero_cost(self):
        energy = tt_api_energy.energy_balance(self.account.id)
        tt_api_energy.change_energy_balance(account_id=self.account.id,
                                            type='test',
                                            energy=-energy,
                                            autocommit=True)

        task = self.ability.activate(self.hero, self.task_data)
        self.assertEqual(task.internal_logic.data['transaction_id'], None)

        task.process(FakePostpondTaskPrototype(), storage=self.storage)

        self.assertTrue(task.state.is_processed)

        self.assertEqual(tt_api_energy.energy_balance(self.account.id), 0)
Пример #6
0
    def give_energy_on_reward(self):
        if not self.hero.can_regenerate_energy:
            return

        energy = sum(person.attrs.on_profite_energy
                     for person in self.positive_results_persons())

        if energy == 0:
            return

        tt_api_energy.change_energy_balance(account_id=self.hero.account_id,
                                            type='for_quest',
                                            energy=energy,
                                            async=True,
                                            autocommit=True)
Пример #7
0
    def activate(self, hero, data):
        from the_tale.game.abilities.postponed_tasks import UseAbilityTask

        data['transaction_id'] = None

        if self.TYPE.cost > 0:

            status, transaction_id = tt_api_energy.change_energy_balance(
                account_id=hero.account_id,
                type='help-{}'.format(self.TYPE.value),
                energy=-self.TYPE.cost,
                async=False,
                autocommit=False)

            if not status:
                return None

            data['transaction_id'] = transaction_id

        data['hero_id'] = hero.id
        data['account_id'] = hero.account_id

        ability_task = UseAbilityTask(processor_id=self.TYPE.value,
                                      hero_id=hero.id,
                                      data=data)

        task = PostponedTaskPrototype.create(ability_task)

        environment.workers.supervisor.cmd_logic_task(hero.account_id, task.id)

        return task
Пример #8
0
    def test_process_success__has_transaction(self):
        energy = tt_api_energy.energy_balance(self.account.id)

        status, transaction_id = tt_api_energy.change_energy_balance(
            account_id=self.account.id, type='test', energy=1
        )  # test, that changes will be applied on commit (but not on start)
        self.task.data['transaction_id'] = transaction_id

        self.assertEqual(tt_api_energy.energy_balance(self.account.id), energy)

        self.assertEqual(
            self.task.process(FakePostpondTaskPrototype(), self.storage),
            POSTPONED_TASK_LOGIC_RESULT.SUCCESS)
        self.assertEqual(self.task.state, ComplexChangeTask.STATE.PROCESSED)

        time.sleep(0.1)

        self.assertEqual(tt_api_energy.energy_balance(self.account.id),
                         energy + 1)