forked from Team254/cheesy-arena
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup_defense_selection.go
138 lines (122 loc) · 3.58 KB
/
setup_defense_selection.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
// Copyright 2016 Team 254. All Rights Reserved.
// Author: [email protected] (Patrick Fairbank)
//
// Web routes for conducting the team defense selection process.
package main
import (
"fmt"
"net/http"
"strconv"
"text/template"
)
// Shows the defense selection page.
func DefenseSelectionGetHandler(w http.ResponseWriter, r *http.Request) {
if !UserIsAdmin(w, r) {
return
}
renderDefenseSelection(w, r, "")
}
// Updates the cache with the latest input from the client.
func DefenseSelectionPostHandler(w http.ResponseWriter, r *http.Request) {
if !UserIsAdmin(w, r) {
return
}
matchId, _ := strconv.Atoi(r.PostFormValue("matchId"))
match, err := db.GetMatchById(matchId)
if err != nil {
handleWebErr(w, err)
return
}
redErr := validateDefenseSelection([]string{r.PostFormValue("redDefense2"),
r.PostFormValue("redDefense3"), r.PostFormValue("redDefense4"), r.PostFormValue("redDefense5")})
if redErr == nil {
match.RedDefense1 = "LB"
match.RedDefense2 = r.PostFormValue("redDefense2")
match.RedDefense3 = r.PostFormValue("redDefense3")
match.RedDefense4 = r.PostFormValue("redDefense4")
match.RedDefense5 = r.PostFormValue("redDefense5")
}
blueErr := validateDefenseSelection([]string{r.PostFormValue("blueDefense2"),
r.PostFormValue("blueDefense3"), r.PostFormValue("blueDefense4"), r.PostFormValue("blueDefense5")})
if blueErr == nil {
match.BlueDefense1 = "LB"
match.BlueDefense2 = r.PostFormValue("blueDefense2")
match.BlueDefense3 = r.PostFormValue("blueDefense3")
match.BlueDefense4 = r.PostFormValue("blueDefense4")
match.BlueDefense5 = r.PostFormValue("blueDefense5")
}
if redErr == nil || blueErr == nil {
err = db.SaveMatch(match)
if err != nil {
handleWebErr(w, err)
return
}
mainArena.defenseSelectionNotifier.Notify(nil)
}
if redErr != nil {
renderDefenseSelection(w, r, redErr.Error())
return
}
if blueErr != nil {
renderDefenseSelection(w, r, blueErr.Error())
return
}
http.Redirect(w, r, "/setup/defense_selection", 302)
}
func renderDefenseSelection(w http.ResponseWriter, r *http.Request, errorMessage string) {
template := template.New("").Funcs(templateHelpers)
_, err := template.ParseFiles("templates/setup_defense_selection.html")
if err != nil {
handleWebErr(w, err)
return
}
matches, err := db.GetMatchesByType("elimination")
if err != nil {
handleWebErr(w, err)
return
}
var unplayedMatches []Match
for _, match := range matches {
if match.Status != "complete" {
unplayedMatches = append(unplayedMatches, match)
}
}
data := struct {
*EventSettings
Matches []Match
DefenseNames map[string]string
ErrorMessage string
}{eventSettings, unplayedMatches, defenseNames, errorMessage}
err = template.ExecuteTemplate(w, "setup_defense_selection.html", data)
if err != nil {
handleWebErr(w, err)
return
}
}
// Takes a slice of the defenses in positions 2-5 and returns an error if they are not valid.
func validateDefenseSelection(defenses []string) error {
// Build map to track which defenses have been used.
defenseCounts := make(map[string]int)
for _, defense := range placeableDefenses {
defenseCounts[defense] = 0
}
numBlankDefenses := 0
for _, defense := range defenses {
if defense == "" {
numBlankDefenses++
continue
}
defenseCount, ok := defenseCounts[defense]
if !ok {
return fmt.Errorf("Invalid defense type: %s", defense)
}
if defenseCount != 0 {
return fmt.Errorf("Defense used more than once: %s", defense)
}
defenseCounts[defense]++
}
if numBlankDefenses > 0 && numBlankDefenses < 4 {
return fmt.Errorf("Cannot leave defenses blank.")
}
return nil
}