Update formatting

This commit is contained in:
Sam Baumgarten
2015-04-01 22:43:39 -07:00
parent 1774197c13
commit 01ae89fedc
2 changed files with 67 additions and 69 deletions

View File

@@ -6,15 +6,15 @@
package main
import (
"fmt"
"time"
"bytes"
"fmt"
"github.com/dchest/uniuri"
"github.com/gorilla/mux"
"html/template"
"net/http"
"strconv"
"strings"
"time"
)
const wpaKeyLength = 8
@@ -231,42 +231,42 @@ func canModifyTeamList() bool {
// Returns the data for the given team number.
func getOfficialTeamInfo(teamId int) (*Team, error) {
// Create the team variable that stores the result
var team Team
// If team info download is enabled, download the current teams data (caching isn't easy with the new paging system in the api)
// Create the team variable that stores the result
var team 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 eventSettings.TBADownloadEnabled {
var tbaTeam *TbaTeam = getTeamFromTba(teamId)
var tbaTeam *TbaTeam = getTeamFromTba(teamId)
// Check if the result is valid. If a team is not found, just return a basic team
if tbaTeam == nil {
team = Team{Id: teamId}
return &team, nil
}
var recentAwards []TbaAward;
if eventSettings.TBAAwardsDownloadEnabled {
recentAwards = getTeamAwardsFromTba(teamId)
}
var accomplishmentsBuffer bytes.Buffer
if tbaTeam == nil {
team = Team{Id: teamId}
return &team, nil
}
// Generate accomplishments string
for _, award := range recentAwards {
if time.Now().Year() - award.Year <= 2 {
accomplishmentsBuffer.WriteString(fmt.Sprint(award.Year, " - ", award.Name, "\n"))
}
}
var recentAwards []TbaAward
if eventSettings.TBAAwardsDownloadEnabled {
recentAwards = getTeamAwardsFromTba(teamId)
}
var accomplishmentsBuffer bytes.Buffer
// Generate accomplishments string
for _, award := range recentAwards {
if time.Now().Year()-award.Year <= 2 {
accomplishmentsBuffer.WriteString(fmt.Sprint(award.Year, " - ", award.Name, "\n"))
}
}
// Use those variables to make a team object
team = Team{Id: teamId, Name: tbaTeam.Name, Nickname: tbaTeam.Nickname,
City: tbaTeam.Locality, StateProv: tbaTeam.Reigon,
Country: tbaTeam.Country, RookieYear: tbaTeam.RookieYear, Accomplishments: accomplishmentsBuffer.String()}
} else {
// If team grab is disabled, just use the team number
team = Team{Id: teamId}
}
// Use those variables to make a team object
team = Team{Id: teamId, Name: tbaTeam.Name, Nickname: tbaTeam.Nickname,
City: tbaTeam.Locality, StateProv: tbaTeam.Reigon,
Country: tbaTeam.Country, RookieYear: tbaTeam.RookieYear, Accomplishments: accomplishmentsBuffer.String()}
} else {
// If team grab is disabled, just use the team number
team = Team{Id: teamId}
}
// Return the team object
return &team, nil
}

68
tba.go
View File

@@ -52,60 +52,58 @@ type TbaRanking 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"`
Nickname string `json:"nickname"`
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"`
}
type TbaAward struct {
Name string `json:"name"`
EventKey string `json:"event_key"`
Year int `json:"year"`
AwardType int `json:"award_type"`
Name string `json:"name"`
EventKey string `json:"event_key"`
Year int `json:"year"`
AwardType int `json:"award_type"`
}
// DATA RETRIEVAL
func getTeamFromTba(teamNumber int) (*TbaTeam) {
url := fmt.Sprint("/api/v2/team/", string(getTbaTeam(teamNumber)))
resp, _ := getTbaRequest(url);
func getTeamFromTba(teamNumber int) *TbaTeam {
url := fmt.Sprint("/api/v2/team/", string(getTbaTeam(teamNumber)))
resp, _ := getTbaRequest(url)
// Get the response and handle errors
// Get the response and handle errors
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil
}
var teamData TbaTeam
json.Unmarshal(body, &teamData)
return &teamData
json.Unmarshal(body, &teamData)
return &teamData
}
func getTeamAwardsFromTba(teamNumber int) ([]TbaAward) {
url := fmt.Sprint("/api/v2/team/", string(getTbaTeam(teamNumber)), "/history/awards")
resp, _ := getTbaRequest(url);
func getTeamAwardsFromTba(teamNumber int) []TbaAward {
url := fmt.Sprint("/api/v2/team/", string(getTbaTeam(teamNumber)), "/history/awards")
resp, _ := getTbaRequest(url)
// Get the response and handle errors
// Get the response and handle errors
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil
}
var awardData []TbaAward
json.Unmarshal(body, &awardData)
return awardData
json.Unmarshal(body, &awardData)
return awardData
}
// PUBLISHING
@@ -293,12 +291,12 @@ func postTbaRequest(resource string, body []byte) (*http.Response, error) {
// Sends a GET request to the TBA API
func getTbaRequest(path string) (*http.Response, error) {
// Make an HTTP GET request with the TBA auth headers
// Make an HTTP GET request with the TBA auth headers
client := &http.Client{}
req, err := http.NewRequest("GET", fmt.Sprint(tbaBaseUrl, path), nil)
if err != nil {
return nil, err
return nil, err
}
req.Header.Set("X-TBA-App-Id", "cheesy-arena:cheesy-fms:v0.1")
return client.Do(req)
return client.Do(req)
}