Exemplo n.º 1
0
 def load_mongo_record(self):
     try:
         self.mongo_record = MongoPurchaseRecord.objects.get(id=self.char_id)
     except DoesNotExist:
         self.mongo_record = MongoPurchaseRecord(id=self.char_id)
         self.mongo_record.times = {}
         self.mongo_record.yueka_sycee = 0
         self.mongo_record.yueka_remained_days = 0
         self.mongo_record.yueka_lock = False
         self.mongo_record.save()
Exemplo n.º 2
0
 def load_mongo_record(self):
     try:
         self.mongo_record = MongoPurchaseRecord.objects.get(id=self.char_id)
     except DoesNotExist:
         self.mongo_record = MongoPurchaseRecord(id=self.char_id)
         self.mongo_record.times = {}
         self.mongo_record.yueka_sycee = 0
         self.mongo_record.yueka_remained_days = 0
         self.mongo_record.yueka_lock = False
         self.mongo_record.save()
Exemplo n.º 3
0
class PurchaseAction(object):
    def __init__(self, char_id):
        self.char_id = char_id
        self.load_mongo_record()

    def load_mongo_record(self):
        try:
            self.mongo_record = MongoPurchaseRecord.objects.get(
                id=self.char_id)
        except DoesNotExist:
            self.mongo_record = MongoPurchaseRecord(id=self.char_id)
            self.mongo_record.times = {}
            self.mongo_record.yueka_sycee = 0
            self.mongo_record.yueka_remained_days = 0
            self.mongo_record.yueka_lock = False
            self.mongo_record.save()

    def all_times(self):
        return {int(k): v for k, v in self.mongo_record.times.iteritems()}

    def check_confirm(self):
        res = api_purchase91_confirm(data={'char_id': self.char_id})
        print "91 confirm"
        print res

        response = Purchase91ConfirmResponse()
        response.ret = res['ret']
        if res['ret']:
            response.reason = res['data']['status']

        response.goods_id = res['data']['goods_id']
        return response

    def send_reward(self, goods_id):
        p = PURCHASE[goods_id]

        first = len(self.mongo_record.times) == 0

        buy_times = self.mongo_record.times.get(str(goods_id), 0)
        is_first = buy_times == 0

        if p.tp_obj.continued_days > 0:
            self.send_reward_yueka(goods_id, is_first)
        else:
            self.send_reward_sycee(goods_id, is_first)

        self.mongo_record.times[str(goods_id)] = buy_times + 1
        self.mongo_record.save()

        self.send_notify()

        title = u'充值成功'
        content = u'获得了: {0}'.format(p.first_des if p.first_des else p.des)
        mail = Mail(self.char_id)
        mail.add(title, content)

        # 首冲奖励
        if first:
            self.send_first_reward()

    def send_first_reward(self):
        standard_drop = get_drop(PURCHASE_FIRST_REWARD_PACKAGE_IDS)

        mail = Mail(self.char_id)
        mail.add(MAIL_PURCHASE_FIRST_TITLE,
                 MAIL_PURCHASE_FIRST_CONTENT,
                 attachment=json.dumps(standard_drop))

    def send_reward_yueka(self, goods_id, is_first):
        # 月卡
        # XXX NOTE
        # 系统只支持一种类型的月卡
        self.send_reward_sycee(goods_id, is_first)

        p = PURCHASE[goods_id]

        try:
            self.set_yueka_remained_days(p.tp_obj.continued_days)
        except YuekaLockTimeOut:
            raise SanguoException(errormsg.PURCHASE_91_FAILURE, self.char_id,
                                  "Purchase", "get yueka lock timeout...")

        self.mongo_record.yueka_sycee = p.tp_obj.day_sycee
        self.mongo_record.save()

    def set_yueka_remained_days(self, add_days):
        for i in range(10):
            self.load_mongo_record()
            if not self.mongo_record.yueka_lock:
                self.mongo_record.yueka_lock = True
                self.mongo_record.save()
                break
            else:
                time.sleep(0.2)
        else:
            raise YuekaLockTimeOut()

        self.mongo_record.yueka_remained_days += add_days
        if self.mongo_record.yueka_remained_days < 0:
            self.mongo_record.yueka_remained_days = 0

        self.mongo_record.yueka_lock = False
        self.mongo_record.save()

    def send_reward_sycee(self, goods_id, is_first):
        # 元宝
        p = PURCHASE[goods_id]
        addition = p.first_addition_sycee if is_first else p.addition_sycee

        purchase_got = p.sycee
        purchase_actual_got = purchase_got + addition

        resource = Resource(self.char_id, "Purchase")
        resource.add(purchase_got=purchase_got,
                     purchase_actual_got=purchase_actual_got)

    def send_notify(self):
        msg = PurchaseStatusNotify()
        times = self.all_times()

        for _id in PURCHASE.keys():
            s = msg.status.add()
            s.id = _id
            s.first = _id not in times

        msg.yueka_remained_days = self.mongo_record.yueka_remained_days

        publish_to_char(self.char_id, pack_msg(msg))
