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
|
package main
import (
"encoding/json"
"muhq.space/go/wrms/llog"
"github.com/google/uuid"
"nhooyr.io/websocket"
)
type Song struct {
Title string `json:"title"`
Artist string `json:"artist"`
Source string `json:"source"`
Uri string `json:"uri"`
Weight int `json:"weight"`
index int `json:"-"` // used by heap.Interface
Upvotes map[uuid.UUID]struct{} `json:"-"`
Downvotes map[uuid.UUID]struct{} `json:"-"`
}
func NewSong(title, artist, source, uri string) Song {
return Song{title, artist, source, uri, 0, 0, map[uuid.UUID]struct{}{}, map[uuid.UUID]struct{}{}}
}
func NewSongFromJson(data []byte) (Song, error) {
var s Song
err := json.Unmarshal(data, &s)
if err != nil {
llog.Error("Failed to parse song data %s with %s", string(data), err)
return s, err
}
s.Upvotes = map[uuid.UUID]struct{}{}
s.Downvotes = map[uuid.UUID]struct{}{}
return s, nil
}
type Event struct {
Event string `json:"cmd"`
Id int `json:"id"`
Songs []Song `json:"songs"`
}
func newEvent(event string, songs []Song) Event {
return Event{Event: event, Songs: songs}
}
func newNotification(notification string) Event {
return Event{Event: notification}
}
func newSearchResultEvent(id int, songs []Song) Event {
return Event{Event: "search", Id: id, Songs: songs}
}
type Connection struct {
Id uuid.UUID
Events chan Event
C *websocket.Conn
}
func (c *Connection) Send(ev Event) {
c.Events <- ev
}
func (c *Connection) Close() {
llog.Info("Closing connection %s", c.Id)
close(c.Events)
c.C.Close(websocket.StatusNormalClosure, "")
}
type Wrms struct {
Connections map[uuid.UUID]*Connection
Songs []Song
queue Playlist
CurrentSong Song
Player Player
Playing bool
Config Config
}
func NewWrms(config Config) *Wrms {
wrms := Wrms{}
wrms.Config = config
wrms.Connections = make(map[uuid.UUID]*Connection)
wrms.Player = NewPlayer(&wrms, config.Backends)
return &wrms
}
func (wrms *Wrms) GetConn(connId uuid.UUID) *Connection {
return wrms.Connections[connId]
}
func (wrms *Wrms) Close() {
llog.Info("Closing WRMS")
for _, conn := range wrms.Connections {
conn.Close()
}
}
func (wrms *Wrms) Broadcast(ev Event) {
llog.Info("Broadcasting %v", ev)
for _, conn := range wrms.Connections {
conn.Send(ev)
}
}
func (wrms *Wrms) startPlaying() {
wrms.Broadcast(newEvent("play", []Song{wrms.CurrentSong}))
wrms.Player.Play(&wrms.CurrentSong)
}
func (wrms *Wrms) AddSong(song Song) {
startPlayingAgain := wrms.Playing && wrms.CurrentSong.Uri == ""
wrms.Songs = append(wrms.Songs, song)
s := &wrms.Songs[len(wrms.Songs)-1]
wrms.queue.Add(s)
llog.Info("Added song %s (ptr=%p) to Songs", s.Uri, s)
wrms.Broadcast(newEvent("add", []Song{song}))
if startPlayingAgain {
wrms.Next()
}
}
func (wrms *Wrms) Next() {
llog.DDebug("Next Song")
next := wrms.queue.PopSong()
if next == nil {
wrms.CurrentSong.Uri = ""
wrms.Broadcast(newNotification("stop"))
return
}
wrms.CurrentSong = *next
llog.Info("popped next song and removing it from the song list %v", next)
for i, s := range wrms.Songs {
if s.Uri == wrms.CurrentSong.Uri {
wrms.Songs[i] = wrms.Songs[len(wrms.Songs)-1]
wrms.Songs = wrms.Songs[:len(wrms.Songs)-1]
break
}
}
if wrms.Playing {
wrms.startPlaying()
}
}
func (wrms *Wrms) PlayPause() {
wrms.Playing = !wrms.Playing
if wrms.Playing {
if wrms.CurrentSong.Uri == "" {
llog.Info("No song currently playing play the next")
wrms.Next()
} else {
wrms.startPlaying()
}
} else {
wrms.Broadcast(newNotification("pause"))
wrms.Player.Pause()
}
}
func (wrms *Wrms) AdjustSongWeight(connId uuid.UUID, songUri string, vote string) {
for i := 0; i < len(wrms.Songs); i++ {
s := &wrms.Songs[i]
if s.Uri != songUri {
continue
}
llog.Info("Adjusting song %s (ptr=%p)", s.Uri, s)
switch vote {
case "up":
if _, ok := s.Upvotes[connId]; ok {
llog.Error("Double upvote of song %s by connections %s", songUri, connId)
return
}
if _, ok := s.Downvotes[connId]; ok {
delete(s.Downvotes, connId)
s.Weight += 2
} else {
s.Weight += 1
}
s.Upvotes[connId] = struct{}{}
case "down":
if _, ok := s.Downvotes[connId]; ok {
llog.Error("Double downvote of song %s by connections %s", songUri, connId)
return
}
if _, ok := s.Upvotes[connId]; ok {
delete(s.Upvotes, connId)
s.Weight -= 2
} else {
s.Weight -= 1
}
s.Downvotes[connId] = struct{}{}
case "unvote":
if _, ok := s.Downvotes[connId]; ok {
delete(s.Downvotes, connId)
s.Weight += 1
}
if _, ok := s.Upvotes[connId]; ok {
delete(s.Upvotes, connId)
s.Weight -= 1
} else {
llog.Error("Double unvote of song %s by connections %s", songUri, connId)
return
}
default:
llog.Fatal("invalid vote")
}
wrms.queue.Adjust(s)
wrms.Broadcast(newEvent("update", []Song{*s}))
break
}
}
|