Sha256: ec1505a8259fcacd44a2fa5d46da51858f5ef1153523d05276960463aafe3887

Contents?: true

Size: 747 Bytes

Versions: 1

Compression:

Stored size: 747 Bytes

Contents

class Radiodan
class Panic
  include Logging
  
  def initialize(config)
    @panic      = false
    @timeout    = config.delete(:duration).to_i
    @state      = State.new(config)
  end
  
  def call(player)
    @player = player
    
    @player.register_event :panic do
      panic!
    end
  end
  
  def panic?
    @panic == true
  end

  def panic!
    return true if panic?
    
    @panic = true
    
    original_state = @player.state
  
    Thread.new do
      logger.debug "panic for #{@timeout} seconds"
      @player.state = @state
      sleep(@timeout)
      return_to_state original_state
    end
  
    @panic
  end

  def return_to_state(state)
    logger.debug "calming"
    @panic = false
    @player.state = state
  end
end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
radiodan-0.0.1 lib/radiodan/middleware/panic.rb