Пример #1
0
def _build_group_data_s2c(data, sid):
    """未上榜的公会只传基础数据
    """
    temp = {}
    temp["name"] = data["name"]
    temp["master"] = GroupService.get_master_name_by_uid(data["master"])
    temp["icon"] = data["icon"]
    temp["level"] = data["level"]
    temp["rank"] = 0
    temp["fight"] = sum([member["fight"] for member in GroupService.members(sid, int(data["id"])) if isinstance(member, dict)])

    return temp
Пример #2
0
def _run_update_cached_group_rank(sid, tag):
    """更新公会排行榜

    抓取所有公会数据 - 按等级排 - 按总战力排 - 存放进cache里

    """
    ranking_name = rediskey_config.RANK_KEY_PREFIX % (sid, tag)
    ranking_list = redis_client.zrevrange(ranking_name, 0, -1, withscores=True)

    group_rank = []

    if ranking_list:
        for key, value in enumerate(ranking_list):
            rank_data = {}
            rank_data['rank'] = 0
            rank_data['group_id'] = int(value[0])
            rank_data['exp'] = int(value[1])

            group_data = GroupService.find(sid, value[0])
            if group_data:
                rank_data['name'] = group_data["name"]
                rank_data['icon'] = group_data["icon"]
                rank_data['level'] = group_data["level"]
                master = User.get(group_data["master"])
                rank_data['master'] = master.name if isinstance(master,
                                                                User) else "会长"
                rank_data['member_number'] = group_data["member_number"]
                rank_data['join_level_limit'] = group_data["join_level_limit"]
                rank_data['join_state'] = group_data["join_state"]
                rank_data['notice'] = group_data["notice"]

                fight = 0
                for uid in [
                        member["uid"]
                        for member in GroupService.members(sid, int(value[0]))
                        if isinstance(member, dict)
                ]:
                    try:
                        user = User.get(uid)
                        fight += user.game_info.fight
                    except:
                        fight += 0

                rank_data['fight'] = fight

            group_rank.append(rank_data)

    def gsorted(x, y):
        if x["level"] == y["level"]:
            return y["fight"] - x["fight"]
        else:
            return y["level"] - x["level"]

    group_rank = sorted(group_rank, cmp=gsorted)

    a = 1
    for x in group_rank:
        x['rank'] = a
        a += 1
    # 放入排行缓存列表中
    group_rank.reverse()

    cache_rank_name = rediskey_config.RANK_CACHE_KEY_PREFIX % (sid, tag)
    while redis_client.llen(cache_rank_name) != 0:
        redis_client.delete(cache_rank_name)
        time.sleep(1)

    # 放入排行缓存列表中
    for rank in group_rank:
        redis_client.lpush(cache_rank_name, rank)

    print 'ranking_name:', cache_rank_name
    print 'ranking_list:', group_rank
    print '=' * 60
def _run_update_cached_group_rank(sid, tag):
    """更新公会排行榜

    抓取所有公会数据 - 按等级排 - 按总战力排 - 存放进cache里

    """
    ranking_name = rediskey_config.RANK_KEY_PREFIX % (sid, tag)
    ranking_list = redis_client.zrevrange(ranking_name, 0, -1, withscores=True)

    group_rank = []

    if ranking_list:
        for key, value in enumerate(ranking_list):
            rank_data = {}
            rank_data['rank'] = 0
            rank_data['group_id'] = int(value[0])
            rank_data['exp'] = int(value[1])

            group_data = GroupService.find(sid, value[0])
            if group_data:
                rank_data['name'] = group_data["name"]
                rank_data['icon'] = group_data["icon"]
                rank_data['level'] = group_data["level"]
                master = User.get(group_data["master"])
                rank_data['master'] = master.name if isinstance(master, User) else "会长"
                rank_data['member_number'] = group_data["member_number"]
                rank_data['join_level_limit'] = group_data["join_level_limit"]
                rank_data['join_state'] = group_data["join_state"]
                rank_data['notice'] = group_data["notice"]

                fight = 0
                for uid in [member["uid"] for member in GroupService.members(sid, int(value[0])) if isinstance(member, dict)]:
                    try:
                        user = User.get(uid)
                        fight += user.game_info.fight
                    except:
                        fight += 0

                rank_data['fight'] = fight

            group_rank.append(rank_data)

    def gsorted(x,y):
        if x["level"] == y["level"]:
           return y["fight"] - x["fight"]
        else:
           return y["level"] - x["level"]

    group_rank = sorted(group_rank, cmp=gsorted)

    a = 1
    for x in group_rank:
        x['rank'] = a
        a += 1
    # 放入排行缓存列表中
    group_rank.reverse()

    cache_rank_name = rediskey_config.RANK_CACHE_KEY_PREFIX % (sid, tag)
    while redis_client.llen(cache_rank_name) != 0:
        redis_client.delete(cache_rank_name)
        time.sleep(1)

    # 放入排行缓存列表中
    for rank in group_rank:
        redis_client.lpush(cache_rank_name, rank)

    print 'ranking_name:', cache_rank_name
    print 'ranking_list:', group_rank
    print '=' * 60