Exemplo n.º 4
0
class BasePurchaseAction(object):
    def __init__(self, char_id):
        self.char_id = char_id
        self.load_mongo_record()

    def load_mongo_record(self):
        try:
            self.mongo_record = MongoPurchaseRecord.objects.get(id=self.char_id)
        except DoesNotExist:
            self.mongo_record = MongoPurchaseRecord(id=self.char_id)
            self.mongo_record.times = {}
            self.mongo_record.yueka_sycee = 0
            self.mongo_record.yueka_remained_days = 0
            self.mongo_record.yueka_lock = False
            self.mongo_record.save()


    def all_times(self):
        return {int(k): v for k, v in self.mongo_record.times.iteritems()}


    def buy_times_of_this_goods(self, goods_id):
        times = self.mongo_record.times.get(str(goods_id), 0)
        return times


    def send_reward(self, goods_id):
        if goods_id == 1:
            # 不再有月卡
            mail_message = "char {0} try to buy goods: {1}".format(self.char_id, goods_id)
            mail_admins(mail_message, mail_message, fail_silently=True)
            return


        p = PURCHASE[goods_id]

        first = len(self.mongo_record.times) == 0

        buy_times = self.buy_times_of_this_goods(goods_id)
        is_first = buy_times == 0

        # if p.tp_obj.continued_days > 0:
        #     self.send_reward_yueka(goods_id, is_first)
        # else:
        #     self.send_reward_sycee(goods_id, is_first)

        self.send_reward_sycee(goods_id, is_first)

        self.mongo_record.times[str(goods_id)] = buy_times + 1
        self.mongo_record.save()

        self.send_notify()

        title = u'充值成功'
        content = u'获得了: {0}'.format(p.first_des if is_first else p.des)
        mail = Mail(self.char_id)
        mail.add(title, content)

        # 首冲奖励
        if first:
            self.send_first_reward()


    def send_first_reward(self):
        standard_drop = get_drop(PURCHASE_FIRST_REWARD_PACKAGE_IDS)

        mail = Mail(self.char_id)
        mail.add(
            MAIL_PURCHASE_FIRST_TITLE,
            MAIL_PURCHASE_FIRST_CONTENT,
            attachment=json.dumps(standard_drop)
        )


    def send_reward_yueka(self, **kwargs):
        # 月卡
        # XXX NOTE
        # 系统只支持一种类型的月卡
        self.add_to_resource(YueKa.reward_sycee, 0, **kwargs)

        try:
            self.set_yueka_remained_days(YueKa.continue_days)
        except YuekaLockTimeOut:
            raise SanguoException(
                errormsg.PURCHASE_91_FAILURE,
                self.char_id,
                "Purchase",
                "get yueka lock timeout..."
            )

        self.mongo_record.yueka_sycee = YueKa.day_sycee
        self.mongo_record.save()


    def set_yueka_remained_days(self, add_days):
        for i in range(10):
            self.load_mongo_record()
            if not self.mongo_record.yueka_lock:
                self.mongo_record.yueka_lock = True
                self.mongo_record.save()
                break
            else:
                time.sleep(0.2)
        else:
            raise YuekaLockTimeOut()

        self.mongo_record.yueka_remained_days += add_days
        if self.mongo_record.yueka_remained_days < 0:
            self.mongo_record.yueka_remained_days = 0

        self.mongo_record.yueka_lock = False
        self.mongo_record.save()


    def send_reward_sycee(self, goods_id, is_first, **kwargs):
        # 元宝
        p = PURCHASE[goods_id]
        addition = p.first_addition_sycee if is_first else p.addition_sycee

        self.add_to_resource(p.sycee, addition, **kwargs)


    def add_to_resource(self, sycee, addition, **kwargs):
        purchase_got = sycee
        purchase_actual_got = purchase_got + addition

        data = kwargs
        data['purchase_got'] = purchase_got
        data['purchase_actual_got'] = purchase_actual_got

        resource = Resource(self.char_id, "Purchase")
        resource.add(**data)

    def send_addition_sycee_via_mail(self, sycee):
        # XXX 充值额外赠送通过邮件发送
        drop = make_standard_drop_from_template()
        drop['sycee'] = sycee

        m = Mail(self.char_id)
        m.add(u"充值额外赠送", u"感谢您的充值,请领取额外赠送", attachment=json.dumps(drop))


    def send_notify(self):
        msg = PurchaseStatusNotify()
        times = self.all_times()

        for _id in PURCHASE.keys():
            if _id not in times:
                s = msg.status.add()
                s.id = _id
                s.first = True

        msg.yueka_remained_days = self.mongo_record.yueka_remained_days
        publish_to_char(self.char_id, pack_msg(msg))


    def check_confirm(self):
        # for third platform
        api = self.get_confirm_api()

        res = api(data={'char_id': self.char_id})
        print "==== PURCHASE CONFIRM ===="
        print res

        response = PurchaseConfirmResponse()
        response.ret = res['ret']
        if res['ret']:
            response.reason = res['data']['status']

        response.goods_id = res['data']['goods_id']
        return response

    def get_confirm_api(self):
        raise NotImplementedError()
