def process(self): from the_tale.accounts.personal_messages import logic as pm_logic from the_tale.accounts import logic accounts_ids = AccountPrototype.live_query().filter( is_fast=False, created_at__lt=datetime.datetime.now() - accounts_settings.RANDOM_PREMIUM_CREATED_AT_BARRIER, active_end_at__gt=datetime.datetime.now(), premium_end_at__lt=datetime.datetime.now()).exclude( id=self.initiator_id).values_list('id', flat=True) if not accounts_ids: return False account = AccountPrototype.get_by_id(random.choice(accounts_ids)) with transaction.atomic(): account.prolong_premium(self.days) account.save() pm_logic.send_message(logic.get_system_user_id(), [account.id], self.MESSAGE % {'days': self.days}, async=True) self.receiver_id = account.id self.state = relations.RANDOM_PREMIUM_REQUEST_STATE.PROCESSED self.save() return True
def setUp(self): super(BuyMarketLotPosponedTaskTests, self).setUp() self.seller_account = self.accounts_factory.create_account() tt_api.debug_clear_service() cards_tt_api.debug_clear_service() self.card = cards.CARD.LEVEL_UP.effect.create_card( available_for_auction=True, type=cards.CARD.LEVEL_UP) cards_tt_api.change_cards(accounts_logic.get_system_user_id(), operation_type='#test', to_add=[self.card]) self.lot = objects.Lot(owner_id=self.seller_account.id, full_type=self.card.item_full_type, item_id=self.card.uid, price=self.amount) tt_api.place_sell_lots([self.lot]) self.task = logic.close_lot(item_type=self.lot.full_type, price=self.amount, buyer_id=self.account.id) self.invoice = bank_prototypes.InvoicePrototype.get_by_id( self.task.transaction.invoice_id) self.market_basic_information = tt_api.info() self.cmd_update_with_account_data__call_count = 0 # no need in updating hero state self.with_referrals = False # no money for referrals pm_tt_api.debug_clear_service()
def remove_old_system_messages(): tt_api.sync_request( url=conf.settings.TT_REMOVE_OLD_MESSAGES_URL, data=personal_messages_pb2.RemoveOldMessagesRequest( accounts_ids=[accounts_logic.get_system_user_id()], barrier=conf.settings.SYSTEM_MESSAGES_LEAVE_TIME.total_seconds()), AnswerType=personal_messages_pb2.RemoveOldMessagesResponse)
def test_remove_lot(self): self.invoice.state = bank_relations.INVOICE_STATE.FROZEN self.invoice.save() self.assertEqual(self.task.process(self.main_task), POSTPONED_TASK_LOGIC_RESULT.CONTINUE) self.assertEqual(self.task.process(self.main_task), POSTPONED_TASK_LOGIC_RESULT.CONTINUE) self.assertEqual(self.task.process(self.main_task, storage=self.logic_storage), POSTPONED_TASK_LOGIC_RESULT.CONTINUE) with mock.patch('the_tale.finances.bank.transaction.Transaction.confirm') as confirm: with self.check_delta(bank_prototypes.InvoicePrototype._model_class.objects.count, 1): with self.check_new_message(self.account_1.id, [accounts_logic.get_system_user_id()]): self.assertEqual(self.task.process(self.main_task), POSTPONED_TASK_LOGIC_RESULT.SUCCESS) self.assertEqual(confirm.call_count, 1) self.assertTrue(self.task.state.is_PROCESSED) self.assertTrue(self.task.step.is_SUCCESS) lot = logic.load_lot(self.task.lot_id) self.assertTrue(lot.state.is_CLOSED_BY_BUYER) self.assertTrue(lot.closed_at < datetime.datetime.now()) self.assertEqual(lot.buyer_id, self.account_2.id) commission_ivoice = bank_prototypes.InvoicePrototype._db_latest() self.assertTrue(commission_ivoice.recipient_type.is_GAME_ACCOUNT) self.assertEqual(commission_ivoice.recipient_id, lot.seller_id) self.assertTrue(commission_ivoice.sender_type.is_GAME_LOGIC) self.assertEqual(commission_ivoice.sender_id, 0) self.assertTrue(commission_ivoice.currency.is_PREMIUM) self.assertEqual(commission_ivoice.amount, -lot.commission) self.assertEqual(commission_ivoice.operation_uid, 'market-buy-commission-%s' % lot.type) self.assertTrue(commission_ivoice.state.is_FORCED)
def give_reward_for_template(template): if template.author_id is None: print(1) return updated = prototypes.ContributionPrototype._db_filter( type=relations.CONTRIBUTION_TYPE.TEMPLATE, entity_id=template.id, account=template.author_id, reward_given=False).update(reward_given=True) if not updated: return cards_logic.give_new_cards( account_id=template.author_id, operation_type='give-card-for-linguistic-template', allow_premium_cards=True, available_for_auction=True) message = '''Поздравляем! Ваша [url={template}]фраза[/url] добавлена в игру!\n\nВ награду вы можете получить дополнительную карту судьбы (на странице игры). Карту можно будет продать на рынке.''' message = message.format( template=full_url('http', 'linguistics:templates:show', template.id)) pm_tt_api.send_message(sender_id=accounts_logic.get_system_user_id(), recipients_ids=[template.author_id], body=message, async=False)
def create_sell_lot(context): if not all(card.available_for_auction for card in context.cards): raise dext_views.ViewError(code='not_available_for_auction', message='Как минимум одна из карт не может быть продана на аукционе') for card in context.cards: if context.price < relations.CARDS_MIN_PRICES[card.type.rarity]: raise dext_views.ViewError(code='too_small_price', message='Цена продажи меньше чем минимально разрешённая цена продажи как минимум одной карты') lots = [] for card in context.cards: lots.append(objects.Lot(owner_id=context.account.id, full_type=card.item_full_type, item_id=card.uid, price=context.price)) cards_tt_api.change_cards_owner(old_owner_id=context.account.id, new_owner_id=accounts_logic.get_system_user_id(), operation_type='#create_sell_lots', new_storage=cards_relations.STORAGE.FAST, cards_ids=[card.uid for card in context.cards]) tt_api.place_sell_lots(lots) return dext_views.AjaxOk()
def give_reward_for_template(template): from the_tale.accounts import logic as accounts_logic from the_tale.accounts.personal_messages import logic as personal_messages_logic from the_tale.game.cards import logic as cards_logic if template.author_id is None: return updated = prototypes.ContributionPrototype._db_filter(type=relations.CONTRIBUTION_TYPE.TEMPLATE, entity_id=template.id, account=template.author_id, reward_given=False).update(reward_given=True) if not updated: return cards_number = conf.settings.SPECIAL_CARDS_REWARDS.get(template.key.name.upper(), conf.settings.DEFAULT_CARDS_REWARDS) cards_logic.give_new_cards(account_id=template.author_id, operation_type='give-card-for-linguistic-template', allow_premium_cards=True, available_for_auction=True, number=cards_number) message = '''Поздравляем! Ваша [url={template}]фраза[/url] добавлена в игру!\n\nВ награду вы можете получить дополнительные карты судьбы (на странице игры, в количестве {cards_number} шт.). Карты можно будет продать на рынке.''' message = message.format(template=utils_urls.full_url('https', 'linguistics:templates:show', template.id), cards_number=cards_number) personal_messages_logic.send_message(sender_id=accounts_logic.get_system_user_id(), recipients_ids=[template.author_id], body=message, asynchronous=False)
def give_reward_for_template(template): from the_tale.accounts import logic as accounts_logic from the_tale.accounts.personal_messages import logic as personal_messages_logic from the_tale.game.cards import logic as cards_logic if template.author_id is None: return updated = prototypes.ContributionPrototype._db_filter(type=relations.CONTRIBUTION_TYPE.TEMPLATE, entity_id=template.id, account=template.author_id, reward_given=False).update(reward_given=True) if not updated: return cards_number = conf.settings.SPECIAL_CARDS_REWARDS.get(template.key.name.upper(), conf.settings.DEFAULT_CARDS_REWARDS) cards_logic.give_new_cards(account_id=template.author_id, operation_type='give-card-for-linguistic-template', allow_premium_cards=True, available_for_auction=True, number=cards_number) message = '''Поздравляем! Ваша [url={template}]фраза[/url] добавлена в игру!\n\nВ награду вы можете получить дополнительные карты судьбы (на странице игры, в количестве {cards_number} шт.). Карты можно будет продать на рынке.''' message = message.format(template=dext_urls.full_url('https', 'linguistics:templates:show', template.id), cards_number=cards_number) personal_messages_logic.send_message(sender_id=accounts_logic.get_system_user_id(), recipients_ids=[template.author_id], body=message, async=False)
def test_remove_friendship__his_request_exists(self): with self.check_new_message(self.account_2.id, [accounts_logic.get_system_user_id()]): with self.check_new_message(self.account_1.id, [self.account_2.id]): FriendshipPrototype.request_friendship(self.account_2, self.account_1, 'text 1') FriendshipPrototype.remove_friendship(self.account_1, self.account_2) self.assertEqual(Friendship.objects.all().count(), 0)
def on_process_transaction_frozen(self, **kwargs): lots = tt_api.close_lot(item_type=self.item_type, price=self.price, buyer_id=self.account_id) if not lots: self.custom_error = 'Не удалось купить карту: только что её купил другой игрок.' return False cards_tt_api.change_cards_owner(old_owner_id=accounts_logic.get_system_user_id(), new_owner_id=self.account_id, operation_type='#close_sell_lots', new_storage=cards_relations.STORAGE.FAST, cards_ids=[lot.item_id for lot in lots]) cards_info = cards_logic.get_cards_info_by_full_types() lot = lots[0] lot_name = cards_info[lot.full_type]['name'] # change receiver to lot owner invoice = self.transaction.get_invoice() invoice._model.recipient_type = bank_relations.ENTITY_TYPE.GAME_ACCOUNT invoice._model.recipient_id = lot.owner_id invoice._model.save() bank_prototypes.InvoicePrototype.create(recipient_type=bank_relations.ENTITY_TYPE.GAME_ACCOUNT, recipient_id=lot.owner_id, sender_type=bank_relations.ENTITY_TYPE.GAME_LOGIC, sender_id=0, currency=bank_relations.CURRENCY_TYPE.PREMIUM, amount=-logic.get_commission(self.price), description_for_sender='Комиссия с продажи «{}»'.format(lot_name), description_for_recipient='Комиссия с продажи «{}»'.format(lot_name), operation_uid='{}-cards-hero-good'.format(conf.payments_settings.MARKET_COMMISSION_OPERATION_UID, lot.full_type), force=True) pm_tt_api.send_message(sender_id=accounts_logic.get_system_user_id(), recipients_ids=[lot.owner_id], body=good_bought_message(name=lot_name, price=self.price - logic.get_commission(self.price)), async=True) return True
def test_remove_friendship__own_request_exists(self): with self.check_new_message( self.account_2.id, [accounts_logic.get_system_user_id(), self.account_1.id], number=2): FriendshipPrototype.request_friendship(self.account_1, self.account_2, 'text 1') FriendshipPrototype.remove_friendship(self.account_1, self.account_2)
def test_request_friendship__his_request_exists(self): with self.check_new_message(self.account_2.id, [accounts_logic.get_system_user_id()]): with self.check_new_message(self.account_1.id, [self.account_2.id]): his_request = FriendshipPrototype.request_friendship(self.account_2, self.account_1, 'text 1') own_request = FriendshipPrototype.request_friendship(self.account_1, self.account_2, 'text 2') self.assertEqual(his_request.id, own_request.id) self.assertTrue(own_request.is_confirmed) self.assertEqual(own_request.text_html, 'text 1') self.assertEqual(Friendship.objects.all().count(), 1)
def process(self, main_task, storage=None): # pylint: disable=R0911 if self.step.is_FREEZE_LOT: lot = logic.load_lot(self.lot_id) if not lot.state.is_ACTIVE: main_task.comment = 'lot is not active, real state is: %s' % lot.state.name self.state = self.STATE.WRONG_LOT_STATE return POSTPONED_TASK_LOGIC_RESULT.ERROR self.account_id = lot.seller_id self.good_type = goods_types.get_type(lot.type) self.good = lot.good lot.state = relations.LOT_STATE.FROZEN logic.save_lot(lot) main_task.extend_postsave_actions((lambda: environment.workers.supervisor.cmd_logic_task(self.account_id, main_task.id),)) self.step = self.STEP.RETURN_GOOD return POSTPONED_TASK_LOGIC_RESULT.CONTINUE if self.step.is_RETURN_GOOD: hero = storage.accounts_to_heroes[self.account_id] # TODO: save hero after receive item? and after extract too?... self.good_type.insert_good(hero, self.good) storage.save_bundle_data(hero.actions.current_action.bundle_id) main_task.extend_postsave_actions((lambda: environment.workers.market_manager.cmd_logic_task(self.account_id, main_task.id),)) self.step = self.STEP.CLOSE_LOT return POSTPONED_TASK_LOGIC_RESULT.CONTINUE if self.step.is_CLOSE_LOT: lot = logic.load_lot(self.lot_id) lot.state = relations.LOT_STATE.CLOSED_BY_TIMEOUT lot.closed_at = datetime.datetime.now() logic.save_lot(lot) seller = account_prototypes.AccountPrototype.get_by_id(lot.seller_id) pm_logic.send_message(sender_id=accounts_logic.get_system_user_id(), recipients_ids=[seller.id], body=good_timeout_message(lot), async=True) self.state = self.STATE.PROCESSED self.step = self.STEP.SUCCESS return POSTPONED_TASK_LOGIC_RESULT.SUCCESS
def _confirm(self): self._model.is_confirmed = True self.save() account_link = '[url={}]{}[/url]'.format( full_url('http', 'accounts:show', self.friend_2.id), self.friend_2.nick_verbose) message = 'игрок {account_link} подтвердил, что вы являетесь друзьями'.format( account_link=account_link) pm_tt_api.send_message(sender_id=accounts_logic.get_system_user_id(), recipients_ids=[self.friend_1.id], body=message)
def test_close_lot(self): self.assertEqual(self.task.process(self.main_task), POSTPONED_TASK_LOGIC_RESULT.CONTINUE) self.assertEqual(self.task.process(self.main_task, storage=self.logic_storage), POSTPONED_TASK_LOGIC_RESULT.CONTINUE) with self.check_new_message(self.account_1.id, [accounts_logic.get_system_user_id()]): self.assertEqual(self.task.process(self.main_task), POSTPONED_TASK_LOGIC_RESULT.SUCCESS) self.assertTrue(self.task.state.is_PROCESSED) self.assertTrue(self.task.step.is_SUCCESS) lot = logic.load_lot(self.task.lot_id) self.assertTrue(lot.state.is_CLOSED_BY_TIMEOUT) self.assertTrue(lot.closed_at < datetime.datetime.now()) self.assertEqual(lot.buyer_id, None)
def test_add_achievements(self): GiveAchievementTaskPrototype.create( account_id=self.account_1.id, achievement_id=self.achievement_3.id) self.assertFalse( self.account_achievements_1.has_achievement(self.achievement_3)) with self.check_new_message(self.account_1.id, [accounts_logic.get_system_user_id()]): self.worker.add_achievements() self.account_achievements_1.reload() self.assertTrue( self.account_achievements_1.has_achievement(self.achievement_3)) self.assertEqual(GiveAchievementTaskPrototype._db_count(), 0)
def notify_about_premium_expiration(self): from the_tale.accounts.personal_messages import tt_api as pm_tt_api from the_tale.accounts import logic current_time = datetime.datetime.now() message = ''' До окончания подписки осталось: %(verbose_timedelta)s. Вы можете продлить подписку на странице нашего %(shop_link)s. ''' % {'verbose_timedelta': verbose_timedelta(self.premium_end_at - current_time), 'shop_link': '[url="%s"]магазина[/url]' % full_url('https', 'shop:')} pm_tt_api.send_message(logic.get_system_user_id(), [self.id], message, async=True)
def cancel_sell_lot(context): lots = tt_api.cancel_lot(item_type=context.item_type, price=context.price, owner_id=context.account.id) if not lots: return dext_views.AjaxOk() cards_tt_api.change_cards_owner(old_owner_id=accounts_logic.get_system_user_id(), new_owner_id=context.account.id, operation_type='#cancel_sell_lots', new_storage=cards_relations.STORAGE.FAST, cards_ids=[lot.item_id for lot in lots]) return dext_views.AjaxOk()
def increment_level(self, send_message=False): self.level += 1 self.add_message('hero_common_journal_level_up', hero=self, level=self.level) self.force_save_required = True if send_message: # TODO: move out logic pm_tt_api.send_message( sender_id=accounts_logic.get_system_user_id(), recipients_ids=[self.account_id], body='Поздравляем, Ваш герой получил {} уровень!'.format( self.level), async=True)
def test_process__has_active_accounts(self): AccountPrototype._db_all().update( active_end_at=datetime.datetime.now() + datetime.timedelta(days=1)) with self.check_new_message(self.account_2.id, [logic.get_system_user_id()]): self.request.process() self.assertEqual( list( AccountPrototype._db_filter( premium_end_at__gt=datetime.datetime.now()).values_list( 'id', flat=True)), [self.account_2.id]) self.request.reload() self.assertTrue(self.request.state.is_PROCESSED) self.assertEqual(self.request.receiver_id, self.account_2.id)
def test_wait__successed(self): self.assertEqual(self.task.process(self.main_task), POSTPONED_TASK_LOGIC_RESULT.CONTINUE) transfer_invoice = self.task.transfer_transaction.get_invoice() transfer_invoice.state = bank_relations.INVOICE_STATE.FROZEN transfer_invoice.save() commission_invoice = self.task.commission_transaction.get_invoice() commission_invoice.state = bank_relations.INVOICE_STATE.FROZEN commission_invoice.save() with self.check_new_message(self.recipient.id, [logic.get_system_user_id()]): self.assertEqual(self.task.process(self.main_task), POSTPONED_TASK_LOGIC_RESULT.SUCCESS) self.assertTrue(self.task.step.is_SUCCESS) self.assertTrue(self.task.state.is_PROCESSED)
def portal_day_started(sender, **kwargs): accounts_query = AccountPrototype.live_query().filter( active_end_at__gt=datetime.datetime.now(), ban_game_end_at__lt=datetime.datetime.now(), ban_forum_end_at__lt=datetime.datetime.now(), premium_end_at__lt=datetime.datetime.now()) accounts_number = accounts_query.count() if accounts_number < 1: return account = None for i in range(1000): account_model = accounts_query[random.randint(0, accounts_number - 1)] account = AccountPrototype(model=account_model) # explicity check for premium, since infinit subscribers does not filtered by previouse query if not account.is_premium: break else: return # if not premium account does not found settings[portal_settings.SETTINGS_ACCOUNT_OF_THE_DAY_KEY] = str(account.id) environment.workers.accounts_manager.cmd_run_account_method( account_id=account.id, method_name=AccountPrototype.prolong_premium.__name__, data={'days': portal_settings.PREMIUM_DAYS_FOR_HERO_OF_THE_DAY}) message = ''' Поздравляем! Ваш герой выбран героем дня и Вы получаете %(days)d дней подписки! ''' % { 'days': portal_settings.PREMIUM_DAYS_FOR_HERO_OF_THE_DAY } pm_tt_api.send_message(sender_id=accounts_logic.get_system_user_id(), recipients_ids=[account.id], body=message, async=True)
def test_send_premium_expired_notifications(self): with self.check_new_message(self.account.id, [logic.get_system_user_id()]): account_1 = self.account account_2 = self.accounts_factory.create_account() account_3 = self.accounts_factory.create_account() account_4 = self.accounts_factory.create_account() account_1.prolong_premium(accounts_settings.PREMIUM_EXPIRED_NOTIFICATION_IN.days-1) account_1.save() account_3.prolong_premium(accounts_settings.PREMIUM_EXPIRED_NOTIFICATION_IN.days-1) account_3.save() account_4.prolong_premium(accounts_settings.PREMIUM_EXPIRED_NOTIFICATION_IN.days+1) account_4.save() zero_time = datetime.datetime.fromtimestamp(0) self.assertEqual(account_1._model.premium_expired_notification_send_at, zero_time) self.assertEqual(account_2._model.premium_expired_notification_send_at, zero_time) self.assertEqual(account_3._model.premium_expired_notification_send_at, zero_time) self.assertEqual(account_4._model.premium_expired_notification_send_at, zero_time) AccountPrototype.send_premium_expired_notifications() account_1.reload() account_2.reload() account_3.reload() account_4.reload() self.assertNotEqual(account_1._model.premium_expired_notification_send_at, zero_time) self.assertEqual(account_2._model.premium_expired_notification_send_at, zero_time) self.assertNotEqual(account_3._model.premium_expired_notification_send_at, zero_time) self.assertEqual(account_4._model.premium_expired_notification_send_at, zero_time) current_time = datetime.datetime.now() self.assertTrue(current_time-datetime.timedelta(seconds=60) < account_1._model.premium_expired_notification_send_at < current_time) self.assertTrue(current_time-datetime.timedelta(seconds=60) < account_3._model.premium_expired_notification_send_at < current_time)
def remove_friendship(cls, initiator, friend): request = cls.get_for_bidirectional(initiator, friend) if request is None: return account_link = '[url="{}"]{}[/url]'.format( full_url('http', 'accounts:show', initiator.id), initiator.nick_verbose) if request.is_confirmed: message = 'игрок {account_link} удалил вас из списка друзей'.format( account_link=account_link) else: message = 'игрок {account_link} отказался добавить вас в список друзей'.format( account_link=account_link) pm_tt_api.send_message(sender_id=accounts_logic.get_system_user_id(), recipients_ids=[friend.id], body=message) request.remove()
def process(self, main_task, storage=None): # pylint: disable=R0911 if self.step.is_FREEZE_MONEY: transaction_state = self.transaction.get_invoice_state() if transaction_state.is_REQUESTED: return POSTPONED_TASK_LOGIC_RESULT.WAIT if transaction_state.is_REJECTED: self.state = self.STATE.TRANSACTION_REJECTED main_task.comment = 'invoice %d rejected' % self.transaction.invoice_id return POSTPONED_TASK_LOGIC_RESULT.ERROR if not transaction_state.is_FROZEN: self.state = self.STATE.WRONG_TRANSACTION_STATE main_task.comment = 'wrong invoice %d state %r on freezing step' % (self.transaction.invoice_id, transaction_state) return POSTPONED_TASK_LOGIC_RESULT.ERROR main_task.extend_postsave_actions((lambda: environment.workers.market_manager.cmd_logic_task(self.buyer_id, main_task.id),)) self.step = self.STEP.FREEZE_LOT return POSTPONED_TASK_LOGIC_RESULT.CONTINUE if self.step.is_FREEZE_LOT: buyer = account_prototypes.AccountPrototype.get_by_id(self.buyer_id) if buyer.is_ban_game: main_task.comment = 'account is banned' self.transaction.cancel() self.state = self.STATE.BUYER_BANNED return POSTPONED_TASK_LOGIC_RESULT.ERROR lot = logic.load_lot(self.lot_id) if not lot.state.is_ACTIVE: main_task.comment = 'lot is not active, real state is: %s' % lot.state.name self.transaction.cancel() self.state = self.STATE.WRONG_LOT_STATE return POSTPONED_TASK_LOGIC_RESULT.ERROR self.good_type = goods_types.get_type(lot.type) self.good = lot.good lot.state = relations.LOT_STATE.FROZEN logic.save_lot(lot) main_task.extend_postsave_actions((lambda: environment.workers.supervisor.cmd_logic_task(self.buyer_id, main_task.id),)) self.step = self.STEP.RECEIVE_GOOD return POSTPONED_TASK_LOGIC_RESULT.CONTINUE if self.step.is_RECEIVE_GOOD: hero = storage.accounts_to_heroes[self.buyer_id] # TODO: save hero after receive item? and after extract too?... self.good_type.insert_good(hero, self.good) storage.save_bundle_data(hero.actions.current_action.bundle_id) main_task.extend_postsave_actions((lambda: environment.workers.market_manager.cmd_logic_task(self.buyer_id, main_task.id),)) self.step = self.STEP.REMOVE_LOT return POSTPONED_TASK_LOGIC_RESULT.CONTINUE if self.step.is_REMOVE_LOT: lot = logic.load_lot(self.lot_id) lot.buyer_id = self.buyer_id lot.state = relations.LOT_STATE.CLOSED_BY_BUYER lot.closed_at = datetime.datetime.now() logic.save_lot(lot) self.transaction.confirm() seller = account_prototypes.AccountPrototype.get_by_id(lot.seller_id) pm_logic.send_message(sender_id=accounts_logic.get_system_user_id(), recipients_ids=[seller.id], body=good_bought_message(lot), async=True) bank_prototypes.InvoicePrototype.create(recipient_type=bank_relations.ENTITY_TYPE.GAME_ACCOUNT, recipient_id=seller.id, sender_type=bank_relations.ENTITY_TYPE.GAME_LOGIC, sender_id=0, currency=bank_relations.CURRENCY_TYPE.PREMIUM, amount=-lot.commission, description_for_sender='Комиссия с продажи «%s»' % lot.name, description_for_recipient='Комиссия с продажи «%s»' % lot.name, operation_uid='%s-%s' % (conf.settings.COMMISSION_OPERATION_UID, lot.type), force=True) self.state = self.STATE.PROCESSED self.step = self.STEP.SUCCESS return POSTPONED_TASK_LOGIC_RESULT.SUCCESS
def process(self, main_task): if self.step.is_INITIALIZE: if self.sender.is_fast: self.state = self.STATE.SENDER_IS_FAST self.step = self.STEP.ERROR return POSTPONED_TASK_LOGIC_RESULT.ERROR if self.recipient.is_fast: self.state = self.STATE.RECIPIENT_IS_FAST self.step = self.STEP.ERROR return POSTPONED_TASK_LOGIC_RESULT.ERROR if self.sender.is_ban_any: self.state = self.STATE.SENDER_BANNED self.step = self.STEP.ERROR return POSTPONED_TASK_LOGIC_RESULT.ERROR if self.recipient.is_ban_any: self.state = self.STATE.RECIPIENT_BANNED self.step = self.STEP.ERROR return POSTPONED_TASK_LOGIC_RESULT.ERROR transfer_invoice = bank_prototypes.InvoicePrototype.create( recipient_type=bank_relations.ENTITY_TYPE.GAME_ACCOUNT, recipient_id=self.recipient_id, sender_type=bank_relations.ENTITY_TYPE.GAME_ACCOUNT, sender_id=self.sender_id, currency=bank_relations.CURRENCY_TYPE.PREMIUM, amount=self.amount, description_for_sender='Перевод игроку «%s»: «%s».' % (self.recipient.nick_verbose, self.comment), description_for_recipient='Перевод от игрока «%s»: «%s».' % (self.sender.nick_verbose, self.comment), operation_uid='transfer-money-between-accounts-transfer') self.transfer_transaction = bank_transaction.Transaction( transfer_invoice.id) commission_invoice = bank_prototypes.InvoicePrototype.create( recipient_type=bank_relations.ENTITY_TYPE.GAME_ACCOUNT, recipient_id=self.sender_id, sender_type=bank_relations.ENTITY_TYPE.GAME_LOGIC, sender_id=0, currency=bank_relations.CURRENCY_TYPE.PREMIUM, amount=-self.commission, description_for_sender='Комиссия с перевода игроку «%s»: «%s».' % (self.recipient.nick_verbose, self.comment), description_for_recipient= 'Комиссия с перевода игроку «%s»: «%s».' % (self.recipient.nick_verbose, self.comment), operation_uid=conf.accounts_settings.COMMISION_TRANSACTION_UID) self.commission_transaction = bank_transaction.Transaction( commission_invoice.id) main_task.extend_postsave_actions( (lambda: amqp_environment.environment.workers.refrigerator. cmd_wait_task(main_task.id), )) self.step = self.STEP.WAIT return POSTPONED_TASK_LOGIC_RESULT.CONTINUE if self.step.is_WAIT: transfer_transaction_state = self.transfer_transaction.get_invoice_state( ) commission_transaction_state = self.commission_transaction.get_invoice_state( ) if transfer_transaction_state.is_REQUESTED: return POSTPONED_TASK_LOGIC_RESULT.WAIT if transfer_transaction_state.is_REJECTED: self.state = self.STATE.TRANSFER_TRANSACTION_REJECTED self.step = self.STEP.ERROR main_task.comment = 'invoice %d rejected' % self.transfer_transaction.invoice_id self.commission_transaction.cancel() return POSTPONED_TASK_LOGIC_RESULT.ERROR if not transfer_transaction_state.is_FROZEN: self.state = self.STATE.TRANSFER_TRANSACTION_WRONG_STATE self.step = self.STEP.ERROR main_task.comment = 'invoice %d rejected' % self.transfer_transaction.invoice_id self.commission_transaction.cancel() return POSTPONED_TASK_LOGIC_RESULT.ERROR if commission_transaction_state.is_REQUESTED: return POSTPONED_TASK_LOGIC_RESULT.WAIT if commission_transaction_state.is_REJECTED: self.state = self.STATE.COMMISSION_TRANSACTION_REJECTED self.step = self.STEP.ERROR main_task.comment = 'invoice %d rejected' % self.commission_transaction.invoice_id self.transfer_transaction.cancel() return POSTPONED_TASK_LOGIC_RESULT.ERROR if not commission_transaction_state.is_FROZEN: self.state = self.STATE.COMMISSION_TRANSACTION_WRONG_STATE self.step = self.STEP.ERROR main_task.comment = 'invoice %d rejected' % self.commission_transaction.invoice_id self.transfer_transaction.cancel() return POSTPONED_TASK_LOGIC_RESULT.ERROR if not (transfer_transaction_state.is_FROZEN and commission_transaction_state.is_FROZEN): return POSTPONED_TASK_LOGIC_RESULT.WAIT self.transfer_transaction.confirm() self.commission_transaction.confirm() message = text = 'Игрок «{sender}» перевёл(-а) вам печеньки: {amount} шт. \n\n[quote]{comment}[/quote]'.format( sender=self.sender.nick_verbose, amount=self.amount, comment=self.comment) pm_tt_api.send_message(sender_id=logic.get_system_user_id(), recipients_ids=[self.recipient.id], body=message, async=True) self.state = self.STATE.PROCESSED self.step = self.STEP.SUCCESS return POSTPONED_TASK_LOGIC_RESULT.SUCCESS
def test_notify_about_premium_expiration(self): with self.check_new_message(self.account.id, [logic.get_system_user_id()]): self.account.notify_about_premium_expiration()