From cc9010513088b5edf84baf134a6093fbbe93bbb0 Mon Sep 17 00:00:00 2001 From: Morgan Bazalgette Date: Thu, 13 Apr 2017 08:57:11 +0200 Subject: [PATCH] make Global- and CountryLeaderboardRank nullable --- app/v1/leaderboard.go | 25 +++++++++++++++++-------- app/v1/user.go | 8 ++++---- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/app/v1/leaderboard.go b/app/v1/leaderboard.go index 158594b..b8519d5 100644 --- a/app/v1/leaderboard.go +++ b/app/v1/leaderboard.go @@ -92,21 +92,30 @@ func LeaderboardGET(md common.MethodData) common.CodeMessager { continue } u.ChosenMode.Level = ocl.GetLevelPrecise(int64(u.ChosenMode.TotalScore)) - if i := leaderboardPosition(md.R, m, u.ID); i != 0 { - u.ChosenMode.GlobalLeaderboardRank = &i + if i := leaderboardPosition(md.R, m, u.ID); i != nil { + u.ChosenMode.GlobalLeaderboardRank = i } - if i := countryPosition(md.R, m, u.ID, u.Country); i != 0 { - u.ChosenMode.CountryLeaderboardRank = &i + if i := countryPosition(md.R, m, u.ID, u.Country); i != nil { + u.ChosenMode.CountryLeaderboardRank = i } resp.Users = append(resp.Users, u) } return resp } -func leaderboardPosition(r *redis.Client, mode string, user int) int { - return int(r.ZRevRank("ripple:leaderboard:"+mode, strconv.Itoa(user)).Val()) + 1 +func leaderboardPosition(r *redis.Client, mode string, user int) *int { + return _position(r, "ripple:leaderboard:"+mode, user) } -func countryPosition(r *redis.Client, mode string, user int, country string) int { - return int(r.ZRevRank("ripple:leaderboard:"+mode+":"+strings.ToLower(country), strconv.Itoa(user)).Val()) + 1 +func countryPosition(r *redis.Client, mode string, user int, country string) *int { + return _position(r, "ripple:leaderboard:"+mode+":"+strings.ToLower(country), user) +} + +func _position(r *redis.Client, key string, user int) *int { + res := r.ZRevRank(key, strconv.Itoa(user)) + if res.Err() == redis.Nil { + return nil + } + x := int(res.Val()) + 1 + return &x } diff --git a/app/v1/user.go b/app/v1/user.go index 2abc321..4d3aa12 100644 --- a/app/v1/user.go +++ b/app/v1/user.go @@ -293,11 +293,11 @@ LIMIT 1 for modeID, m := range [...]*modeData{&r.STD, &r.Taiko, &r.CTB, &r.Mania} { m.Level = ocl.GetLevelPrecise(int64(m.TotalScore)) - if i := leaderboardPosition(md.R, modesToReadable[modeID], r.ID); i != 0 { - m.GlobalLeaderboardRank = &i + if i := leaderboardPosition(md.R, modesToReadable[modeID], r.ID); i != nil { + m.GlobalLeaderboardRank = i } - if i := countryPosition(md.R, modesToReadable[modeID], r.ID, r.Country); i != 0 { - m.CountryLeaderboardRank = &i + if i := countryPosition(md.R, modesToReadable[modeID], r.ID, r.Country); i != nil { + m.CountryLeaderboardRank = i } }