mirror of
https://github.com/Team254/cheesy-arena-lite.git
synced 2026-03-09 13:46:44 -04:00
Migrate to TBA v3 API.
This commit is contained in:
@@ -17,6 +17,11 @@ import (
|
||||
"strconv"
|
||||
)
|
||||
|
||||
const (
|
||||
tbaBaseUrl = "https://www.thebluealliance.com"
|
||||
tbaAuthKey = "MAApv9MCuKY9MSFkXLuzTSYBCdosboxDq8Q3ujUE2Mn8PD3Nmv64uczu5Lvy0NQ3"
|
||||
)
|
||||
|
||||
type TbaClient struct {
|
||||
BaseUrl string
|
||||
eventCode string
|
||||
@@ -25,8 +30,6 @@ type TbaClient struct {
|
||||
eventNamesCache map[string]string
|
||||
}
|
||||
|
||||
const tbaBaseUrl = "https://www.thebluealliance.com"
|
||||
|
||||
type TbaMatch struct {
|
||||
CompLevel string `json:"comp_level"`
|
||||
SetNumber int `json:"set_number"`
|
||||
@@ -84,27 +87,24 @@ type TbaRankings struct {
|
||||
}
|
||||
|
||||
type TbaTeam struct {
|
||||
Website string `json:"website"`
|
||||
Name string `json:"name"`
|
||||
Locality string `json:"locality"`
|
||||
RookieYear int `json:"rookie_year"`
|
||||
Reigon string `json:"region"`
|
||||
TeamNumber int `json:"team_number"`
|
||||
Location string `json:"location"`
|
||||
Key string `json:"key"`
|
||||
Country string `json:"country_name"`
|
||||
Name string `json:"name"`
|
||||
Nickname string `json:"nickname"`
|
||||
City string `json:"city"`
|
||||
StateProv string `json:"state_prov"`
|
||||
Country string `json:"country"`
|
||||
RookieYear int `json:"rookie_year"`
|
||||
}
|
||||
|
||||
type TbaRobot struct {
|
||||
Name string `json:"name"`
|
||||
RobotName string `json:"robot_name"`
|
||||
Year int `json:"year"`
|
||||
}
|
||||
|
||||
type TbaAward struct {
|
||||
Name string `json:"name"`
|
||||
EventKey string `json:"event_key"`
|
||||
Year int `json:"year"`
|
||||
AwardType int `json:"award_type"`
|
||||
EventName string
|
||||
}
|
||||
|
||||
@@ -118,7 +118,7 @@ func NewTbaClient(eventCode, secretId, secret string) *TbaClient {
|
||||
}
|
||||
|
||||
func (client *TbaClient) GetTeam(teamNumber int) (*TbaTeam, error) {
|
||||
path := fmt.Sprintf("/api/v2/team/%s", getTbaTeam(teamNumber))
|
||||
path := fmt.Sprintf("/api/v3/team/%s", getTbaTeam(teamNumber))
|
||||
resp, err := client.getRequest(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -138,7 +138,7 @@ func (client *TbaClient) GetTeam(teamNumber int) (*TbaTeam, error) {
|
||||
}
|
||||
|
||||
func (client *TbaClient) GetRobotName(teamNumber int, year int) (string, error) {
|
||||
path := fmt.Sprintf("/api/v2/team/%s/history/robots", getTbaTeam(teamNumber))
|
||||
path := fmt.Sprintf("/api/v3/team/%s/robots", getTbaTeam(teamNumber))
|
||||
resp, err := client.getRequest(path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
@@ -151,19 +151,21 @@ func (client *TbaClient) GetRobotName(teamNumber int, year int) (string, error)
|
||||
return "", err
|
||||
}
|
||||
|
||||
var robots map[string]TbaRobot
|
||||
var robots []*TbaRobot
|
||||
err = json.Unmarshal(body, &robots)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if robotName, ok := robots[strconv.Itoa(year)]; ok {
|
||||
return robotName.Name, nil
|
||||
for _, robot := range robots {
|
||||
if robot.Year == year {
|
||||
return robot.RobotName, nil
|
||||
}
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
|
||||
func (client *TbaClient) GetTeamAwards(teamNumber int) ([]*TbaAward, error) {
|
||||
path := fmt.Sprintf("/api/v2/team/%s/history/awards", getTbaTeam(teamNumber))
|
||||
path := fmt.Sprintf("/api/v3/team/%s/awards", getTbaTeam(teamNumber))
|
||||
resp, err := client.getRequest(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -367,7 +369,7 @@ func (client *TbaClient) DeletePublishedMatches() error {
|
||||
}
|
||||
|
||||
func (client *TbaClient) getEventName(eventCode string) (string, error) {
|
||||
path := fmt.Sprintf("/api/v2/event/%s", eventCode)
|
||||
path := fmt.Sprintf("/api/v3/event/%s", eventCode)
|
||||
resp, err := client.getRequest(path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
@@ -404,7 +406,7 @@ func (client *TbaClient) getRequest(path string) (*http.Response, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("X-TBA-App-Id", "cheesy-arena:cheesy-fms:v0.1")
|
||||
req.Header.Set("X-TBA-Auth-Key", tbaAuthKey)
|
||||
return httpClient.Do(req)
|
||||
}
|
||||
|
||||
|
||||
@@ -264,7 +264,6 @@ func (web *Web) getOfficialTeamInfo(teamId int) (*model.Team, error) {
|
||||
// Create the team variable that stores the result
|
||||
var team model.Team
|
||||
|
||||
// If team info download is enabled, download the current teams data (caching isn't easy with the new paging system in the api)
|
||||
if web.arena.EventSettings.TBADownloadEnabled {
|
||||
tbaTeam, err := web.arena.TbaClient.GetTeam(teamId)
|
||||
if err != nil {
|
||||
@@ -287,16 +286,18 @@ func (web *Web) getOfficialTeamInfo(teamId int) (*model.Team, error) {
|
||||
|
||||
var accomplishmentsBuffer bytes.Buffer
|
||||
|
||||
// Generate accomplishments string
|
||||
for _, award := range recentAwards {
|
||||
// Generate string of recent awards in reverse chronological order.
|
||||
for i := len(recentAwards) - 1; i >= 0; i-- {
|
||||
award := recentAwards[i]
|
||||
if time.Now().Year()-award.Year <= 2 {
|
||||
accomplishmentsBuffer.WriteString(fmt.Sprintf("<p>%d %s - %s</p>", award.Year, award.EventName, award.Name))
|
||||
accomplishmentsBuffer.WriteString(fmt.Sprintf("<p>%d %s - %s</p>", award.Year, award.EventName,
|
||||
award.Name))
|
||||
}
|
||||
}
|
||||
|
||||
// Use those variables to make a team object
|
||||
team = model.Team{Id: teamId, Name: tbaTeam.Name, Nickname: tbaTeam.Nickname, City: tbaTeam.Locality,
|
||||
StateProv: tbaTeam.Reigon, Country: tbaTeam.Country, RookieYear: tbaTeam.RookieYear,
|
||||
team = model.Team{Id: teamId, Name: tbaTeam.Name, Nickname: tbaTeam.Nickname, City: tbaTeam.City,
|
||||
StateProv: tbaTeam.StateProv, Country: tbaTeam.Country, RookieYear: tbaTeam.RookieYear,
|
||||
RobotName: robotName, Accomplishments: accomplishmentsBuffer.String()}
|
||||
}
|
||||
} else {
|
||||
|
||||
@@ -25,23 +25,23 @@ func TestSetupTeams(t *testing.T) {
|
||||
teamInfoBody := `{
|
||||
"website": "http://www.team254.com",
|
||||
"name": "NASA Ames Research Center",
|
||||
"locality": "San Jose",
|
||||
"city": "San Jose",
|
||||
"rookie_year": 1999,
|
||||
"region": "CA",
|
||||
"state_prov": "CA",
|
||||
"team_number": 254,
|
||||
"location": "San Jose, CA, USA",
|
||||
"key": "frc254",
|
||||
"country_name": "USA",
|
||||
"country": "USA",
|
||||
"nickname": "The Cheesy Poofs"
|
||||
}`
|
||||
teamRobotsBody := `{
|
||||
"2017": {
|
||||
teamRobotsBody := `[
|
||||
{
|
||||
"team_key": "frc33",
|
||||
"name": "Buzz 22",
|
||||
"key": "frc33_2017",
|
||||
"year": 2017
|
||||
}
|
||||
}`
|
||||
]`
|
||||
teamAwardsBody := `[{
|
||||
"event_key": "2014cmp",
|
||||
"award_type": 1,
|
||||
@@ -68,9 +68,9 @@ func TestSetupTeams(t *testing.T) {
|
||||
}]`
|
||||
eventBody := `{ "name": "Championship" }`
|
||||
tbaServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if strings.Contains(r.RequestURI, "history/robots") {
|
||||
if strings.Contains(r.RequestURI, "robots") {
|
||||
fmt.Fprintln(w, teamRobotsBody)
|
||||
} else if strings.Contains(r.RequestURI, "history/awards") {
|
||||
} else if strings.Contains(r.RequestURI, "awards") {
|
||||
fmt.Fprintln(w, teamAwardsBody)
|
||||
} else if strings.Contains(r.RequestURI, "team") {
|
||||
fmt.Fprintln(w, teamInfoBody)
|
||||
|
||||
Reference in New Issue
Block a user