2016-04-03 17:59:27 +00:00
|
|
|
// Package peppy implements the osu! API as defined on the osu-api repository wiki (https://github.com/ppy/osu-api/wiki).
|
|
|
|
package peppy
|
2016-05-15 11:57:04 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
2017-04-11 21:18:02 +00:00
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"strings"
|
2016-05-15 11:57:04 +00:00
|
|
|
|
2016-08-15 11:45:42 +00:00
|
|
|
"github.com/jmoiron/sqlx"
|
2016-05-15 11:57:04 +00:00
|
|
|
"github.com/thehowl/go-osuapi"
|
2017-02-02 12:40:28 +00:00
|
|
|
"github.com/valyala/fasthttp"
|
2017-04-11 21:18:02 +00:00
|
|
|
"gopkg.in/redis.v5"
|
2019-02-23 13:09:10 +00:00
|
|
|
"github.com/osuYozora/ocl"
|
|
|
|
"github.com/osuYozora/rippleapi/common"
|
2016-05-15 11:57:04 +00:00
|
|
|
)
|
|
|
|
|
2017-04-11 21:18:02 +00:00
|
|
|
// R is a redis client.
|
|
|
|
var R *redis.Client
|
|
|
|
|
2016-05-15 11:57:04 +00:00
|
|
|
// GetUser retrieves general user information.
|
2017-02-02 12:40:28 +00:00
|
|
|
func GetUser(c *fasthttp.RequestCtx, db *sqlx.DB) {
|
|
|
|
if query(c, "u") == "" {
|
|
|
|
json(c, 200, defaultResponse)
|
2016-05-15 11:57:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
var user osuapi.User
|
|
|
|
whereClause, p := genUser(c, db)
|
2016-05-18 16:37:36 +00:00
|
|
|
whereClause = "WHERE " + whereClause
|
2016-05-15 11:57:04 +00:00
|
|
|
|
2017-02-02 12:40:28 +00:00
|
|
|
mode := genmode(query(c, "m"))
|
2016-05-15 11:57:04 +00:00
|
|
|
|
|
|
|
err := db.QueryRow(fmt.Sprintf(
|
|
|
|
`SELECT
|
|
|
|
users.id, users.username,
|
|
|
|
users_stats.playcount_%s, users_stats.ranked_score_%s, users_stats.total_score_%s,
|
2017-04-11 21:18:02 +00:00
|
|
|
users_stats.pp_%s, users_stats.avg_accuracy_%s,
|
2016-07-06 21:43:43 +00:00
|
|
|
users_stats.country
|
2016-05-15 11:57:04 +00:00
|
|
|
FROM users
|
|
|
|
LEFT JOIN users_stats ON users_stats.id = users.id
|
|
|
|
%s
|
|
|
|
LIMIT 1`,
|
2017-04-11 21:18:02 +00:00
|
|
|
mode, mode, mode, mode, mode, whereClause,
|
2016-05-15 11:57:04 +00:00
|
|
|
), p).Scan(
|
|
|
|
&user.UserID, &user.Username,
|
|
|
|
&user.Playcount, &user.RankedScore, &user.TotalScore,
|
2017-04-11 21:18:02 +00:00
|
|
|
&user.PP, &user.Accuracy,
|
2016-07-06 21:43:43 +00:00
|
|
|
&user.Country,
|
2016-05-15 11:57:04 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
2017-02-02 12:40:28 +00:00
|
|
|
json(c, 200, defaultResponse)
|
2016-05-30 17:18:31 +00:00
|
|
|
if err != sql.ErrNoRows {
|
2017-02-02 12:40:28 +00:00
|
|
|
common.Err(c, err)
|
2016-05-30 17:18:31 +00:00
|
|
|
}
|
2016-05-15 11:57:04 +00:00
|
|
|
return
|
|
|
|
}
|
2017-04-11 21:18:02 +00:00
|
|
|
|
|
|
|
user.Rank = int(R.ZRevRank("ripple:leaderboard:"+mode, strconv.Itoa(user.UserID)).Val()) + 1
|
|
|
|
user.CountryRank = int(R.ZRevRank("ripple:leaderboard:"+mode+":"+strings.ToLower(user.Country), strconv.Itoa(user.UserID)).Val()) + 1
|
2016-05-17 15:00:09 +00:00
|
|
|
user.Level = ocl.GetLevelPrecise(user.TotalScore)
|
2016-05-15 11:57:04 +00:00
|
|
|
|
2017-02-02 12:40:28 +00:00
|
|
|
json(c, 200, []osuapi.User{user})
|
2016-05-15 11:57:04 +00:00
|
|
|
}
|