forked from Team254/cheesy-arena
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tba.go
229 lines (206 loc) · 7.06 KB
/
tba.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// Copyright 2014 Team 254. All Rights Reserved.
// Author: [email protected] (Patrick Fairbank)
//
// Methods for publishing data to and retrieving data from The Blue Alliance.
package main
import (
"bytes"
"crypto/md5"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
)
var tbaBaseUrl = "http://www.thebluealliance.com"
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"`
TimeString string `json:"time_string"`
TimeUtc string `json:"time_utc"`
}
type TbaScoreBreakdown struct {
Auto int `json:"auto"`
Assist int `json:"assist"`
TrussCatch int `json:"truss+catch"`
GoalFoul int `json:"teleop_goal+foul"`
}
type TbaRanking struct {
TeamKey string `json:"team_key"`
Rank int `json:"rank"`
Wins int `json:"wins"`
Losses int `json:"losses"`
Ties int `json:"ties"`
Played int `json:"played"`
Dqs int `json:"dqs"`
QS int
Assist int
Auto int
TrussCatch int `json:"T&C"`
GoalFoul int `json:"G&F"`
}
// Uploads the event team list to The Blue Alliance.
func PublishTeams() error {
teams, err := db.GetAllTeams()
if err != nil {
return err
}
// Build a JSON array of TBA-format team keys (e.g. "frc254").
teamKeys := make([]string, len(teams))
for i, team := range teams {
teamKeys[i] = getTbaTeam(team.Id)
}
jsonBody, err := json.Marshal(teamKeys)
if err != nil {
return err
}
resp, err := postTbaRequest("team_list", 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
}
// Uploads the qualification and elimination match schedule and results to The Blue Alliance.
func PublishMatches() error {
qualMatches, err := db.GetMatchesByType("qualification")
if err != nil {
return err
}
elimMatches, err := db.GetMatchesByType("elimination")
if err != nil {
return err
}
matches := append(qualMatches, elimMatches...)
tbaMatches := make([]TbaMatch, len(matches))
// Build a JSON array of TBA-format matches.
for i, match := range matches {
matchNumber, _ := strconv.Atoi(match.DisplayName)
redAlliance := map[string]interface{}{"teams": []string{getTbaTeam(match.Red1), getTbaTeam(match.Red2),
getTbaTeam(match.Red3)}, "score": nil}
blueAlliance := map[string]interface{}{"teams": []string{getTbaTeam(match.Blue1), getTbaTeam(match.Blue2),
getTbaTeam(match.Blue3)}, "score": nil}
var scoreBreakdown map[string]TbaScoreBreakdown
// Fill in scores if the match has been played.
if match.Status == "complete" {
matchResult, err := db.GetMatchResultForMatch(match.Id)
if err != nil {
return err
}
if matchResult != nil {
redScoreSummary := matchResult.RedScoreSummary()
blueScoreSummary := matchResult.BlueScoreSummary()
redAlliance["score"] = redScoreSummary.Score
blueAlliance["score"] = blueScoreSummary.Score
scoreBreakdown = make(map[string]TbaScoreBreakdown)
scoreBreakdown["red"] = TbaScoreBreakdown{redScoreSummary.AutoPoints, redScoreSummary.AssistPoints,
redScoreSummary.TrussCatchPoints, redScoreSummary.GoalPoints + redScoreSummary.FoulPoints}
scoreBreakdown["blue"] = TbaScoreBreakdown{blueScoreSummary.AutoPoints, blueScoreSummary.AssistPoints,
blueScoreSummary.TrussCatchPoints, blueScoreSummary.GoalPoints + blueScoreSummary.FoulPoints}
}
}
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
}
}
jsonBody, err := json.Marshal(tbaMatches)
if err != nil {
return err
}
resp, err := postTbaRequest("matches", 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
}
// Uploads the team standings to The Blue Alliance.
func PublishRankings() error {
rankings, err := db.GetAllRankings()
if err != nil {
return err
}
// Build a JSON object of TBA-format rankings.
breakdowns := []string{"QS", "Assist", "Auto", "T&C", "G&F"}
tbaRankings := make([]TbaRanking, len(rankings))
for i, ranking := range rankings {
tbaRankings[i] = TbaRanking{getTbaTeam(ranking.TeamId), ranking.Rank, ranking.Wins, ranking.Losses,
ranking.Ties, ranking.Played, ranking.Disqualifications, ranking.QualificationScore,
ranking.AssistPoints, ranking.AutoPoints, ranking.TrussCatchPoints, ranking.GoalFoulPoints}
}
jsonBody, err := json.Marshal(map[string]interface{}{"breakdowns": breakdowns, "rankings": tbaRankings})
if err != nil {
return err
}
resp, err := postTbaRequest("rankings", 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
}
// Uploads the alliances selection results to The Blue Alliance.
func PublishAlliances() error {
alliances, err := db.GetAllAlliances()
if err != nil {
return err
}
// Build a JSON object of TBA-format alliances.
tbaAlliances := make([][]string, len(alliances))
for i, alliance := range alliances {
for _, team := range alliance {
tbaAlliances[i] = append(tbaAlliances[i], getTbaTeam(team.TeamId))
}
}
jsonBody, err := json.Marshal(tbaAlliances)
if err != nil {
return err
}
resp, err := postTbaRequest("alliance_selections", 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
}
// Converts an integer team number into the "frcXXXX" format TBA expects.
func getTbaTeam(team int) string {
return fmt.Sprintf("frc%d", team)
}
// 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)
signature := fmt.Sprintf("%x", md5.Sum(append([]byte(eventSettings.TbaSecret+path), body...)))
client := &http.Client{}
request, err := http.NewRequest("POST", fmt.Sprintf("%s%s", tbaBaseUrl, path), bytes.NewReader(body))
if err != nil {
return nil, err
}
request.Header.Add("X-TBA-Auth-Id", eventSettings.TbaSecretId)
request.Header.Add("X-TBA-Auth-Sig", signature)
return client.Do(request)
}