This repository has been archived on 2019-06-23. You can view files and clone it, but cannot push or open issues or pull requests.
mumbledj/songqueue.go

101 lines
2.4 KiB
Go
Raw Normal View History

2014-12-15 06:37:55 +01:00
/*
* MumbleDJ
* By Matthieu Grieger
* songqueue.go
2015-01-18 23:44:40 +01:00
* Copyright (c) 2014, 2015 Matthieu Grieger (MIT License)
2014-12-15 06:37:55 +01:00
*/
package main
2014-12-15 06:37:55 +01:00
import "errors"
// SongQueue type declaration.
type SongQueue struct {
queue []Song
}
2014-12-15 06:37:55 +01:00
2014-12-27 19:05:13 +01:00
// Initializes a new queue and returns the new SongQueue.
func NewSongQueue() *SongQueue {
return &SongQueue{
queue: make([]Song, 0),
}
}
// Adds a Song to the SongQueue.
func (q *SongQueue) AddSong(s Song) error {
beforeLen := q.Len()
q.queue = append(q.queue, s)
if len(q.queue) == beforeLen+1 {
return nil
} else {
return errors.New("Could not add Song to the SongQueue.")
}
}
// Returns the current Song.
func (q *SongQueue) CurrentSong() Song {
return q.queue[0]
}
// Moves to the next Song in SongQueue. NextSong() removes the first Song in the queue.
func (q *SongQueue) NextSong() {
if q.CurrentSong().playlist != nil {
if s, err := q.PeekNext(); err == nil {
if s.playlist != nil && (q.CurrentSong().playlist.id != s.playlist.id) {
q.CurrentSong().playlist.DeleteSkippers()
}
} else {
q.CurrentSong().playlist.DeleteSkippers()
}
}
q.queue = q.queue[1:]
}
// Peeks at the next Song and returns it.
func (q *SongQueue) PeekNext() (Song, error) {
if q.Len() > 1 {
return q.queue[1], nil
2015-01-13 01:02:20 +01:00
} else {
return nil, errors.New("There isn't a Song coming up next.")
2015-01-13 01:02:20 +01:00
}
}
2014-12-27 19:05:13 +01:00
// Returns the length of the SongQueue.
func (q *SongQueue) Len() int {
return len(q.queue)
}
// A traversal function for SongQueue. Allows a visit function to be passed in which performs
// the specified action on each queue item.
func (q *SongQueue) Traverse(visit func(i int, s Song)) {
for sQueue, queueSong := range q.queue {
visit(sQueue, queueSong)
}
}
// OnSongFinished event. Deletes Song that just finished playing, then queues the next Song (if exists).
func (q *SongQueue) OnSongFinished() {
2015-01-06 04:16:59 +01:00
if q.Len() != 0 {
if dj.queue.CurrentSong().dontSkip == true {
dj.queue.CurrentSong().dontSkip = false
q.PrepareAndPlayNextSong()
2015-01-06 04:16:59 +01:00
} else {
q.NextSong()
if q.Len() != 0 {
q.PrepareAndPlayNextSong()
}
}
}
}
// Prepares next song and plays it if the download succeeds. Otherwise the function will print an error message
// to the channel and skip to the next song.
func (q *SongQueue) PrepareAndPlayNextSong() {
if err := q.CurrentSong().Download(); err == nil {
q.CurrentSong().Play()
} else {
dj.client.Self.Channel.Send(AUDIO_FAIL_MSG, false)
q.OnSongFinished()
}
}