forked from Team254/cheesy-arena
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup_settings.go
191 lines (175 loc) · 5.32 KB
/
setup_settings.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
// Copyright 2014 Team 254. All Rights Reserved.
// Author: [email protected] (Patrick Fairbank)
//
// Web routes for configuring the event settings.
package main
import (
"fmt"
"html/template"
"io"
"io/ioutil"
"net/http"
"os"
"regexp"
"strconv"
"strings"
"time"
)
// Shows the event settings editing page.
func SettingsGetHandler(w http.ResponseWriter, r *http.Request) {
renderSettings(w, r, "")
}
// Saves the event settings.
func SettingsPostHandler(w http.ResponseWriter, r *http.Request) {
eventSettings.Name = r.PostFormValue("name")
eventSettings.Code = r.PostFormValue("code")
match, _ := regexp.MatchString("^#([0-9A-Fa-f]{3}){1,2}$", r.PostFormValue("displayBackgroundColor"))
if !match {
renderSettings(w, r, "Display background color must be a valid hex color value.")
return
}
eventSettings.DisplayBackgroundColor = r.PostFormValue("displayBackgroundColor")
numAlliances, _ := strconv.Atoi(r.PostFormValue("numElimAlliances"))
if numAlliances < 2 || numAlliances > 16 {
renderSettings(w, r, "Number of alliances must be between 2 and 16.")
return
}
eventSettings.NumElimAlliances = numAlliances
eventSettings.SelectionRound2Order = r.PostFormValue("selectionRound2Order")
eventSettings.SelectionRound3Order = r.PostFormValue("selectionRound3Order")
eventSettings.TeamInfoDownloadEnabled = r.PostFormValue("teamInfoDownloadEnabled") == "on"
eventSettings.AllianceDisplayHotGoals = r.PostFormValue("allianceDisplayHotGoals") == "on"
eventSettings.RedGoalLightsAddress = r.PostFormValue("redGoalLightsAddress")
eventSettings.BlueGoalLightsAddress = r.PostFormValue("blueGoalLightsAddress")
eventSettings.TbaPublishingEnabled = r.PostFormValue("tbaPublishingEnabled") == "on"
eventSettings.TbaEventCode = r.PostFormValue("tbaEventCode")
eventSettings.TbaSecretId = r.PostFormValue("tbaSecretId")
eventSettings.TbaSecret = r.PostFormValue("tbaSecret")
eventSettings.NetworkSecurityEnabled = r.PostFormValue("networkSecurityEnabled") == "on"
eventSettings.ApAddress = r.PostFormValue("apAddress")
eventSettings.ApUsername = r.PostFormValue("apUsername")
eventSettings.ApPassword = r.PostFormValue("apPassword")
eventSettings.SwitchAddress = r.PostFormValue("switchAddress")
eventSettings.SwitchPassword = r.PostFormValue("switchPassword")
err := db.SaveEventSettings(eventSettings)
if err != nil {
handleWebErr(w, err)
return
}
// Set up the light controller connections again in case the address changed.
err = mainArena.lights.SetupConnections()
if err != nil {
handleWebErr(w, err)
return
}
http.Redirect(w, r, "/setup/settings", 302)
}
// Sends a copy of the event database file to the client as a download.
func SaveDbHandler(w http.ResponseWriter, r *http.Request) {
dbFile, err := os.Open(db.path)
defer dbFile.Close()
if err != nil {
handleWebErr(w, err)
return
}
filename := fmt.Sprintf("%s-%s.db", strings.Replace(eventSettings.Name, " ", "_", -1),
time.Now().Format("20060102150405"))
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", filename))
http.ServeContent(w, r, "", time.Now(), dbFile)
}
// Accepts an event database file as an upload and loads it.
func RestoreDbHandler(w http.ResponseWriter, r *http.Request) {
file, _, err := r.FormFile("databaseFile")
if err != nil {
renderSettings(w, r, "No database backup file was specified.")
return
}
// Write the file to a temporary location on disk and verify that it can be opened as a database.
tempFile, err := ioutil.TempFile(".", "uploaded-db-")
if err != nil {
handleWebErr(w, err)
return
}
defer tempFile.Close()
tempFilePath := tempFile.Name()
defer os.Remove(tempFilePath)
_, err = io.Copy(tempFile, file)
if err != nil {
handleWebErr(w, err)
return
}
tempFile.Close()
tempDb, err := OpenDatabase(tempFilePath)
if err != nil {
renderSettings(w, r, "Could not read uploaded database backup file. Please verify that it a valid "+
"database file.")
return
}
tempDb.Close()
// Back up the current database.
err = db.Backup("pre_restore")
if err != nil {
handleWebErr(w, err)
return
}
// Replace the current database with the new one.
db.Close()
err = os.Remove(eventDbPath)
if err != nil {
handleWebErr(w, err)
return
}
err = os.Rename(tempFilePath, eventDbPath)
if err != nil {
handleWebErr(w, err)
return
}
initDb()
http.Redirect(w, r, "/setup/settings", 302)
}
// Deletes all data except for the team list.
func ClearDbHandler(w http.ResponseWriter, r *http.Request) {
// Back up the database.
err := db.Backup("pre_clear")
if err != nil {
handleWebErr(w, err)
return
}
err = db.TruncateMatches()
if err != nil {
handleWebErr(w, err)
return
}
err = db.TruncateMatchResults()
if err != nil {
handleWebErr(w, err)
return
}
err = db.TruncateRankings()
if err != nil {
handleWebErr(w, err)
return
}
err = db.TruncateAllianceTeams()
if err != nil {
handleWebErr(w, err)
return
}
http.Redirect(w, r, "/setup/settings", 302)
}
func renderSettings(w http.ResponseWriter, r *http.Request, errorMessage string) {
template, err := template.ParseFiles("templates/setup_settings.html", "templates/base.html")
if err != nil {
handleWebErr(w, err)
return
}
data := struct {
*EventSettings
ErrorMessage string
}{eventSettings, errorMessage}
err = template.ExecuteTemplate(w, "base", data)
if err != nil {
handleWebErr(w, err)
return
}
}