From 954ec755b8ca73d9480dcc57abd98df2ecc6d989 Mon Sep 17 00:00:00 2001 From: Howl Date: Mon, 15 Aug 2016 21:13:40 +0200 Subject: [PATCH] Implement get_scores in peppyapi --- app/peppy/common.go | 4 +++ app/peppy/score.go | 78 +++++++++++++++++++++++++++++++++++++++++++++ app/peppy/user_x.go | 2 +- app/start.go | 1 + 4 files changed, 84 insertions(+), 1 deletion(-) diff --git a/app/peppy/common.go b/app/peppy/common.go index 87d44bb..b8b95e3 100644 --- a/app/peppy/common.go +++ b/app/peppy/common.go @@ -32,6 +32,10 @@ func genmodei(m string) int { } return v } +func rankable(m string) bool { + x := genmodei(m) + return x == 0 || x == 3 +} func genUser(c *gin.Context, db *sqlx.DB) (string, string) { var whereClause string diff --git a/app/peppy/score.go b/app/peppy/score.go index 3fdbc4b..e618025 100644 --- a/app/peppy/score.go +++ b/app/peppy/score.go @@ -1 +1,79 @@ package peppy + +import ( + "database/sql" + "strconv" + + "git.zxq.co/ripple/rippleapi/common" + + "github.com/gin-gonic/gin" + "github.com/jmoiron/sqlx" + "github.com/thehowl/go-osuapi" +) + +// GetScores retrieve information about the top 100 scores of a specified beatmap. +func GetScores(c *gin.Context, db *sqlx.DB) { + if c.Query("b") == "" { + c.JSON(200, defaultResponse) + return + } + var beatmapMD5 string + err := db.Get(&beatmapMD5, "SELECT beatmap_md5 FROM beatmaps WHERE beatmap_id = ? LIMIT 1", c.Query("b")) + switch { + case err == sql.ErrNoRows: + c.JSON(200, defaultResponse) + return + case err != nil: + c.Error(err) + c.JSON(200, defaultResponse) + return + } + var sb = "scores.score" + if rankable(c.Query("m")) { + sb = "scores.pp" + } + rows, err := db.Query(` +SELECT + scores.id, scores.score, users.username, scores.300_count, scores.100_count, + scores.50_count, scores.misses_count, scores.gekis_count, scores.katus_count, + scores.max_combo, scores.full_combo, scores.mods, users.id, scores.time, scores.pp +FROM scores +INNER JOIN users ON users.id = scores.userid +WHERE scores.completed = '3' + AND users.privileges & 1 > 0 + AND scores.beatmap_md5 = ? + AND scores.play_mode = ? +ORDER BY `+sb+` DESC LIMIT `+strconv.Itoa(common.InString(1, c.Query("limit"), 100, 50)), + beatmapMD5, genmodei(c.Query("m"))) + if err != nil { + c.Error(err) + c.JSON(200, defaultResponse) + return + } + var results []osuapi.GSScore + for rows.Next() { + var ( + s osuapi.GSScore + fullcombo bool + mods int + date common.OsuTime + ) + err := rows.Scan( + &s.ScoreID, &s.Score.Score, &s.Username, &s.Count300, &s.Count100, + &s.Count50, &s.CountMiss, &s.CountGeki, &s.CountKatu, + &s.MaxCombo, &fullcombo, &mods, &s.UserID, &date, &s.PP, + ) + if err != nil { + if err != sql.ErrNoRows { + c.Error(err) + } + continue + } + s.FullCombo = osuapi.OsuBool(fullcombo) + s.Mods = osuapi.Mods(mods) + s.Date = osuapi.MySQLDate(date) + results = append(results, s) + } + c.JSON(200, results) + return +} diff --git a/app/peppy/user_x.go b/app/peppy/user_x.go index fbd3883..5a3ff45 100644 --- a/app/peppy/user_x.go +++ b/app/peppy/user_x.go @@ -20,7 +20,7 @@ func GetUserRecent(c *gin.Context, db *sqlx.DB) { // GetUserBest retrieves an user's best scores. func GetUserBest(c *gin.Context, db *sqlx.DB) { var sb string - if genmodei(c.Query("m")) == 0 { + if rankable(c.Query("m")) { sb = "scores.pp" } else { sb = "scores.score" diff --git a/app/start.go b/app/start.go index 01695dd..999d13b 100644 --- a/app/start.go +++ b/app/start.go @@ -119,6 +119,7 @@ func Start(conf common.Conf, dbO *sqlx.DB) *gin.Engine { api.GET("/get_match", PeppyMethod(peppy.GetMatch)) api.GET("/get_user_recent", PeppyMethod(peppy.GetUserRecent)) api.GET("/get_user_best", PeppyMethod(peppy.GetUserBest)) + api.GET("/get_scores", PeppyMethod(peppy.GetScores)) } r.NoRoute(v1.Handle404)