Exemple #1
0
 def deserialize(cls, data):
     from the_tale.finances.market import goods_types
     obj = cls(type=data['type'],
               name=data['name'],
               uid=data['uid'],
               item=goods_types.get_type(data['type']).deserialize_item(data['item']))
     return obj
Exemple #2
0
 def deserialize(cls, data):
     from the_tale.finances.market import goods_types
     obj = cls(type=data['type'],
               name=data['name'],
               uid=data['uid'],
               item=goods_types.get_type(data['type']).deserialize_item(
                   data['item']))
     return obj
Exemple #3
0
 def serialize(self):
     from the_tale.finances.market import goods_types
     return {
         'type': self.type,
         'name': self.name,
         'uid': self.uid,
         'item': goods_types.get_type(self.type).serialize_item(self.item)
     }
Exemple #4
0
 def __init__(self, lot_id, account_id=None, good_type=None, good=None, step=STEP.FREEZE_LOT, state=STATE.UNPROCESSED):
     super(CloseLotByTimoutTask, self).__init__()
     self.account_id = account_id
     self.lot_id = lot_id
     self.state = state if isinstance(state, rels.Record) else self.STATE(state)
     self.step = step if isinstance(step, rels.Record) else self.STEP(step)
     self.good_type = goods_types.get_type(good_type) if good_type else None
     self.good = objects.Good.deserialize(good) if isinstance(good, dict) else good
 def __init__(self, lot_id, account_id=None, good_type=None, good=None, step=STEP.FREEZE_LOT, state=STATE.UNPROCESSED):
     super(CloseLotByTimoutTask, self).__init__()
     self.account_id = account_id
     self.lot_id = lot_id
     self.state = state if isinstance(state, rels.Record) else self.STATE(state)
     self.step = step if isinstance(step, rels.Record) else self.STEP(step)
     self.good_type = goods_types.get_type(good_type) if good_type else None
     self.good = objects.Good.deserialize(good) if isinstance(good, dict) else good
    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 __init__(self, buyer_id, seller_id, lot_id, transaction, good_type=None, good=None, step=STEP.FREEZE_MONEY, state=STATE.UNPROCESSED):
     super(BuyLotTask, self).__init__()
     self.buyer_id = buyer_id
     self.seller_id = seller_id
     self.lot_id = lot_id
     self.state = state if isinstance(state, rels.Record) else self.STATE(state)
     self.step = step if isinstance(step, rels.Record) else self.STEP(step)
     self.good_type = goods_types.get_type(good_type) if good_type else None
     self.good = objects.Good.deserialize(good) if isinstance(good, dict) else good
     self.transaction = bank_transaction.Transaction.deserialize(transaction) if isinstance(transaction, dict) else transaction
Exemple #8
0
 def __init__(self, buyer_id, seller_id, lot_id, transaction, good_type=None, good=None, step=STEP.FREEZE_MONEY, state=STATE.UNPROCESSED):
     super(BuyLotTask, self).__init__()
     self.buyer_id = buyer_id
     self.seller_id = seller_id
     self.lot_id = lot_id
     self.state = state if isinstance(state, rels.Record) else self.STATE(state)
     self.step = step if isinstance(step, rels.Record) else self.STEP(step)
     self.good_type = goods_types.get_type(good_type) if good_type else None
     self.good = objects.Good.deserialize(good) if isinstance(good, dict) else good
     self.transaction = bank_transaction.Transaction.deserialize(transaction) if isinstance(transaction, dict) else transaction
Exemple #9
0
    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
Exemple #10
0
 def group(self):
     from the_tale.finances.market import goods_types
     return goods_types.get_type(self.type).group_of(self.item)
Exemple #11
0
 def html_label(self):
     from the_tale.finances.market import goods_types
     return goods_types.get_type(self.type).item_html(self.item)
Exemple #12
0
    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
Exemple #13
0
 def serialize(self):
     from the_tale.finances.market import goods_types
     return {'type': self.type,
             'name': self.name,
             'uid': self.uid,
             'item': goods_types.get_type(self.type).serialize_item(self.item)}
Exemple #14
0
    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
Exemple #15
0
    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
Exemple #16
0
 def html_label(self):
     from the_tale.finances.market import goods_types
     return goods_types.get_type(self.type).item_html(self.item)
Exemple #17
0
    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
Exemple #18
0
 def group(self):
     from the_tale.finances.market import goods_types
     return goods_types.get_type(self.type).group_of(self.item)