-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_scores.go
127 lines (117 loc) · 3.31 KB
/
user_scores.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package v1
import (
"fmt"
"strings"
"gopkg.in/thehowl/go-osuapi.v1"
"zxq.co/ripple/rippleapi/common"
"zxq.co/x/getrank"
)
type userScore struct {
Score
Beatmap beatmap `json:"beatmap"`
}
type userScoresResponse struct {
common.ResponseBase
Scores []userScore `json:"scores"`
}
const userScoreSelectBase = `
SELECT
scores.id, scores.beatmap_md5, scores.score,
scores.max_combo, scores.full_combo, scores.mods,
scores.300_count, scores.100_count, scores.50_count,
scores.gekis_count, scores.katus_count, scores.misses_count,
scores.time, scores.play_mode, scores.accuracy, scores.pp,
scores.completed,
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
FROM scores
INNER JOIN beatmaps ON beatmaps.beatmap_md5 = scores.beatmap_md5
INNER JOIN users ON users.id = scores.userid
`
// UserScoresBestGET retrieves the best scores of an user, sorted by PP if
// mode is standard and sorted by ranked score otherwise.
func UserScoresBestGET(md common.MethodData) common.CodeMessager {
cm, wc, param := whereClauseUser(md, "users")
if cm != nil {
return *cm
}
mc := genModeClause(md)
// For all modes that have PP, we leave out 0 PP scores.
if getMode(md.Query("mode")) != "ctb" {
mc += " AND scores.pp > 0"
}
return scoresPuts(md, fmt.Sprintf(
`WHERE
scores.completed = '3'
AND %s
%s
AND `+md.User.OnlyUserPublic(true)+`
ORDER BY scores.pp DESC, scores.score DESC %s`,
wc, mc, common.Paginate(md.Query("p"), md.Query("l"), 100),
), param)
}
// UserScoresRecentGET retrieves an user's latest scores.
func UserScoresRecentGET(md common.MethodData) common.CodeMessager {
cm, wc, param := whereClauseUser(md, "users")
if cm != nil {
return *cm
}
return scoresPuts(md, fmt.Sprintf(
`WHERE
%s
%s
AND `+md.User.OnlyUserPublic(true)+`
ORDER BY scores.id DESC %s`,
wc, genModeClause(md), common.Paginate(md.Query("p"), md.Query("l"), 100),
), param)
}
func scoresPuts(md common.MethodData, whereClause string, params ...interface{}) common.CodeMessager {
rows, err := md.DB.Query(userScoreSelectBase+whereClause, params...)
if err != nil {
md.Err(err)
return Err500
}
var scores []userScore
for rows.Next() {
var (
us userScore
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)
}
r := userScoresResponse{}
r.Code = 200
r.Scores = scores
return r
}