/
user_x.go
143 lines (126 loc) · 3.04 KB
/
user_x.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package peppy
import (
"fmt"
"strings"
"github.com/jmoiron/sqlx"
"github.com/osuAkatsuki/akatsuki-api/common"
"github.com/valyala/fasthttp"
"gopkg.in/thehowl/go-osuapi.v1"
"zxq.co/x/getrank"
)
// GetUserRecent retrieves an user's recent scores.
func GetUserRecent(c *fasthttp.RequestCtx, db *sqlx.DB) {
relax := query(c, "rx")
if relax == "" {
relax = "0"
}
rx := common.Int(relax)
table := "scores"
switch rx {
case 1:
table = "scores_relax"
case 2:
table = "scores_ap"
}
getUserX(c, db, "ORDER BY "+table+".time DESC", common.InString(1, query(c, "limit"), 50, 10))
}
// GetUserBest retrieves an user's best scores.
func GetUserBest(c *fasthttp.RequestCtx, db *sqlx.DB) {
var sb string
relax := query(c, "rx")
if relax == "" {
relax = "0"
}
rx := common.Int(relax)
table := "scores"
switch rx {
case 1:
table = "scores_relax"
case 2:
table = "scores_ap"
}
if rx != 0 {
sb = table + ".pp"
} else {
sb = table + ".score"
}
getUserX(c, db, "AND completed = '3' ORDER BY "+sb+" DESC", common.InString(1, query(c, "limit"), 100, 10))
}
func getUserX(c *fasthttp.RequestCtx, db *sqlx.DB, orderBy string, limit int) {
whereClause, p := genUser(c, db)
relax := query(c, "rx")
if relax == "" {
relax = "0"
}
rx := common.Int(relax)
table := "scores"
switch rx {
case 1:
table = "scores_relax"
case 2:
table = "scores_ap"
}
sqlQuery := fmt.Sprintf(
`SELECT
beatmaps.beatmap_id, %[1]s.score, %[1]s.max_combo,
%[1]s.300_count, %[1]s.100_count, %[1]s.50_count,
%[1]s.gekis_count, %[1]s.katus_count, %[1]s.misses_count,
%[1]s.full_combo, %[1]s.mods, users.id, %[1]s.time,
%[1]s.pp, %[1]s.accuracy
FROM %[1]s
LEFT JOIN beatmaps ON beatmaps.beatmap_md5 = %[1]s.beatmap_md5
LEFT JOIN users ON %[1]s.userid = users.id
WHERE %[2]s AND %[1]s.play_mode = ? AND users.privileges & 1 > 0
%[3]s
LIMIT %[4]d`, table, whereClause, orderBy, limit,
)
scores := make([]osuapi.GUSScore, 0, limit)
m := genmodei(query(c, "m"))
rows, err := db.Query(sqlQuery, p, m)
if err != nil {
json(c, 200, defaultResponse)
common.Err(c, err)
return
}
for rows.Next() {
var (
curscore osuapi.GUSScore
rawTime common.UnixTimestamp
acc float64
fc bool
mods int
bid *int
)
err := rows.Scan(
&bid, &curscore.Score.Score, &curscore.MaxCombo,
&curscore.Count300, &curscore.Count100, &curscore.Count50,
&curscore.CountGeki, &curscore.CountKatu, &curscore.CountMiss,
&fc, &mods, &curscore.UserID, &rawTime,
&curscore.PP, &acc,
)
if err != nil {
json(c, 200, defaultResponse)
common.Err(c, err)
return
}
if bid == nil {
curscore.BeatmapID = 0
} else {
curscore.BeatmapID = *bid
}
curscore.FullCombo = osuapi.OsuBool(fc)
curscore.Mods = osuapi.Mods(mods)
curscore.Date = osuapi.MySQLDate(rawTime)
curscore.Rank = strings.ToUpper(getrank.GetRank(
osuapi.Mode(m),
curscore.Mods,
acc,
curscore.Count300,
curscore.Count100,
curscore.Count50,
curscore.CountMiss,
))
scores = append(scores, curscore)
}
json(c, 200, scores)
}