Esempio n. 1
0
def purchase_lot(buyer_id, lot):
    from the_tale.common.postponed_tasks import PostponedTaskPrototype
    from the_tale.finances.market import postponed_tasks

    invoice = bank_prototypes.InvoicePrototype.create(
        recipient_type=bank_relations.ENTITY_TYPE.GAME_ACCOUNT,
        recipient_id=lot.seller_id,
        sender_type=bank_relations.ENTITY_TYPE.GAME_ACCOUNT,
        sender_id=buyer_id,
        currency=bank_relations.CURRENCY_TYPE.PREMIUM,
        amount=lot.price,
        description_for_sender=u'Покупка «%s»' % lot.name,
        description_for_recipient=u'Продажа «%s»' % lot.name,
        operation_uid=u'market-buy-lot-%s' % lot.type)

    transaction = bank_transaction.Transaction(invoice.id)

    logic_task = postponed_tasks.BuyLotTask(seller_id=lot.seller_id,
                                            buyer_id=buyer_id,
                                            lot_id=lot.id,
                                            transaction=transaction)

    task = PostponedTaskPrototype.create(logic_task)

    amqp_environment.environment.workers.refrigerator.cmd_wait_task(task.id)

    return task
Esempio n. 2
0
    def test_purchase_lot(self):

        with mock.patch(
                'the_tale.common.postponed_tasks.workers.refrigerator.Worker.cmd_wait_task'
        ) as cmd_wait_task:
            with self.check_delta(
                    postponed_tasks_models.PostponedTask.objects.count, 1):
                with self.check_delta(bank_models.Invoice.objects.count, 1):
                    task = logic.purchase_lot(buyer_id=self.account_2.id,
                                              lot=self.lot_1)

        invoice = bank_prototypes.InvoicePrototype._db_latest()

        self.assertTrue(invoice.state.is_REQUESTED)
        self.assertTrue(invoice.recipient_type.is_GAME_ACCOUNT)
        self.assertTrue(invoice.sender_type.is_GAME_ACCOUNT)
        self.assertTrue(invoice.currency.is_PREMIUM)
        self.assertEqual(invoice.recipient_id, self.account_1.id)
        self.assertEqual(invoice.sender_id, self.account_2.id)
        self.assertEqual(invoice.amount, self.lot_1.price)

        self.assertEqual(task.internal_logic.seller_id, self.account_1.id)
        self.assertEqual(task.internal_logic.buyer_id, self.account_2.id)
        self.assertEqual(task.internal_logic.lot_id, self.lot_1.id)
        self.assertEqual(task.internal_logic.transaction.serialize(),
                         bank_transaction.Transaction(invoice.id).serialize())

        self.assertEqual(cmd_wait_task.call_args_list, [mock.call(task.id)])
Esempio n. 3
0
    def setUp(self):
        super(TaskTests, self).setUp()

        create_test_map()

        self.good_1_uid = 'good-1'

        self.account_1 = self.accounts_factory.create_account()
        self.goods_1 = logic.load_goods(self.account_1.id)

        self.account_2 = self.accounts_factory.create_account()
        self.goods_2 = logic.load_goods(self.account_2.id)

        self.good_1 = goods_types.test_hero_good.create_good(self.good_1_uid)

        self.goods_1.add_good(self.good_1)
        logic.save_goods(self.goods_1)

        self.logic_storage = logic_storage.LogicStorage()
        self.hero_1 = self.logic_storage.load_account_data(self.account_1)
        self.hero_2 = self.logic_storage.load_account_data(self.account_2)

        self.price = 666

        self.lot_1 = logic.reserve_lot(self.account_1.id,
                                       self.good_1,
                                       price=self.price)
        self.lot_1.state = relations.LOT_STATE.ACTIVE
        logic.save_lot(self.lot_1)

        self.invoice = bank_prototypes.InvoicePrototype.create(
            recipient_type=bank_relations.ENTITY_TYPE.GAME_ACCOUNT,
            recipient_id=self.account_1.id,
            sender_type=bank_relations.ENTITY_TYPE.GAME_ACCOUNT,
            sender_id=self.account_2.id,
            currency=bank_relations.CURRENCY_TYPE.PREMIUM,
            amount=self.price,
            description_for_sender='transaction-description-for_sender',
            description_for_recipient='transaction-description-for-recipient',
            operation_uid='transaction-operation-ui')

        self.transaction = bank_transaction.Transaction(self.invoice.id)

        self.task = postponed_tasks.BuyLotTask(seller_id=self.account_1.id,
                                               buyer_id=self.account_2.id,
                                               lot_id=self.lot_1.id,
                                               transaction=self.transaction)

        self.main_task = mock.Mock(comment=None, id=777)
Esempio n. 4
0
def close_lot(item_type, price, buyer_id):
    from . import postponed_tasks

    cards_info = cards_logic.get_cards_info_by_full_types()

    name = cards_info[item_type]['name']

    invoice = bank_prototypes.InvoicePrototype.create(recipient_type=bank_relations.ENTITY_TYPE.GAME_LOGIC,
                                                      recipient_id=0,
                                                      sender_type=bank_relations.ENTITY_TYPE.GAME_ACCOUNT,
                                                      sender_id=buyer_id,
                                                      currency=bank_relations.CURRENCY_TYPE.PREMIUM,
                                                      amount=price,
                                                      description_for_sender='Покупка «%s»' % name,
                                                      description_for_recipient='Продажа «%s»' % name,
                                                      operation_uid='market-buy-lot-cards-hero-good')

    transaction = bank_transaction.Transaction(invoice.id)

    return postponed_tasks.BuyMarketLot(account_id=buyer_id,
                                        price=price,
                                        item_type=item_type,
                                        transaction=transaction)
Esempio n. 5
0
    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()

            personal_messages_prototypes.MessagePrototype.create(
                logic.get_system_user(),
                self.recipient,
                text=
                'Игрок «%(sender)s» перевёл(-а) вам печеньки: %(amount)d шт. \n\n[quote]%(comment)s[/quote]'
                % {
                    'sender': self.sender.nick_verbose,
                    'amount': self.amount,
                    'comment': self.comment
                })

            self.state = self.STATE.PROCESSED
            self.step = self.STEP.SUCCESS
            return POSTPONED_TASK_LOGIC_RESULT.SUCCESS