forked from Team254/cheesy-arena
-
Notifications
You must be signed in to change notification settings - Fork 0
/
match_review.go
220 lines (197 loc) · 5.82 KB
/
match_review.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
// Copyright 2014 Team 254. All Rights Reserved.
// Author: [email protected] (Patrick Fairbank)
//
// Web routes for editing match results.
package main
import (
"fmt"
"github.com/gorilla/mux"
"net/http"
"strconv"
"text/template"
)
type MatchReviewListItem struct {
Id int
DisplayName string
Time string
RedTeams []int
BlueTeams []int
RedScore int
BlueScore int
ColorClass string
}
// Shows the match review interface.
func MatchReviewHandler(w http.ResponseWriter, r *http.Request) {
if !UserIsReader(w, r) {
return
}
practiceMatches, err := buildMatchReviewList("practice")
if err != nil {
handleWebErr(w, err)
return
}
qualificationMatches, err := buildMatchReviewList("qualification")
if err != nil {
handleWebErr(w, err)
return
}
eliminationMatches, err := buildMatchReviewList("elimination")
if err != nil {
handleWebErr(w, err)
return
}
template, err := template.ParseFiles("templates/match_review.html", "templates/base.html")
if err != nil {
handleWebErr(w, err)
return
}
matchesByType := map[string][]MatchReviewListItem{"practice": practiceMatches,
"qualification": qualificationMatches, "elimination": eliminationMatches}
if currentMatchType == "" {
currentMatchType = "practice"
}
data := struct {
*EventSettings
MatchesByType map[string][]MatchReviewListItem
CurrentMatchType string
}{eventSettings, matchesByType, currentMatchType}
err = template.ExecuteTemplate(w, "base", data)
if err != nil {
handleWebErr(w, err)
return
}
}
// Shows the page to edit the results for a match.
func MatchReviewEditGetHandler(w http.ResponseWriter, r *http.Request) {
if !UserIsAdmin(w, r) {
return
}
match, matchResult, _, err := getMatchResultFromRequest(r)
if err != nil {
handleWebErr(w, err)
return
}
template, err := template.ParseFiles("templates/edit_match_result.html", "templates/base.html")
if err != nil {
handleWebErr(w, err)
return
}
matchResultJson, err := matchResult.serialize()
if err != nil {
handleWebErr(w, err)
return
}
data := struct {
*EventSettings
Match *Match
MatchResultJson *MatchResultDb
}{eventSettings, match, matchResultJson}
err = template.ExecuteTemplate(w, "base", data)
if err != nil {
handleWebErr(w, err)
return
}
}
// Updates the results for a match.
func MatchReviewEditPostHandler(w http.ResponseWriter, r *http.Request) {
if !UserIsAdmin(w, r) {
return
}
match, matchResult, isCurrent, err := getMatchResultFromRequest(r)
if err != nil {
handleWebErr(w, err)
return
}
r.ParseForm()
matchResultJson := MatchResultDb{Id: matchResult.Id, MatchId: match.Id, PlayNumber: matchResult.PlayNumber,
MatchType: matchResult.MatchType, RedScoreJson: r.PostFormValue("redScoreJson"),
BlueScoreJson: r.PostFormValue("blueScoreJson"), RedCardsJson: r.PostFormValue("redCardsJson"),
BlueCardsJson: r.PostFormValue("blueCardsJson")}
// Deserialize the JSON using the same mechanism as to store scoring information in the database.
matchResult, err = matchResultJson.deserialize()
if err != nil {
handleWebErr(w, err)
return
}
if isCurrent {
// If editing the current match, just save it back to memory.
mainArena.redRealtimeScore.CurrentScore = matchResult.RedScore
mainArena.blueRealtimeScore.CurrentScore = matchResult.BlueScore
mainArena.redRealtimeScore.Cards = matchResult.RedCards
mainArena.blueRealtimeScore.Cards = matchResult.BlueCards
http.Redirect(w, r, "/match_play", 302)
} else {
err = CommitMatchScore(match, matchResult, false)
if err != nil {
handleWebErr(w, err)
return
}
http.Redirect(w, r, "/match_review", 302)
}
}
// Load the match result for the match referenced in the HTTP query string.
func getMatchResultFromRequest(r *http.Request) (*Match, *MatchResult, bool, error) {
vars := mux.Vars(r)
// If editing the current match, get it from memory instead of the DB.
if vars["matchId"] == "current" {
return mainArena.currentMatch, GetCurrentMatchResult(), true, nil
}
matchId, _ := strconv.Atoi(vars["matchId"])
match, err := db.GetMatchById(matchId)
if err != nil {
return nil, nil, false, err
}
if match == nil {
return nil, nil, false, fmt.Errorf("Error: No such match: %d", matchId)
}
matchResult, err := db.GetMatchResultForMatch(matchId)
if err != nil {
return nil, nil, false, err
}
if matchResult == nil {
// We're scoring a match that hasn't been played yet, but that's okay.
matchResult = NewMatchResult()
matchResult.MatchType = match.Type
}
return match, matchResult, false, nil
}
// Constructs the list of matches to display in the match review interface.
func buildMatchReviewList(matchType string) ([]MatchReviewListItem, error) {
matches, err := db.GetMatchesByType(matchType)
if err != nil {
return []MatchReviewListItem{}, err
}
prefix := ""
if matchType == "practice" {
prefix = "P"
} else if matchType == "qualification" {
prefix = "Q"
}
matchReviewList := make([]MatchReviewListItem, len(matches))
for i, match := range matches {
matchReviewList[i].Id = match.Id
matchReviewList[i].DisplayName = prefix + match.DisplayName
matchReviewList[i].Time = match.Time.Local().Format("Mon 1/02 03:04 PM")
matchReviewList[i].RedTeams = []int{match.Red1, match.Red2, match.Red3}
matchReviewList[i].BlueTeams = []int{match.Blue1, match.Blue2, match.Blue3}
matchResult, err := db.GetMatchResultForMatch(match.Id)
if err != nil {
return []MatchReviewListItem{}, err
}
if matchResult != nil {
matchReviewList[i].RedScore = matchResult.RedScoreSummary().Score
matchReviewList[i].BlueScore = matchResult.BlueScoreSummary().Score
}
switch match.Winner {
case "R":
matchReviewList[i].ColorClass = "danger"
case "B":
matchReviewList[i].ColorClass = "info"
case "T":
matchReviewList[i].ColorClass = "warning"
default:
matchReviewList[i].ColorClass = ""
}
}
return matchReviewList, nil
}