def setUp(self): super(TaskTests, self).setUp() create_test_map() goods_types.test_hero_good._clear() 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.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.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.task = postponed_tasks.CloseLotByTimoutTask(lot_id=self.lot_1.id) self.main_task = mock.Mock(comment=None, id=777)
def setUp(self): super(LogicTests, self).setUp() create_test_map() self.account_1 = self.accounts_factory.create_account() self.account_2 = self.accounts_factory.create_account() self.goods_1 = logic.load_goods(self.account_1.id) self.good_1 = goods_types.test_hero_good.create_good('good-1') self.good_2 = goods_types.test_hero_good.create_good('good-2') self.good_3 = goods_types.test_hero_good.create_good('good-3') self.good_4 = goods_types.test_hero_good.create_good('good-4') self.goods_1.add_good(self.good_1) self.goods_1.add_good(self.good_2) self.goods_1.add_good(self.good_3) self.goods_1.add_good(self.good_4) logic.save_goods(self.goods_1) 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)
def test_close_lots_by_timeout(self): lot_2 = logic.reserve_lot(self.account_1.id, self.good_2, price=2) lot_2.state = relations.LOT_STATE.ACTIVE logic.save_lot(lot_2) lot_3 = logic.reserve_lot(self.account_1.id, self.good_3, price=3) self.assertFalse(lot_3.state.is_ACTIVE) lot_4 = logic.reserve_lot(self.account_1.id, self.good_4, price=4) lot_4.state = relations.LOT_STATE.ACTIVE logic.save_lot(lot_4) models.Lot.objects.filter( id__in=[self.lot_1.id, lot_3.id, lot_4.id]).update( created_at=datetime.datetime.now() - datetime.timedelta(days=conf.settings.LOT_LIVE_TIME + 1)) with mock.patch( 'the_tale.finances.market.workers.market_manager.Worker.cmd_logic_task' ) as cmd_logic_task: with self.check_delta( postponed_tasks_models.PostponedTask.objects.count, 2): tasks = logic.close_lots_by_timeout() self.assertEqual(cmd_logic_task.call_count, 2) self.assertEqual( set((tasks[0].internal_logic.lot_id, tasks[1].internal_logic.lot_id)), set((self.lot_1.id, lot_4.id)))
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) personal_messages_prototypes.MessagePrototype.create( accounts_logic.get_system_user(), seller, good_timeout_message(lot)) self.state = self.STATE.PROCESSED self.step = self.STEP.SUCCESS return POSTPONED_TASK_LOGIC_RESULT.SUCCESS
def test_freeze_lot__wrong_lot_state(self): self.lot_1.state = random.choice([state for state in relations.LOT_STATE.records if not state.is_ACTIVE]) logic.save_lot(self.lot_1) self.assertEqual(self.task.process(self.main_task), POSTPONED_TASK_LOGIC_RESULT.ERROR) self.assertTrue(self.task.state.is_WRONG_LOT_STATE) self.assertTrue(self.task.step.is_FREEZE_LOT)
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) personal_messages_prototypes.MessagePrototype.create(accounts_logic.get_system_user(), seller, good_timeout_message(lot)) self.state = self.STATE.PROCESSED self.step = self.STEP.SUCCESS return POSTPONED_TASK_LOGIC_RESULT.SUCCESS
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)
def setUp(self): super(PurchaseRequestsTests, self).setUp() self.good_1 = goods_types.test_hero_good.create_good('good-1') self.goods_1 = logic.load_goods(self.account_1.id) self.goods_1.add_good(self.good_1) logic.save_goods(self.goods_1) self.lot_1 = logic.reserve_lot(self.account_1.id, self.goods_1.get_good(self.good_1.uid), price=777) self.lot_1.state = relations.LOT_STATE.ACTIVE logic.save_lot(self.lot_1) self.requested_url = url('market:purchase', self.lot_1.id)
def test_normal_view__disabled_records(self): self.request_login(self.account_1.email) for lot_state in relations.LOT_STATE.records: if lot_state.is_ACTIVE: continue self.lot_2.state = lot_state logic.save_lot(self.lot_2) self.check_html_ok(self.request_html(self.requested_url), texts=[('pgf-no-lots-message', 0), self.lot_1.name, (self.lot_2.name, 0), (self.lot_3.name, 0), self.lot_4.name])
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)
def test_freeze_lot__lot_not_active(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.lot_1.state = random.choice([state for state in relations.LOT_STATE.records if not state.is_ACTIVE]) logic.save_lot(self.lot_1) with mock.patch('the_tale.finances.bank.transaction.Transaction.cancel') as cancel: self.assertEqual(self.task.process(self.main_task), POSTPONED_TASK_LOGIC_RESULT.ERROR) self.assertTrue(self.task.state.is_WRONG_LOT_STATE) self.assertTrue(self.task.step.is_FREEZE_LOT) self.assertEqual(cancel.call_count, 1)
def test_close_lots_by_timeout(self): lot_2 = logic.reserve_lot(self.account_1.id, self.good_2, price=2) lot_2.state = relations.LOT_STATE.ACTIVE logic.save_lot(lot_2) lot_3 = logic.reserve_lot(self.account_1.id, self.good_3, price=3) self.assertFalse(lot_3.state.is_ACTIVE) lot_4 = logic.reserve_lot(self.account_1.id, self.good_4, price=4) lot_4.state = relations.LOT_STATE.ACTIVE logic.save_lot(lot_4) models.Lot.objects.filter(id__in=[self.lot_1.id, lot_3.id, lot_4.id]).update(created_at=datetime.datetime.now()-datetime.timedelta(days=conf.settings.LOT_LIVE_TIME+1)) with mock.patch('the_tale.finances.market.workers.market_manager.Worker.cmd_logic_task') as cmd_logic_task: with self.check_delta(postponed_tasks_models.PostponedTask.objects.count, 2): tasks = logic.close_lots_by_timeout() self.assertEqual(cmd_logic_task.call_count, 2) self.assertEqual(set((tasks[0].internal_logic.lot_id, tasks[1].internal_logic.lot_id)), set((self.lot_1.id, lot_4.id)))
def setUp(self): super(IndexRequestsTests, self).setUp() self.good_1 = goods_types.test_hero_good.create_good('good-#-1') self.good_2 = goods_types.test_hero_good.create_good('good-#-2') self.good_3 = goods_types.test_hero_good.create_good('good-#-3') self.good_4 = goods_types.test_hero_good.create_good('good-#-4') self.price_1 = 1 self.price_2 = 2 self.price_3 = 3 self.price_4 = 4 self.lot_1 = logic.reserve_lot(self.account_1.id, self.good_1, price=self.price_1) self.lot_2 = logic.reserve_lot(self.account_1.id, self.good_2, price=self.price_2) self.lot_3 = logic.reserve_lot(self.account_1.id, self.good_3, price=self.price_3) self.lot_4 = logic.reserve_lot(self.account_2.id, self.good_4, price=self.price_4) self.lot_1.state = relations.LOT_STATE.ACTIVE self.lot_2.state = relations.LOT_STATE.ACTIVE self.lot_4.state = relations.LOT_STATE.ACTIVE logic.save_lot(self.lot_1) logic.save_lot(self.lot_2) logic.save_lot(self.lot_4) self.requested_url = url('market:')
def process(self, main_task, storage=None): # pylint: disable=R0911 good_type = goods_types.get_type(self.good_type) if self.step.is_UNPROCESSED: account = account_prototypes.AccountPrototype.get_by_id(self.account_id) if account.is_ban_game: main_task.comment = 'account is banned' self.state = self.STATE.BANNED return POSTPONED_TASK_LOGIC_RESULT.ERROR goods = logic.load_goods(self.account_id) if not goods.has_good(self.good_uid): main_task.comment = 'account has no good %s' % self.good_uid self.state = self.STATE.NO_GOOD return POSTPONED_TASK_LOGIC_RESULT.ERROR if logic.has_lot(self.account_id, self.good_uid): main_task.comment = 'account %d has lot for <%s> %s' % (self.account_id, self.good_type, self.good_uid) self.state = self.STATE.ALREADY_RESERVED return POSTPONED_TASK_LOGIC_RESULT.ERROR lot = logic.reserve_lot(self.account_id, goods.get_good(self.good_uid), price=self.price) self.lot_id = lot.id main_task.extend_postsave_actions((lambda: environment.workers.supervisor.cmd_logic_task(self.account_id, main_task.id),)) self.step = self.STEP.RESERVED return POSTPONED_TASK_LOGIC_RESULT.CONTINUE if self.step.is_RESERVED: hero = storage.accounts_to_heroes[self.account_id] if not good_type.has_good(hero, self.good_uid): main_task.comment = 'hero has no good %s' % self.good_uid self.state = self.STATE.NO_GOOD self.step = self.STEP.ROLLBACK main_task.extend_postsave_actions((lambda: environment.workers.market_manager.cmd_logic_task(self.account_id, main_task.id),)) return POSTPONED_TASK_LOGIC_RESULT.CONTINUE good_type.extract_good(hero, self.good_uid) 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.GOTTEN return POSTPONED_TASK_LOGIC_RESULT.CONTINUE if self.step.is_ROLLBACK: goods = logic.load_goods(self.account_id) lot = logic.load_lot(self.lot_id) lot.state = relations.LOT_STATE.CLOSED_BY_ERROR logic.save_lot(lot) self.step = self.STEP.ROLLBACKED self.state = self.STATE.PROCESSED return POSTPONED_TASK_LOGIC_RESULT.SUCCESS if self.step.is_GOTTEN: goods = logic.load_goods(self.account_id) lot = logic.load_lot(self.lot_id) lot.state = relations.LOT_STATE.ACTIVE logic.save_lot(lot) goods.remove_good(self.good_uid) logic.save_goods(goods) self.step = self.STEP.ACTIVATED self.state = self.STATE.PROCESSED return POSTPONED_TASK_LOGIC_RESULT.SUCCESS
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) personal_messages_prototypes.MessagePrototype.create(accounts_logic.get_system_user(), seller, good_bought_message(lot)) 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, storage=None): # pylint: disable=R0911 good_type = goods_types.get_type(self.good_type) if self.step.is_UNPROCESSED: account = account_prototypes.AccountPrototype.get_by_id( self.account_id) if account.is_ban_game: main_task.comment = 'account is banned' self.state = self.STATE.BANNED return POSTPONED_TASK_LOGIC_RESULT.ERROR goods = logic.load_goods(self.account_id) if not goods.has_good(self.good_uid): main_task.comment = 'account has no good %s' % self.good_uid self.state = self.STATE.NO_GOOD return POSTPONED_TASK_LOGIC_RESULT.ERROR if logic.has_lot(self.account_id, self.good_uid): main_task.comment = 'account %d has lot for <%s> %s' % ( self.account_id, self.good_type, self.good_uid) self.state = self.STATE.ALREADY_RESERVED return POSTPONED_TASK_LOGIC_RESULT.ERROR lot = logic.reserve_lot(self.account_id, goods.get_good(self.good_uid), price=self.price) self.lot_id = lot.id main_task.extend_postsave_actions( (lambda: environment.workers.supervisor.cmd_logic_task( self.account_id, main_task.id), )) self.step = self.STEP.RESERVED return POSTPONED_TASK_LOGIC_RESULT.CONTINUE if self.step.is_RESERVED: hero = storage.accounts_to_heroes[self.account_id] if not good_type.has_good(hero, self.good_uid): main_task.comment = 'hero has no good %s' % self.good_uid self.state = self.STATE.NO_GOOD self.step = self.STEP.ROLLBACK main_task.extend_postsave_actions( (lambda: environment.workers.market_manager.cmd_logic_task( self.account_id, main_task.id), )) return POSTPONED_TASK_LOGIC_RESULT.CONTINUE good_type.extract_good(hero, self.good_uid) 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.GOTTEN return POSTPONED_TASK_LOGIC_RESULT.CONTINUE if self.step.is_ROLLBACK: goods = logic.load_goods(self.account_id) lot = logic.load_lot(self.lot_id) lot.state = relations.LOT_STATE.CLOSED_BY_ERROR logic.save_lot(lot) self.step = self.STEP.ROLLBACKED self.state = self.STATE.PROCESSED return POSTPONED_TASK_LOGIC_RESULT.SUCCESS if self.step.is_GOTTEN: goods = logic.load_goods(self.account_id) lot = logic.load_lot(self.lot_id) lot.state = relations.LOT_STATE.ACTIVE logic.save_lot(lot) goods.remove_good(self.good_uid) logic.save_goods(goods) self.step = self.STEP.ACTIVATED self.state = self.STATE.PROCESSED return POSTPONED_TASK_LOGIC_RESULT.SUCCESS
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) personal_messages_prototypes.MessagePrototype.create( accounts_logic.get_system_user(), seller, good_bought_message(lot)) 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