Beispiel #1
0
 def __init__(self, char_id):
     self.char_id = char_id
     try:
         self.attachment = MongoAttachment.objects.get(id=self.char_id)
     except DoesNotExist:
         self.attachment = MongoAttachment(id=self.char_id)
         self.attachment.prize_ids = []
         self.attachment.attachments = {}
         self.attachment.save()
Beispiel #2
0
class Attachment(object):
    def __init__(self, char_id):
        self.char_id = char_id
        try:
            self.attachment = MongoAttachment.objects.get(id=self.char_id)
        except DoesNotExist:
            self.attachment = MongoAttachment(id=self.char_id)
            self.attachment.prize_ids = []
            self.attachment.attachments = {}
            self.attachment.save()

    @staticmethod
    def cron_job():
        # collection = MongoAttachment._get_collection().find({'prize_ids': {'$in': NEED_RESET_PRIZE_IDS}})

        for a in MongoAttachment.objects.filter(prize_ids__in=NEED_RESET_PRIZE_IDS):
            for i in NEED_RESET_PRIZE_IDS:
                if i in a.prize_ids:
                    a.prize_ids.remove(i)

            a.save()

    def save_to_prize(self, prize_id):
        if prize_id not in self.attachment.prize_ids:
            self.attachment.prize_ids.append(prize_id)
            self.attachment.save()
        self.send_notify()

    def save_to_attachment(self, prize_id, **kwargs):
        self.attachment.attachments[str(prize_id)] = json.dumps(kwargs)

        if prize_id not in self.attachment.prize_ids:
            self.attachment.prize_ids.append(prize_id)
        self.attachment.save()

        self.send_notify()

    def get_attachment(self, prize_id, param=0):
        # prizie_id == 1 挂机不在这里
        if prize_id == 4:
            # 成就
            from core.achievement import Achievement

            ach = Achievement(self.char_id)
            att_msg = ach.get_reward(param)
            if ach.has_prizes():
                prize_id = None

        elif prize_id == 5:
            # 任务
            from core.task import Task

            task = Task(self.char_id)
            att_msg = task.get_reward(param)
            if task.has_prizes():
                prize_id = None

        elif prize_id == 6:
            # 官职每日登录
            # from core.daily import OfficialDailyReward
            # od = OfficialDailyReward(self.char_id)
            # att_msg = od.get_reward()
            att_msg = None
        elif prize_id == 7:
            # 团队本
            att_msg = None

        else:
            try:
                attachment = self.attachment.attachments[str(prize_id)]
            except KeyError:
                raise SanguoException(
                    errormsg.ATTACHMENT_NOT_EXIST, self.char_id, "Attachment Get", "{0} not exist".format(prize_id)
                )

            attachment = json.loads(attachment)
            resource = Resource(self.char_id, "Prize {0}".format(prize_id))
            standard_drop = resource.add(**attachment)

            self.attachment.attachments.pop(str(prize_id))
            self.attachment.save()

            att_msg = standard_drop_to_attachment_protomsg(standard_drop)

        # 删除此prize_id
        if prize_id:
            if prize_id in self.attachment.prize_ids:
                self.attachment.prize_ids.remove(prize_id)
            if str(prize_id) in self.attachment.attachments:
                self.attachment.attachments.pop(str(prize_id))

        self.attachment.save()
        self.send_notify()

        return att_msg

    def send_notify(self):
        msg = PrizeNotify()
        msg.prize_ids.extend(self.attachment.prize_ids)
        publish_to_char(self.char_id, pack_msg(msg))
