aboutsummaryrefslogtreecommitdiff
path: root/wrms.go
blob: 5dfdc537043d409c5d9ae773766c4e53b79aeb78 (plain)
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package main

import (
	"sync"
	"sync/atomic"

	"muhq.space/go/wrms/llog"

	"github.com/google/uuid"
)

type Event struct {
	Event string  `json:"cmd"`
	Id    uint64  `json:"id"`
	Songs []*Song `json:"songs"`
}

func (wrms *Wrms) incEventId() uint64 {
	id := wrms.eventId.Add(1)
	llog.DDebug("Increment event id to: %d", id)
	return id
}

// A private event does not appear in the global event order and
// thus does not increment the global event count.
// Private events are used for example to report search results.
func (wrms *Wrms) newPrivateEvent(id uint64, event string, songs []*Song) Event {
	return Event{Id: id, Event: event, Songs: songs}
}

func (wrms *Wrms) newEvent(event string, songs []*Song) Event {
	return Event{Id: wrms.incEventId(), Event: event, Songs: songs}
}

func (wrms *Wrms) newNotification(notification string) Event {
	return wrms.newEvent(notification, nil)
}

type Wrms struct {
	Connections sync.Map
	nextConnNr  atomic.Uint64
	// The rwlock must be held when using most internal state
	rwlock      sync.RWMutex
	Songs       []*Song
	queue       Playlist
	CurrentSong atomic.Pointer[Song]
	Player      Player
	playing     bool
	Config      Config
	eventId     atomic.Uint64
}

func NewWrms(config Config) *Wrms {
	wrms := Wrms{}
	wrms.Config = config
	wrms.Player = NewMpvPlayer(&wrms, config.Backends)

	if len(wrms.Config.Playlists) > 0 {
		wrms.loadPlaylists(wrms.Config.Playlists)
	}

	return &wrms
}

func (wrms *Wrms) addConn(conn *Connection) {
	llog.DDebug("Adding Connection %s", conn.Id)
	wrms.Connections.Store(conn.Id, conn)
}

func (wrms *Wrms) delConn(conn *Connection) {
	llog.DDebug("Deleting Connection %s", conn.Id)
	_oldConn, _ := wrms.Connections.Load(conn.Id)
	oldConn := _oldConn.(*Connection)
	// Only delete the connection if no newer one is currently active
	if oldConn.nr == conn.nr {
		wrms.Connections.Delete(conn.Id)
	}
}

func (wrms *Wrms) initConn(conn *Connection) error {
	wrms.rwlock.RLock()

	curEventId := wrms.eventId.Load()
	conn.nextEvent = curEventId + 1

	initialCmds := []interface{}{}

	if wrms.Config.timeBonus != 0 {
		ev := map[string]any{"cmd": "timeBonus", "timeBonus": wrms.Config.timeBonus}
		initialCmds = append(initialCmds, ev)
	}

	if wrms.playing {
		var songs []*Song
		if currentSong := wrms.CurrentSong.Load(); currentSong != nil {
			songs = []*Song{currentSong}
		}
		initialCmds = append(initialCmds, wrms.newPrivateEvent(curEventId, "play", songs))
	}

	upvoted := []*Song{}
	downvoted := []*Song{}
	if len(wrms.Songs) > 0 {
		initialCmds = append(initialCmds, wrms.newPrivateEvent(curEventId, "add", wrms.Songs))

		for _, song := range wrms.queue.OrderedList() {
			llog.DDebug("Looking at the votes of song: %v", song)
			if _, ok := song.Upvotes[conn.Id]; ok {
				upvoted = append(upvoted, song)
			} else if _, ok := song.Downvotes[conn.Id]; ok {
				downvoted = append(downvoted, song)
			}
		}
	}

	if len(upvoted) > 0 {
		initialCmds = append(initialCmds, wrms.newPrivateEvent(curEventId, "upvoted", upvoted))
	}

	if len(downvoted) > 0 {
		initialCmds = append(initialCmds, wrms.newPrivateEvent(curEventId, "downvoted", downvoted))
	}

	wrms.rwlock.RUnlock()
	llog.Info("Sending initial cmds %v", initialCmds)
	return conn._send(initialCmds)
}

func (wrms *Wrms) GetConn(connId uuid.UUID) *Connection {
	conn, _ := wrms.Connections.Load(connId)
	return conn.(*Connection)
}

func (wrms *Wrms) Broadcast(ev Event) {
	llog.Info("Broadcasting %v", ev)
	wrms.Connections.Range(func(_, conn any) bool {
		conn.(*Connection).Send(ev)
		return true
	})
}

func (wrms *Wrms) _addSong(song *Song) {
	wrms.Songs = append(wrms.Songs, song)
	wrms.queue.Add(song)
}

