-
Notifications
You must be signed in to change notification settings - Fork 0
/
beatmap.go
133 lines (122 loc) · 3.63 KB
/
beatmap.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
package peppy
import (
"strconv"
"strings"
"github.com/jmoiron/sqlx"
"github.com/thehowl/go-osuapi"
"github.com/valyala/fasthttp"
"zxq.co/ripple/rippleapi/common"
)
// GetBeatmap retrieves general beatmap information.
func GetBeatmap(c *fasthttp.RequestCtx, db *sqlx.DB) {
var whereClauses []string
var params []interface{}
limit := strconv.Itoa(common.InString(1, query(c, "limit"), 500, 500))
// since value is not stored, silently ignore
if query(c, "s") != "" {
whereClauses = append(whereClauses, "beatmaps.beatmapset_id = ?")
params = append(params, query(c, "s"))
}
if query(c, "b") != "" {
whereClauses = append(whereClauses, "beatmaps.beatmap_id = ?")
params = append(params, query(c, "b"))
// b is unique, so change limit to 1
limit = "1"
}
// creator is not stored, silently ignore u and type
if query(c, "m") != "" {
m := genmode(query(c, "m"))
if m == "std" {
// Since STD beatmaps are converted, all of the diffs must be != 0
for _, i := range modes {
whereClauses = append(whereClauses, "beatmaps.difficulty_"+i+" != 0")
}
} else {
whereClauses = append(whereClauses, "beatmaps.difficulty_"+m+" != 0")
if query(c, "a") == "1" {
whereClauses = append(whereClauses, "beatmaps.difficulty_std = 0")
}
}
}
if query(c, "h") != "" {
whereClauses = append(whereClauses, "beatmaps.beatmap_md5 = ?")
params = append(params, query(c, "h"))
}
where := strings.Join(whereClauses, " AND ")
if where != "" {
where = "WHERE " + where
}
rows, err := db.Query(`SELECT
beatmapset_id, beatmap_id, ranked, hit_length,
song_name, beatmap_md5, ar, od, bpm, playcount,
passcount, max_combo, difficulty_std, difficulty_taiko, difficulty_ctb, difficulty_mania,
latest_update
FROM beatmaps `+where+" ORDER BY id DESC LIMIT "+limit,
params...)
if err != nil {
common.Err(c, err)
json(c, 200, defaultResponse)
return
}
var bms []osuapi.Beatmap
for rows.Next() {
var (
bm osuapi.Beatmap
rawRankedStatus int
rawName string
rawLastUpdate common.UnixTimestamp
diffs [4]float64
)
err := rows.Scan(
&bm.BeatmapSetID, &bm.BeatmapID, &rawRankedStatus, &bm.HitLength,
&rawName, &bm.FileMD5, &bm.ApproachRate, &bm.OverallDifficulty, &bm.BPM, &bm.Playcount,
&bm.Passcount, &bm.MaxCombo, &diffs[0], &diffs[1], &diffs[2], &diffs[3],
&rawLastUpdate,
)
if err != nil {
common.Err(c, err)
continue
}
bm.TotalLength = bm.HitLength
bm.LastUpdate = osuapi.MySQLDate(rawLastUpdate)
if rawRankedStatus >= 2 {
bm.ApprovedDate = osuapi.MySQLDate(rawLastUpdate)
}
// zero value of ApprovedStatus == osuapi.StatusPending, so /shrug
bm.Approved = rippleToOsuRankedStatus[rawRankedStatus]
bm.Artist, bm.Title, bm.DiffName = parseDiffName(rawName)
for i, diffVal := range diffs {
if diffVal != 0 {
bm.Mode = osuapi.Mode(i)
bm.DifficultyRating = diffVal
break
}
}
bms = append(bms, bm)
}
json(c, 200, bms)
}
var rippleToOsuRankedStatus = map[int]osuapi.ApprovedStatus{
0: osuapi.StatusPending,
1: osuapi.StatusWIP, // it means "needs updating", as the one in the db needs to be updated, but whatever
2: osuapi.StatusRanked,
3: osuapi.StatusApproved,
4: osuapi.StatusQualified,
5: osuapi.StatusLoved,
}
// buggy diffname parser
func parseDiffName(name string) (author string, title string, diffName string) {
parts := strings.SplitN(name, " - ", 2)
author = parts[0]
if len(parts) > 1 {
title = parts[1]
if s := strings.Index(title, " ["); s != -1 {
diffName = title[s+2:]
if len(diffName) != 0 && diffName[len(diffName)-1] == ']' {
diffName = diffName[:len(diffName)-1]
}
title = title[:s]
}
}
return
}