Add relaxboard and autoboard to api

This commit is contained in:
Alicia 2019-02-23 14:06:24 +00:00
parent b444bf15dd
commit 21646d8db7
3 changed files with 147 additions and 1 deletions

View File

@ -100,6 +100,8 @@ func Start(conf common.Conf, dbO *sqlx.DB) *fhr.Router {
r.Method("/api/v1/badges/members", v1.BadgeMembersGET)
r.Method("/api/v1/beatmaps", v1.BeatmapGET)
r.Method("/api/v1/leaderboard", v1.LeaderboardGET)
r.Method("/api/v1/relaxboard", v1.LeaderboardRxGET)
r.Method("/api/v1/autoboard", v1.LeaderboardApGET)
r.Method("/api/v1/tokens", v1.TokenGET)
r.Method("/api/v1/users/self", v1.UserSelfGET)
r.Method("/api/v1/tokens/self", v1.TokenSelfGET)

View File

@ -119,3 +119,147 @@ func _position(r *redis.Client, key string, user int) *int {
x := int(res.Val()) + 1
return &x
}
// LeaderboardGET gets the leaderboard.
func LeaderboardRxGET(md common.MethodData) common.CodeMessager {
m := getMode(md.Query("mode"))
// md.Query.Country
p := common.Int(md.Query("p")) - 1
if p < 0 {
p = 0
}
l := common.InString(1, md.Query("l"), 500, 50)
key := "ripple:leaderboard_relax:" + m
if md.Query("country") != "" {
key += ":" + md.Query("country")
}
results, err := md.R.ZRevRange(key, int64(p*l), int64(p*l+l-1)).Result()
if err != nil {
md.Err(err)
return Err500
}
var resp leaderboardResponse
resp.Code = 200
if len(results) == 0 {
return resp
}
query := fmt.Sprintf(lbUserQuery+` ORDER BY users_stats.pp_%[1]s_rx DESC, users_stats.ranked_score_%[1]s_rx DESC`, m)
query, params, _ := sqlx.In(query, results)
rows, err := md.DB.Query(query, params...)
if err != nil {
md.Err(err)
return Err500
}
for rows.Next() {
var u leaderboardUser
err := rows.Scan(
&u.ID, &u.Username, &u.RegisteredOn, &u.Privileges, &u.LatestActivity,
&u.UsernameAKA, &u.Country, &u.PlayStyle, &u.FavouriteMode,
&u.ChosenMode.RankedScore, &u.ChosenMode.TotalScore, &u.ChosenMode.PlayCount,
&u.ChosenMode.ReplaysWatched, &u.ChosenMode.TotalHits,
&u.ChosenMode.Accuracy, &u.ChosenMode.PP,
)
if err != nil {
md.Err(err)
continue
}
u.ChosenMode.Level = ocl.GetLevelPrecise(int64(u.ChosenMode.TotalScore))
if i := leaderboardPositionRx(md.R, m, u.ID); i != nil {
u.ChosenMode.GlobalLeaderboardRank = i
}
if i := countryPositionRx(md.R, m, u.ID, u.Country); i != nil {
u.ChosenMode.CountryLeaderboardRank = i
}
resp.Users = append(resp.Users, u)
}
return resp
}
// LeaderboardGET gets the leaderboard.
func LeaderboardApGET(md common.MethodData) common.CodeMessager {
m := getMode(md.Query("mode"))
// md.Query.Country
p := common.Int(md.Query("p")) - 1
if p < 0 {
p = 0
}
l := common.InString(1, md.Query("l"), 500, 50)
key := "ripple:leaderboard_auto:" + m
if md.Query("country") != "" {
key += ":" + md.Query("country")
}
results, err := md.R.ZRevRange(key, int64(p*l), int64(p*l+l-1)).Result()
if err != nil {
md.Err(err)
return Err500
}
var resp leaderboardResponse
resp.Code = 200
if len(results) == 0 {
return resp
}
query := fmt.Sprintf(lbUserQuery+` ORDER BY users_stats.pp_%[1]s_auto DESC, users_stats.ranked_score_%[1]s_ap DESC`, m)
query, params, _ := sqlx.In(query, results)
rows, err := md.DB.Query(query, params...)
if err != nil {
md.Err(err)
return Err500
}
for rows.Next() {
var u leaderboardUser
err := rows.Scan(
&u.ID, &u.Username, &u.RegisteredOn, &u.Privileges, &u.LatestActivity,
&u.UsernameAKA, &u.Country, &u.PlayStyle, &u.FavouriteMode,
&u.ChosenMode.RankedScore, &u.ChosenMode.TotalScore, &u.ChosenMode.PlayCount,
&u.ChosenMode.ReplaysWatched, &u.ChosenMode.TotalHits,
&u.ChosenMode.Accuracy, &u.ChosenMode.PP,
)
if err != nil {
md.Err(err)
continue
}
u.ChosenMode.Level = ocl.GetLevelPrecise(int64(u.ChosenMode.TotalScore))
if i := leaderboardPositionAp(md.R, m, u.ID); i != nil {
u.ChosenMode.GlobalLeaderboardRank = i
}
if i := countryPositionAp(md.R, m, u.ID, u.Country); i != nil {
u.ChosenMode.CountryLeaderboardRank = i
}
resp.Users = append(resp.Users, u)
}
return resp
}
func leaderboardPositionRx(r *redis.Client, mode string, user int) *int {
return _position(r, "ripple:leaderboard_relax:"+mode, user)
}
func countryPositionRx(r *redis.Client, mode string, user int, country string) *int {
return _position(r, "ripple:leaderboard_relax:"+mode+":"+strings.ToLower(country), user)
}
func leaderboardPositionAp(r *redis.Client, mode string, user int) *int {
return _position(r, "ripple:leaderboard_auto:"+mode, user)
}
func countryPositionAp(r *redis.Client, mode string, user int, country string) *int {
return _position(r, "ripple:leaderboard_auto:"+mode+":"+strings.ToLower(country), user)
}

View File

@ -12,7 +12,7 @@ import (
"github.com/jmoiron/sqlx"
"github.com/osuYozora/api/common"
"github.com/osuYozora/schiavolib"
"zxq.co/ripple/schiavolib"
)
// TokenSelfDeletePOST deletes the token the user is connecting with.