2023-10-01 05:42:55 +04:00
|
|
|
package radio
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Song_ stores artist and title of a song, a timestamp of when it started, and
|
|
|
|
// a maximum number of listeners.
|
|
|
|
type Song_ struct {
|
|
|
|
Artist string
|
|
|
|
Title string
|
2023-10-02 01:26:22 +04:00
|
|
|
Duration time.Duration
|
2023-10-01 05:42:55 +04:00
|
|
|
MaxListeners int
|
|
|
|
StartAt time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Song_) ArtistTitle() string {
|
|
|
|
return s.Artist + " - " + s.Title
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Song_) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(&struct {
|
|
|
|
Artist string `json:"artist"`
|
|
|
|
Title string `json:"title"`
|
2023-10-02 01:26:22 +04:00
|
|
|
Duration int64 `json:"duration_milliseconds"`
|
2023-10-01 05:42:55 +04:00
|
|
|
MaxListeners int `json:"listeners"`
|
|
|
|
StartAt string `json:"start_at"`
|
|
|
|
}{
|
|
|
|
Artist: s.Artist,
|
|
|
|
Title: s.Title,
|
2023-10-02 01:26:22 +04:00
|
|
|
Duration: s.Duration.Milliseconds(),
|
2023-10-01 05:42:55 +04:00
|
|
|
MaxListeners: s.MaxListeners,
|
|
|
|
StartAt: s.StartAt.UTC().Format(time.RFC3339)})
|
|
|
|
}
|
|
|
|
|
|
|
|
type SongList struct {
|
|
|
|
mut sync.Mutex
|
|
|
|
current Song_
|
|
|
|
lastSongs []Song_
|
|
|
|
listMaxLen int
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSongList(length int) *SongList {
|
|
|
|
return &SongList{listMaxLen: length, lastSongs: make([]Song_, 0, length)}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a new song that is currently playing and update a list.
|
|
|
|
func (sl *SongList) Add(newSong Song_) {
|
|
|
|
sl.mut.Lock()
|
|
|
|
defer sl.mut.Unlock()
|
2023-10-01 06:37:47 +04:00
|
|
|
|
|
|
|
if sl.current.StartAt.Year() == 1 {
|
|
|
|
sl.current = newSong
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-01 05:42:55 +04:00
|
|
|
if len(sl.lastSongs) == sl.listMaxLen {
|
|
|
|
sl.lastSongs = append(sl.lastSongs[1:], sl.current)
|
|
|
|
} else {
|
|
|
|
sl.lastSongs = append(sl.lastSongs, sl.current)
|
|
|
|
}
|
2023-10-01 06:37:47 +04:00
|
|
|
|
2023-10-01 05:42:55 +04:00
|
|
|
sl.current = newSong
|
|
|
|
}
|
|
|
|
|
|
|
|
// Current returns a current playing song.
|
|
|
|
func (sl *SongList) Current() *Song_ {
|
|
|
|
sl.mut.Lock()
|
|
|
|
defer sl.mut.Unlock()
|
2023-10-01 22:02:54 +04:00
|
|
|
if sl.current.StartAt.Year() == 1 {
|
|
|
|
return nil
|
|
|
|
}
|
2023-10-01 05:42:55 +04:00
|
|
|
return &sl.current
|
|
|
|
}
|
|
|
|
|
|
|
|
// List returns a list of lastly played songs.
|
|
|
|
func (sl *SongList) List() []Song_ {
|
|
|
|
sl.mut.Lock()
|
|
|
|
defer sl.mut.Unlock()
|
|
|
|
return sl.lastSongs
|
|
|
|
}
|