Skip to content

Commit

Permalink
Changed Around The World and Shanghai to use multiplier as points
Browse files Browse the repository at this point in the history
  • Loading branch information
thordy committed Mar 18, 2024
1 parent 9c2d750 commit 759ba1e
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
2 changes: 1 addition & 1 deletion cmd/fourtwenty.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 23 additions & 0 deletions cmd/onesecenety.go
Original file line number Diff line number Diff line change
@@ -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)
}
6 changes: 5 additions & 1 deletion controllers/match_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions data/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}
Expand Down
2 changes: 2 additions & 0 deletions data/recalculate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
3 changes: 2 additions & 1 deletion data/statistics_around_the_world.go
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions models/visit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 759ba1e

Please sign in to comment.