2016-05-15 11:57:04 +00:00
|
|
|
package peppy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"strconv"
|
|
|
|
|
2016-06-13 19:48:09 +00:00
|
|
|
"git.zxq.co/ripple/rippleapi/common"
|
|
|
|
|
2016-05-15 11:57:04 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2016-08-15 11:45:42 +00:00
|
|
|
"github.com/jmoiron/sqlx"
|
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)
|
|
|
|
return x == 0 || x == 3
|
|
|
|
}
|
2016-05-15 11:57:04 +00:00
|
|
|
|
2016-08-15 11:45:42 +00:00
|
|
|
func genUser(c *gin.Context, 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
|
2016-05-18 15:47:39 +00:00
|
|
|
s, err := strconv.Atoi(c.Query("u"))
|
2016-05-15 11:57:04 +00:00
|
|
|
|
|
|
|
switch {
|
|
|
|
// We know for sure that it's an username.
|
|
|
|
case c.Query("type") == "string":
|
2016-05-18 16:37:36 +00:00
|
|
|
whereClause = "users.username = ?"
|
2016-05-15 11:57:04 +00:00
|
|
|
p = c.Query("u")
|
|
|
|
// 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-05-15 11:57:04 +00:00
|
|
|
p = c.Query("u")
|
2016-05-18 16:37:36 +00:00
|
|
|
whereClause = "users.username = ?"
|
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:
|
|
|
|
p = c.Query("u")
|
2016-05-18 16:37:36 +00:00
|
|
|
whereClause = "users.username = ?"
|
2016-05-15 11:57:04 +00:00
|
|
|
}
|
|
|
|
return whereClause, p
|
|
|
|
}
|