Skip to content

Commit

Permalink
Merge branch 'nexus-format' into 2025-Sussex-Scrimmage
Browse files Browse the repository at this point in the history
  • Loading branch information
shortstuffsushi committed Feb 6, 2025
2 parents 9a25f33 + e058d36 commit 90c6e09
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
2 changes: 2 additions & 0 deletions templates/nexus-schedule.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{{range $match := .}}{{$match.Type}} {{$match.ShortName}} {{$match.Blue1}} {{$match.Blue2}} {{$match.Blue3}} {{$match.Red1}} {{$match.Red2}} {{$match.Red3}}
{{end}}
47 changes: 47 additions & 0 deletions web/reports.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import (
"math"
"net/http"
"strconv"
"strings"
"time"
"unicode"

"github.com/Team254/cheesy-arena/game"
"github.com/Team254/cheesy-arena/model"
Expand All @@ -21,6 +23,20 @@ import (
"github.com/jung-kurt/gofpdf"
)

// ExtractNumbersFromString will return just the numbers in the provided string;
// the returned number-only string will be in the same order it was passed, e.g.
//
// ExtractNumbersFromString("a7b3c04") // => "7304"
func ExtractNumbersFromString(s string) string {
return strings.Map(func(r rune) rune {
if unicode.IsDigit(r) {
return r
}

return -1
}, s)
}

// Generates a CSV-formatted report of the qualification rankings.
func (web *Web) rankingsCsvReportHandler(w http.ResponseWriter, r *http.Request) {
rankings, err := web.arena.Database.GetAllRankings()
Expand Down Expand Up @@ -386,6 +402,37 @@ func (web *Web) scheduleCsvReportHandler(w http.ResponseWriter, r *http.Request)
}
}

func (web *Web) scheduleNexusCsvReportHandler(w http.ResponseWriter, r *http.Request) {
matchType, err := model.MatchTypeFromString(r.PathValue("type"))
if err != nil {
handleWebErr(w, err)
return
}

matches, err := web.arena.Database.GetMatchesByType(matchType, false)
if err != nil {
handleWebErr(w, err)
return
}

for index, match := range matches {
matches[index].ShortName = ExtractNumbersFromString(match.ShortName)
}

// Don't set the content type as "text/csv", as that will trigger an automatic download in the browser.
w.Header().Set("Content-Type", "text/plain")
template, err := web.parseFiles("templates/nexus-schedule.csv")
if err != nil {
handleWebErr(w, err)
return
}
err = template.ExecuteTemplate(w, "nexus-schedule.csv", matches)
if err != nil {
handleWebErr(w, err)
return
}
}

// Generates a PDF-formatted report of the match schedule.
func (web *Web) schedulePdfReportHandler(w http.ResponseWriter, r *http.Request) {
matchType, err := model.MatchTypeFromString(r.PathValue("type"))
Expand Down
50 changes: 50 additions & 0 deletions web/reports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,56 @@ import (
"time"
)

func Test_ExtractNumbersFromString(t *testing.T) {
type TestCase struct {
Input string
Output string
}

cases := []TestCase{
{
Input: "P1P2",
Output: "12",
},
{
Input: "P1P32P9PP93",
Output: "132993",
},
{
Input: "1PP2",
Output: "12",
},
{
Input: "1P",
Output: "1",
},
{
Input: "P2",
Output: "2",
},
{
Input: "ASDF",
Output: "",
},
{
Input: "1234",
Output: "1234",
},
}

for _, c := range cases {
actual := ExtractNumbersFromString(c.Input)
success := assert.Equal(t, c.Output, actual,
"Expected input %q to be extracted %q; was %q",
c.Input,
c.Output,
actual)
if !success {
return
}
}
}

func TestRankingsCsvReport(t *testing.T) {
web := setupTestWeb(t)

Expand Down
1 change: 1 addition & 0 deletions web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ func (web *Web) newHandler() http.Handler {
mux.HandleFunc("GET /reports/csv/fta", web.ftaCsvReportHandler)
mux.HandleFunc("GET /reports/csv/rankings", web.rankingsCsvReportHandler)
mux.HandleFunc("GET /reports/csv/schedule/{type}", web.scheduleCsvReportHandler)
mux.HandleFunc("GET /reports/csv/nexus-schedule/{type}", web.scheduleNexusCsvReportHandler)
mux.HandleFunc("GET /reports/csv/teams", web.teamsCsvReportHandler)
mux.HandleFunc("GET /reports/csv/wpa_keys", web.wpaKeysCsvReportHandler)
mux.HandleFunc("GET /reports/pdf/alliances", web.alliancesPdfReportHandler)
Expand Down

0 comments on commit 90c6e09

Please sign in to comment.