-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfrc.go
194 lines (177 loc) · 4.76 KB
/
frc.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
package main
import (
"fmt"
"log"
"os"
"strconv"
"strings"
"time"
"github.com/fatih/color"
"github.com/frc1418/tbago"
"github.com/urfave/cli"
"github.com/willf/pad"
)
const (
key = "EzMD6D489Qttrf80Efz0rF9j3zRVz0pWuE0jfc4RlrUNA1yHDoaow8EN4THKIiJt"
version = "0.3.0"
)
// Used for printing in color
var (
c = color.New(color.FgCyan, color.Underline)
b = color.New(color.FgBlue)
r = color.New(color.FgRed)
g = color.New(color.FgGreen)
)
func main() {
// Initialize TBA parser
tba, err := tbago.New(key)
if err != nil {
log.Fatal(err)
os.Exit(1)
}
app := cli.NewApp()
app.Name = "frc"
app.Usage = "handle FRC-related tasks in the command line."
app.Version = version
app.EnableBashCompletion = true
app.Commands = []cli.Command{
{
Name: "team",
Aliases: []string{"t"},
Usage: "Get data on a team",
Action: func(c *cli.Context) error {
if !c.Args().Present() {
log.Fatal("Usage: " + c.Command.Usage)
}
num, err := strconv.Atoi(c.Args()[0])
if err != nil {
log.Fatal("Invalid team number.\n")
os.Exit(1)
}
team, err := tba.Team(num).Get()
if err != nil {
log.Fatal("TBA request unsuccessful.\n")
os.Exit(1)
}
displayTeam(team)
return nil
},
},
{
Name: "event",
Aliases: []string{"e"},
Usage: "Get data on an event",
Action: func(c *cli.Context) error {
if !c.Args().Present() {
log.Fatal("Usage: " + c.Command.Usage)
os.Exit(1)
}
event, err := tba.Event(c.Args()[0]).Get()
if err != nil {
log.Fatal("Invalid event key or failed TBA request.\n")
os.Exit(1)
}
displayEvent(event)
return nil
},
},
{
Name: "match",
Aliases: []string{"m"},
Usage: "Get data on a match",
Action: func(c *cli.Context) error {
match, err := tba.Match(c.Args()[0]).Get()
if err != nil {
log.Fatal("Invalid match key or failed TBA request.\n")
os.Exit(1)
}
displayMatch(match)
return nil
},
},
{
Name: "eventmatches",
Aliases: []string{"em"},
Usage: "Get data on the matches at an event",
Action: func(c *cli.Context) error {
var matches []tbago.Match
// TODO: Don't discard error
if len(c.Args()) > 1 {
matches, _ = tba.Event(c.Args()[0]).Matches().Get()
} else {
team, err := strconv.Atoi(c.Args()[1])
if err != nil {
log.Fatal("Invalid team key.")
os.Exit(1)
}
matches, _ = tba.Team(team).Event(c.Args()[0]).Matches().Get()
}
if len(matches) == 0 {
log.Fatal("No matches found.\n")
os.Exit(1)
}
for _, match := range matches {
displayMatch(match)
}
return nil
},
},
}
app.Run(os.Args)
}
func listInfo(header string, titles []string, data []interface{}) {
fmt.Printf("\n ")
c.Printf("%s:\n", header)
for i := range titles {
g.Printf("\t%s ", pad.Right(titles[i]+":", 10, " "))
fmt.Println(data[i])
}
fmt.Println()
}
func displayTeam(team tbago.Team) {
header := fmt.Sprintf("Team %d", team.TeamNumber)
titles := []string{"Nickname", "Website", "Rookie", "Country", "Motto"}
data := []interface{}{team.Nickname, team.Website, team.RookieYear, team.Country, team.Motto}
listInfo(header, titles, data)
}
func displayEvent(event tbago.Event) {
header := fmt.Sprintf("%d %s (%s)", event.Year, event.Name, event.Key)
titles := []string{"Date", "Timezone", "Website", "Location", "Address", "District", "Type"}
data := []interface{}{fmt.Sprintf("%s - %s", event.StartDate, event.EndDate), event.Timezone, event.Website, event.LocationName, strings.Replace(event.Address, "\n", ", ", -1), event.District.DisplayName, fmt.Sprintf("%s (ID %d)", event.EventTypeString, event.EventType)}
listInfo(header, titles, data)
}
func displayMatch(match tbago.Match) {
header := fmt.Sprintf("%s %s #%d", strings.ToUpper(match.EventKey), strings.ToUpper(match.CompLevel), match.MatchNumber)
if match.CompLevel == "qm" {
header += fmt.Sprintf(" (%s)", match.Key)
} else {
header += fmt.Sprintf(", Round %d (%s)", match.SetNumber, match.Key)
}
titles := []string{"Date/Time", "Alliances"}
data := []interface{}{time.Unix(match.Time, 0).Format("06-01-02 at 15:01"), ""}
listInfo(header, titles, data)
if match.Alliances.Red.Score > match.Alliances.Blue.Score {
r.Printf("\t 🏆 ")
} else {
r.Printf("\t ")
}
for index, team := range match.Alliances.Red.TeamKeys {
r.Printf("%s", team[3:])
if index < 2 {
r.Print(" | ")
}
}
r.Printf(" => %d points\n", match.Alliances.Red.Score)
if match.Alliances.Red.Score < match.Alliances.Blue.Score {
b.Printf("\t 🏆 ")
} else {
b.Printf("\t ")
}
for index, team := range match.Alliances.Blue.TeamKeys {
b.Printf("%s", team[3:])
if index < 2 {
b.Print(" | ")
}
}
b.Printf(" => %d points\n\n", match.Alliances.Blue.Score)
}