示例#1
0
    def create(self, name):
        if len(name) > UNION_NAME_MAX_LENGTH:
            raise SanguoException(
                errormsg.UNION_NAME_TOO_LONG, self.char_id, "Union Create",
                "name too long: {0}".format(name.encode('utf-8')))

        if MongoUnion.objects.filter(name=name).count() > 0:
            raise SanguoException(
                errormsg.UNION_NAME_ALREADY_EXIST, self.char_id,
                "Union Create",
                "name already exist: {0}".format(name.encode('utf-8')))

        resource = Resource(self.char_id, "Union Create")

        with resource.check(sycee=-UNION_CREATE_NEEDS_SYCEE):
            new_id = id_generator('union')[0]
            mu = MongoUnion(id=new_id)
            mu.owner = self.char_id
            mu.name = name
            mu.bulletin = UNION_DEFAULT_DES
            mu.level = 1
            mu.contribute_points = 0
            mu.score = get_battle_init_score()
            mu.save()
            Member(self.char_id).join_union(new_id)

        send_notify(self.char_id)
示例#2
0
    def create(self, name):
        if len(name) > UNION_NAME_MAX_LENGTH:
            raise SanguoException(
                    errormsg.UNION_NAME_TOO_LONG,
                    self.char_id,
                    "Union Create",
                    "name too long: {0}".format(name.encode('utf-8'))
                    )


        if MongoUnion.objects.filter(name=name).count() > 0:
            raise SanguoException(
                    errormsg.UNION_NAME_ALREADY_EXIST,
                    self.char_id,
                    "Union Create",
                    "name already exist: {0}".format(name.encode('utf-8'))
                    )


        resource = Resource(self.char_id, "Union Create")

        with resource.check(sycee=-UNION_CREATE_NEEDS_SYCEE):
            new_id = id_generator('union')[0]
            mu = MongoUnion(id=new_id)
            mu.owner = self.char_id
            mu.name = name
            mu.bulletin = UNION_DEFAULT_DES
            mu.level = 1
            mu.contribute_points = 0
            mu.score = get_battle_init_score()
            mu.save()
            Member(self.char_id).join_union(new_id)

        send_notify(self.char_id)
示例#3
0
    def apply_union(self, union_id):
        # 申请加入工会
        from core.union.union import Union, UnionList

        doc = MongoUnion._get_collection().find_one({'_id': union_id},
                                                    {'owner': 1})

        if not doc:
            raise SanguoException(errormsg.UNION_NOT_EXIST, self.char_id,
                                  "Member Apply Union",
                                  "union {0} not exist".format(union_id))

        if self.mongo_union_member.joined:
            raise SanguoException(
                errormsg.UNION_CANNOT_APPLY_ALREADY_IN, self.char_id,
                "Member Apply Union",
                "already in {0}".format(self.mongo_union_member.joined))

        if len(self.mongo_union_member.applied) >= 10:
            raise SanguoException(errormsg.UNION_CANNOT_APPLY_FULL,
                                  self.char_id, "Member Apply Union",
                                  "apply list too long")

        if union_id not in self.mongo_union_member.applied:
            self.mongo_union_member.applied.append(union_id)
            self.mongo_union_member.save()

            Union(doc['owner']).send_apply_list_notify()

        UnionList.send_list_notify(self.char_id)