Exemplo n.º 5
0
class PurchaseAction(object):
    def __init__(self, char_id):
        self.char_id = char_id
        self.load_mongo_record()

    def load_mongo_record(self):
        try:
            self.mongo_record = MongoPurchaseRecord.objects.get(id=self.char_id)
        except DoesNotExist:
            self.mongo_record = MongoPurchaseRecord(id=self.char_id)
            self.mongo_record.times = {}
            self.mongo_record.yueka_sycee = 0
            self.mongo_record.yueka_remained_days = 0
            self.mongo_record.yueka_lock = False
            self.mongo_record.save()


    def all_times(self):
        return {int(k): v for k, v in self.mongo_record.times.iteritems()}


    def check_confirm(self):
        res = api_purchase91_confirm(data={'char_id': self.char_id})
        print "91 confirm"
        print res

        response = Purchase91ConfirmResponse()
        response.ret = res['ret']
        if res['ret']:
            response.reason = res['data']['status']

        response.goods_id = res['data']['goods_id']
        return response


    def send_reward(self, goods_id):
        p = PURCHASE[goods_id]

        first = len(self.mongo_record.times) == 0

        buy_times = self.mongo_record.times.get(str(goods_id), 0)
        is_first = buy_times == 0

        if p.tp_obj.continued_days > 0:
            self.send_reward_yueka(goods_id, is_first)
        else:
            self.send_reward_sycee(goods_id, is_first)

        self.mongo_record.times[str(goods_id)] = buy_times + 1
        self.mongo_record.save()

        self.send_notify()

        title = u'充值成功'
        content = u'获得了: {0}'.format(p.first_des if p.first_des else p.des)
        mail = Mail(self.char_id)
        mail.add(title, content)

        # 首冲奖励
        if first:
            self.send_first_reward()


    def send_first_reward(self):
        standard_drop = get_drop(PURCHASE_FIRST_REWARD_PACKAGE_IDS)

        mail = Mail(self.char_id)
        mail.add(
            MAIL_PURCHASE_FIRST_TITLE,
            MAIL_PURCHASE_FIRST_CONTENT,
            attachment=json.dumps(standard_drop)
        )


    def send_reward_yueka(self, goods_id, is_first):
        # 月卡
        # XXX NOTE
        # 系统只支持一种类型的月卡
        self.send_reward_sycee(goods_id, is_first)

        p = PURCHASE[goods_id]

        try:
            self.set_yueka_remained_days(p.tp_obj.continued_days)
        except YuekaLockTimeOut:
            raise SanguoException(
                errormsg.PURCHASE_91_FAILURE,
                self.char_id,
                "Purchase",
                "get yueka lock timeout..."
            )

        self.mongo_record.yueka_sycee = p.tp_obj.day_sycee
        self.mongo_record.save()


    def set_yueka_remained_days(self, add_days):
        for i in range(10):
            self.load_mongo_record()
            if not self.mongo_record.yueka_lock:
                self.mongo_record.yueka_lock = True
                self.mongo_record.save()
                break
            else:
                time.sleep(0.2)
        else:
            raise YuekaLockTimeOut()

        self.mongo_record.yueka_remained_days += add_days
        if self.mongo_record.yueka_remained_days < 0:
            self.mongo_record.yueka_remained_days = 0

        self.mongo_record.yueka_lock = False
        self.mongo_record.save()


    def send_reward_sycee(self, goods_id, is_first):
        # 元宝
        p = PURCHASE[goods_id]
        addition = p.first_addition_sycee if is_first else p.addition_sycee

        purchase_got = p.sycee
        purchase_actual_got = purchase_got + addition

        resource = Resource(self.char_id, "Purchase")
        resource.add(purchase_got=purchase_got, purchase_actual_got=purchase_actual_got)


    def send_notify(self):
        msg = PurchaseStatusNotify()
        times = self.all_times()

        for _id in PURCHASE.keys():
            s = msg.status.add()
            s.id = _id
            s.first = _id not in times

        msg.yueka_remained_days = self.mongo_record.yueka_remained_days

        publish_to_char(self.char_id, pack_msg(msg))
