Skip to content

Commit 60beecb

Browse files
committed
Merge branch 'match_type_leaderboard' into develop
2 parents 3436cd6 + 2b7bca1 commit 60beecb

17 files changed

Lines changed: 198 additions & 48 deletions
File renamed without changes.

cmd/recalculate_badge.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ var recalculateBadgeCmd = &cobra.Command{
1111
Short: "Recalculate badge",
1212
Long: `Recalculate badges earned by each player`,
1313
PersistentPreRun: func(cmd *cobra.Command, args []string) {
14+
SetupConfig(cmd)
1415
models.InitDB(models.GetMysqlConnectionString())
1516
},
1617
}

cmd/recalculate_elo.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ var recalculateEloCmd = &cobra.Command{
1515
This will reset the elo for all players, and regenerate the elo changelog
1616
Elo will be recalculated based on 'updated_at' timestamp of each match`,
1717
Run: func(cmd *cobra.Command, args []string) {
18+
SetupConfig(cmd)
1819
models.InitDB(models.GetMysqlConnectionString())
1920

2021
dryRun, _ := cmd.Flags().GetBool("dry-run")

cmd/recalculate_statistics.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ var recalculateStatisticsCmd = &cobra.Command{
1515
Short: "Recalculate statistics",
1616
Long: `Recalculate statistics for the given match type`,
1717
PersistentPreRun: func(cmd *cobra.Command, args []string) {
18+
SetupConfig(cmd)
1819
models.InitDB(models.GetMysqlConnectionString())
1920

2021
since, _ = cmd.Flags().GetString("since")

cmd/root.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,18 @@ var rootCmd = &cobra.Command{
1717
Short: "Backend API for kcapp frontend",
1818
Long: `kcapp-api is the backend API for kcapp dart scoring application frontend`,
1919
PersistentPreRun: func(cmd *cobra.Command, args []string) {
20-
configFileParam, err := cmd.Flags().GetString("config")
21-
if err != nil {
22-
panic(err)
23-
}
24-
InitConfig(&configFileParam)
20+
SetupConfig(cmd)
2521
},
2622
}
2723

24+
func SetupConfig(cmd *cobra.Command) {
25+
configFileParam, err := cmd.Flags().GetString("config")
26+
if err != nil {
27+
panic(err)
28+
}
29+
InitConfig(&configFileParam)
30+
}
31+
2832
func InitConfig(configFile *string) {
2933
viper.SetConfigName("config")
3034
viper.SetConfigType("yaml")

cmd/serve.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,11 @@ var serveCmd = &cobra.Command{
9999
router.HandleFunc("/statistics/office/{from}/{to}", controllers.GetOfficeStatistics).Methods("GET")
100100
router.HandleFunc("/statistics/office/{office_id}/{from}/{to}", controllers.GetOfficeStatistics).Methods("GET")
101101
router.HandleFunc("/statistics/{dart}/hits", controllers.GetDartStatistics).Methods("GET")
102-
router.HandleFunc("/statistics/x01/player/{legs}", controllers.GetPlayersLastXLegsStatistics).Methods("GET")
102+
router.HandleFunc("/statistics/x01/player/", controllers.GetPlayersLastXLegsStatistics).Methods("GET")
103103
router.HandleFunc("/statistics/{match_type}/{from}/{to}", controllers.GetStatistics).Methods("GET")
104104

105+
router.HandleFunc("/leaderboard/matchtypes", controllers.GetMatchTypeLeaderboard).Methods("GET")
106+
105107
router.HandleFunc("/owe", controllers.GetOwes).Methods("GET")
106108
router.HandleFunc("/owe/payback", controllers.RegisterPayback).Methods("PUT")
107109

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package controllers
2+
3+
import (
4+
"encoding/json"
5+
"log"
6+
"net/http"
7+
8+
"github.com/kcapp/api/data"
9+
)
10+
11+
// GetMatchTypeLeaderboard will return leaderboard for each match type
12+
func GetMatchTypeLeaderboard(w http.ResponseWriter, r *http.Request) {
13+
SetHeaders(w)
14+
leaderboard, err := data.GetMatchTypeLeaderboard()
15+
if err != nil {
16+
log.Println("Unable to get match type leaderboards", err)
17+
http.Error(w, err.Error(), http.StatusInternalServerError)
18+
return
19+
}
20+
json.NewEncoder(w).Encode(leaderboard)
21+
}

controllers/player_controller.go

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -642,14 +642,51 @@ func UpdatePlayer(w http.ResponseWriter, r *http.Request) {
642642
func GetPlayerProgression(w http.ResponseWriter, r *http.Request) {
643643
SetHeaders(w)
644644
params := mux.Vars(r)
645-
id, err := strconv.Atoi(params["id"])
645+
playerID, err := strconv.Atoi(params["id"])
646646
if err != nil {
647647
log.Println("Invalid id parameter")
648648
http.Error(w, err.Error(), http.StatusBadRequest)
649649
return
650650
}
651651

652-
stats, err := data.GetPlayerProgression(id)
652+
// Get query parameters
653+
bucketSizeStr := r.URL.Query().Get("bucket_size")
654+
startingScoreStr := r.URL.Query().Get("starting_score")
655+
oneVOneOnlyStr := r.URL.Query().Get("1v1_only")
656+
officialOnlyStr := r.URL.Query().Get("official_only")
657+
658+
// Set default values
659+
bucketSize := 50
660+
var startingScore *int
661+
oneVOneOnly := false
662+
officialOnly := false
663+
664+
if bucketSizeStr != "" {
665+
var err error
666+
bucketSize, err = strconv.Atoi(bucketSizeStr)
667+
if err != nil {
668+
http.Error(w, "Invalid bucket_size", http.StatusBadRequest)
669+
return
670+
}
671+
}
672+
673+
if startingScoreStr != "" {
674+
var score int
675+
score, err := strconv.Atoi(startingScoreStr)
676+
if err == nil {
677+
startingScore = &score
678+
}
679+
}
680+
681+
if oneVOneOnlyStr != "" {
682+
oneVOneOnly = oneVOneOnlyStr == "true"
683+
}
684+
685+
if officialOnlyStr != "" {
686+
officialOnly = officialOnlyStr == "true"
687+
}
688+
689+
stats, err := data.GetPlayerProgression(playerID, bucketSize, startingScore, oneVOneOnly, officialOnly)
653690
if err != nil {
654691
log.Println("Unable to get player progression", err)
655692
http.Error(w, err.Error(), http.StatusInternalServerError)

data/leaderboard.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package data
2+
3+
import "github.com/kcapp/api/models"
4+
5+
// GetMatchTypeLeaderboard will return leaderboard statistics for each match type
6+
func GetMatchTypeLeaderboard() (map[int][]*models.MatchTypeLeaderboard, error) {
7+
rows, err := models.DB.Query(`CALL get_leaderboard_per_match_type(5);`)
8+
if err != nil {
9+
return nil, err
10+
}
11+
defer rows.Close()
12+
13+
leaderboard := make(map[int][]*models.MatchTypeLeaderboard)
14+
for rows.Next() {
15+
mtl := new(models.MatchTypeLeaderboard)
16+
err := rows.Scan(&mtl.MatchTypeID, &mtl.PlayerID, &mtl.LegID, &mtl.DartsThrown, &mtl.Score)
17+
if err != nil {
18+
return nil, err
19+
}
20+
if _, ok := leaderboard[mtl.MatchTypeID]; !ok {
21+
leaderboard[mtl.MatchTypeID] = make([]*models.MatchTypeLeaderboard, 0)
22+
}
23+
leaderboard[mtl.MatchTypeID] = append(leaderboard[mtl.MatchTypeID], mtl)
24+
}
25+
return leaderboard, nil
26+
}

data/leg.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ func FinishLeg(legID int, currentPlayer int, winnerID null.Int) error {
160160
}
161161
for playerID, stats := range statisticsMap {
162162
_, err = tx.Exec(`
163-
INSERT INTO statistics_shootout(leg_id, player_id, score, ppd, 60s_plus, 100s_plus, 140s_plus, 180s)
164-
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, legID, playerID, stats.Score, stats.PPD, stats.Score60sPlus,
163+
INSERT INTO statistics_shootout(leg_id, player_id, score, ppd, darts_thrown, 60s_plus, 100s_plus, 140s_plus, 180s)
164+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`, legID, playerID, stats.Score, stats.PPD, stats.DartsThrown, stats.Score60sPlus,
165165
stats.Score100sPlus, stats.Score140sPlus, stats.Score180s)
166166
if err != nil {
167167
tx.Rollback()

0 commit comments

Comments
 (0)