示例#4
0
    def checkin(self):
        # 签到
        from core.union.union import Union
        from core.union.battle import UnionBattle

        if not self.mongo_union_member.joined:
            raise SanguoException(
                errormsg.INVALID_OPERATE,
                self.char_id,
                "Union Checkin",
                "not join union"
            )

        if self.mongo_union_member.checkin_times + 1 > self.checkin_total_amount:
            raise SanguoException(
                errormsg.UNION_CHECKIN_REACH_MAX_TIMES,
                self.char_id,
                "Union Checkin",
                "reached max times"
            )

        try:
            c = UNION_CHECKIN[self.mongo_union_member.checkin_times+1]
        except KeyError:
            raise SanguoException(
                errormsg.UNION_CHECKIN_REACH_MAX_TIMES,
                self.char_id,
                "Union Checkin",
                "reached max times. UNION_CHECKIN KeyError: {0}".format(self.mongo_union_member.checkin_times+1)
            )

        if c.cost_type == 1:
            needs = {'gold': -c.cost_value}
        else:
            needs = {'sycee': -c.cost_value}

        resources = Resource(self.char_id, "Union Checkin")
        with resources.check(**needs):
            self.mongo_union_member.checkin_times += 1
            self.mongo_union_member.last_checkin_timestamp = arrow.utcnow().timestamp

            self.add_coin(c.got_coin, send_notify=False)
            self.add_contribute_points(c.got_contributes, send_notify=False)
            self.mongo_union_member.save()
        self.send_personal_notify()

        Union(self.char_id).add_contribute_points(c.got_contributes)

        UnionBattle(self.char_id).send_notify()
        doc = MongoUnion._get_collection().find_one(
                {'_id': self.mongo_union_member.joined},
                {'owner': 1}
        )
        owner = doc['owner']
        UnionBattle(owner).send_notify()

        drop = make_standard_drop_from_template()
        drop['union_coin'] = c.got_coin
        drop['union_contribute_points'] = c.got_contributes
        return standard_drop_to_attachment_protomsg(drop)
示例#5
0
    def __new__(cls, char_id, union_id=None):
        mongo_member = MongoUnionMember._get_collection().find_one(
            {'_id': char_id}, {'joined': 1})
        if not mongo_member:
            return UnionDummy(char_id)

        char_union_id = mongo_member['joined']
        if not char_union_id and not union_id:
            return UnionDummy(char_id)

        if not union_id:
            union_id = char_union_id

        # FIXME 本来是有这个判断的,但是目前为了省事,对于这个最后返回的是 UnionMember
        # 其实应该为这个添加一个新类: UnionObserver
        # 表示自己加入了一个工会,但是要查看其他工会的信息
        # if char_union_id != union_id:
        #     return UnionDummy(char_id)

        mongo_union = MongoUnion._get_collection().find_one({'_id': union_id},
                                                            {'owner': 1})

        if char_id == mongo_union['owner']:
            return UnionOwner(char_id, union_id)
        return UnionMember(char_id, union_id)
示例#6
0
    def __new__(cls, char_id, union_id=None):
        mongo_member = MongoUnionMember._get_collection().find_one(
                {'_id': char_id},
                {'joined': 1}
        )
        if not mongo_member:
            return UnionDummy(char_id)

        char_union_id = mongo_member['joined']
        if not char_union_id and not union_id:
            return UnionDummy(char_id)

        if not union_id:
            union_id = char_union_id

        # FIXME 本来是有这个判断的,但是目前为了省事,对于这个最后返回的是 UnionMember
        # 其实应该为这个添加一个新类: UnionObserver
        # 表示自己加入了一个工会,但是要查看其他工会的信息
        # if char_union_id != union_id:
        #     return UnionDummy(char_id)

        mongo_union = MongoUnion._get_collection().find_one(
                {'_id': union_id},
                {'owner': 1}
        )

        if char_id == mongo_union['owner']:
            return UnionOwner(char_id, union_id)
        return UnionMember(char_id, union_id)
示例#7
0
    def checkin(self):
        # 签到
        from core.union.union import Union
        from core.union.battle import UnionBattle

        if not self.mongo_union_member.joined:
            raise SanguoException(errormsg.INVALID_OPERATE, self.char_id,
                                  "Union Checkin", "not join union")

        if self.mongo_union_member.checkin_times + 1 > self.checkin_total_amount:
            raise SanguoException(errormsg.UNION_CHECKIN_REACH_MAX_TIMES,
                                  self.char_id, "Union Checkin",
                                  "reached max times")

        try:
            c = UNION_CHECKIN[self.mongo_union_member.checkin_times + 1]
        except KeyError:
            raise SanguoException(
                errormsg.UNION_CHECKIN_REACH_MAX_TIMES, self.char_id,
                "Union Checkin",
                "reached max times. UNION_CHECKIN KeyError: {0}".format(
                    self.mongo_union_member.checkin_times + 1))

        if c.cost_type == 1:
            needs = {'gold': -c.cost_value}
        else:
            needs = {'sycee': -c.cost_value}

        resources = Resource(self.char_id, "Union Checkin")
        with resources.check(**needs):
            self.mongo_union_member.checkin_times += 1
            self.mongo_union_member.last_checkin_timestamp = arrow.utcnow(
            ).timestamp

            self.add_coin(c.got_coin, send_notify=False)
            self.add_contribute_points(c.got_contributes, send_notify=False)
            self.mongo_union_member.save()
        self.send_personal_notify()

        Union(self.char_id).add_contribute_points(c.got_contributes)

        UnionBattle(self.char_id).send_notify()
        doc = MongoUnion._get_collection().find_one(
            {'_id': self.mongo_union_member.joined}, {'owner': 1})
        owner = doc['owner']
        UnionBattle(owner).send_notify()

        drop = make_standard_drop_from_template()
        drop['union_coin'] = c.got_coin
        drop['union_contribute_points'] = c.got_contributes
        return standard_drop_to_attachment_protomsg(drop)
