Skip to content

Commit b927070

Browse files
committed
Fixed endpoints for getting player 170 statistics
1 parent 4658836 commit b927070

4 files changed

Lines changed: 48 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
- Endpoint to get leaderboard data for each match type
77
- New `Still Regining` badge when surviving a kill slayer attempt
88

9+
#### Fixed
10+
- Endpoints for getting player 170 statistics
11+
912
## [2.9.0] - 2025-04-06
1013
#### Feature
1114
- Added method for updating venue of a match

controllers/player_controller.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,16 @@ func GetPlayerMatchTypeStatistics(w http.ResponseWriter, r *http.Request) {
352352
json.NewEncoder(w).Encode(stats)
353353
return
354354

355+
case models.ONESEVENTY:
356+
stats, err := data.Get170StatisticsForPlayer(id)
357+
if err != nil {
358+
log.Println("Unable to get 170 Statistics for player", err)
359+
http.Error(w, err.Error(), http.StatusInternalServerError)
360+
return
361+
}
362+
json.NewEncoder(w).Encode(stats)
363+
return
364+
355365
default:
356366
log.Println("Unknown match type parameter")
357367
http.Error(w, err.Error(), http.StatusBadRequest)
@@ -542,6 +552,16 @@ func GetPlayerMatchTypeHistory(w http.ResponseWriter, r *http.Request) {
542552
json.NewEncoder(w).Encode(legs)
543553
return
544554

555+
case models.ONESEVENTY:
556+
legs, err := data.Get170HistoryForPlayer(id, 0, limit)
557+
if err != nil {
558+
log.Println("Unable to get 170 history for player", err)
559+
http.Error(w, err.Error(), http.StatusInternalServerError)
560+
return
561+
}
562+
json.NewEncoder(w).Encode(legs)
563+
return
564+
545565
default:
546566
log.Println("Unknown match type parameter")
547567
http.Error(w, err.Error(), http.StatusBadRequest)

data/statistics_170.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ func Get170StatisticsForPlayer(id int) (*models.Statistics170, error) {
195195
err := models.DB.QueryRow(`
196196
SELECT
197197
p.id,
198+
COUNT(DISTINCT m.id) AS 'matches_played',
199+
COUNT(DISTINCT m2.id) AS 'matches_won',
200+
COUNT(DISTINCT l.id) AS 'legs_played',
201+
COUNT(DISTINCT l2.id) AS 'legs_won',
198202
SUM(s.points),
199203
SUM(s.ppd_score) / SUM(s.darts_thrown),
200204
SUM(s.rounds),
@@ -218,8 +222,9 @@ func Get170StatisticsForPlayer(id int) (*models.Statistics170, error) {
218222
WHERE s.player_id = ?
219223
AND l.is_finished = 1 AND m.is_abandoned = 0 AND m.is_walkover = 0
220224
AND m.match_type_id = 17
221-
GROUP BY p.id`, id).Scan(&s.LegID, &s.PlayerID, &s.Points, &s.PPD, &s.Rounds, &s.CheckoutPercentage, &s.CheckoutAttempts,
222-
&s.HighestCheckout, &s.DartsThrown, &darts9, &darts8, &darts7, &darts6, &darts5, &darts4, &darts3)
225+
GROUP BY p.id`, id).Scan(&s.PlayerID, &s.MatchesPlayed, &s.MatchesWon, &s.LegsPlayed, &s.LegsWon,
226+
&s.Points, &s.PPD, &s.Rounds, &s.CheckoutPercentage, &s.CheckoutAttempts, &s.HighestCheckout,
227+
&s.DartsThrown, &darts9, &darts8, &darts7, &darts6, &darts5, &darts4, &darts3)
223228
if err != nil {
224229
if err == sql.ErrNoRows {
225230
return new(models.Statistics170), nil
@@ -285,13 +290,24 @@ func Get170HistoryForPlayer(id int, start int, limit int) ([]*models.Leg, error)
285290
legs = make([]*models.Leg, 0)
286291
for rows.Next() {
287292
s := new(models.Statistics170)
288-
s.CheckoutDarts = make(map[int]int)
293+
var darts9, darts8, darts7, darts6, darts5, darts4, darts3 int
294+
289295
err := rows.Scan(&s.LegID, &s.PlayerID, &s.Points, &s.PPD, &s.PPDScore, &s.Rounds, &s.CheckoutPercentage,
290-
&s.CheckoutAttempts, &s.CheckoutCompleted, &s.HighestCheckout, s.CheckoutDarts[9], s.CheckoutDarts[8],
291-
s.CheckoutDarts[7], s.CheckoutDarts[6], s.CheckoutDarts[5], s.CheckoutDarts[4], s.CheckoutDarts[3])
296+
&s.CheckoutAttempts, &s.CheckoutCompleted, &s.HighestCheckout, &darts9, &darts8, &darts7, &darts6, &darts5,
297+
&darts4, &darts3)
292298
if err != nil {
293299
return nil, err
294300
}
301+
checkoutDarts := make(map[int]int, 0)
302+
checkoutDarts[9] = darts9
303+
checkoutDarts[8] = darts8
304+
checkoutDarts[7] = darts7
305+
checkoutDarts[6] = darts6
306+
checkoutDarts[5] = darts5
307+
checkoutDarts[4] = darts4
308+
checkoutDarts[3] = darts3
309+
s.CheckoutDarts = checkoutDarts
310+
295311
leg := m[s.LegID]
296312
leg.Statistics = s
297313
legs = append(legs, leg)

models/statistics_170.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ type Statistics170 struct {
99
ID int `json:"id"`
1010
LegID int `json:"leg_id"`
1111
PlayerID int `json:"player_id"`
12+
MatchesPlayed int `json:"matches_played"`
13+
MatchesWon int `json:"matches_won"`
14+
LegsPlayed int `json:"legs_played"`
15+
LegsWon int `json:"legs_won"`
1216
Points int `json:"points"`
1317
PPD float32 `json:"ppd"`
1418
PPDScore int `json:"-"`

0 commit comments

Comments
 (0)