From 3782ed3c7c8fd4bb99dc3dec96e1330bde0b13c6 Mon Sep 17 00:00:00 2001 From: Howl Date: Wed, 27 Apr 2016 20:03:06 +0200 Subject: [PATCH] beatmap ranking in API --- app/start.go | 3 +++ app/v1/beatmap.go | 61 ++++++++++++++++++++++++++++++++++++++++++++ common/privileges.go | 10 +++++++- 3 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 app/v1/beatmap.go diff --git a/app/start.go b/app/start.go index 2ed336f..8e294bc 100644 --- a/app/start.go +++ b/app/start.go @@ -46,6 +46,9 @@ func Start(conf common.Conf, db *sql.DB) *gin.Engine { gv1.POST("/friends/del", Method(v1.FriendsDelPOST, db, common.PrivilegeWrite)) gv1.GET("/friends/del/:id", Method(v1.FriendsDelGET, db, common.PrivilegeWrite)) + // Admin: beatmap + gv1.POST("/beatmaps/set_status", Method(v1.BeatmapSetStatusPOST, db, common.PrivilegeBeatmap)) + // Admin: user managing gv1.POST("/users/manage/set_allowed", Method(v1.UserManageSetAllowedPOST, db, common.PrivilegeManageUser)) diff --git a/app/v1/beatmap.go b/app/v1/beatmap.go new file mode 100644 index 0000000..020721c --- /dev/null +++ b/app/v1/beatmap.go @@ -0,0 +1,61 @@ +package v1 + +import "git.zxq.co/ripple/rippleapi/common" + +type beatmap struct { + BeatmapID int `json:"beatmap_id"` + BeatmapsetID int `json:"beatmapset_id"` + BeatmapMD5 int `json:"beatmap_md5"` + SongName int `json:"song_name"` + AR float32 `json:"ar"` + OD float32 `json:"od"` + Difficulty float64 `json:"difficulty"` + MaxCombo int `json:"max_combo"` + HitLength int `json:"hit_length"` + BPM float64 `json:"bpm"` + Ranked int `json:"ranked"` + RankedStatusFrozen int `json:"ranked_status_frozen"` + LatestUpdate int `json:"latest_update"` +} +type beatmapResponse struct { + common.ResponseBase + beatmap +} +type beatmapSetResponse struct { + common.ResponseBase + Beatmaps []beatmap `json:"beatmaps"` +} + +type beatmapSetStatusData struct { + BeatmapSetID int `json:"beatmapset_id"` + RankedStatus int `json:"ranked_status"` + Frozen int `json:"frozen"` +} + +// BeatmapSetStatusPOST changes the ranked status of a beatmap, and whether the beatmap ranked status is frozen. Or freezed. Freezed best meme 2k16 +func BeatmapSetStatusPOST(md common.MethodData) common.CodeMessager { + var req beatmapSetStatusData + md.RequestData.Unmarshal(&req) + + var miss []string + if req.BeatmapSetID == 0 { + miss = append(miss, "beatmapset_id") + } + if len(miss) != 0 { + return ErrMissingField(miss...) + } + + if req.Frozen != 0 && req.Frozen != 1 { + return common.SimpleResponse(400, "frozen status must be either 0 or 1") + } + if req.RankedStatus > 3 || -2 > req.RankedStatus { + return common.SimpleResponse(400, "ranked status must be 4 < x < -3") + } + + md.DB.Exec("UPDATE beatmaps SET ranked = ?, ranked_status_freezed = ? WHERE beatmapset_id = ?", req.RankedStatus, req.Frozen, req.BeatmapSetID) + + // TODO: replace with beatmapSetResponse when implemented + return common.ResponseBase{ + Code: 200, + } +} diff --git a/common/privileges.go b/common/privileges.go index 614f0d0..e8abcc6 100644 --- a/common/privileges.go +++ b/common/privileges.go @@ -16,6 +16,7 @@ const ( PrivilegeManageAPIKeys // admin permission to manage user permission, not only self permissions. Only ever do this if you completely trust the application, because this essentially means to put the entire ripple database in the hands of a (potentially evil?) application. PrivilegeBlog // can do pretty much anything to the blog, and the documentation. PrivilegeAPIMeta // can do /meta API calls. basically means they can restart the API server. + PrivilegeBeatmap // rank/unrank beatmaps. also BAT when implemented ) // Privileges is a bitwise enum of the privileges of an user's API key. @@ -76,11 +77,16 @@ func (p Privileges) HasPrivilegeBlog() bool { return p&PrivilegeBlog != 0 } -// HasPrivilegeAPIMeta returns whether the Blog privilege is included in the privileges. +// HasPrivilegeAPIMeta returns whether the APIMeta privilege is included in the privileges. func (p Privileges) HasPrivilegeAPIMeta() bool { return p&PrivilegeAPIMeta != 0 } +// HasPrivilegeBeatmap returns whether the Beatmap privilege is included in the privileges. +func (p Privileges) HasPrivilegeBeatmap() bool { + return p&PrivilegeBeatmap != 0 +} + var privilegeString = [...]string{ "Read", "ReadConfidential", @@ -94,6 +100,7 @@ var privilegeString = [...]string{ "ManageAPIKeys", "Blog", "APIMeta", + "Beatmap", } func (p Privileges) String() string { @@ -119,6 +126,7 @@ var privilegeMustBe = [...]int{ 4, 3, 4, + 4, } // CanOnly removes any privilege that the user has requested to have, but cannot have due to their rank.