From 759ba1e8a24ca4c59bb585911706a74b8b3fab82 Mon Sep 17 00:00:00 2001 From: Thord Setsaas Date: Mon, 18 Mar 2024 20:53:38 +0100 Subject: [PATCH] Changed Around The World and Shanghai to use multiplier as points --- CHANGELOG.md | 1 + cmd/fourtwenty.go | 2 +- cmd/onesecenety.go | 23 +++++++++++++++++++++++ controllers/match_controller.go | 6 +++++- data/match.go | 6 +++--- data/recalculate.go | 2 ++ data/statistics_around_the_world.go | 3 ++- models/visit.go | 6 +++--- 8 files changed, 40 insertions(+), 9 deletions(-) create mode 100644 cmd/onesecenety.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 8fbcd834..cd44f7bc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ #### Changed - Sort tournament best statistics by leg id for equal values - Use tournament elo if available for odds +- Switched `Around the World` and `Shanghai` to count multiplers as points instead of value #### Fixed - Return `outshot_type` for `X01 Handicap` diff --git a/cmd/fourtwenty.go b/cmd/fourtwenty.go index 74889d7d..344b4c11 100644 --- a/cmd/fourtwenty.go +++ b/cmd/fourtwenty.go @@ -8,7 +8,7 @@ import ( // fourtwentyCmd represents the fourtwenty command var fourtwentyCmd = &cobra.Command{ - Use: "fourtwenty", + Use: "420", Short: "Recalculate 420 statistics", Run: func(cmd *cobra.Command, args []string) { err := data.RecalculateStatistics(models.FOURTWENTY, legID, since, dryRun) diff --git a/cmd/onesecenety.go b/cmd/onesecenety.go new file mode 100644 index 00000000..f9f93903 --- /dev/null +++ b/cmd/onesecenety.go @@ -0,0 +1,23 @@ +package cmd + +import ( + "github.com/kcapp/api/data" + "github.com/kcapp/api/models" + "github.com/spf13/cobra" +) + +// oneseventyCmd represents the oneseventy command +var oneseventyCmd = &cobra.Command{ + Use: "170", + Short: "Recalculate 170 statistics", + Run: func(cmd *cobra.Command, args []string) { + err := data.RecalculateStatistics(models.ONESEVENTY, legID, since, dryRun) + if err != nil { + panic(err) + } + }, +} + +func init() { + recalculateStatisticsCmd.AddCommand(oneseventyCmd) +} diff --git a/controllers/match_controller.go b/controllers/match_controller.go index 3908acf0..eb7759cb 100644 --- a/controllers/match_controller.go +++ b/controllers/match_controller.go @@ -98,7 +98,11 @@ func GetMatches(w http.ResponseWriter, r *http.Request) { // GetActiveMatches will return a list of active matches func GetActiveMatches(w http.ResponseWriter, r *http.Request) { SetHeaders(w) - matches, err := data.GetActiveMatches() + since, err := strconv.Atoi(r.URL.Query().Get("since")) + if err != nil { + since = 2 + } + matches, err := data.GetActiveMatches(since) if err != nil { log.Println("Unable to get active matches", err) http.Error(w, err.Error(), http.StatusInternalServerError) diff --git a/data/match.go b/data/match.go index 624d849f..0086d1c4 100644 --- a/data/match.go +++ b/data/match.go @@ -181,7 +181,7 @@ func GetMatchesCount() (int, error) { } // GetActiveMatches returns all active matches -func GetActiveMatches() ([]*models.Match, error) { +func GetActiveMatches(since int) ([]*models.Match, error) { rows, err := models.DB.Query(` SELECT m.id, m.is_finished, m.is_abandoned, m.is_walkover, m.is_bye, m.current_leg_id, m.winner_id, m.office_id, m.is_practice, @@ -196,10 +196,10 @@ func GetActiveMatches() ([]*models.Match, error) { LEFT JOIN venue v on v.id = m.venue_id LEFT JOIN player2leg p2l ON p2l.match_id = m.id WHERE m.is_finished = 0 - AND l.updated_at > NOW() - INTERVAL 2 MINUTE + AND l.updated_at > NOW() - INTERVAL ? MINUTE AND m.is_bye <> 1 GROUP BY m.id - ORDER BY m.id DESC`) + ORDER BY m.id DESC`, since) if err != nil { return nil, err } diff --git a/data/recalculate.go b/data/recalculate.go index 1423ea62..ac47f5e9 100644 --- a/data/recalculate.go +++ b/data/recalculate.go @@ -67,6 +67,8 @@ func RecalculateStatistics(matchType int, legID int, since string, dryRun bool) queries, err = RecalculateKnockoutStatistics(legs) case models.SCAM: queries, err = ReCalculateScamStatistics(legs) + case models.ONESEVENTY: + queries, err = ReCalculate170Statistics(legs) default: return fmt.Errorf("cannot recalculate statistics for type %d", matchType) } diff --git a/data/statistics_around_the_world.go b/data/statistics_around_the_world.go index 5284a8c4..d3869698 100644 --- a/data/statistics_around_the_world.go +++ b/data/statistics_around_the_world.go @@ -493,6 +493,7 @@ func GetShanghaiStatisticsForMatch(id int) ([]*models.StatisticsAroundThe, error CAST(SUM(s.score) / COUNT(DISTINCT l.id) AS SIGNED) as 'avg_score', SUM(s.mpr) / COUNT(DISTINCT l.id) as 'mpr', SUM(s.total_hit_rate) / COUNT(l.id) as 'total_hit_rate', + MAX(shanghai) as 'shanghai', IFNULL(SUM(s.hit_rate_1) / SUM(IF(shanghai < 1, 0, 1)), 0) as 'hit_rate_1', IFNULL(SUM(s.hit_rate_2) / SUM(IF(shanghai < 2, 0, 1)), 0) as 'hit_rate_2', IFNULL(SUM(s.hit_rate_3) / SUM(IF(shanghai < 3, 0, 1)), 0) as 'hit_rate_3', @@ -530,7 +531,7 @@ func GetShanghaiStatisticsForMatch(id int) ([]*models.StatisticsAroundThe, error for rows.Next() { s := new(models.StatisticsAroundThe) h := make([]*null.Float, 21) - err := rows.Scan(&s.PlayerID, &s.DartsThrown, &s.Score, &s.MPR, &s.TotalHitRate, + err := rows.Scan(&s.PlayerID, &s.DartsThrown, &s.Score, &s.MPR, &s.TotalHitRate, &s.Shanghai, &h[1], &h[2], &h[3], &h[4], &h[5], &h[6], &h[7], &h[8], &h[9], &h[10], &h[11], &h[12], &h[13], &h[14], &h[15], &h[16], &h[17], &h[18], &h[19], &h[20]) if err != nil { diff --git a/models/visit.go b/models/visit.go index 8e004a92..1b48e02c 100644 --- a/models/visit.go +++ b/models/visit.go @@ -441,13 +441,13 @@ func (visit *Visit) CalculateAroundTheClockScore(currentScore int) int { func (visit *Visit) CalculateAroundTheWorldScore(round int) int { score := 0 if round == visit.FirstDart.ValueRaw() || (round == 21 && visit.FirstDart.IsBull()) { - score += visit.FirstDart.GetScore() + score += int(visit.FirstDart.Multiplier) } if round == visit.SecondDart.ValueRaw() || (round == 21 && visit.SecondDart.IsBull()) { - score += visit.SecondDart.GetScore() + score += int(visit.SecondDart.Multiplier) } if round == visit.ThirdDart.ValueRaw() || (round == 21 && visit.ThirdDart.IsBull()) { - score += visit.ThirdDart.GetScore() + score += int(visit.ThirdDart.Multiplier) } return score }