Exemplo n.º 6
0
class BasePurchaseAction(object):
    def __init__(self, char_id):
        self.char_id = char_id
        self.load_mongo_record()

    def load_mongo_record(self):
        try:
            self.mongo_record = MongoPurchaseRecord.objects.get(id=self.char_id)
        except DoesNotExist:
            self.mongo_record = MongoPurchaseRecord(id=self.char_id)
            self.mongo_record.times = {}
            self.mongo_record.yueka_sycee = 0
            self.mongo_record.yueka_remained_days = 0
            self.mongo_record.yueka_lock = False
            self.mongo_record.save()


    def all_times(self):
        return {int(k): v for k, v in self.mongo_record.times.iteritems()}


    def buy_times_of_this_goods(self, goods_id):
        times = self.mongo_record.times.get(str(goods_id), 0)
        return times


    def send_reward(self, goods_id):
        if goods_id == 1:
            # 不再有月卡
            mail_message = "char {0} try to buy goods: {1}".format(self.char_id, goods_id)
            mail_admins(mail_message, mail_message, fail_silently=True)
            return


        p = PURCHASE[goods_id]

        first = len(self.mongo_record.times) == 0

        buy_times = self.buy_times_of_this_goods(goods_id)
        is_first = buy_times == 0

        # if p.tp_obj.continued_days > 0:
        #     self.send_reward_yueka(goods_id, is_first)
        # else:
        #     self.send_reward_sycee(goods_id, is_first)

        self.send_reward_sycee(goods_id, is_first)

        self.mongo_record.times[str(goods_id)] = buy_times + 1
        self.mongo_record.save()

        self.send_notify()

        title = u'充值成功'
        content = u'获得了: {0}'.format(p.first_des if is_first else p.des)
        mail = Mail(self.char_id)
        mail.add(title, content)

        # 首冲奖励
        if first:
            self.send_first_reward()


    def send_first_reward(self):
        standard_drop = get_drop(PURCHASE_FIRST_REWARD_PACKAGE_IDS)

        mail = Mail(self.char_id)
        mail.add(
            MAIL_PURCHASE_FIRST_TITLE,
            MAIL_PURCHASE_FIRST_CONTENT,
            attachment=json.dumps(standard_drop)
        )


    def send_reward_yueka(self, **kwargs):
        # 月卡
        # XXX NOTE
        # 系统只支持一种类型的月卡
        self.add_to_resource(YueKa.reward_sycee, 0, **kwargs)

        try:
            self.set_yueka_remained_days(YueKa.continue_days)
        except YuekaLockTimeOut:
            raise SanguoException(
                errormsg.PURCHASE_91_FAILURE,
                self.char_id,
                "Purchase",
                "get yueka lock timeout..."
            )

        self.mongo_record.yueka_sycee = YueKa.day_sycee
        self.mongo_record.save()


    def set_yueka_remained_days(self, add_days):
        for i in range(10):
            self.load_mongo_record()
            if not self.mongo_record.yueka_lock:
                self.mongo_record.yueka_lock = True
                self.mongo_record.save()
                break
            else:
                time.sleep(0.2)
        else:
            raise YuekaLockTimeOut()

        self.mongo_record.yueka_remained_days += add_days
        if self.mongo_record.yueka_remained_days < 0:
            self.mongo_record.yueka_remained_days = 0

        self.mongo_record.yueka_lock = False
        self.mongo_record.save()


    def send_reward_sycee(self, goods_id, is_first, **kwargs):
        # 元宝
        p = PURCHASE[goods_id]
        addition = p.first_addition_sycee if is_first else p.addition_sycee

        self.add_to_resource(p.sycee, addition, **kwargs)


    def add_to_resource(self, sycee, addition, **kwargs):
        purchase_got = sycee
        purchase_actual_got = purchase_got + addition

        data = kwargs
        data['purchase_got'] = purchase_got
        data['purchase_actual_got'] = purchase_actual_got

        resource = Resource(self.char_id, "Purchase")
        resource.add(**data)


    def send_notify(self):
        msg = PurchaseStatusNotify()
        times = self.all_times()

        for _id in PURCHASE.keys():
            s = msg.status.add()
            s.id = _id
            s.first = _id not in times

        msg.yueka_remained_days = self.mongo_record.yueka_remained_days

        publish_to_char(self.char_id, pack_msg(msg))


    def check_confirm(self):
        # for third platform
        api = self.get_confirm_api()

        res = api(data={'char_id': self.char_id})
        print "==== PURCHASE CONFIRM ===="
        print res

        response = PurchaseConfirmResponse()
        response.ret = res['ret']
        if res['ret']:
            response.reason = res['data']['status']

        response.goods_id = res['data']['goods_id']
        return response

    def get_confirm_api(self):
        raise NotImplementedError()