2016-05-15 11:57:04 +00:00
|
|
|
package peppy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2017-02-02 12:40:28 +00:00
|
|
|
_json "encoding/json"
|
2016-05-15 11:57:04 +00:00
|
|
|
"strconv"
|
|
|
|
|
2016-08-15 11:45:42 +00:00
|
|
|
"github.com/jmoiron/sqlx"
|
2017-02-02 12:40:28 +00:00
|
|
|
"github.com/valyala/fasthttp"
|
2019-02-25 21:04:55 +00:00
|
|
|
"github.com/osuyozora/api/common"
|
2016-05-15 11:57:04 +00:00
|
|
|
)
|
|
|
|
|
2016-08-22 21:04:52 +00:00
|
|
|
var modes = []string{"std", "taiko", "ctb", "mania"}
|
|
|
|
|
2016-05-28 18:24:39 +00:00
|
|
|
var defaultResponse = []struct{}{}
|
|
|
|
|
2016-05-15 11:57:04 +00:00
|
|
|
func genmode(m string) string {
|
2016-08-22 21:04:52 +00:00
|
|
|
i := genmodei(m)
|
|
|
|
return modes[i]
|
2016-05-15 11:57:04 +00:00
|
|
|
}
|
2016-05-28 18:24:39 +00:00
|
|
|
func genmodei(m string) int {
|
2016-06-13 19:48:09 +00:00
|
|
|
v := common.Int(m)
|
2016-05-28 18:24:39 +00:00
|
|
|
if v > 3 || v < 0 {
|
|
|
|
v = 0
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
2016-08-15 19:13:40 +00:00
|
|
|
func rankable(m string) bool {
|
|
|
|
x := genmodei(m)
|
2017-09-18 19:38:33 +00:00
|
|
|
return x != 2
|
2016-08-15 19:13:40 +00:00
|
|
|
}
|
2016-05-15 11:57:04 +00:00
|
|
|
|
2017-02-02 12:40:28 +00:00
|
|
|
func genUser(c *fasthttp.RequestCtx, db *sqlx.DB) (string, string) {
|
2016-05-15 11:57:04 +00:00
|
|
|
var whereClause string
|
|
|
|
var p string
|
|
|
|
|
|
|
|
// used in second case of switch
|
2017-02-02 12:40:28 +00:00
|
|
|
s, err := strconv.Atoi(query(c, "u"))
|
2016-05-15 11:57:04 +00:00
|
|
|
|
|
|
|
switch {
|
|
|
|
// We know for sure that it's an username.
|
2017-02-02 12:40:28 +00:00
|
|
|
case query(c, "type") == "string":
|
2016-10-16 16:52:34 +00:00
|
|
|
whereClause = "users.username_safe = ?"
|
2017-02-02 12:40:28 +00:00
|
|
|
p = common.SafeUsername(query(c, "u"))
|
2016-05-15 11:57:04 +00:00
|
|
|
// It could be an user ID, so we look for an user with that username first.
|
|
|
|
case err == nil:
|
2016-05-18 15:47:39 +00:00
|
|
|
err = db.QueryRow("SELECT id FROM users WHERE id = ? LIMIT 1", s).Scan(&p)
|
2016-05-15 11:57:04 +00:00
|
|
|
if err == sql.ErrNoRows {
|
2016-05-18 15:47:39 +00:00
|
|
|
// If no user with that userID were found, assume username.
|
2016-10-16 16:52:34 +00:00
|
|
|
whereClause = "users.username_safe = ?"
|
2017-02-02 12:40:28 +00:00
|
|
|
p = common.SafeUsername(query(c, "u"))
|
2016-05-18 15:47:39 +00:00
|
|
|
} else {
|
|
|
|
// An user with that userID was found. Thus it's an userID.
|
2016-05-18 16:37:36 +00:00
|
|
|
whereClause = "users.id = ?"
|
2016-05-15 11:57:04 +00:00
|
|
|
}
|
|
|
|
// u contains letters, so it's an username.
|
|
|
|
default:
|
2016-10-16 16:52:34 +00:00
|
|
|
whereClause = "users.username_safe = ?"
|
2017-02-02 12:40:28 +00:00
|
|
|
p = common.SafeUsername(query(c, "u"))
|
2016-05-15 11:57:04 +00:00
|
|
|
}
|
|
|
|
return whereClause, p
|
|
|
|
}
|
2017-02-02 12:40:28 +00:00
|
|
|
|
|
|
|
func query(c *fasthttp.RequestCtx, s string) string {
|
|
|
|
return string(c.QueryArgs().Peek(s))
|
|
|
|
}
|
|
|
|
|
|
|
|
func json(c *fasthttp.RequestCtx, code int, data interface{}) {
|
|
|
|
c.SetStatusCode(code)
|
|
|
|
d, err := _json.Marshal(data)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
c.Write(d)
|
|
|
|
}
|