Beispiel #3
0
class Attachment(object):
    def __init__(self, char_id):
        self.char_id = char_id
        try:
            self.attachment = MongoAttachment.objects.get(id=self.char_id)
        except DoesNotExist:
            self.attachment = MongoAttachment(id=self.char_id)
            self.attachment.prize_ids = []
            self.attachment.attachments = {}
            self.attachment.save()

    def save_to_prize(self, prize_id):
        if prize_id not in self.attachment.prize_ids:
            self.attachment.prize_ids.append(prize_id)
            self.attachment.save()
        self.send_notify()


    def save_to_attachment(self, prize_id, **kwargs):
        self.attachment.attachments[str(prize_id)] = json.dumps(kwargs)

        if prize_id not in self.attachment.prize_ids:
            self.attachment.prize_ids.append(prize_id)
        self.attachment.save()

        self.send_notify()

    def get_attachment(self, prize_id, param=0):
        if prize_id == 1:
            # 挂机
            from core.stage import Hang
            h = Hang(self.char_id)
            att_msg = h.save_drop()
        elif prize_id == 4:
            # 成就
            from core.achievement import Achievement
            ach = Achievement(self.char_id)
            att_msg = ach.get_reward(param)
        elif prize_id == 5:
            # 任务
            from core.task import Task
            task = Task(self.char_id)
            att_msg = task.get_reward(param)
        elif prize_id == 6:
            # 官职每日登录
            from core.daily import OfficalDailyReward
            od = OfficalDailyReward(self.char_id)
            att_msg = od.get_reward()
        elif prize_id == 7:
            # 团队本
            from core.stage import TeamBattle
            tb = TeamBattle(self.char_id)
            att_msg = tb.get_reward()
        else:
            try:
                attachment = self.attachment.attachments[str(prize_id)]
            except KeyError:
                raise SanguoException(
                    errormsg.ATTACHMENT_NOT_EXIST,
                    self.char_id,
                    "Attachment Get",
                    "{0} not exist".format(prize_id)
                )

            attachment = json.loads(attachment)
            resource = Resource(self.char_id, "Prize {0}".format(prize_id))
            standard_drop = resource.add(**attachment)

            self.attachment.attachments.pop(str(prize_id))
            self.attachment.save()

            att_msg = standard_drop_to_attachment_protomsg(standard_drop)

        # 删除此prize_id
        if prize_id in self.attachment.prize_ids:
            self.attachment.prize_ids.remove(prize_id)
        if str(prize_id) in self.attachment.attachments:
            self.attachment.attachments.pop(str(prize_id))

        self.attachment.save()
        self.send_notify()

        return att_msg


    def send_notify(self):
        msg = PrizeNotify()
        msg.prize_ids.extend(self.attachment.prize_ids)
        publish_to_char(self.char_id, pack_msg(msg))
Beispiel #4
0
class Attachment(object):
    def __init__(self, char_id):
        self.char_id = char_id
        try:
            self.attachment = MongoAttachment.objects.get(id=self.char_id)
        except DoesNotExist:
            self.attachment = MongoAttachment(id=self.char_id)
            self.attachment.prize_ids = []
            self.attachment.attachments = {}
            self.attachment.save()

    @staticmethod
    def cron_job():
        # collection = MongoAttachment._get_collection().find({'prize_ids': {'$in': NEED_RESET_PRIZE_IDS}})

        for a in MongoAttachment.objects.filter(
                prize_ids__in=NEED_RESET_PRIZE_IDS):
            for i in NEED_RESET_PRIZE_IDS:
                if i in a.prize_ids:
                    a.prize_ids.remove(i)

            a.save()

    def save_to_prize(self, prize_id):
        if prize_id not in self.attachment.prize_ids:
            self.attachment.prize_ids.append(prize_id)
            self.attachment.save()
        self.send_notify()

    def save_to_attachment(self, prize_id, **kwargs):
        self.attachment.attachments[str(prize_id)] = json.dumps(kwargs)

        if prize_id not in self.attachment.prize_ids:
            self.attachment.prize_ids.append(prize_id)
        self.attachment.save()

        self.send_notify()

    def get_attachment(self, prize_id, param=0):
        # prizie_id == 1 挂机不在这里
        if prize_id == 4:
            # 成就
            from core.achievement import Achievement
            ach = Achievement(self.char_id)
            att_msg = ach.get_reward(param)
            if ach.has_prizes():
                prize_id = None

        elif prize_id == 5:
            # 任务
            from core.task import Task
            task = Task(self.char_id)
            att_msg = task.get_reward(param)
            if task.has_prizes():
                prize_id = None

        elif prize_id == 6:
            # 官职每日登录
            # from core.daily import OfficialDailyReward
            # od = OfficialDailyReward(self.char_id)
            # att_msg = od.get_reward()
            att_msg = None
        elif prize_id == 7:
            # 团队本
            att_msg = None

        else:
            try:
                attachment = self.attachment.attachments[str(prize_id)]
            except KeyError:
                raise SanguoException(errormsg.ATTACHMENT_NOT_EXIST,
                                      self.char_id, "Attachment Get",
                                      "{0} not exist".format(prize_id))

            attachment = json.loads(attachment)
            resource = Resource(self.char_id, "Prize {0}".format(prize_id))
            standard_drop = resource.add(**attachment)

            self.attachment.attachments.pop(str(prize_id))
            self.attachment.save()

            att_msg = standard_drop_to_attachment_protomsg(standard_drop)

        # 删除此prize_id
        if prize_id:
            if prize_id in self.attachment.prize_ids:
                self.attachment.prize_ids.remove(prize_id)
            if str(prize_id) in self.attachment.attachments:
                self.attachment.attachments.pop(str(prize_id))

        self.attachment.save()
        self.send_notify()

        return att_msg

    def send_notify(self):
        msg = PrizeNotify()
        msg.prize_ids.extend(self.attachment.prize_ids)
        publish_to_char(self.char_id, pack_msg(msg))