本文整理匯總了Golang中github.com/ginuerzh/sports/models.RedisLogger.SetGameScore方法的典型用法代碼示例。如果您正苦於以下問題:Golang RedisLogger.SetGameScore方法的具體用法?Golang RedisLogger.SetGameScore怎麽用?Golang RedisLogger.SetGameScore使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/ginuerzh/sports/models.RedisLogger
的用法示例。
在下文中一共展示了RedisLogger.SetGameScore方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: gamelbHandler
func gamelbHandler(r *http.Request, w http.ResponseWriter,
redis *models.RedisLogger, user *models.Account, p Parameter) {
form := p.(gamelbForm)
if form.Index <= 0 {
form.Index = 0
}
if form.Count <= 0 {
form.Count = 20
}
gt := gameType(form.Game)
var ids []string
var kvs []models.KV
switch form.Query {
case "FRIEND":
ids = redis.Friends(models.RelFriend, user.Id)
if len(ids) == 0 {
break
}
ids = append(ids, user.Id)
if form.Index*form.Count >= len(ids) {
break
}
start := form.Index * form.Count
end := start + form.Count
if end > len(ids) {
end = len(ids)
}
scores := redis.UserGameScores(gt, ids...)
if len(scores) != len(ids) {
scores = make([]int64, len(ids))
}
kvs = make([]models.KV, len(ids))
for i, _ := range kvs {
kvs[i].K = ids[i]
kvs[i].V = scores[i]
if ids[i] == user.Id {
kvs[i].V = int64(form.Score)
}
}
sort.Sort(sort.Reverse(models.KVSlice(kvs)))
kvs = kvs[start:end]
ids = []string{}
for _, kv := range kvs {
ids = append(ids, kv.K)
}
case "TOP":
fallthrough
default:
maxScore := 0
if scores := redis.UserGameScores(gt, user.Id); len(scores) == 1 {
maxScore = int(scores[0])
}
redis.SetGameScore(gt, user.Id, form.Score) // current score
kvs = redis.GameScores(gt, form.Index*form.Count, form.Count)
redis.SetGameScore(gt, user.Id, maxScore) // recover max score
for i, kv := range kvs {
ids = append(ids, kv.K)
if kv.K == user.Id {
kvs[i].V = int64(form.Score)
}
}
}
var respData struct {
List []*leaderboardResp `json:"members_list"`
}
users, _ := models.FindUsersByIds(1, ids...)
index := 0
for _, kv := range kvs {
for i, _ := range users {
if users[i].Id == kv.K {
respData.List = append(respData.List, &leaderboardResp{
Userid: users[i].Id,
Score: kv.V,
Rank: form.Index*form.Count + index + 1,
Level: users[i].Level(),
Profile: users[i].Profile,
Nickname: users[i].Nickname,
Actor: users[i].Actor,
Gender: users[i].Gender,
LastLog: users[i].LastGameTime(gt).Unix(),
Birth: users[i].Birth,
Location: users[i].Loc,
Phone: users[i].Phone,
})
index++
break
}
}
//.........這裏部分代碼省略.........
示例2: gameResultHandler
func gameResultHandler(r *http.Request, w http.ResponseWriter,
redis *models.RedisLogger, user *models.Account, p Parameter) {
form := p.(gameResultForm)
var respData struct {
Total []*leaderboardResp `json:"total_list"`
Friends []*leaderboardResp `json:"friends_list"`
Score int `json:"total_score"`
Percent int `json:"percent"`
PerFriend int `json:"percentFri"`
}
if form.Count == 0 {
form.Count = 3
}
gt := gameType(form.Type)
if scores := redis.UserGameScores(gt, user.Id); len(scores) == 1 {
respData.Score = int(scores[0])
}
redis.SetGameScore(gt, user.Id, form.Score) // current score
kvs := redis.GameScores(gt, 0, form.Count)
var ids []string
for _, kv := range kvs {
ids = append(ids, kv.K)
}
ranks := redis.UserGameRanks(gt, user.Id)
redis.SetGameScore(gt, user.Id, respData.Score) // recover max score
n := redis.GameUserCount(gt) - 1
//log.Println(ranks, n)
if len(ranks) == 1 && n > 0 && form.Score > 0 {
respData.Percent = int(float64(n-ranks[0]) / float64(n) * 100.0)
}
//log.Println(ids)
users, _ := models.FindUsersByIds(1, ids...)
index := 0
for _, kv := range kvs {
for i, _ := range users {
if users[i].Id == kv.K {
respData.Total = append(respData.Total, &leaderboardResp{
Userid: users[i].Id,
Score: kv.V,
Rank: index + 1,
Level: users[i].Level(),
Profile: users[i].Profile,
Nickname: users[i].Nickname,
Gender: users[i].Gender,
LastLog: users[i].LastGameTime(gt).Unix(),
Birth: users[i].Birth,
Location: users[i].Loc,
Phone: users[i].Phone,
Actor: users[i].Actor,
})
index++
break
}
}
}
ids = redis.Friends(models.RelFriend, user.Id)
if len(ids) > 0 {
total := len(ids)
ids = append(ids, user.Id)
scores := redis.UserGameScores(gt, ids...)
if len(scores) != len(ids) {
scores = make([]int64, total)
}
kvs = make([]models.KV, total+1)
for i, _ := range ids {
kvs[i].K = ids[i]
kvs[i].V = scores[i]
if ids[i] == user.Id {
kvs[i].V = int64(form.Score)
}
}
sort.Sort(sort.Reverse(models.KVSlice(kvs)))
lb := kvs
if len(kvs) > 3 {
kvs = kvs[0:3]
}
ids = []string{}
for _, kv := range kvs {
ids = append(ids, kv.K)
}
users, _ = models.FindUsersByIds(1, ids...)
index := 0
rank := 0
for i, _ := range lb {
if lb[i].K == user.Id {
rank = i
break
}
//.........這裏部分代碼省略.........