-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
165 lines (141 loc) · 3.34 KB
/
client.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
package main
import (
"encoding/json"
"math/rand"
"net/http"
"github.com/Jeffail/gabs/v2"
"github.com/gorilla/websocket"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
type Event struct {
Type string `json:"type"`
Player string `json:"player"`
Data json.RawMessage `json:"data"`
}
type PlayEvent struct {
QueueType string `json:"queueType"`
QueueID string `json:"queueId"`
StartSong json.RawMessage `json:"startSong"`
Shuffle bool `json:"shuffle"`
}
type Client struct {
Conn *websocket.Conn
}
var clients []*Client
var metricConnectedClients = promauto.NewGauge(prometheus.GaugeOpts{
Name: "slimytm_connected_clients",
Help: "The number of clients currently connected",
})
func (c *Client) Listener() {
for {
var e Event
err := c.Conn.ReadJSON(&e)
if err != nil {
logger.Debugw("client disconnected",
"err", err)
metricConnectedClients.Dec()
return
}
// Find the correct queue
var queue *Queue
for _, v := range queues {
if v.Player.GetID() == e.Player {
queue = v
}
}
if e.Type == "PLAY" {
var p PlayEvent
err := json.Unmarshal(e.Data, &p)
if err != nil {
logger.Warnw("unable to unmarshal event",
"err", err)
continue
}
c.PlaySongs(queue, p)
} else if e.Type == "NEXT" {
queue.Next()
} else if e.Type == "PREVIOUS" {
queue.Previous()
} else if e.Type == "PAUSE" {
queue.Pause()
} else if e.Type == "VOLUME" {
var v int
err := json.Unmarshal(e.Data, &v)
if err != nil {
logger.Warnw("unable to unmarshal event",
"err", err)
continue
}
queue.Player.SetVolume(v)
queue.UpdateClients()
} else {
logger.Warnw("received unknown event from web client",
"event", e.Type)
}
}
}
func (c *Client) PlaySongs(q *Queue, p PlayEvent) {
if q == nil {
logger.Warnw("unknown player for event, dropping")
return
}
// Add the start song to the queue
var startSong Song
err := json.Unmarshal(p.StartSong, &startSong)
if err != nil {
logger.Warnw("unable to unmarshal event",
"err", err)
return
}
q.Songs = []Song{startSong}
q.Index = -1
q.Next()
// Retrieve the rest of the songs and enqueue them
var songs []Song
switch p.QueueType {
case "playlist":
resp, err := http.Get("http://localhost:9000/api/playlist/" + p.QueueID)
if err != nil {
logger.Errorw("unable to retrieve playlist",
"err", err)
return
}
playlist, err := gabs.ParseJSONBuffer(resp.Body)
if err != nil {
logger.Errorw("unable to parse json playlist info",
"err", err)
return
}
err = json.Unmarshal(playlist.Path("tracks").Bytes(), &songs)
if err != nil {
logger.Errorw("unable to get tracks from playlist info",
"err", err)
return
}
default:
logger.Warn("unknown queue type")
return
}
if p.Shuffle {
logger.Debug("shuffling playlist")
rand.Shuffle(len(songs), func(i, j int) { songs[i], songs[j] = songs[j], songs[i] })
// Remove the already playing song from the list
for k, v := range songs {
if startSong.ID == v.ID {
songs = append(songs[:k], songs[k+1:]...)
break
}
}
q.Songs = append(q.Songs, songs...)
} else {
q.Songs = songs
for k, v := range q.Songs {
// Set the queue index to the start song
if startSong.ID == v.ID {
q.Index = k
break
}
}
}
}