Add weekly rankings and ranking pagination support
parent
48c533cb78
commit
53ccdc5921
43
api/daily.go
43
api/daily.go
|
@ -81,8 +81,19 @@ func (s *Server) HandleSeed(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
func (s *Server) HandleRankings(w http.ResponseWriter, r *http.Request) {
|
||||
var err error
|
||||
var category int
|
||||
var page int
|
||||
|
||||
if r.URL.Query().Has("category") {
|
||||
category, err = strconv.Atoi(r.URL.Query().Get("category"))
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("failed to convert category: %s", err), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
category = 0
|
||||
}
|
||||
|
||||
if r.URL.Query().Has("page") {
|
||||
page, err = strconv.Atoi(r.URL.Query().Get("page"))
|
||||
if err != nil {
|
||||
|
@ -93,7 +104,7 @@ func (s *Server) HandleRankings(w http.ResponseWriter, r *http.Request) {
|
|||
page = 1
|
||||
}
|
||||
|
||||
rankings, err := db.FetchRankings(page)
|
||||
rankings, err := db.FetchRankings(category, page)
|
||||
if err != nil {
|
||||
log.Print("failed to retrieve rankings")
|
||||
}
|
||||
|
@ -106,3 +117,33 @@ func (s *Server) HandleRankings(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
w.Write(response)
|
||||
}
|
||||
|
||||
// /daily/rankingpagecount - fetch daily ranking page count
|
||||
|
||||
func (s *Server) HandleRankingPageCount(w http.ResponseWriter, r *http.Request) {
|
||||
var err error
|
||||
var category int
|
||||
|
||||
if r.URL.Query().Has("category") {
|
||||
category, err = strconv.Atoi(r.URL.Query().Get("category"))
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("failed to convert category: %s", err), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
category = 0
|
||||
}
|
||||
|
||||
pageCount, err := db.FetchRankingPageCount(category)
|
||||
if err != nil {
|
||||
log.Print("failed to retrieve ranking page count")
|
||||
}
|
||||
|
||||
response, err := json.Marshal(pageCount)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("failed to marshal response json: %s", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Write(response)
|
||||
}
|
||||
|
|
|
@ -47,6 +47,8 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
s.HandleSeed(w, r)
|
||||
case "/daily/rankings":
|
||||
s.HandleRankings(w, r)
|
||||
case "/daily/rankingpagecount":
|
||||
s.HandleRankingPageCount(w, r)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
33
db/daily.go
33
db/daily.go
|
@ -1,6 +1,8 @@
|
|||
package db
|
||||
|
||||
import (
|
||||
"math"
|
||||
|
||||
"github.com/Flashfyre/pokerogue-server/defs"
|
||||
)
|
||||
|
||||
|
@ -22,12 +24,20 @@ func AddOrUpdateAccountDailyRun(uuid []byte, score int, wave int) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func FetchRankings(page int) ([]defs.DailyRanking, error) {
|
||||
func FetchRankings(category int, page int) ([]defs.DailyRanking, error) {
|
||||
var rankings []defs.DailyRanking
|
||||
|
||||
offset := (page - 1) * 10
|
||||
|
||||
results, err := handle.Query("SELECT RANK() OVER (ORDER BY adr.score DESC, adr.timestamp), a.username, adr.score, adr.wave FROM accountDailyRuns adr JOIN dailyRuns dr ON dr.date = adr.date JOIN accounts a ON adr.uuid = a.uuid WHERE dr.date = UTC_DATE() LIMIT 10 OFFSET ?", offset)
|
||||
var query string
|
||||
switch category {
|
||||
case 0:
|
||||
query = "SELECT RANK() OVER (ORDER BY adr.score DESC, adr.timestamp), a.username, adr.score, adr.wave FROM accountDailyRuns adr JOIN dailyRuns dr ON dr.date = adr.date JOIN accounts a ON adr.uuid = a.uuid WHERE dr.date = UTC_DATE() LIMIT 10 OFFSET ?"
|
||||
case 1:
|
||||
query = "SELECT RANK() OVER (ORDER BY SUM(adr.score) DESC, adr.timestamp), a.username, SUM(adr.score), 0 FROM accountDailyRuns adr JOIN dailyRuns dr ON dr.date = adr.date JOIN accounts a ON adr.uuid = a.uuid WHERE dr.date >= DATE_SUB(DATE(UTC_TIMESTAMP()), INTERVAL DAYOFWEEK(UTC_TIMESTAMP()) - 1 DAY) GROUP BY a.username ORDER BY 1 LIMIT 10 OFFSET ?"
|
||||
}
|
||||
|
||||
results, err := handle.Query(query, offset)
|
||||
if err != nil {
|
||||
return rankings, err
|
||||
}
|
||||
|
@ -46,3 +56,22 @@ func FetchRankings(page int) ([]defs.DailyRanking, error) {
|
|||
|
||||
return rankings, nil
|
||||
}
|
||||
|
||||
func FetchRankingPageCount(category int) (int, error) {
|
||||
var recordCount int
|
||||
|
||||
var query string
|
||||
switch category {
|
||||
case 0:
|
||||
query = "SELECT COUNT(a.username) FROM accountDailyRuns adr JOIN dailyRuns dr ON dr.date = adr.date JOIN accounts a ON adr.uuid = a.uuid WHERE dr.date = UTC_DATE()"
|
||||
case 1:
|
||||
query = "SELECT COUNT(a.username) FROM accountDailyRuns adr JOIN dailyRuns dr ON dr.date = adr.date JOIN accounts a ON adr.uuid = a.uuid WHERE dr.date >= DATE_SUB(DATE(UTC_TIMESTAMP()), INTERVAL DAYOFWEEK(UTC_TIMESTAMP()) - 1 DAY)"
|
||||
}
|
||||
|
||||
err := handle.QueryRow(query).Scan(&recordCount)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return int(math.Ceil(float64(recordCount) / 10)), nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue