diff --git a/mumbledj/mumbledj.rb b/mumbledj/mumbledj.rb index 6499d06..da90bdc 100644 --- a/mumbledj/mumbledj.rb +++ b/mumbledj/mumbledj.rb @@ -42,7 +42,7 @@ class MumbleDJ end end - self.set_callbacks + set_callbacks @client.connect @client.on_connected do @@ -131,20 +131,28 @@ class MumbleDJ def add(sender, url) if OUTPUT_ENABLED - puts("#{@sender} has added a song to the queue.") + puts("#{sender} has added a song to the queue.") end - if song_add_successful?(url, @sender) - @client.text_channel(@client.me.current_channel.name, "#{@sender} has added a song to the queue.") + if song_add_successful?(url, sender) + @client.text_channel(@client.me.current_channel.name, "#{sender} has added a song to the queue.") else - @client.text_user(@sender, "The URL you provided was not valid.") + @client.text_user(sender, "The URL you provided was not valid.") end end def skip(sender) if OUTPUT_ENABLED - puts("#{@sender} has voted to skip the current song.") + puts("#{sender} has voted to skip the current song.") + end + if @song_queue.get_current_song.add_skip?(sender) + @client.text_channel(@client.me.current_channel.name, "#{sender} has voted to skip the current song.") + if @song_queue.get_current_song.skip_now?(@client.me.current_channel.users.count - 1) + @client.text_channel(@client.me.current_channel.name, "Number of required skip votes has been met. Skipping song!") + @song_queue.get_current_song.skip + end + else + @client.text_user(sender, "You have already voted to skip this song.") end - @song_queue.get_current_song.add_skip(@sender) end def volume(sender, vol) diff --git a/mumbledj/song.rb b/mumbledj/song.rb index 51cefb0..a7fe303 100644 --- a/mumbledj/song.rb +++ b/mumbledj/song.rb @@ -16,8 +16,19 @@ class Song end # Adds a skipper to the skips array for the current song. - def add_skip(username) - + def add_skip?(username) + if not @skips.include?(username) + @skips << username + return true + else + return false + end + end + + # Determines if a skip should occur. Returns true if a skip is needed, + # false otherwise. + def skip_now?(total_users) + return (total_users / @skips.count) >= SKIP_RATIO end end diff --git a/mumbledj/song_queue.rb b/mumbledj/song_queue.rb index 697a271..0cafa5e 100644 --- a/mumbledj/song_queue.rb +++ b/mumbledj/song_queue.rb @@ -15,7 +15,7 @@ class SongQueue # Checks if song already exists in the queue, and adds it if it doesn't # already exist. - def add_song(url, submitter) + def add_song?(url, submitter) # TODO: Determine which kind of URL is given (probably using regex), # and instantiate the correct Song object. # Example for a YouTube URL given below. @@ -26,10 +26,11 @@ class SongQueue else @queue.each do |song| if song.url == url - song = YouTubeSong.new(url, submitter) - @queue.push(song) + return false end end + song = YouTubeSong.new(url, submitter) + @queue.push(song) end end