Class: HWPing::Handler

Inherits:
Object
  • Object
show all
Defined in:
lib/hwping/handler.rb

Instance Method Summary collapse

Constructor Details

#initialize(launcher, config) ⇒ Handler

Set up the handler with the configuration



4
5
6
7
8
9
# File 'lib/hwping/handler.rb', line 4

def initialize(launcher, config)
  @targets    = config.targets
  @auth       = config.auth
  @nick       = config.nick
  @launcher   = launcher
end

Instance Method Details

#channel(event) ⇒ Object

If the event happened in a channel, return just with a symbol



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/hwping/handler.rb', line 12

def channel(event)
  if event.message =~ /^hwping,?\s+(.*)\s*$/
    if @auth.include?(event.user.to_s)
      if @targets.include?($1)
        @launcher.point_and_fire(@targets[$1])
        return :firing
      else
        return :notarget
      end
    else
      return :unauthorized
    end
  end
end

#private(event) ⇒ Object

If the event happened in a private window, return with an array



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/hwping/handler.rb', line 28

def private(event)
  if @auth.include?(event.user.to_s)
    case event.message
    when "fire"
      @launcher.fire
      return [:fire]
    when "position"
      return @launcher.position.unshift(:position)
    when "help"
      return [:help]
    when "reset"
      @launcher.reset
      return [:reset]
    when "target list"
      return [:target_list, @targets.keys.join(', ')]
    when /^target set ([^\s]+)( (\d+) (\d+))?$/
      @targets[$1] = $2 ? [$3.to_i, $4.to_i] : @launcher.position
      return [:target_set, @targets[$1]].flatten
    when /^target ((get)|(del)) ([^\s]+)$/
      if @targets.has_key?($+)
        if $1 == "del"
          @targets.delete($+)
          return [:target_del]
        else
          return [:target_get, @targets[$+]].flatten
        end
      else
        return [:notarget]
      end
    when /^((up)|(down)|(left)|(right)) (\d+)$/
      @launcher.send($1, $+.to_i)
      return [:move]
    else
      return [:badcommand]
    end
  else
    return [:unauthorized]
  end
end