From c5409a8d8bc7904438bbd55acf9cbf6b78e904d2 Mon Sep 17 00:00:00 2001 From: Gabriel Plassard Date: Fri, 9 Oct 2015 00:15:34 +0200 Subject: [PATCH] Fix first song not being shuffled --- service.go | 3 +++ songqueue.go | 16 +++++++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/service.go b/service.go index 1e855b5..245947e 100644 --- a/service.go +++ b/service.go @@ -115,6 +115,9 @@ func FindServiceAndAdd(user *gumble.User, url string) error { // Starts playing the new song if nothing else is playing if oldLength == 0 && dj.queue.Len() != 0 && !dj.audioStream.IsPlaying() { + if (dj.conf.General.AutomaticShuffleOn){ + dj.queue.RandomNextSong(true) + } if err := dj.queue.CurrentSong().Download(); err == nil { dj.queue.CurrentSong().Play() } else { diff --git a/songqueue.go b/songqueue.go index 0255d00..182455d 100644 --- a/songqueue.go +++ b/songqueue.go @@ -64,9 +64,8 @@ func (q *SongQueue) NextSong() { // PeekNext peeks at the next Song and returns it. func (q *SongQueue) PeekNext() (Song, error) { if q.Len() > 1 { - if (dj.conf.General.AutomaticShuffleOn){ //Shuffle mode is active - swapIndex := 1 + rand.Intn(q.Len()) - q.queue[1], q.queue[swapIndex] = q.queue[swapIndex], q.queue[1] + if dj.conf.General.AutomaticShuffleOn{ //Shuffle mode is active + q.RandomNextSong(false) } return q.queue[1], nil } @@ -121,3 +120,14 @@ func (q *SongQueue) ShuffleSongs() { q.queue[i + 1], q.queue[j + 1] = q.queue[j + 1], q.queue[i + 1] } } + +// Sets a random song as next song to be played +// queueWasEmpty wether the queue was empty before adding the last Song +func (q *SongQueue) RandomNextSong(queueWasEmpty bool){ + nextSongIndex := 1 + if queueWasEmpty{ + nextSongIndex = 0 + } + swapIndex := nextSongIndex + rand.Intn(q.Len()) + q.queue[nextSongIndex], q.queue[swapIndex] = q.queue[swapIndex], q.queue[nextSongIndex] +}