forked from Team254/cheesy-arena
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alliance_station_display.go
222 lines (208 loc) · 6.89 KB
/
alliance_station_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
// Copyright 2014 Team 254. All Rights Reserved.
// Author: [email protected] (Patrick Fairbank)
//
// Web handlers for the alliance station display.
package main
import (
"fmt"
"io"
"log"
"net/http"
"strconv"
"text/template"
)
// Renders the team number and status display shown above each alliance station.
func AllianceStationDisplayHandler(w http.ResponseWriter, r *http.Request) {
if !UserIsReader(w, r) {
return
}
template := template.New("").Funcs(templateHelpers)
_, err := template.ParseFiles("templates/alliance_station_display.html")
if err != nil {
handleWebErr(w, err)
return
}
displayId := ""
if _, ok := r.URL.Query()["displayId"]; ok {
// Register the display in memory by its ID so that it can be configured to a certain station.
displayId = r.URL.Query()["displayId"][0]
}
data := struct {
*EventSettings
DisplayId string
}{eventSettings, displayId}
err = template.ExecuteTemplate(w, "alliance_station_display.html", data)
if err != nil {
handleWebErr(w, err)
return
}
}
// The websocket endpoint for the alliance station display client to receive status updates.
func AllianceStationDisplayWebsocketHandler(w http.ResponseWriter, r *http.Request) {
if !UserIsReader(w, r) {
return
}
websocket, err := NewWebsocket(w, r)
if err != nil {
handleWebErr(w, err)
return
}
defer websocket.Close()
displayId := r.URL.Query()["displayId"][0]
station, ok := mainArena.allianceStationDisplays[displayId]
if !ok {
station = ""
mainArena.allianceStationDisplays[displayId] = station
}
rankings := make(map[string]*Ranking)
for _, allianceStation := range mainArena.AllianceStations {
if allianceStation.Team != nil {
rankings[strconv.Itoa(allianceStation.Team.Id)], _ = db.GetRankingForTeam(allianceStation.Team.Id)
}
}
allianceStationDisplayListener := mainArena.allianceStationDisplayNotifier.Listen()
defer close(allianceStationDisplayListener)
matchLoadTeamsListener := mainArena.matchLoadTeamsNotifier.Listen()
defer close(matchLoadTeamsListener)
robotStatusListener := mainArena.robotStatusNotifier.Listen()
defer close(robotStatusListener)
matchTimeListener := mainArena.matchTimeNotifier.Listen()
defer close(matchTimeListener)
realtimeScoreListener := mainArena.realtimeScoreNotifier.Listen()
defer close(realtimeScoreListener)
reloadDisplaysListener := mainArena.reloadDisplaysNotifier.Listen()
defer close(reloadDisplaysListener)
// Send the various notifications immediately upon connection.
var data interface{}
err = websocket.Write("setAllianceStationDisplay", mainArena.allianceStationDisplayScreen)
if err != nil {
log.Printf("Websocket error: %s", err)
return
}
err = websocket.Write("matchTiming", mainArena.matchTiming)
if err != nil {
log.Printf("Websocket error: %s", err)
return
}
err = websocket.Write("matchTime", MatchTimeMessage{mainArena.MatchState, int(mainArena.lastMatchTimeSec)})
if err != nil {
log.Printf("Websocket error: %s", err)
return
}
data = struct {
AllianceStation string
Teams map[string]*Team
Rankings map[string]*Ranking
}{station, map[string]*Team{"R1": mainArena.AllianceStations["R1"].Team,
"R2": mainArena.AllianceStations["R2"].Team, "R3": mainArena.AllianceStations["R3"].Team,
"B1": mainArena.AllianceStations["B1"].Team, "B2": mainArena.AllianceStations["B2"].Team,
"B3": mainArena.AllianceStations["B3"].Team}, rankings}
err = websocket.Write("setMatch", data)
if err != nil {
log.Printf("Websocket error: %s", err)
return
}
data = struct {
RedScoreFields *RealtimeScoreFields
BlueScoreFields *RealtimeScoreFields
}{mainArena.redRealtimeScore.ScoreFields(mainArena.blueRealtimeScore.CurrentScore.Fouls),
mainArena.blueRealtimeScore.ScoreFields(mainArena.redRealtimeScore.CurrentScore.Fouls)}
err = websocket.Write("realtimeScore", data)
if err != nil {
log.Printf("Websocket error: %s", err)
return
}
// 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 := <-allianceStationDisplayListener:
if !ok {
return
}
websocket.Write("matchTime", MatchTimeMessage{mainArena.MatchState, int(mainArena.lastMatchTimeSec)})
messageType = "setAllianceStationDisplay"
message = mainArena.allianceStationDisplayScreen
case _, ok := <-matchLoadTeamsListener:
if !ok {
return
}
messageType = "setMatch"
station = mainArena.allianceStationDisplays[displayId]
rankings := make(map[string]*Ranking)
for _, allianceStation := range mainArena.AllianceStations {
if allianceStation.Team != nil {
rankings[strconv.Itoa(allianceStation.Team.Id)], _ = db.GetRankingForTeam(allianceStation.Team.Id)
}
}
message = struct {
AllianceStation string
Teams map[string]*Team
Rankings map[string]*Ranking
}{station, map[string]*Team{"R1": mainArena.AllianceStations["R1"].Team,
"R2": mainArena.AllianceStations["R2"].Team, "R3": mainArena.AllianceStations["R3"].Team,
"B1": mainArena.AllianceStations["B1"].Team, "B2": mainArena.AllianceStations["B2"].Team,
"B3": mainArena.AllianceStations["B3"].Team}, rankings}
case _, ok := <-robotStatusListener:
if !ok {
return
}
messageType = "status"
message = mainArena
case matchTimeSec, ok := <-matchTimeListener:
if !ok {
return
}
messageType = "matchTime"
message = MatchTimeMessage{mainArena.MatchState, matchTimeSec.(int)}
case _, ok := <-realtimeScoreListener:
if !ok {
return
}
messageType = "realtimeScore"
message = struct {
RedScoreFields *RealtimeScoreFields
BlueScoreFields *RealtimeScoreFields
}{mainArena.redRealtimeScore.ScoreFields(mainArena.blueRealtimeScore.CurrentScore.Fouls),
mainArena.blueRealtimeScore.ScoreFields(mainArena.redRealtimeScore.CurrentScore.Fouls)}
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 "setAllianceStation":
// The client knows what station it is (e.g. across a server restart) and is informing the server.
station, ok := data.(string)
if !ok {
websocket.WriteError(fmt.Sprintf("Failed to parse '%s' message.", messageType))
continue
}
mainArena.allianceStationDisplays[displayId] = station
default:
websocket.WriteError(fmt.Sprintf("Invalid message type '%s'.", messageType))
}
}
}