Skip to content

Commit

Permalink
added the shuffle command (#61)
Browse files Browse the repository at this point in the history
* added the shuffle command
  • Loading branch information
svenwiltink authored Jan 17, 2019
1 parent ef03001 commit dfd1d52
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
1 change: 1 addition & 0 deletions pkg/bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func (bot *MusicBot) registerCommands() {
bot.registerCommand(currentCommand)
bot.registerCommand(queueCommand)
bot.registerCommand(flushCommand)
bot.registerCommand(shuffleCommand)
bot.registerCommand(whiteListCommand)
bot.registerCommand(volCommand)
bot.registerCommand(aboutCommand)
Expand Down
13 changes: 13 additions & 0 deletions pkg/bot/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,19 @@ var flushCommand = &Command{
},
}

var shuffleCommand = &Command{
Name: "shuffle",
Function: func(bot *MusicBot, message Message) {
bot.musicPlayer.GetQueue().Shuffle()

if message.IsPrivate {
bot.BroadcastMessage(fmt.Sprintf("%s shuffled the queue", message.Sender.NickName))
}

bot.ReplyToMessage(message, "Queue shuffled")
},
}

var whiteListCommand = &Command{
Name: "whitelist",
MasterOnly: true,
Expand Down
21 changes: 17 additions & 4 deletions pkg/music/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package music

import (
"log"
"math/rand"
"sync"

"errors"
Expand All @@ -16,8 +17,9 @@ const (
// Queue holds an array of songs
type Queue struct {
*eventemitter.Emitter
songs []*Song
lock sync.Mutex
songs []*Song
lock sync.Mutex
randSource *rand.Rand
}

func (queue *Queue) Append(songs ...*Song) {
Expand Down Expand Up @@ -88,6 +90,16 @@ func (queue *Queue) GetNextN(limit int) ([]Song, error) {
return result, nil
}

func (queue *Queue) Shuffle() {
queue.lock.Lock()
defer queue.lock.Unlock()

// Shuffle numbers, swapping corresponding entries in letters at the same time.
queue.randSource.Shuffle(len(queue.songs), func(i, j int) {
queue.songs[i], queue.songs[j] = queue.songs[j], queue.songs[i]
})
}

func (queue *Queue) Flush() {
queue.lock.Lock()
defer queue.lock.Unlock()
Expand Down Expand Up @@ -115,7 +127,8 @@ func (queue *Queue) WaitForNext() *Song {
// NewQueue creates a new instance of Queue
func NewQueue() *Queue {
return &Queue{
songs: make([]*Song, 0),
Emitter: eventemitter.NewEmitter(true),
songs: make([]*Song, 0),
Emitter: eventemitter.NewEmitter(true),
randSource: rand.New(rand.NewSource(time.Now().UTC().UnixNano())),
}
}
17 changes: 17 additions & 0 deletions pkg/music/queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package music

import (
"github.com/stretchr/testify/assert"
"math/rand"
"testing"
"time"
)
Expand Down Expand Up @@ -124,6 +125,22 @@ func TestQueue_GetTotalDuration(t *testing.T) {
assert.Equal(t, 2*time.Minute, queue.GetTotalDuration())
}

func TestQueue_Shuffle(t *testing.T) {
queue, _, _ := getTestQueue()

queue.Append(&Song{
Duration: time.Minute,
Name: "song3",
})

queue.randSource = rand.New(rand.NewSource(1))
original := append([]*Song(nil), queue.songs...)

queue.Shuffle()

assert.NotEqual(t, original, queue.songs)
}

func TestQueue_WaitForNext(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit dfd1d52

Please sign in to comment.