Files
cheesy-arena-lite/tba.go

303 lines
8.8 KiB
Go
Raw Normal View History

2014-07-30 22:55:14 -07:00
// Copyright 2014 Team 254. All Rights Reserved.
// Author: pat@patfairbank.com (Patrick Fairbank)
//
// Methods for publishing data to and retrieving data from The Blue Alliance.
package main
import (
"bytes"
"crypto/md5"
2014-07-30 22:55:14 -07:00
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
)
var tbaBaseUrl = "http://www.thebluealliance.com"
2014-07-30 22:55:14 -07:00
2015-04-01 14:19:14 -07:00
// MODELS
2014-07-30 22:55:14 -07:00
type TbaMatch struct {
CompLevel string `json:"comp_level"`
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 {
Coopertition int `json:"coopertition"`
Auto int `json:"auto"`
Container int `json:"container"`
Tote int `json:"tote"`
Litter int `json:"litter"`
Foul int `json:"foul"`
2014-07-30 22:55:14 -07:00
}
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"`
Dqs int `json:"dqs"`
Played int `json:"played"`
2014-07-30 22:55:14 -07:00
}
2015-04-01 14:19:14 -07:00
type TbaTeam struct {
2015-04-01 22:43:39 -07:00
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"`
Nickname string `json:"nickname"`
2015-04-01 14:19:14 -07:00
}
type TbaAward struct {
2015-04-01 22:43:39 -07:00
Name string `json:"name"`
EventKey string `json:"event_key"`
Year int `json:"year"`
AwardType int `json:"award_type"`
2015-04-01 14:19:14 -07:00
}
// DATA RETRIEVAL
2015-04-01 22:43:39 -07:00
func getTeamFromTba(teamNumber int) *TbaTeam {
url := fmt.Sprint("/api/v2/team/", string(getTbaTeam(teamNumber)))
resp, _ := getTbaRequest(url)
2015-04-01 14:19:14 -07:00
2015-04-01 22:43:39 -07:00
// Get the response and handle errors
2015-04-01 14:19:14 -07:00
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil
}
2015-04-01 22:43:39 -07:00
2015-04-01 14:19:14 -07:00
var teamData TbaTeam
2015-04-01 22:43:39 -07:00
json.Unmarshal(body, &teamData)
return &teamData
2015-04-01 14:19:14 -07:00
}
2015-04-01 22:43:39 -07:00
func getTeamAwardsFromTba(teamNumber int) []TbaAward {
url := fmt.Sprint("/api/v2/team/", string(getTbaTeam(teamNumber)), "/history/awards")
resp, _ := getTbaRequest(url)
2015-04-01 14:19:14 -07:00
2015-04-01 22:43:39 -07:00
// Get the response and handle errors
2015-04-01 14:19:14 -07:00
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil
}
2015-04-01 22:43:39 -07:00
2015-04-01 14:19:14 -07:00
var awardData []TbaAward
2015-04-01 22:43:39 -07:00
json.Unmarshal(body, &awardData)
return awardData
2015-04-01 14:19:14 -07:00
}
// PUBLISHING
2014-07-30 22:55:14 -07:00
// 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)
2014-07-30 22:55:14 -07:00
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
2014-07-30 22:55:14 -07:00
// 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.CoopertitionPoints,
redScoreSummary.AutoPoints, redScoreSummary.ContainerPoints, redScoreSummary.TotePoints,
redScoreSummary.LitterPoints, redScoreSummary.FoulPoints}
scoreBreakdown["blue"] = TbaScoreBreakdown{blueScoreSummary.CoopertitionPoints,
blueScoreSummary.AutoPoints, blueScoreSummary.ContainerPoints, blueScoreSummary.TotePoints,
blueScoreSummary.LitterPoints, blueScoreSummary.FoulPoints}
2014-07-30 22:55:14 -07:00
}
}
tbaMatches[i] = TbaMatch{"qm", matchNumber, map[string]interface{}{"red": redAlliance,
"blue": blueAlliance}, scoreBreakdown, match.Time.Local().Format("3:04 PM"),
match.Time.Format("2006-01-02T15:04:05")}
2014-07-30 22:55:14 -07:00
if match.Type == "elimination" {
tbaMatches[i].CompLevel = map[int]string{1: "f", 2: "sf", 4: "qf", 8: "ef"}[match.ElimRound]
tbaMatches[i].MatchNumber = match.ElimInstance
}
}
jsonBody, err := json.Marshal(tbaMatches)
if err != nil {
return err
}
resp, err := postTbaRequest("matches", jsonBody)
2014-07-30 22:55:14 -07:00
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", "wins", "losses", "ties"}
2014-07-30 22:55:14 -07:00
tbaRankings := make([]TbaRanking, len(rankings))
for i, ranking := range rankings {
tbaRankings[i] = TbaRanking{getTbaTeam(ranking.TeamId), ranking.Rank, ranking.QualificationAverage,
ranking.CoopertitionPoints, ranking.AutoPoints, ranking.ContainerPoints, ranking.TotePoints,
ranking.LitterPoints, ranking.Disqualifications, ranking.Played}
2014-07-30 22:55:14 -07:00
}
jsonBody, err := json.Marshal(map[string]interface{}{"breakdowns": breakdowns, "rankings": tbaRankings})
if err != nil {
return err
}
resp, err := postTbaRequest("rankings", jsonBody)
2014-07-30 22:55:14 -07:00
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)
2014-07-30 22:55:14 -07:00
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)
}
2015-04-01 14:19:14 -07:00
// 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)
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)
}
2015-04-01 14:19:14 -07:00
// Sends a GET request to the TBA API
func getTbaRequest(path string) (*http.Response, error) {
2015-04-01 22:43:39 -07:00
// Make an HTTP GET request with the TBA auth headers
2015-04-01 14:19:14 -07:00
client := &http.Client{}
req, err := http.NewRequest("GET", fmt.Sprint(tbaBaseUrl, path), nil)
if err != nil {
2015-04-01 22:43:39 -07:00
return nil, err
2015-04-01 14:19:14 -07:00
}
req.Header.Set("X-TBA-App-Id", "cheesy-arena:cheesy-fms:v0.1")
2015-04-01 22:43:39 -07:00
return client.Do(req)
2015-04-01 14:19:14 -07:00
}