func (wrms *Wrms) AddSong(song *Song) {
	wrms.rwlock.Lock()

	startPlayingAgain := wrms.playing && wrms.CurrentSong.Load() == nil

	if wrms.Config.timeBonus != 0 {
		llog.Info("Apply time bonus %v", wrms.Config.timeBonus)
		wrms.queue.applyTimeBonus(wrms.Config.timeBonus)
	}

	wrms._addSong(song)

	ev := wrms.newEvent("add", []*Song{song})
	wrms.rwlock.Unlock()

	llog.Info("Added song %s (ptr=%p) to Songs", song.Uri, song)
	wrms.Broadcast(ev)

	if startPlayingAgain {
		wrms._lockedNext()
	}
}

func (wrms *Wrms) DeleteSong(songUri string) {
	wrms.rwlock.Lock()

	for i := 0; i < len(wrms.Songs); i++ {
		s := wrms.Songs[i]
		if s.Uri != songUri {
			continue
		}

		wrms.Songs[i] = wrms.Songs[len(wrms.Songs)-1]
		wrms.Songs = wrms.Songs[:len(wrms.Songs)-1]

		wrms.queue.RemoveSong(s)

		ev := wrms.newEvent("delete", []*Song{s})
		wrms.rwlock.Unlock()

		wrms.Broadcast(ev)
		break
	}
}

func (wrms *Wrms) Next() {
	wrms.rwlock.Lock()

	// Terminate the Player if it is currently playing
	if wrms.Player.Playing() {
		wrms.Player.Stop()
	}

	wrms._next()
}

func (wrms *Wrms) _lockedNext() {
	wrms.rwlock.Lock()
	wrms._next()
}

func (wrms *Wrms) _next() {
	llog.DDebug("Next Song")

	next := wrms.queue.PopSong()
	if next == nil {
		wrms.CurrentSong.Store(nil)
		wrms.Broadcast(wrms.newNotification("stop"))
		wrms.rwlock.Unlock()
		return
	}

	wrms.CurrentSong.Store(next)

	llog.Info("popped next song and removing it from the song list %v", next)

	for i, s := range wrms.Songs {
		if s.Uri == next.Uri {
			wrms.Songs[i] = wrms.Songs[len(wrms.Songs)-1]
			wrms.Songs = wrms.Songs[:len(wrms.Songs)-1]
			break
		}
	}

	cmd := "next"
	// We are playing -> start playing the next song
	if wrms.playing {
		wrms.Player.Play(next)
		cmd = "play"
	}

	ev := wrms.newEvent(cmd, []*Song{next})
	wrms.rwlock.Unlock()

	wrms.Broadcast(ev)
}

func (wrms *Wrms) PlayPause() {
	wrms.rwlock.Lock()
	// Toggle the playback state
	wrms.playing = !wrms.playing

	// Wrms was playing -> pause the player
	if !wrms.playing {
		wrms.Player.Pause()
		wrms.rwlock.Unlock()
		wrms.Broadcast(wrms.newNotification("pause"))
		return
	}

	// Wrms was not playing
	currentSong := wrms.CurrentSong.Load()

	// Wrms has no current song -> try to play the next song
	if currentSong == nil {
		llog.Info("No song currently playing play the next")
		// _next() releases the rwlock
		wrms._next()
		return
	}

	// The player is playing -> continue playing
	if wrms.Player.Playing() {
		wrms.Player.Continue()
		// The player is stopped -> start it
	} else {
		wrms.Player.Play(currentSong)
	}

	ev := wrms.newEvent("play", []*Song{currentSong})
	wrms.rwlock.Unlock()

	wrms.Broadcast(ev)
}

func (wrms *Wrms) AdjustSongWeight(connId uuid.UUID, songUri string, vote string) {
	wrms.rwlock.Lock()

	for i := 0; i < len(wrms.Songs); i++ {
		s := wrms.Songs[i]
		if s.Uri != songUri {
			continue
		}

		llog.Info("Adjusting song %v (ptr=%p)", s, 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.0
				llog.Debug("Flip downvote")
			} else {
				s.Weight += 1.0
			}

			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)
				llog.Debug("Flip upvote")
				s.Weight -= 2.0
			} else {
				s.Weight -= 1.0
			}

			s.Downvotes[connId] = struct{}{}

		case "unvote":
			if _, ok := s.Downvotes[connId]; ok {
				delete(s.Downvotes, connId)
				s.Weight += 1.0

			} else if _, ok := s.Upvotes[connId]; ok {
				delete(s.Upvotes, connId)
				s.Weight -= 1.0
			} else {
				llog.Error("Double unvote of song %s by connections %s", songUri, connId)
				return
			}

		default:
			llog.Fatal("invalid vote")
		}

		wrms.queue.Adjust(s)
		ev := wrms.newEvent("update", []*Song{s})
		wrms.rwlock.Unlock()

		wrms.Broadcast(ev)
		break
	}
}

func (wrms *Wrms) Search(pattern map[string]string) chan []*Song {
	return wrms.Player.Search(pattern)
}

func (wrms *Wrms) loadPlaylists(playlists []string) {
	for _, playlist := range playlists {
		wrms.appendPlaylist(playlist)
	}
}

func (wrms *Wrms) appendPlaylist(playlist string) {
	songs := wrms.Player.LoadPlaylist(playlist)

	for _, song := range songs {
		wrms._addSong(song)
	}
}