forked from Team254/cheesy-arena
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup_teams.go
309 lines (271 loc) · 6.95 KB
/
setup_teams.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// Copyright 2014 Team 254. All Rights Reserved.
// Author: [email protected] (Patrick Fairbank)
//
// Web routes for configuring the team list.
package main
import (
"bytes"
"fmt"
"github.com/dchest/uniuri"
"github.com/gorilla/mux"
"html/template"
"net/http"
"strconv"
"strings"
"time"
)
const wpaKeyLength = 8
// Shows the team list.
func TeamsGetHandler(w http.ResponseWriter, r *http.Request) {
if !UserIsAdmin(w, r) {
return
}
renderTeams(w, r, false)
}
// Adds teams to the team list.
func TeamsPostHandler(w http.ResponseWriter, r *http.Request) {
if !UserIsAdmin(w, r) {
return
}
if !canModifyTeamList() {
renderTeams(w, r, true)
return
}
var teamNumbers []int
for _, teamNumberString := range strings.Split(r.PostFormValue("teamNumbers"), "\r\n") {
teamNumber, err := strconv.Atoi(teamNumberString)
if err == nil {
teamNumbers = append(teamNumbers, teamNumber)
}
}
for _, teamNumber := range teamNumbers {
team, err := getOfficialTeamInfo(teamNumber)
if err != nil {
handleWebErr(w, err)
return
}
err = db.CreateTeam(team)
if err != nil {
handleWebErr(w, err)
return
}
}
http.Redirect(w, r, "/setup/teams", 302)
}
// Clears the team list.
func TeamsClearHandler(w http.ResponseWriter, r *http.Request) {
if !UserIsAdmin(w, r) {
return
}
if !canModifyTeamList() {
renderTeams(w, r, true)
return
}
err := db.TruncateTeams()
if err != nil {
handleWebErr(w, err)
return
}
http.Redirect(w, r, "/setup/teams", 302)
}
// Shows the page to edit a team's fields.
func TeamEditGetHandler(w http.ResponseWriter, r *http.Request) {
if !UserIsAdmin(w, r) {
return
}
vars := mux.Vars(r)
teamId, _ := strconv.Atoi(vars["id"])
team, err := db.GetTeamById(teamId)
if err != nil {
handleWebErr(w, err)
return
}
if team == nil {
http.Error(w, fmt.Sprintf("Error: No such team: %d", teamId), 400)
return
}
template, err := template.ParseFiles("templates/edit_team.html", "templates/base.html")
if err != nil {
handleWebErr(w, err)
return
}
data := struct {
*EventSettings
*Team
}{eventSettings, team}
err = template.ExecuteTemplate(w, "base", data)
if err != nil {
handleWebErr(w, err)
return
}
}
// Updates a team's fields.
func TeamEditPostHandler(w http.ResponseWriter, r *http.Request) {
if !UserIsAdmin(w, r) {
return
}
vars := mux.Vars(r)
teamId, _ := strconv.Atoi(vars["id"])
team, err := db.GetTeamById(teamId)
if err != nil {
handleWebErr(w, err)
return
}
if team == nil {
http.Error(w, fmt.Sprintf("Error: No such team: %d", teamId), 400)
return
}
team.Name = r.PostFormValue("name")
team.Nickname = r.PostFormValue("nickname")
team.City = r.PostFormValue("city")
team.StateProv = r.PostFormValue("stateProv")
team.Country = r.PostFormValue("country")
team.RookieYear, _ = strconv.Atoi(r.PostFormValue("rookieYear"))
team.RobotName = r.PostFormValue("robotName")
team.Accomplishments = r.PostFormValue("accomplishments")
if eventSettings.NetworkSecurityEnabled {
team.WpaKey = r.PostFormValue("wpaKey")
if len(team.WpaKey) < 8 || len(team.WpaKey) > 63 {
handleWebErr(w, fmt.Errorf("WPA key must be between 8 and 63 characters."))
return
}
}
err = db.SaveTeam(team)
if err != nil {
handleWebErr(w, err)
return
}
http.Redirect(w, r, "/setup/teams", 302)
}
// Removes a team from the team list.
func TeamDeletePostHandler(w http.ResponseWriter, r *http.Request) {
if !UserIsAdmin(w, r) {
return
}
if !canModifyTeamList() {
renderTeams(w, r, true)
return
}
vars := mux.Vars(r)
teamId, _ := strconv.Atoi(vars["id"])
team, err := db.GetTeamById(teamId)
if err != nil {
handleWebErr(w, err)
return
}
if team == nil {
http.Error(w, fmt.Sprintf("Error: No such team: %d", teamId), 400)
return
}
err = db.DeleteTeam(team)
if err != nil {
handleWebErr(w, err)
return
}
http.Redirect(w, r, "/setup/teams", 302)
}
// Publishes the team list to the web.
func TeamsPublishHandler(w http.ResponseWriter, r *http.Request) {
if !UserIsAdmin(w, r) {
return
}
err := PublishTeams()
if err != nil {
http.Error(w, "Failed to publish teams: "+err.Error(), 500)
return
}
http.Redirect(w, r, "/setup/teams", 302)
}
// Generates random WPA keys and saves them to the team models.
func TeamsGenerateWpaKeysHandler(w http.ResponseWriter, r *http.Request) {
if !UserIsAdmin(w, r) {
return
}
generateAllKeys := false
if all, ok := r.URL.Query()["all"]; ok {
generateAllKeys = all[0] == "true"
}
teams, err := db.GetAllTeams()
if err != nil {
handleWebErr(w, err)
return
}
for _, team := range teams {
if len(team.WpaKey) == 0 || generateAllKeys {
team.WpaKey = uniuri.NewLen(wpaKeyLength)
db.SaveTeam(&team)
}
}
http.Redirect(w, r, "/setup/teams", 302)
}
func renderTeams(w http.ResponseWriter, r *http.Request, showErrorMessage bool) {
teams, err := db.GetAllTeams()
if err != nil {
handleWebErr(w, err)
return
}
template, err := template.ParseFiles("templates/setup_teams.html", "templates/base.html")
if err != nil {
handleWebErr(w, err)
return
}
data := struct {
*EventSettings
Teams []Team
ShowErrorMessage bool
}{eventSettings, teams, showErrorMessage}
err = template.ExecuteTemplate(w, "base", data)
if err != nil {
handleWebErr(w, err)
return
}
}
// Returns true if it is safe to change the team list (i.e. no matches/results exist yet).
func canModifyTeamList() bool {
matches, err := db.GetMatchesByType("qualification")
if err != nil || len(matches) > 0 {
return false
}
return true
}
// 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)
if eventSettings.TBADownloadEnabled {
tbaTeam, err := getTeamFromTba(teamId)
if err != nil {
return nil, err
}
// Check if the result is valid. If a team is not found, just return a basic team
if tbaTeam.TeamNumber == 0 {
team = Team{Id: teamId}
} else {
robotName, err := getRobotNameFromTba(teamId, time.Now().Year())
if err != nil {
return nil, err
}
recentAwards, err := getTeamAwardsFromTba(teamId)
if err != nil {
return nil, err
}
var accomplishmentsBuffer bytes.Buffer
// Generate accomplishments string
for _, award := range recentAwards {
if time.Now().Year()-award.Year <= 2 {
accomplishmentsBuffer.WriteString(fmt.Sprintf("<p>%d %s - %s</p>", award.Year, award.EventName, award.Name))
}
}
// 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,
RobotName: robotName, 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
}