From 64247079b9ae4ec58e801658ba62c2a64fee1e72 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Mon, 25 Jan 2016 11:52:43 +0000 Subject: player: support "consume" mode for tracklist When activated, this boolean deletes a song from the tracklist after it is played. --- lib/dtas/player/client_handler.rb | 14 ++++++++++++++ lib/dtas/tracklist.rb | 27 +++++++++++++++++++-------- 2 files changed, 33 insertions(+), 8 deletions(-) (limited to 'lib') diff --git a/lib/dtas/player/client_handler.rb b/lib/dtas/player/client_handler.rb index a6f5dba..7b33967 100644 --- a/lib/dtas/player/client_handler.rb +++ b/lib/dtas/player/client_handler.rb @@ -630,6 +630,20 @@ module DTAS::Player::ClientHandler # :nodoc: io.emit("tl shuffle #{prev}") end + def _dpc_tl_consume(io, msg) + prev = (!!@tl.consume).to_s + v = msg.shift + case v + when nil + else + set_bool(io, 'tl consume', v) do |b| + @tl.consume = b + __wall("tl consume #{b}") + end + end + io.emit("tl consume #{prev}") + end + def _dpc_tl_max(io, msg) prev = @tl.max case msg.shift diff --git a/lib/dtas/tracklist.rb b/lib/dtas/tracklist.rb index 6e0dd75..779b973 100644 --- a/lib/dtas/tracklist.rb +++ b/lib/dtas/tracklist.rb @@ -12,11 +12,13 @@ class DTAS::Tracklist # :nodoc: attr_accessor :repeat # true, false, 1 attr_reader :shuffle # false or shuffled @list attr_accessor :max # integer + attr_accessor :consume # boolean TL_DEFAULTS = { 'pos' => -1, 'repeat' => false, 'max' => 20_000, + 'consume' => false, } SIVS = TL_DEFAULTS.keys @@ -116,20 +118,25 @@ class DTAS::Tracklist # :nodoc: def advance_track(repeat_ok = true) cur = @shuffle || @list return if cur.empty? + prev = cur[@pos] if @consume && @pos >= 0 # @repeat == 1 for single track repeat repeat = repeat_ok ? @repeat : false next_pos = @goto_pos || @pos + (repeat == 1 ? 0 : 1) next_off = @goto_off # nil by default @goto_pos = @goto_off = nil - if cur[next_pos] + + if nxt = cur[next_pos] @pos = next_pos + remove_track(prev.track_id) if prev else + remove_track(prev.track_id) if prev # reshuffle the tracklist when we've exhausted it cur.shuffle! if @shuffle - return unless repeat + return if !repeat || cur.empty? next_pos = @pos = 0 + nxt = cur[0] end - [ cur[next_pos].to_path, next_off ] + [ nxt.to_path, next_off ] end def cur_track @@ -189,14 +196,18 @@ class DTAS::Tracklist # :nodoc: def remove_track(track_id) idx = _idx_of(@list, track_id) or return false + track = @list.delete_at(idx) if @shuffle - si = _idx_of(@shuffle, track_id) or return false - @shuffle.delete_at(si) + idx = _idx_of(@shuffle, track_id) or return false + @shuffle.delete_at(idx) end - track = @list.delete_at(idx) len = @list.size - @pos = len - 1 if @pos >= len - @goto_pos = @goto_pos = nil # TODO: reposition? + if @pos >= len + @pos = len - 1 + elsif idx <= @pos + @pos -= 1 + end + @goto_pos = @goto_off = nil # TODO: reposition? track.to_path end -- cgit v1.2.3-24-ge0c7