Skip to content

Commit

Permalink
Configurable default options for matches
Browse files Browse the repository at this point in the history
  • Loading branch information
thordy committed Dec 7, 2024
1 parent 1668422 commit bfc2537
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- Improved Tournament Generation
- Support for `Max Rounds` in `X01`
- Added `players` to match presets
- Default options when configuring matches
- Lots of new badges

#### Changed
Expand Down
2 changes: 2 additions & 0 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ var serveCmd = &cobra.Command{
router.HandleFunc("/preset/{id}", controllers.UpdatePreset).Methods("PUT")
router.HandleFunc("/preset/{id}", controllers.DeletePreset).Methods("DELETE")

router.HandleFunc("/option/default", controllers.GetDefaultOptions).Methods("GET")

router.HandleFunc("/statistics/global", controllers.GetGlobalStatistics).Methods("GET")
router.HandleFunc("/statistics/global/fnc", controllers.GetGlobalStatisticsFnc).Methods("GET")
router.HandleFunc("/statistics/office/{from}/{to}", controllers.GetOfficeStatistics).Methods("GET")
Expand Down
21 changes: 21 additions & 0 deletions controllers/option_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package controllers

import (
"encoding/json"
"log"
"net/http"

"github.com/kcapp/api/data"
)

// GetDefaultOptions will return the default options
func GetDefaultOptions(w http.ResponseWriter, r *http.Request) {
SetHeaders(w)
offices, err := data.GetDefaultOptions()
if err != nil {
log.Println("Unable to get default options", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
json.NewEncoder(w).Encode(offices)
}
36 changes: 36 additions & 0 deletions data/option.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package data

import (
"database/sql"

"github.com/kcapp/api/models"
)

// GetDefaultOptions will return default options
func GetDefaultOptions() (*models.DefaultOptions, error) {
opts := new(models.DefaultOptions)
opts.MatchType = new(models.MatchType)
opts.MatchMode = new(models.MatchMode)
opts.OutshotType = new(models.OutshotType)

err := models.DB.QueryRow(`
SELECT
mt.id, mt.name, mt.description,
mm.id, mm.name, mm.short_name, mm.wins_required, mm.legs_required,
starting_score, max_rounds, ot.id, ot.name, ot.short_name
FROM match_default md
LEFT JOIN match_type mt ON mt.id = md.match_type_id
LEFT JOIN match_mode mm ON mm.id = md.match_mode_id
LEFT JOIN outshot_type ot ON ot.id = md.outshot_type_id
LIMIT 1`).
Scan(&opts.MatchType.ID, &opts.MatchType.Name, &opts.MatchType.Description, &opts.MatchMode.ID, &opts.MatchMode.Name,
&opts.MatchMode.ShortName, &opts.MatchMode.WinsRequired, &opts.MatchMode.LegsRequired, &opts.StartingScore, &opts.MaxRounds,
&opts.OutshotType.ID, &opts.OutshotType.Name, &opts.OutshotType.ShortName)
if err != nil {
if err == sql.ErrNoRows {
return opts, nil
}
return nil, err
}
return opts, nil
}
12 changes: 12 additions & 0 deletions models/option.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package models

import "github.com/guregu/null"

// DefaultOptions struct used for storing default options
type DefaultOptions struct {
MatchType *MatchType `json:"match_type"`
MatchMode *MatchMode `json:"match_mode"`
StartingScore int `json:"starting_score"`
MaxRounds null.Int `json:"max_rounds,omitempty"`
OutshotType *OutshotType `json:"outshot_type,omitempty"`
}

0 comments on commit bfc2537

Please sign in to comment.