feat: progress bar, more realiable reconnects
This commit is contained in:
parent
bcc1bce743
commit
7e8f6786fe
5 changed files with 305 additions and 30 deletions
|
@ -22,6 +22,7 @@ import (
|
|||
type JukeboxPlayer struct {
|
||||
bot *Bot
|
||||
currentSong *subsonic.Song
|
||||
currentSongStarted time.Time
|
||||
playlist []subsonic.Song
|
||||
playlistMutex sync.Mutex
|
||||
playingMutex sync.Mutex
|
||||
|
@ -29,6 +30,7 @@ type JukeboxPlayer struct {
|
|||
songHistory []*subsonic.Song
|
||||
historyMutex sync.RWMutex
|
||||
maxHistorySize int
|
||||
initialized bool
|
||||
}
|
||||
|
||||
// NewJukeboxPlayer creates a new jukebox player
|
||||
|
@ -38,6 +40,7 @@ func NewJukeboxPlayer(bot *Bot) *JukeboxPlayer {
|
|||
playlist: make([]subsonic.Song, 0),
|
||||
songHistory: make([]*subsonic.Song, 0),
|
||||
maxHistorySize: 20, // Store the last 20 songs
|
||||
initialized: true,
|
||||
}
|
||||
|
||||
// Register command handlers
|
||||
|
@ -236,6 +239,7 @@ func (j *JukeboxPlayer) startPlaying() {
|
|||
// Clear current song when stopping
|
||||
j.playingMutex.Lock()
|
||||
j.currentSong = nil
|
||||
j.currentSongStarted = time.Time{} // Reset time to zero value
|
||||
j.playingMutex.Unlock()
|
||||
|
||||
return
|
||||
|
@ -273,6 +277,7 @@ func (j *JukeboxPlayer) startPlaying() {
|
|||
|
||||
j.playingMutex.Lock()
|
||||
j.currentSong = song
|
||||
j.currentSongStarted = time.Now()
|
||||
j.playingMutex.Unlock()
|
||||
|
||||
// Add song to history when it starts playing
|
||||
|
@ -781,6 +786,13 @@ func (j *JukeboxPlayer) GetCurrentSong() *subsonic.Song {
|
|||
return j.currentSong
|
||||
}
|
||||
|
||||
// GetCurrentSongInfo returns the current song and its start time
|
||||
func (j *JukeboxPlayer) GetCurrentSongInfo() (*subsonic.Song, time.Time) {
|
||||
j.playingMutex.Lock()
|
||||
defer j.playingMutex.Unlock()
|
||||
return j.currentSong, j.currentSongStarted
|
||||
}
|
||||
|
||||
// addToHistory adds a song to the playback history
|
||||
func (j *JukeboxPlayer) addToHistory(song *subsonic.Song) {
|
||||
if song == nil {
|
||||
|
@ -790,6 +802,12 @@ func (j *JukeboxPlayer) addToHistory(song *subsonic.Song) {
|
|||
j.historyMutex.Lock()
|
||||
defer j.historyMutex.Unlock()
|
||||
|
||||
// Avoid duplicate entries at the beginning of history
|
||||
if len(j.songHistory) > 0 && j.songHistory[0] != nil &&
|
||||
j.songHistory[0].ID == song.ID {
|
||||
return
|
||||
}
|
||||
|
||||
// Add the song to the beginning of the history list
|
||||
j.songHistory = append([]*subsonic.Song{song}, j.songHistory...)
|
||||
|
||||
|
@ -804,6 +822,11 @@ func (j *JukeboxPlayer) GetSongHistory() []*subsonic.Song {
|
|||
j.historyMutex.RLock()
|
||||
defer j.historyMutex.RUnlock()
|
||||
|
||||
// Ensure we don't return a nil slice even if no history yet
|
||||
if j.songHistory == nil {
|
||||
return make([]*subsonic.Song, 0)
|
||||
}
|
||||
|
||||
// Create a copy of the history to return
|
||||
history := make([]*subsonic.Song, len(j.songHistory))
|
||||
copy(history, j.songHistory)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue