Skip to content

Commit

Permalink
Add hypothetical rank endpoint (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsunyoku authored Dec 21, 2023
1 parent c629ca4 commit 47bf0ea
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func Start(dbO *sqlx.DB) *fhr.Router {
r.Method("/api/v1/scores", v1.ScoresGET)
r.Method("/api/v1/beatmaps/rank_requests/status", v1.BeatmapRankRequestsStatusGET)
r.Method("/api/v1/countries", v1.CountriesGET)
r.Method("/api/v1/hypothetical-rank", v1.HypotheticalRankGET)

// ReadConfidential privilege required
r.Method("/api/v1/friends", v1.FriendsGET, common.PrivilegeReadConfidential)
Expand Down
77 changes: 77 additions & 0 deletions app/v1/rank.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package v1

import (
"fmt"
"strconv"

"github.com/osuAkatsuki/akatsuki-api/common"
redis "gopkg.in/redis.v5"
)

type hypotheticalRankResponse struct {
common.ResponseBase
Rank int `json:"rank"`
}

func HypotheticalRankGET(md common.MethodData) common.CodeMessager {
modeInt, err := strconv.Atoi(md.Query("mode"))
if err != nil || modeInt > 3 || modeInt < 0 {
return common.SimpleResponse(400, "invalid mode")
}

mode := modesToReadable[modeInt]

rx, err := strconv.Atoi(md.Query("rx"))
if err != nil || rx > 2 || rx < 0 {
return common.SimpleResponse(400, "invalid relax int")
}

performancePoints, err := strconv.Atoi(md.Query("pp"))
if err != nil || performancePoints < 0 {
return common.SimpleResponse(400, "invalid performance points")
}

var rank int
var rankErr error
if rx == 0 {
rank, rankErr = rankAtPerformancePoints(md.R, mode, performancePoints)
} else if rx == 1 {
rank, rankErr = relaxRankAtPerformancePoints(md.R, mode, performancePoints)
} else if rx == 2 {
rank, rankErr = autopilotRankAtPerformancePoints(md.R, mode, performancePoints)
}

if rankErr != nil {
md.Err(err)
return common.SimpleResponse(500, "failed to calculate hypothetical rank")
}

resp := hypotheticalRankResponse{
Rank: rank,
}
resp.Code = 200
return resp
}

func rankAtPerformancePoints(r *redis.Client, mode string, performancePoints int) (int, error) {
return _rankAtPerformancePoints(r, "ripple:leaderboard:"+mode, performancePoints)
}

func relaxRankAtPerformancePoints(r *redis.Client, mode string, performancePoints int) (int, error) {
return _rankAtPerformancePoints(r, "ripple:relaxboard:"+mode, performancePoints)
}

func autopilotRankAtPerformancePoints(r *redis.Client, mode string, performancePoints int) (int, error) {
return _rankAtPerformancePoints(r, "ripple:autoboard:"+mode, performancePoints)
}

func _rankAtPerformancePoints(r *redis.Client, key string, performancePoints int) (int, error) {
res := r.ZCount(key, fmt.Sprintf("(%d", performancePoints), "inf")
err := res.Err()
if err != nil {
return -1, err
}

x := int(res.Val()) + 1
return x, nil
}

0 comments on commit 47bf0ea

Please sign in to comment.