示例#8
0
    def join_union(self, union_id):
        # 加入工会 - 由Union调用
        doc = MongoUnion._get_collection().find_one({'_id': union_id},
                                                    {'_id': 1})
        if not doc:
            raise SanguoException(errormsg.UNION_NOT_EXIST, self.char_id,
                                  "UnionMember Join Union",
                                  "union {0} not eixst".format(union_id))

        if self.mongo_union_member.joined == union_id:
            return

        self.mongo_union_member.applied = []
        self.mongo_union_member.joined = union_id
        self.mongo_union_member.contribute_points = 0
        self.mongo_union_member.position = 1
        self.mongo_union_member.last_checkin_timestamp = 0
        self.mongo_union_member.save()
        self.send_personal_notify()
示例#9
0
    def apply_union(self, union_id):
        # 申请加入工会
        from core.union.union import Union, UnionList

        doc = MongoUnion._get_collection().find_one(
                {'_id': union_id},
                {'owner': 1}
        )

        if not doc:
            raise SanguoException(
                errormsg.UNION_NOT_EXIST,
                self.char_id,
                "Member Apply Union",
                "union {0} not exist".format(union_id)
            )

        if self.mongo_union_member.joined:
            raise SanguoException(
                errormsg.UNION_CANNOT_APPLY_ALREADY_IN,
                self.char_id,
                "Member Apply Union",
                "already in {0}".format(self.mongo_union_member.joined)
            )

        if len(self.mongo_union_member.applied) >= 10:
            raise SanguoException(
                errormsg.UNION_CANNOT_APPLY_FULL,
                self.char_id,
                "Member Apply Union",
                "apply list too long"
            )

        if union_id not in self.mongo_union_member.applied:
            self.mongo_union_member.applied.append(union_id)
            self.mongo_union_member.save()

            Union(doc['owner']).send_apply_list_notify()


        UnionList.send_list_notify(self.char_id)
示例#10
0
    def join_union(self, union_id):
        # 加入工会 - 由Union调用
        doc = MongoUnion._get_collection().find_one({'_id': union_id}, {'_id': 1})
        if not doc:
            raise SanguoException(
                    errormsg.UNION_NOT_EXIST,
                    self.char_id,
                    "UnionMember Join Union",
                    "union {0} not eixst".format(union_id)
                    )

        if self.mongo_union_member.joined == union_id:
            return

        self.mongo_union_member.applied = []
        self.mongo_union_member.joined = union_id
        self.mongo_union_member.contribute_points = 0
        self.mongo_union_member.position = 1
        self.mongo_union_member.last_checkin_timestamp = 0
        self.mongo_union_member.save()
        self.send_personal_notify()
示例#11
0
 def cron_job():
     unions = MongoUnion._get_collection().find({'battle_times': {'$gt': 0}}, {'owner': 1})
     MongoUnion._get_collection().update({}, {'$set': {'battle_times': 0}}, multi=True)
     for u in unions:
         UnionBattle(u['owner']).send_notify()
