2016-08-15 18:07:40 +00:00
|
|
|
package peppy
|
2016-08-15 19:13:40 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"strconv"
|
2016-08-15 19:37:24 +00:00
|
|
|
"strings"
|
2016-08-15 19:13:40 +00:00
|
|
|
|
2019-02-25 21:04:55 +00:00
|
|
|
"github.com/osuyozora/api/common"
|
2016-08-15 19:13:40 +00:00
|
|
|
|
|
|
|
"github.com/jmoiron/sqlx"
|
2017-02-02 12:40:28 +00:00
|
|
|
"github.com/valyala/fasthttp"
|
2016-08-15 19:37:24 +00:00
|
|
|
"gopkg.in/thehowl/go-osuapi.v1"
|
2017-02-02 12:40:28 +00:00
|
|
|
"zxq.co/x/getrank"
|
2016-08-15 19:13:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetScores retrieve information about the top 100 scores of a specified beatmap.
|
2017-02-02 12:40:28 +00:00
|
|
|
func GetScores(c *fasthttp.RequestCtx, db *sqlx.DB) {
|
|
|
|
if query(c, "b") == "" {
|
|
|
|
json(c, 200, defaultResponse)
|
2016-08-15 19:13:40 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
var beatmapMD5 string
|
2017-02-02 12:40:28 +00:00
|
|
|
err := db.Get(&beatmapMD5, "SELECT beatmap_md5 FROM beatmaps WHERE beatmap_id = ? LIMIT 1", query(c, "b"))
|
2016-08-15 19:13:40 +00:00
|
|
|
switch {
|
|
|
|
case err == sql.ErrNoRows:
|
2017-02-02 12:40:28 +00:00
|
|
|
json(c, 200, defaultResponse)
|
2016-08-15 19:13:40 +00:00
|
|
|
return
|
|
|
|
case err != nil:
|
2017-02-02 12:40:28 +00:00
|
|
|
common.Err(c, err)
|
|
|
|
json(c, 200, defaultResponse)
|
2016-08-15 19:13:40 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
var sb = "scores.score"
|
2017-02-02 12:40:28 +00:00
|
|
|
if rankable(query(c, "m")) {
|
2016-08-15 19:13:40 +00:00
|
|
|
sb = "scores.pp"
|
|
|
|
}
|
2016-08-15 19:51:51 +00:00
|
|
|
var (
|
|
|
|
extraWhere string
|
|
|
|
extraParams []interface{}
|
|
|
|
)
|
2017-02-02 12:40:28 +00:00
|
|
|
if query(c, "u") != "" {
|
2016-08-15 19:51:51 +00:00
|
|
|
w, p := genUser(c, db)
|
|
|
|
extraWhere = "AND " + w
|
|
|
|
extraParams = append(extraParams, p)
|
|
|
|
}
|
2017-02-02 12:40:28 +00:00
|
|
|
mods := common.Int(query(c, "mods"))
|
2016-08-15 19:13:40 +00:00
|
|
|
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,
|
2016-08-15 19:37:24 +00:00
|
|
|
scores.max_combo, scores.full_combo, scores.mods, users.id, scores.time, scores.pp,
|
|
|
|
scores.accuracy
|
2016-08-15 19:13:40 +00:00
|
|
|
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 = ?
|
2016-10-25 18:03:33 +00:00
|
|
|
AND scores.mods & ? = ?
|
2016-08-15 19:51:51 +00:00
|
|
|
`+extraWhere+`
|
2017-02-02 12:40:28 +00:00
|
|
|
ORDER BY `+sb+` DESC LIMIT `+strconv.Itoa(common.InString(1, query(c, "limit"), 100, 50)),
|
|
|
|
append([]interface{}{beatmapMD5, genmodei(query(c, "m")), mods, mods}, extraParams...)...)
|
2016-08-15 19:13:40 +00:00
|
|
|
if err != nil {
|
2017-02-02 12:40:28 +00:00
|
|
|
common.Err(c, err)
|
|
|
|
json(c, 200, defaultResponse)
|
2016-08-15 19:13:40 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
var results []osuapi.GSScore
|
|
|
|
for rows.Next() {
|
|
|
|
var (
|
|
|
|
s osuapi.GSScore
|
|
|
|
fullcombo bool
|
|
|
|
mods int
|
2016-08-19 15:02:51 +00:00
|
|
|
date common.UnixTimestamp
|
2016-08-15 19:37:24 +00:00
|
|
|
accuracy float64
|
2016-08-15 19:13:40 +00:00
|
|
|
)
|
|
|
|
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,
|
2016-08-15 19:37:24 +00:00
|
|
|
&accuracy,
|
2016-08-15 19:13:40 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
if err != sql.ErrNoRows {
|
2017-02-02 12:40:28 +00:00
|
|
|
common.Err(c, err)
|
2016-08-15 19:13:40 +00:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
s.FullCombo = osuapi.OsuBool(fullcombo)
|
|
|
|
s.Mods = osuapi.Mods(mods)
|
|
|
|
s.Date = osuapi.MySQLDate(date)
|
2017-02-02 12:40:28 +00:00
|
|
|
s.Rank = strings.ToUpper(getrank.GetRank(osuapi.Mode(genmodei(query(c, "m"))), s.Mods,
|
2016-08-15 19:37:24 +00:00
|
|
|
accuracy, s.Count300, s.Count100, s.Count50, s.CountMiss))
|
2016-08-15 19:13:40 +00:00
|
|
|
results = append(results, s)
|
|
|
|
}
|
2017-02-02 12:40:28 +00:00
|
|
|
json(c, 200, results)
|
2016-08-15 19:13:40 +00:00
|
|
|
return
|
|
|
|
}
|