-
Notifications
You must be signed in to change notification settings - Fork 0
/
score.go
98 lines (94 loc) · 2.54 KB
/
score.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
package peppy
import (
"database/sql"
"strconv"
"strings"
"github.com/JustinOsu/api/common"
"github.com/jmoiron/sqlx"
"github.com/valyala/fasthttp"
"gopkg.in/thehowl/go-osuapi.v1"
"zxq.co/x/getrank"
)
// GetScores retrieve information about the top 100 scores of a specified beatmap.
func GetScores(c *fasthttp.RequestCtx, db *sqlx.DB) {
if query(c, "b") == "" {
json(c, 200, defaultResponse)
return
}
var beatmapMD5 string
err := db.Get(&beatmapMD5, "SELECT beatmap_md5 FROM beatmaps WHERE beatmap_id = ? LIMIT 1", query(c, "b"))
switch {
case err == sql.ErrNoRows:
json(c, 200, defaultResponse)
return
case err != nil:
common.Err(c, err)
json(c, 200, defaultResponse)
return
}
var sb = "scores.score"
if rankable(query(c, "m")) {
sb = "scores.pp"
}
var (
extraWhere string
extraParams []interface{}
)
if query(c, "u") != "" {
w, p := genUser(c, db)
extraWhere = "AND " + w
extraParams = append(extraParams, p)
}
mods := common.Int(query(c, "mods"))
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,
scores.accuracy
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 = ?
AND scores.mods & ? = ?
`+extraWhere+`
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...)...)
if err != nil {
common.Err(c, err)
json(c, 200, defaultResponse)
return
}
var results []osuapi.GSScore
for rows.Next() {
var (
s osuapi.GSScore
fullcombo bool
mods int
date common.UnixTimestamp
accuracy float64
)
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,
&accuracy,
)
if err != nil {
if err != sql.ErrNoRows {
common.Err(c, err)
}
continue
}
s.FullCombo = osuapi.OsuBool(fullcombo)
s.Mods = osuapi.Mods(mods)
s.Date = osuapi.MySQLDate(date)
s.Rank = strings.ToUpper(getrank.GetRank(osuapi.Mode(genmodei(query(c, "m"))), s.Mods,
accuracy, s.Count300, s.Count100, s.Count50, s.CountMiss))
results = append(results, s)
}
json(c, 200, results)
return
}