示例#12
0
    def cronjob_auto_transfer_union(cls):
        transfer_dict = {}

        now = arrow.utcnow()
        local_date = now.to(settings.TIME_ZONE).date()
        timestamp = now.timestamp - 3600 * 24 * 3

        unions = MongoUnion._get_collection().find({}, {'owner': 1})
        owner_union_id_dict = {doc['owner']: doc['_id'] for doc in unions}

        conditions = {
            '$and': [{
                '_id': {
                    '$in': owner_union_id_dict.keys()
                }
            }, {
                'last_checkin_timestamp': {
                    '$lte': timestamp
                }
            }]
        }

        member_docs = MongoUnionMember._get_collection().find(
            conditions, {'last_checkin_timestamp': 1})

        for doc in member_docs:
            char_id = doc['_id']
            union_id = owner_union_id_dict[char_id]

            last_checkin_date = arrow.get(doc['last_checkin_timestamp']).to(
                settings.TIME_ZONE).date()
            days = (local_date - last_checkin_date).days
            # 昨天签到了,今天检测的时候, days 就是1
            # 但是从逻辑上看,应该是连续签到的,
            # 前天签到,昨天没有,今天检测的时候,days是2
            # 逻辑看来是已经 一天 没有签到了
            # 所以 days 在这里 -1
            days -= 1

            if days < 3:
                continue

            u = Union(char_id, union_id)
            if days >= 7:
                # transfer
                next_owner = u.find_next_owner()

                transfer_dict[union_id] = (char_id, next_owner)
                if next_owner:
                    u.transfer(next_owner)

                    m = Mail(char_id)
                    m.add(
                        MAIL_UNION_OWNER_TRANSFER_DONE_TITLE,
                        MAIL_UNION_OWNER_TRANSFER_DONE_CONTENT.format(
                            get_char_property(char_id, 'name')))
            else:
                members = u.member_list
                for mid in members:
                    m = Mail(mid)
                    m.add(
                        MAIL_UNION_OWNER_TRANSFER_NOTIFY_TITLE,
                        MAIL_UNION_OWNER_TRANSFER_NOTIFY_CONTENT.format(days))

        return transfer_dict
示例#13
0
    def cronjob_auto_transfer_union(cls):
        transfer_dict = {}

        now = arrow.utcnow()
        local_date = now.to(settings.TIME_ZONE).date()
        timestamp = now.timestamp - 3600 * 24 * 3

        unions = MongoUnion._get_collection().find({}, {'owner': 1})
        owner_union_id_dict = {doc['owner']: doc['_id'] for doc in unions}

        conditions = {
            '$and': [
                {'_id': {'$in': owner_union_id_dict.keys()}},
                {'last_checkin_timestamp': {'$lte': timestamp}}
            ]
        }

        member_docs = MongoUnionMember._get_collection().find(
                conditions,
                {'last_checkin_timestamp':1}
        )

        for doc in member_docs:
            char_id = doc['_id']
            union_id = owner_union_id_dict[char_id]

            last_checkin_date = arrow.get(doc['last_checkin_timestamp']).to(settings.TIME_ZONE).date()
            days = (local_date - last_checkin_date).days
            # 昨天签到了,今天检测的时候, days 就是1
            # 但是从逻辑上看,应该是连续签到的,
            # 前天签到,昨天没有,今天检测的时候,days是2
            # 逻辑看来是已经 一天 没有签到了
            # 所以 days 在这里 -1
            days -= 1

            if days < 3:
                continue

            u = Union(char_id, union_id)
            if days >= 7:
                # transfer
                next_owner = u.find_next_owner()

                transfer_dict[union_id] = (char_id, next_owner)
                if next_owner:
                    u.transfer(next_owner)

                    m = Mail(char_id)
                    m.add(
                        MAIL_UNION_OWNER_TRANSFER_DONE_TITLE,
                        MAIL_UNION_OWNER_TRANSFER_DONE_CONTENT.format(get_char_property(char_id, 'name'))
                    )
            else:
                members = u.member_list
                for mid in members:
                    m = Mail(mid)
                    m.add(
                        MAIL_UNION_OWNER_TRANSFER_NOTIFY_TITLE,
                        MAIL_UNION_OWNER_TRANSFER_NOTIFY_CONTENT.format(days)
                    )

        return transfer_dict