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

115 lines
2.8 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"
"fmt"
"time"
)
// SongQueue type declaration.
type SongQueue struct {
queue []Song
}
2014-12-15 06:37:55 +01:00
2015-05-10 07:00:24 +02:00
// NewSongQueue initializes a new queue and returns it.
func NewSongQueue() *SongQueue {
return &SongQueue{
queue: make([]Song, 0),
}
}
2015-05-10 07:00:24 +02:00
// AddSong 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
}
2015-05-10 07:00:24 +02:00
return errors.New("Could not add Song to the SongQueue.")
}
2015-05-10 07:00:24 +02:00
// CurrentSong returns the current Song.
func (q *SongQueue) CurrentSong() Song {
return q.queue[0]
}
2015-05-10 07:00:24 +02:00
// NextSong moves to the next Song in SongQueue. NextSong() removes the first Song in the queue.
func (q *SongQueue) NextSong() {
2015-07-28 15:56:20 +02:00
if !isNil(q.CurrentSong().Playlist()) {
if s, err := q.PeekNext(); err == nil {
if !isNil(s.Playlist()) {
if q.CurrentSong().Playlist().ID() != s.Playlist().ID() {
q.CurrentSong().Playlist().DeleteSkippers()
}
}
2015-07-28 15:56:20 +02:00
} else {
q.CurrentSong().Playlist().DeleteSkippers()
}
}
q.queue = q.queue[1:]
}
2015-05-10 07:00:24 +02:00
// PeekNext 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
}
2015-05-10 07:00:24 +02:00
return nil, errors.New("There isn't a Song coming up next.")
2015-01-13 01:02:20 +01:00
}
2015-05-10 07:00:24 +02:00
// Len returns the length of the SongQueue.
func (q *SongQueue) Len() int {
return len(q.queue)
}
2015-05-10 07:00:24 +02:00
// Traverse is 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)
}
}
// Gets the song at a specific point in the queue
func (q *SongQueue) Get(i int) (Song, error) {
if q.Len() > i+1 {
return q.queue[i], nil
}
return nil, errors.New("Out of Bounds")
}
// OnSongFinished event. Deletes Song that just finished playing, then queues the next Song (if exists).
func (q *SongQueue) OnSongFinished() {
resetOffset, _ := time.ParseDuration(fmt.Sprintf("%ds", 0))
dj.audioStream.Offset = resetOffset
2015-01-06 04:16:59 +01:00
if q.Len() != 0 {
2015-04-16 08:44:35 +02:00
if dj.queue.CurrentSong().DontSkip() == true {
dj.queue.CurrentSong().SetDontSkip(false)
q.PrepareAndPlayNextSong()
2015-01-06 04:16:59 +01:00
} else {
q.NextSong()
if q.Len() != 0 {
q.PrepareAndPlayNextSong()
}
}
}
}
2015-05-10 07:00:24 +02:00
// 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()
}
}