ripple-api/app/v1/user_scores_ap.go

128 lines
3.5 KiB
Go
Raw Normal View History

2019-02-03 12:45:37 +00:00
package v1
import (
"fmt"
"strings"
"gopkg.in/thehowl/go-osuapi.v1"
2019-02-25 21:04:55 +00:00
"github.com/osuyozora/api/common"
2019-02-03 12:45:37 +00:00
"zxq.co/x/getrank"
)
2019-02-03 12:59:47 +00:00
type userScoreAuto struct {
2019-02-03 12:45:37 +00:00
Score
Beatmap beatmap `json:"beatmap"`
}
2019-02-03 12:59:47 +00:00
type userScoresResponseAuto struct {
2019-02-03 12:45:37 +00:00
common.ResponseBase
2019-02-03 12:59:47 +00:00
Scores []userScoreAuto `json:"scores"`
2019-02-03 12:45:37 +00:00
}
2019-02-03 12:59:47 +00:00
const userScoreSelectBaseAp = `
2019-02-03 12:45:37 +00:00
SELECT
2019-02-03 12:49:32 +00:00
scores_auto.id, scores_auto.beatmap_md5, scores_auto.score,
scores_auto.max_combo, scores_auto.full_combo, scores_auto.mods,
scores_auto.300_count, scores_auto.100_count, scores_auto.50_count,
scores_auto.gekis_count, scores_auto.katus_count, scores_auto.misses_count,
scores_auto.time, scores_auto.play_mode, scores_auto.accuracy, scores_auto.pp,
scores_auto.completed,
2019-02-03 12:45:37 +00:00
beatmaps.beatmap_id, beatmaps.beatmapset_id, beatmaps.beatmap_md5,
beatmaps.song_name, beatmaps.ar, beatmaps.od, beatmaps.difficulty_std,
beatmaps.difficulty_taiko, beatmaps.difficulty_ctb, beatmaps.difficulty_mania,
beatmaps.max_combo, beatmaps.hit_length, beatmaps.ranked,
beatmaps.ranked_status_freezed, beatmaps.latest_update
2019-02-03 13:07:54 +00:00
FROM scores_auto
2019-02-03 12:49:32 +00:00
INNER JOIN beatmaps ON beatmaps.beatmap_md5 = scores_auto.beatmap_md5
INNER JOIN users ON users.id = scores_auto.userid
2019-02-03 12:45:37 +00:00
`
// UserScoresBestGET retrieves the best scores of an user, sorted by PP if
// mode is standard and sorted by ranked score otherwise.
2019-02-03 12:49:32 +00:00
func UserScoresBestAPGET(md common.MethodData) common.CodeMessager {
2019-02-03 12:45:37 +00:00
cm, wc, param := whereClauseUser(md, "users")
if cm != nil {
return *cm
}
2019-02-03 13:21:31 +00:00
mc := genModeClauseAp(md)
2019-02-03 12:49:32 +00:00
// For all modes that have PP, we leave out 0 PP scores_auto.
2019-02-03 12:45:37 +00:00
if getMode(md.Query("mode")) != "ctb" {
2019-02-03 12:49:32 +00:00
mc += " AND scores_auto.pp > 0"
2019-02-03 12:45:37 +00:00
}
2019-02-03 12:59:47 +00:00
return scoresPutsAp(md, fmt.Sprintf(
2019-02-03 12:45:37 +00:00
`WHERE
2019-02-03 12:49:32 +00:00
scores_auto.completed = '3'
2019-02-03 12:45:37 +00:00
AND %s
%s
AND `+md.User.OnlyUserPublic(true)+`
2019-02-03 12:49:32 +00:00
ORDER BY scores_auto.pp DESC, scores_auto.score DESC %s`,
2019-02-03 12:45:37 +00:00
wc, mc, common.Paginate(md.Query("p"), md.Query("l"), 100),
), param)
}
2019-02-03 12:49:32 +00:00
// UserScoresRecentGET retrieves an user's latest scores_auto.
func UserScoresRecentAPGET(md common.MethodData) common.CodeMessager {
2019-02-03 12:45:37 +00:00
cm, wc, param := whereClauseUser(md, "users")
if cm != nil {
return *cm
}
2019-02-03 12:59:47 +00:00
return scoresPutsAp(md, fmt.Sprintf(
2019-02-03 12:45:37 +00:00
`WHERE
%s
%s
AND `+md.User.OnlyUserPublic(true)+`
2019-02-03 12:49:32 +00:00
ORDER BY scores_auto.id DESC %s`,
2019-02-03 13:21:31 +00:00
wc, genModeClauseAp(md), common.Paginate(md.Query("p"), md.Query("l"), 100),
2019-02-03 12:45:37 +00:00
), param)
}
2019-02-03 12:59:47 +00:00
func scoresPutsAp(md common.MethodData, whereClause string, params ...interface{}) common.CodeMessager {
rows, err := md.DB.Query(userScoreSelectBaseAp+whereClause, params...)
2019-02-03 12:45:37 +00:00
if err != nil {
md.Err(err)
return Err500
}
2019-02-03 12:59:47 +00:00
var scores []userScoreAuto
2019-02-03 12:45:37 +00:00
for rows.Next() {
var (
2019-02-03 12:59:47 +00:00
us userScoreAuto
2019-02-03 12:45:37 +00:00
b beatmap
)
err = rows.Scan(
&us.ID, &us.BeatmapMD5, &us.Score.Score,
&us.MaxCombo, &us.FullCombo, &us.Mods,
&us.Count300, &us.Count100, &us.Count50,
&us.CountGeki, &us.CountKatu, &us.CountMiss,
&us.Time, &us.PlayMode, &us.Accuracy, &us.PP,
&us.Completed,
&b.BeatmapID, &b.BeatmapsetID, &b.BeatmapMD5,
&b.SongName, &b.AR, &b.OD, &b.Diff2.STD,
&b.Diff2.Taiko, &b.Diff2.CTB, &b.Diff2.Mania,
&b.MaxCombo, &b.HitLength, &b.Ranked,
&b.RankedStatusFrozen, &b.LatestUpdate,
)
if err != nil {
md.Err(err)
return Err500
}
b.Difficulty = b.Diff2.STD
us.Beatmap = b
us.Rank = strings.ToUpper(getrank.GetRank(
osuapi.Mode(us.PlayMode),
osuapi.Mods(us.Mods),
us.Accuracy,
us.Count300,
us.Count100,
us.Count50,
us.CountMiss,
))
scores = append(scores, us)
}
2019-02-03 12:59:47 +00:00
r := userScoresResponseAuto{}
2019-02-03 12:45:37 +00:00
r.Code = 200
r.Scores = scores
return r
}