// 获取排行榜
func GetRankData(lsid int32, num int32) []redis.Z {
datas, err := GetServerRedis(lsid).ZRevRangeWithScores(RedisKeyServerKickFight(lsid), int64(0), int64(num-1)).Result()
if err != nil {
antnet.LogError("kickFight GetOpponents error:%v", err)
} else {
return datas
}
return []redis.Z{}
}
// 排行榜
func HandleKickFightRankC2S(gamer *ControlGamer, msgque antnet.IMsgQue, msg *antnet.Message) bool {
s2c := &pb.GamerKickFightRankS2C{}
ranks := GetRankData(GetLsid(gamer.Id), Config.Doc.KickingConf[21].GetValue())
index := int32(0)
for _, v := range ranks {
index++
id := antnet.Atoi64(v.Member.(string))
score := int32(int64(v.Score) / Additional)
if id > 4000 {
s2c.Rank = append(s2c.Rank, buildKickFightRankGamer(id, index, score))
} else {
s2c.Rank = append(s2c.Rank, buildKickFightRankRobot(gamer, id, index, score))
}
}
msgque.Send(antnet.NewDataMsg(antnet.PbData(s2c)).CopyTag(msg))
return true
}
// 获取排名
func (r *modelGamerKickFight) GetOpponentRank() int32 {
lsid := GetLsid(r.gid)
rank, err := GetServerRedis(lsid).ZRevRank(RedisKeyServerKickFight(lsid), antnet.Itoa(r.OpponentId)).Result()
if err != nil {
return 0
}
return int32(rank) + 1
}
func InitKickFightRobotRank(lsid int32) bool {
// 初始该区的排行榜
sep := 0
key := RedisKeyServerKickFight(lsid)
pipe := GetServerRedis(lsid).Pipeline()
for _, v := range Config.Doc.KickingRobotConf {
z := redis.Z{Member: int64(v.GetGuid()), Score: float64(int64(v.GetCredit())*Additional + additionalTime())}
pipe.ZAdd(key, z)
sep++
if sep >= 100 {
sep = 0
_, err := pipe.Exec()
if err != nil {
antnet.LogError("InitKickFightRobotRank 1 db err:%v", err)
return false
}
pipe = GetServerRedis(lsid).Pipeline()
}
}
//删除临时数据
_, err := GetServerRedis(lsid).ZRem(key, "test").Result()
if err != nil {
antnet.LogError("InitKickFightRobotRank 2 db err:%v", err)
return false
}
antnet.LogInfo("InitKickFightRobotRank server:%v ok", lsid)
return true
}
http://www.runoob.com/redis/sorted-sets-zrevrank.html
http://www.runoob.com/redis/sorted-sets-zadd.html