mirror of
https://github.com/Team254/cheesy-arena-lite.git
synced 2026-03-09 13:46:44 -04:00
Add publishing of awards to TBA.
This commit is contained in:
@@ -146,6 +146,12 @@ type TbaMediaItem struct {
|
||||
Type string `json:"type"`
|
||||
}
|
||||
|
||||
type TbaPublishedAward struct {
|
||||
Name string `json:"name_str"`
|
||||
TeamKey string `json:"team_key"`
|
||||
Awardee string `json:"awardee"`
|
||||
}
|
||||
|
||||
var habLevelMapping = []string{"None", "HabLevel1", "HabLevel2", "HabLevel3"}
|
||||
var bayStatusMapping = []string{"None", "Panel", "PanelAndCargo", "Cargo"}
|
||||
var sandstormBonusMapping = map[bool]string{false: "None", true: "CrossedHabLineInSandstorm"}
|
||||
@@ -593,3 +599,34 @@ func createTbaScoringBreakdown(match *model.Match, matchResult *model.MatchResul
|
||||
|
||||
return &breakdown
|
||||
}
|
||||
|
||||
// Uploads the awards to The Blue Alliance.
|
||||
func (client *TbaClient) PublishAwards(database *model.Database) error {
|
||||
awards, err := database.GetAllAwards()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Build a JSON array of TBA-format award models.
|
||||
tbaAwards := make([]TbaPublishedAward, len(awards))
|
||||
for i, award := range awards {
|
||||
tbaAwards[i].Name = award.AwardName
|
||||
tbaAwards[i].TeamKey = getTbaTeam(award.TeamId)
|
||||
tbaAwards[i].Awardee = award.PersonName
|
||||
}
|
||||
jsonBody, err := json.Marshal(tbaAwards)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
resp, err := client.postRequest("awards", "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
|
||||
}
|
||||
|
||||
@@ -125,6 +125,27 @@ func TestPublishingErrors(t *testing.T) {
|
||||
assert.NotNil(t, client.PublishAlliances(database))
|
||||
}
|
||||
|
||||
func TestPublishAwards(t *testing.T) {
|
||||
database := setupTestDb(t)
|
||||
|
||||
database.CreateAward(&model.Award{0, model.JudgedAward, "Saftey Award", 254, ""})
|
||||
database.CreateAward(&model.Award{0, model.JudgedAward, "Spirt Award", 0, "Bob Dorough"})
|
||||
|
||||
// Mock the TBA server.
|
||||
tbaServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
assert.Contains(t, r.URL.String(), "event/my_event_code")
|
||||
var reader bytes.Buffer
|
||||
reader.ReadFrom(r.Body)
|
||||
assert.Equal(t, "[{\"name_str\":\"Saftey Award\",\"team_key\":\"frc254\",\"awardee\":\"\"},"+
|
||||
"{\"name_str\":\"Spirt Award\",\"team_key\":\"frc0\",\"awardee\":\"Bob Dorough\"}]", reader.String())
|
||||
}))
|
||||
defer tbaServer.Close()
|
||||
client := NewTbaClient("my_event_code", "my_secret_id", "my_secret")
|
||||
client.BaseUrl = tbaServer.URL
|
||||
|
||||
assert.Nil(t, client.PublishAwards(database))
|
||||
}
|
||||
|
||||
func setupTestDb(t *testing.T) *model.Database {
|
||||
return model.SetupTestDb(t, "partner")
|
||||
}
|
||||
|
||||
@@ -54,6 +54,36 @@
|
||||
</form>
|
||||
{{end}}
|
||||
Winner and Finalist awards will be automatically generated once the playoff tournament is complete.
|
||||
{{if .EventSettings.TbaPublishingEnabled}}
|
||||
<br /><br />
|
||||
<div class="row text-center">
|
||||
<div class="form-group">
|
||||
<button type="button" class="btn btn-info" onclick="$('#confirmPublishAwards').modal('show');">
|
||||
Publish Awards to TBA
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
{{end}}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div id="confirmPublishAwards" class="modal" style="top: 20%;">
|
||||
<div class="modal-dialog">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
|
||||
<h4 class="modal-title">Confirm</h4>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<p>Are you sure you want to publish the awards to The Blue Alliance? This will overwrite any existing award
|
||||
data.</p>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<form class="form-horizontal" action="/setup/awards/publish" method="POST">
|
||||
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
|
||||
<button type="submit" class="btn btn-primary">Publish Awards</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -125,7 +125,7 @@ func CreateOrUpdateWinnerAndFinalistAwards(database *model.Database, winnerAllia
|
||||
}
|
||||
|
||||
// Create the finalist awards first since they're usually presented first.
|
||||
finalistAward := model.Award{AwardName: "Event Finalist", Type: model.FinalistAward,
|
||||
finalistAward := model.Award{AwardName: "Finalist", Type: model.FinalistAward,
|
||||
TeamId: finalistAllianceTeams[0].TeamId}
|
||||
if err = CreateOrUpdateAward(database, &finalistAward, true); err != nil {
|
||||
return err
|
||||
@@ -139,7 +139,7 @@ func CreateOrUpdateWinnerAndFinalistAwards(database *model.Database, winnerAllia
|
||||
}
|
||||
|
||||
// Create the winner awards.
|
||||
winnerAward := model.Award{AwardName: "Event Winner", Type: model.WinnerAward,
|
||||
winnerAward := model.Award{AwardName: "Winner", Type: model.WinnerAward,
|
||||
TeamId: winnerAllianceTeams[0].TeamId}
|
||||
if err = CreateOrUpdateAward(database, &winnerAward, true); err != nil {
|
||||
return err
|
||||
|
||||
@@ -54,7 +54,7 @@ func TestCreateOrUpdateAwardWithoutIntro(t *testing.T) {
|
||||
otherLowerThird := model.LowerThird{TopText: "Marco", BottomText: "Polo"}
|
||||
database.CreateLowerThird(&otherLowerThird)
|
||||
|
||||
award := model.Award{0, model.WinnerAward, "Event Winner", 0, "Bob Dorough"}
|
||||
award := model.Award{0, model.WinnerAward, "Winner", 0, "Bob Dorough"}
|
||||
err := CreateOrUpdateAward(database, &award, false)
|
||||
assert.Nil(t, err)
|
||||
award2, _ := database.GetAwardById(award.Id)
|
||||
@@ -62,7 +62,7 @@ func TestCreateOrUpdateAwardWithoutIntro(t *testing.T) {
|
||||
lowerThirds, _ := database.GetAllLowerThirds()
|
||||
if assert.Equal(t, 2, len(lowerThirds)) {
|
||||
assert.Equal(t, otherLowerThird, lowerThirds[0])
|
||||
assert.Equal(t, "Event Winner", lowerThirds[1].TopText)
|
||||
assert.Equal(t, "Winner", lowerThirds[1].TopText)
|
||||
assert.Equal(t, "Bob Dorough", lowerThirds[1].BottomText)
|
||||
}
|
||||
|
||||
@@ -74,7 +74,7 @@ func TestCreateOrUpdateAwardWithoutIntro(t *testing.T) {
|
||||
lowerThirds, _ = database.GetAllLowerThirds()
|
||||
if assert.Equal(t, 2, len(lowerThirds)) {
|
||||
assert.Equal(t, otherLowerThird, lowerThirds[0])
|
||||
assert.Equal(t, "Event Winner", lowerThirds[1].TopText)
|
||||
assert.Equal(t, "Winner", lowerThirds[1].TopText)
|
||||
assert.Equal(t, "Bob Dorough – Team 254, Teh Chezy Pofs", lowerThirds[1].BottomText)
|
||||
}
|
||||
|
||||
@@ -102,22 +102,22 @@ func TestCreateOrUpdateWinnerAndFinalistAwards(t *testing.T) {
|
||||
assert.Nil(t, err)
|
||||
awards, _ := database.GetAllAwards()
|
||||
if assert.Equal(t, 6, len(awards)) {
|
||||
assert.Equal(t, model.Award{1, model.FinalistAward, "Event Finalist", 1, ""}, awards[0])
|
||||
assert.Equal(t, model.Award{2, model.FinalistAward, "Event Finalist", 10, ""}, awards[1])
|
||||
assert.Equal(t, model.Award{3, model.FinalistAward, "Event Finalist", 100, ""}, awards[2])
|
||||
assert.Equal(t, model.Award{4, model.WinnerAward, "Event Winner", 2, ""}, awards[3])
|
||||
assert.Equal(t, model.Award{5, model.WinnerAward, "Event Winner", 20, ""}, awards[4])
|
||||
assert.Equal(t, model.Award{6, model.WinnerAward, "Event Winner", 200, ""}, awards[5])
|
||||
assert.Equal(t, model.Award{1, model.FinalistAward, "Finalist", 1, ""}, awards[0])
|
||||
assert.Equal(t, model.Award{2, model.FinalistAward, "Finalist", 10, ""}, awards[1])
|
||||
assert.Equal(t, model.Award{3, model.FinalistAward, "Finalist", 100, ""}, awards[2])
|
||||
assert.Equal(t, model.Award{4, model.WinnerAward, "Winner", 2, ""}, awards[3])
|
||||
assert.Equal(t, model.Award{5, model.WinnerAward, "Winner", 20, ""}, awards[4])
|
||||
assert.Equal(t, model.Award{6, model.WinnerAward, "Winner", 200, ""}, awards[5])
|
||||
}
|
||||
lowerThirds, _ := database.GetAllLowerThirds()
|
||||
if assert.Equal(t, 8, len(lowerThirds)) {
|
||||
assert.Equal(t, "Event Finalist", lowerThirds[0].TopText)
|
||||
assert.Equal(t, "Finalist", lowerThirds[0].TopText)
|
||||
assert.Equal(t, "", lowerThirds[0].BottomText)
|
||||
assert.Equal(t, "Event Finalist", lowerThirds[1].TopText)
|
||||
assert.Equal(t, "Finalist", lowerThirds[1].TopText)
|
||||
assert.Equal(t, "Team 1, ", lowerThirds[1].BottomText)
|
||||
assert.Equal(t, "Event Winner", lowerThirds[4].TopText)
|
||||
assert.Equal(t, "Winner", lowerThirds[4].TopText)
|
||||
assert.Equal(t, "", lowerThirds[4].BottomText)
|
||||
assert.Equal(t, "Event Winner", lowerThirds[5].TopText)
|
||||
assert.Equal(t, "Winner", lowerThirds[5].TopText)
|
||||
assert.Equal(t, "Team 2, ", lowerThirds[5].BottomText)
|
||||
}
|
||||
|
||||
@@ -125,22 +125,22 @@ func TestCreateOrUpdateWinnerAndFinalistAwards(t *testing.T) {
|
||||
assert.Nil(t, err)
|
||||
awards, _ = database.GetAllAwards()
|
||||
if assert.Equal(t, 6, len(awards)) {
|
||||
assert.Equal(t, model.Award{1, model.FinalistAward, "Event Finalist", 2, ""}, awards[0])
|
||||
assert.Equal(t, model.Award{2, model.FinalistAward, "Event Finalist", 20, ""}, awards[1])
|
||||
assert.Equal(t, model.Award{3, model.FinalistAward, "Event Finalist", 200, ""}, awards[2])
|
||||
assert.Equal(t, model.Award{4, model.WinnerAward, "Event Winner", 1, ""}, awards[3])
|
||||
assert.Equal(t, model.Award{5, model.WinnerAward, "Event Winner", 10, ""}, awards[4])
|
||||
assert.Equal(t, model.Award{6, model.WinnerAward, "Event Winner", 100, ""}, awards[5])
|
||||
assert.Equal(t, model.Award{1, model.FinalistAward, "Finalist", 2, ""}, awards[0])
|
||||
assert.Equal(t, model.Award{2, model.FinalistAward, "Finalist", 20, ""}, awards[1])
|
||||
assert.Equal(t, model.Award{3, model.FinalistAward, "Finalist", 200, ""}, awards[2])
|
||||
assert.Equal(t, model.Award{4, model.WinnerAward, "Winner", 1, ""}, awards[3])
|
||||
assert.Equal(t, model.Award{5, model.WinnerAward, "Winner", 10, ""}, awards[4])
|
||||
assert.Equal(t, model.Award{6, model.WinnerAward, "Winner", 100, ""}, awards[5])
|
||||
}
|
||||
lowerThirds, _ = database.GetAllLowerThirds()
|
||||
if assert.Equal(t, 8, len(lowerThirds)) {
|
||||
assert.Equal(t, "Event Finalist", lowerThirds[0].TopText)
|
||||
assert.Equal(t, "Finalist", lowerThirds[0].TopText)
|
||||
assert.Equal(t, "", lowerThirds[0].BottomText)
|
||||
assert.Equal(t, "Event Finalist", lowerThirds[1].TopText)
|
||||
assert.Equal(t, "Finalist", lowerThirds[1].TopText)
|
||||
assert.Equal(t, "Team 2, ", lowerThirds[1].BottomText)
|
||||
assert.Equal(t, "Event Winner", lowerThirds[4].TopText)
|
||||
assert.Equal(t, "Winner", lowerThirds[4].TopText)
|
||||
assert.Equal(t, "", lowerThirds[4].BottomText)
|
||||
assert.Equal(t, "Event Winner", lowerThirds[5].TopText)
|
||||
assert.Equal(t, "Winner", lowerThirds[5].TopText)
|
||||
assert.Equal(t, "Team 1, ", lowerThirds[5].BottomText)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,3 +73,17 @@ func (web *Web) awardsPostHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
http.Redirect(w, r, "/setup/awards", 303)
|
||||
}
|
||||
|
||||
// Publishes the awards to the web.
|
||||
func (web *Web) awardsPublishHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if !web.userIsAdmin(w, r) {
|
||||
return
|
||||
}
|
||||
|
||||
err := web.arena.TbaClient.PublishAwards(web.arena.Database)
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to publish awards: "+err.Error(), 500)
|
||||
return
|
||||
}
|
||||
http.Redirect(w, r, "/setup/awards", 303)
|
||||
}
|
||||
|
||||
@@ -33,3 +33,14 @@ func TestSetupAwards(t *testing.T) {
|
||||
assert.Equal(t, 200, recorder.Code)
|
||||
assert.Contains(t, recorder.Body.String(), "Englebert")
|
||||
}
|
||||
|
||||
func TestSetupAwardsPublish(t *testing.T) {
|
||||
web := setupTestWeb(t)
|
||||
|
||||
web.arena.TbaClient.BaseUrl = "fakeurl"
|
||||
web.arena.EventSettings.TbaPublishingEnabled = true
|
||||
|
||||
recorder := web.postHttpResponse("/setup/awards/publish", "")
|
||||
assert.Equal(t, 500, recorder.Code)
|
||||
assert.Contains(t, recorder.Body.String(), "Failed to publish awards")
|
||||
}
|
||||
|
||||
@@ -145,6 +145,7 @@ func (web *Web) newHandler() http.Handler {
|
||||
router.HandleFunc("/reports/csv/wpa_keys", web.wpaKeysCsvReportHandler).Methods("GET")
|
||||
router.HandleFunc("/setup/awards", web.awardsGetHandler).Methods("GET")
|
||||
router.HandleFunc("/setup/awards", web.awardsPostHandler).Methods("POST")
|
||||
router.HandleFunc("/setup/awards/publish", web.awardsPublishHandler).Methods("POST")
|
||||
router.HandleFunc("/setup/db/clear", web.clearDbHandler).Methods("POST")
|
||||
router.HandleFunc("/setup/db/restore", web.restoreDbHandler).Methods("POST")
|
||||
router.HandleFunc("/setup/db/save", web.saveDbHandler).Methods("GET")
|
||||
|
||||
Reference in New Issue
Block a user