forked from Team254/cheesy-arena
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup_schedule.go
182 lines (167 loc) · 5.16 KB
/
setup_schedule.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
// Copyright 2014 Team 254. All Rights Reserved.
// Author: [email protected] (Patrick Fairbank)
//
// Web routes for generating practice and qualification schedules.
package main
import (
"fmt"
"html/template"
"net/http"
"strconv"
"time"
)
// Global vars to hold schedules that are in the process of being generated.
var cachedMatchType string
var cachedScheduleBlocks []ScheduleBlock
var cachedMatches []Match
var cachedTeamFirstMatches map[int]string
// Shows the schedule editing page.
func ScheduleGetHandler(w http.ResponseWriter, r *http.Request) {
if len(cachedScheduleBlocks) == 0 {
tomorrow := time.Now().AddDate(0, 0, 1)
location, _ := time.LoadLocation("Local")
startTime := time.Date(tomorrow.Year(), tomorrow.Month(), tomorrow.Day(), 9, 0, 0, 0, location)
cachedScheduleBlocks = append(cachedScheduleBlocks, ScheduleBlock{startTime, 10, 360})
cachedMatchType = "practice"
}
renderSchedule(w, r, "")
}
// Generates the schedule and presents it for review without saving it to the database.
func ScheduleGeneratePostHandler(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
cachedMatchType = r.PostFormValue("matchType")
scheduleBlocks, err := getScheduleBlocks(r)
cachedScheduleBlocks = scheduleBlocks // Show the same blocks even if there is an error.
if err != nil {
renderSchedule(w, r, "Incomplete or invalid schedule block parameters specified.")
return
}
// Build the schedule.
teams, err := db.GetAllTeams()
if err != nil {
handleWebErr(w, err)
return
}
if len(teams) == 0 {
renderSchedule(w, r, "No team list is configured. Set up the list of teams at the event before "+
"generating the schedule.")
return
}
if len(teams) < 18 {
renderSchedule(w, r, fmt.Sprintf("There are only %d teams. There must be at least 18 teams to generate "+
"a schedule.", len(teams)))
return
}
matches, err := BuildRandomSchedule(teams, scheduleBlocks, r.PostFormValue("matchType"))
if err != nil {
renderSchedule(w, r, fmt.Sprintf("Error generating schedule: %s.", err.Error()))
return
}
cachedMatches = matches
// Determine each team's first match.
teamFirstMatches := make(map[int]string)
for _, match := range matches {
checkTeam := func(team int) {
_, ok := teamFirstMatches[team]
if !ok {
teamFirstMatches[team] = match.DisplayName
}
}
checkTeam(match.Red1)
checkTeam(match.Red2)
checkTeam(match.Red3)
checkTeam(match.Blue1)
checkTeam(match.Blue2)
checkTeam(match.Blue3)
}
cachedTeamFirstMatches = teamFirstMatches
http.Redirect(w, r, "/setup/schedule", 302)
}
// Saves the generated schedule to the database.
func ScheduleSavePostHandler(w http.ResponseWriter, r *http.Request) {
existingMatches, err := db.GetMatchesByType(cachedMatchType)
if err != nil {
handleWebErr(w, err)
return
}
if len(existingMatches) > 0 {
renderSchedule(w, r, fmt.Sprintf("Can't save schedule because a schedule of %d %s matches already "+
"exists. Clear it first on the Settings page.", len(existingMatches), cachedMatchType))
return
}
for _, match := range cachedMatches {
err = db.CreateMatch(&match)
if err != nil {
handleWebErr(w, err)
return
}
}
// Back up the database.
err = db.Backup("post_scheduling")
if err != nil {
handleWebErr(w, err)
return
}
if eventSettings.TbaPublishingEnabled && cachedMatchType != "practice" {
// Publish schedule to The Blue Alliance.
err = PublishMatches()
if err != nil {
http.Error(w, "Failed to publish matches: "+err.Error(), 500)
return
}
}
http.Redirect(w, r, "/setup/schedule", 302)
}
func renderSchedule(w http.ResponseWriter, r *http.Request, errorMessage string) {
teams, err := db.GetAllTeams()
if err != nil {
handleWebErr(w, err)
return
}
template, err := template.ParseFiles("templates/setup_schedule.html", "templates/base.html")
if err != nil {
handleWebErr(w, err)
return
}
data := struct {
*EventSettings
MatchType string
ScheduleBlocks []ScheduleBlock
NumTeams int
Matches []Match
TeamFirstMatches map[int]string
ErrorMessage string
}{eventSettings, cachedMatchType, cachedScheduleBlocks, len(teams), cachedMatches, cachedTeamFirstMatches,
errorMessage}
err = template.ExecuteTemplate(w, "base", data)
if err != nil {
handleWebErr(w, err)
return
}
}
// Converts the post form variables into a slice of schedule blocks.
func getScheduleBlocks(r *http.Request) ([]ScheduleBlock, error) {
numScheduleBlocks, err := strconv.Atoi(r.PostFormValue("numScheduleBlocks"))
if err != nil {
return []ScheduleBlock{}, err
}
var returnErr error
scheduleBlocks := make([]ScheduleBlock, numScheduleBlocks)
location, _ := time.LoadLocation("Local")
for i := 0; i < numScheduleBlocks; i++ {
scheduleBlocks[i].StartTime, err = time.ParseInLocation("2006-01-02 03:04:05 PM",
r.PostFormValue(fmt.Sprintf("startTime%d", i)), location)
if err != nil {
returnErr = err
}
scheduleBlocks[i].NumMatches, err = strconv.Atoi(r.PostFormValue(fmt.Sprintf("numMatches%d", i)))
if err != nil {
returnErr = err
}
scheduleBlocks[i].MatchSpacingSec, err = strconv.Atoi(r.PostFormValue(fmt.Sprintf("matchSpacingSec%d", i)))
if err != nil {
returnErr = err
}
}
return scheduleBlocks, returnErr
}