forked from Team254/cheesy-arena
-
Notifications
You must be signed in to change notification settings - Fork 0
/
referee_display.go
241 lines (227 loc) · 6.56 KB
/
referee_display.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
// Copyright 2014 Team 254. All Rights Reserved.
// Author: [email protected] (Patrick Fairbank)
//
// Web handlers for the referee interface.
package main
import (
"fmt"
"github.com/mitchellh/mapstructure"
"io"
"log"
"net/http"
"strconv"
"text/template"
)
var rules = []string{"G3", "G5", "G10", "G11", "G12", "G14", "G15", "G16", "G17", "G18", "G19", "G21", "G22",
"G23", "G24", "G25", "G26", "G26-1", "G27", "G28", "G29", "G30", "G31", "G32", "G34", "G35", "G36", "G37",
"G38", "G39", "G40", "G41", "G42"}
// Renders the referee interface for assigning fouls.
func RefereeDisplayHandler(w http.ResponseWriter, r *http.Request) {
template := template.New("").Funcs(templateHelpers)
_, err := template.ParseFiles("templates/referee_display.html")
if err != nil {
handleWebErr(w, err)
return
}
match := mainArena.currentMatch
matchType := match.CapitalizedType()
red1 := mainArena.AllianceStations["R1"].team
if red1 == nil {
red1 = &Team{}
}
red2 := mainArena.AllianceStations["R2"].team
if red2 == nil {
red2 = &Team{}
}
red3 := mainArena.AllianceStations["R3"].team
if red3 == nil {
red3 = &Team{}
}
blue1 := mainArena.AllianceStations["B1"].team
if blue1 == nil {
blue1 = &Team{}
}
blue2 := mainArena.AllianceStations["B2"].team
if blue2 == nil {
blue2 = &Team{}
}
blue3 := mainArena.AllianceStations["B3"].team
if blue3 == nil {
blue3 = &Team{}
}
data := struct {
*EventSettings
MatchType string
MatchDisplayName string
Red1 *Team
Red2 *Team
Red3 *Team
Blue1 *Team
Blue2 *Team
Blue3 *Team
RedFouls []Foul
BlueFouls []Foul
RedCards map[string]string
BlueCards map[string]string
Rules []string
EntryEnabled bool
}{eventSettings, matchType, match.DisplayName, red1, red2, red3, blue1, blue2, blue3,
mainArena.redRealtimeScore.Fouls, mainArena.blueRealtimeScore.Fouls, mainArena.redRealtimeScore.Cards,
mainArena.blueRealtimeScore.Cards, rules,
!(mainArena.redRealtimeScore.FoulsCommitted && mainArena.blueRealtimeScore.FoulsCommitted)}
err = template.ExecuteTemplate(w, "referee_display.html", data)
if err != nil {
handleWebErr(w, err)
return
}
}
// The websocket endpoint for the refereee interface client to send control commands and receive status updates.
func RefereeDisplayWebsocketHandler(w http.ResponseWriter, r *http.Request) {
websocket, err := NewWebsocket(w, r)
if err != nil {
handleWebErr(w, err)
return
}
defer websocket.Close()
matchLoadTeamsListener := mainArena.matchLoadTeamsNotifier.Listen()
defer close(matchLoadTeamsListener)
reloadDisplaysListener := mainArena.reloadDisplaysNotifier.Listen()
defer close(reloadDisplaysListener)
// Spin off a goroutine to listen for notifications and pass them on through the websocket.
go func() {
for {
var messageType string
var message interface{}
select {
case _, ok := <-matchLoadTeamsListener:
if !ok {
return
}
messageType = "reload"
message = nil
case _, ok := <-reloadDisplaysListener:
if !ok {
return
}
messageType = "reload"
message = nil
}
err = websocket.Write(messageType, message)
if err != nil {
// The client has probably closed the connection; nothing to do here.
return
}
}
}()
// Loop, waiting for commands and responding to them, until the client closes the connection.
for {
messageType, data, err := websocket.Read()
if err != nil {
if err == io.EOF {
// Client has closed the connection; nothing to do here.
return
}
log.Printf("Websocket error: %s", err)
return
}
switch messageType {
case "addFoul":
args := struct {
Alliance string
TeamId int
Rule string
IsTechnical bool
}{}
err = mapstructure.Decode(data, &args)
if err != nil {
websocket.WriteError(err.Error())
continue
}
// Add the foul to the correct alliance's list.
foul := Foul{TeamId: args.TeamId, Rule: args.Rule, IsTechnical: args.IsTechnical,
TimeInMatchSec: mainArena.MatchTimeSec()}
if args.Alliance == "red" {
mainArena.redRealtimeScore.Fouls = append(mainArena.redRealtimeScore.Fouls, foul)
} else {
mainArena.blueRealtimeScore.Fouls = append(mainArena.blueRealtimeScore.Fouls, foul)
}
mainArena.realtimeScoreNotifier.Notify(nil)
case "deleteFoul":
args := struct {
Alliance string
TeamId int
Rule string
TimeInMatchSec float64
IsTechnical bool
}{}
err = mapstructure.Decode(data, &args)
if err != nil {
websocket.WriteError(err.Error())
continue
}
// Remove the foul from the correct alliance's list.
deleteFoul := Foul{TeamId: args.TeamId, Rule: args.Rule, IsTechnical: args.IsTechnical,
TimeInMatchSec: args.TimeInMatchSec}
var fouls *[]Foul
if args.Alliance == "red" {
fouls = &mainArena.redRealtimeScore.Fouls
} else {
fouls = &mainArena.blueRealtimeScore.Fouls
}
for i, foul := range *fouls {
if foul == deleteFoul {
*fouls = append((*fouls)[:i], (*fouls)[i+1:]...)
break
}
}
mainArena.realtimeScoreNotifier.Notify(nil)
case "card":
args := struct {
Alliance string
TeamId int
Card string
}{}
err = mapstructure.Decode(data, &args)
if err != nil {
websocket.WriteError(err.Error())
continue
}
// Set the card in the correct alliance's score.
var cards map[string]string
if args.Alliance == "red" {
cards = mainArena.redRealtimeScore.Cards
} else {
cards = mainArena.blueRealtimeScore.Cards
}
cards[strconv.Itoa(args.TeamId)] = args.Card
continue
case "signalReset":
if mainArena.MatchState != POST_MATCH {
// Don't allow clearing the field until the match is over.
continue
}
mainArena.redRealtimeScore.FieldReset = true
mainArena.blueRealtimeScore.FieldReset = true
continue // Don't reload.
case "commitMatch":
if mainArena.MatchState != POST_MATCH {
// Don't allow committing the fouls until the match is over.
continue
}
mainArena.redRealtimeScore.FoulsCommitted = true
mainArena.blueRealtimeScore.FoulsCommitted = true
mainArena.redRealtimeScore.FieldReset = true
mainArena.blueRealtimeScore.FieldReset = true
mainArena.scoringStatusNotifier.Notify(nil)
default:
websocket.WriteError(fmt.Sprintf("Invalid message type '%s'.", messageType))
continue
}
// Force a reload of the client to render the updated foul list.
err = websocket.Write("reload", nil)
if err != nil {
log.Printf("Websocket error: %s", err)
return
}
}
}