From ab5b4931475d722935bff01c844a78991e08fb0d Mon Sep 17 00:00:00 2001 From: Patrick Fairbank Date: Thu, 10 Sep 2015 20:13:05 -0700 Subject: [PATCH] Fixed TBA match and rankings publishing. --- setup_schedule.go | 5 +++++ tba.go | 54 ++++++++++++++++++++++++++++++----------------- tba_test.go | 29 +++++++++++++------------ 3 files changed, 55 insertions(+), 33 deletions(-) diff --git a/setup_schedule.go b/setup_schedule.go index c8ef7dc..0a251ee 100644 --- a/setup_schedule.go +++ b/setup_schedule.go @@ -128,6 +128,11 @@ func ScheduleSavePostHandler(w http.ResponseWriter, r *http.Request) { if eventSettings.TbaPublishingEnabled && cachedMatchType != "practice" { // Publish schedule to The Blue Alliance. + err = DeletePublishedMatches() + if err != nil { + http.Error(w, "Failed to delete published matches: "+err.Error(), 500) + return + } err = PublishMatches() if err != nil { http.Error(w, "Failed to publish matches: "+err.Error(), 500) diff --git a/tba.go b/tba.go index 53adb00..c362fb3 100644 --- a/tba.go +++ b/tba.go @@ -29,6 +29,7 @@ var tbaEventNames = make(map[string]string) type TbaMatch struct { CompLevel string `json:"comp_level"` + SetNumber int `json:"set_number"` MatchNumber int `json:"match_number"` Alliances map[string]interface{} `json:"alliances"` ScoreBreakdown map[string]TbaScoreBreakdown `json:"score_breakdown"` @@ -37,23 +38,23 @@ type TbaMatch struct { } type TbaScoreBreakdown struct { - Coopertition int `json:"coopertition"` - Auto int `json:"auto"` - Container int `json:"container"` - Tote int `json:"tote"` - Litter int `json:"litter"` - Foul int `json:"foul"` + Coopertition int `json:"coopertition_points"` + Auto int `json:"auto_points"` + Container int `json:"container_points"` + Tote int `json:"tote_points"` + Litter int `json:"litter_points"` + Foul int `json:"foul_points"` } type TbaRanking struct { TeamKey string `json:"team_key"` Rank int `json:"rank"` - QA float64 `json:"qa"` - Coopertition int `json:"coopertition"` - Auto int `json:"auto"` - Container int `json:"container"` - Tote int `json:"tote"` - Litter int `json:"litter"` + QA float64 `json:"QA"` + Coopertition int `json:"Coopertition"` + Auto int `json:"Auto"` + Container int `json:"Container"` + Tote int `json:"Tote"` + Litter int `json:"Litter"` Dqs int `json:"dqs"` Played int `json:"played"` } @@ -208,7 +209,7 @@ func PublishTeams() error { return err } - resp, err := postTbaRequest("team_list", jsonBody) + resp, err := postTbaRequest("team_list", "update", jsonBody) if err != nil { return err } @@ -263,11 +264,12 @@ func PublishMatches() error { } } - tbaMatches[i] = TbaMatch{"qm", matchNumber, map[string]interface{}{"red": redAlliance, + tbaMatches[i] = TbaMatch{"qm", 0, matchNumber, map[string]interface{}{"red": redAlliance, "blue": blueAlliance}, scoreBreakdown, match.Time.Local().Format("3:04 PM"), match.Time.Format("2006-01-02T15:04:05")} if match.Type == "elimination" { tbaMatches[i].CompLevel = map[int]string{1: "f", 2: "sf", 4: "qf", 8: "ef"}[match.ElimRound] + tbaMatches[i].SetNumber = match.ElimGroup tbaMatches[i].MatchNumber = match.ElimInstance } } @@ -276,7 +278,7 @@ func PublishMatches() error { return err } - resp, err := postTbaRequest("matches", jsonBody) + resp, err := postTbaRequest("matches", "update", jsonBody) if err != nil { return err } @@ -308,7 +310,7 @@ func PublishRankings() error { return err } - resp, err := postTbaRequest("rankings", jsonBody) + resp, err := postTbaRequest("rankings", "update", jsonBody) if err != nil { return err } @@ -339,7 +341,21 @@ func PublishAlliances() error { return err } - resp, err := postTbaRequest("alliance_selections", jsonBody) + resp, err := postTbaRequest("alliance_selections", "update", jsonBody) + if err != nil { + return err + } + if resp.StatusCode != 200 { + defer resp.Body.Close() + body, _ := ioutil.ReadAll(resp.Body) + return fmt.Errorf("Got status code %d from TBA: %s", resp.StatusCode, body) + } + return nil +} + +// Clears out the existing match data on The Blue Alliance for the event. +func DeletePublishedMatches() error { + resp, err := postTbaRequest("matches", "delete_all", []byte(eventSettings.TbaEventCode)) if err != nil { return err } @@ -359,8 +375,8 @@ func getTbaTeam(team int) string { // HELPERS // Signs the request and sends it to the TBA API. -func postTbaRequest(resource string, body []byte) (*http.Response, error) { - path := fmt.Sprintf("/api/trusted/v1/event/%s/%s/update", eventSettings.TbaEventCode, resource) +func postTbaRequest(resource string, action string, body []byte) (*http.Response, error) { + path := fmt.Sprintf("/api/trusted/v1/event/%s/%s/%s", eventSettings.TbaEventCode, resource, action) signature := fmt.Sprintf("%x", md5.Sum(append([]byte(eventSettings.TbaSecret+path), body...))) client := &http.Client{} diff --git a/tba_test.go b/tba_test.go index de8646f..823e8c4 100644 --- a/tba_test.go +++ b/tba_test.go @@ -51,7 +51,7 @@ func TestPublishMatches(t *testing.T) { eventSettings, _ = db.GetEventSettings() match1 := Match{Type: "qualification", DisplayName: "2", Time: time.Unix(600, 0), Red1: 7, Red2: 8, Red3: 9, Blue1: 10, Blue2: 11, Blue3: 12, Status: "complete"} - match2 := Match{Type: "elimination", DisplayName: "SF2-2", ElimRound: 2, ElimInstance: 2} + match2 := Match{Type: "elimination", DisplayName: "SF2-2", ElimRound: 2, ElimGroup: 2, ElimInstance: 2} db.CreateMatch(&match1) db.CreateMatch(&match2) matchResult1 := buildTestMatchResult(match1.Id, 1) @@ -61,15 +61,16 @@ func TestPublishMatches(t *testing.T) { tbaServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { var reader bytes.Buffer reader.ReadFrom(r.Body) - assert.Equal(t, "[{\"comp_level\":\"qm\",\"match_number\":2,\"alliances\":{\"blue\":{\"score\":104,"+ - "\"teams\":[\"frc10\",\"frc11\",\"frc12\"]},\"red\":{\"score\":92,\"teams\":[\"frc7\","+ - "\"frc8\",\"frc9\"]}},\"score_breakdown\":{\"blue\":{\"coopertition\":40,\"auto\":10,"+ - "\"container\":24,\"tote\":24,\"litter\":6,\"foul\":0},\"red\":{\"coopertition\":40,"+ - "\"auto\":28,\"container\":24,\"tote\":12,\"litter\":6,\"foul\":18}},\"time_string\":"+ - "\"4:10 PM\",\"time_utc\":\"1970-01-01T00:10:00\"},{\"comp_level\":\"sf\",\"match_number\":2,"+ - "\"alliances\":{\"blue\":{\"score\":null,\"teams\":[\"frc0\",\"frc0\",\"frc0\"]},\"red\":"+ - "{\"score\":null,\"teams\":[\"frc0\",\"frc0\",\"frc0\"]}},\"score_breakdown\":null,"+ - "\"time_string\":\"4:00 PM\",\"time_utc\":\"0001-01-01T00:00:00\"}]", reader.String()) + assert.Equal(t, "[{\"comp_level\":\"qm\",\"set_number\":0,\"match_number\":2,\"alliances\":{\"blue\""+ + ":{\"score\":104,\"teams\":[\"frc10\",\"frc11\",\"frc12\"]},\"red\":{\"score\":92,\"teams\":[\"f"+ + "rc7\",\"frc8\",\"frc9\"]}},\"score_breakdown\":{\"blue\":{\"coopertition_points\":40,\"auto_poi"+ + "nts\":10,\"container_points\":24,\"tote_points\":24,\"litter_points\":6,\"foul_points\":0},\"re"+ + "d\":{\"coopertition_points\":40,\"auto_points\":28,\"container_points\":24,\"tote_points\":12,"+ + "\"litter_points\":6,\"foul_points\":18}},\"time_string\":\"4:10 PM\",\"time_utc\":\"1970-01-01T"+ + "00:10:00\"},{\"comp_level\":\"sf\",\"set_number\":2,\"match_number\":2,\"alliances\":{\"blue\":"+ + "{\"score\":null,\"teams\":[\"frc0\",\"frc0\",\"frc0\"]},\"red\":{\"score\":null,\"teams\":[\"fr"+ + "c0\",\"frc0\",\"frc0\"]}},\"score_breakdown\":null,\"time_string\":\"4:00 PM\",\"time_utc\":\"0"+ + "001-01-01T00:00:00\"}]", reader.String()) })) defer tbaServer.Close() tbaBaseUrl = tbaServer.URL @@ -93,10 +94,10 @@ func TestPublishRankings(t *testing.T) { var reader bytes.Buffer reader.ReadFrom(r.Body) assert.Equal(t, "{\"breakdowns\":[\"QA\",\"Coopertition\",\"Auto\",\"Container\",\"Tote\","+ - "\"Litter\"],\"rankings\":[{\"team_key\":\"frc254\",\"rank\":1,\"qa\":20,\"coopertition\":1100,"+ - "\"auto\":625,\"container\":90,\"tote\":554,\"litter\":10,\"dqs\":0,\"played\":10},"+ - "{\"team_key\":\"frc1114\",\"rank\":2,\"qa\":20,\"coopertition\":1100,\"auto\":625,"+ - "\"container\":90,\"tote\":554,\"litter\":10,\"dqs\":0,\"played\":10}]}", reader.String()) + "\"Litter\"],\"rankings\":[{\"team_key\":\"frc254\",\"rank\":1,\"QA\":20,\"Coopertition\":1100,"+ + "\"Auto\":625,\"Container\":90,\"Tote\":554,\"Litter\":10,\"dqs\":0,\"played\":10},"+ + "{\"team_key\":\"frc1114\",\"rank\":2,\"QA\":20,\"Coopertition\":1100,\"Auto\":625,"+ + "\"Container\":90,\"Tote\":554,\"Litter\":10,\"dqs\":0,\"played\":10}]}", reader.String()) })) defer tbaServer.Close() tbaBaseUrl = tbaServer.URL