forked from Team254/cheesy-arena
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup_lower_thirds.go
205 lines (190 loc) · 4.65 KB
/
setup_lower_thirds.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
// Copyright 2014 Team 254. All Rights Reserved.
// Author: [email protected] (Patrick Fairbank)
//
// Web routes for managing lower thirds.
package main
import (
"fmt"
"github.com/mitchellh/mapstructure"
"html/template"
"io"
"log"
"net/http"
)
// Shows the lower third configuration page.
func LowerThirdsGetHandler(w http.ResponseWriter, r *http.Request) {
if !UserIsAdmin(w, r) {
return
}
template, err := template.ParseFiles("templates/setup_lower_thirds.html", "templates/base.html")
if err != nil {
handleWebErr(w, err)
return
}
lowerThirds, err := db.GetAllLowerThirds()
if err != nil {
handleWebErr(w, err)
return
}
data := struct {
*EventSettings
LowerThirds []LowerThird
}{eventSettings, lowerThirds}
err = template.ExecuteTemplate(w, "base", data)
if err != nil {
handleWebErr(w, err)
return
}
}
// The websocket endpoint for the lower thirds client to send control commands.
func LowerThirdsWebsocketHandler(w http.ResponseWriter, r *http.Request) {
if !UserIsAdmin(w, r) {
return
}
websocket, err := NewWebsocket(w, r)
if err != nil {
handleWebErr(w, err)
return
}
defer websocket.Close()
// 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 "saveLowerThird":
var lowerThird LowerThird
err = mapstructure.Decode(data, &lowerThird)
if err != nil {
websocket.WriteError(err.Error())
continue
}
saveLowerThird(&lowerThird)
case "deleteLowerThird":
var lowerThird LowerThird
err = mapstructure.Decode(data, &lowerThird)
if err != nil {
websocket.WriteError(err.Error())
continue
}
err = db.DeleteLowerThird(&lowerThird)
if err != nil {
websocket.WriteError(err.Error())
continue
}
case "showLowerThird":
var lowerThird LowerThird
err = mapstructure.Decode(data, &lowerThird)
if err != nil {
websocket.WriteError(err.Error())
continue
}
saveLowerThird(&lowerThird)
mainArena.lowerThirdNotifier.Notify(lowerThird)
mainArena.audienceDisplayScreen = "lowerThird"
mainArena.audienceDisplayNotifier.Notify(nil)
continue
case "hideLowerThird":
var lowerThird LowerThird
err = mapstructure.Decode(data, &lowerThird)
if err != nil {
websocket.WriteError(err.Error())
continue
}
saveLowerThird(&lowerThird)
mainArena.audienceDisplayScreen = "blank"
mainArena.audienceDisplayNotifier.Notify(nil)
continue
case "reorderLowerThird":
args := struct {
Id int
MoveUp bool
}{}
err = mapstructure.Decode(data, &args)
if err != nil {
websocket.WriteError(err.Error())
continue
}
err = reorderLowerThird(args.Id, args.MoveUp)
if err != nil {
websocket.WriteError(err.Error())
continue
}
default:
websocket.WriteError(fmt.Sprintf("Invalid message type '%s'.", messageType))
continue
}
// Force a reload of the client to render the updated lower thirds list.
err = websocket.Write("reload", nil)
if err != nil {
log.Printf("Websocket error: %s", err)
return
}
}
}
func saveLowerThird(lowerThird *LowerThird) error {
oldLowerThird, err := db.GetLowerThirdById(lowerThird.Id)
if err != nil {
return err
}
// Create or update lower third.
if oldLowerThird == nil {
err = db.CreateLowerThird(lowerThird)
} else {
err = db.SaveLowerThird(lowerThird)
}
if err != nil {
return err
}
return nil
}
func reorderLowerThird(id int, moveUp bool) error {
lowerThird, err := db.GetLowerThirdById(id)
if err != nil {
return err
}
// Get the lower third to swap positions with.
lowerThirds, err := db.GetAllLowerThirds()
if err != nil {
return err
}
var lowerThirdIndex int
for i, third := range lowerThirds {
if third.Id == lowerThird.Id {
lowerThirdIndex = i
break
}
}
if moveUp {
lowerThirdIndex--
} else {
lowerThirdIndex++
}
if lowerThirdIndex < 0 || lowerThirdIndex == len(lowerThirds) {
// The one to move is already at the limit; return an error to prevent a page reload.
return fmt.Errorf("Already at the limit.")
}
adjacentLowerThird, err := db.GetLowerThirdById(lowerThirds[lowerThirdIndex].Id)
if err != nil {
return err
}
// Swap their display orders and save.
lowerThird.DisplayOrder, adjacentLowerThird.DisplayOrder =
adjacentLowerThird.DisplayOrder, lowerThird.DisplayOrder
err = db.SaveLowerThird(lowerThird)
if err != nil {
return err
}
err = db.SaveLowerThird(adjacentLowerThird)
if err != nil {
return err
}
return nil
}