Class: Brawl::BotProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/brawl/bot_proxy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ BotProxy

Returns a new instance of BotProxy.



7
8
9
10
11
12
13
14
15
16
# File 'lib/brawl/bot_proxy.rb', line 7

def initialize(params)
  @clock      = params[:clock]
  @bot        = params[:bot]
  @code       = params[:code]

  @last_tick  = @clock.ticks
  @run        = false

  add_proxy_methods
end

Instance Attribute Details

#botObject

Returns the value of attribute bot.



5
6
7
# File 'lib/brawl/bot_proxy.rb', line 5

def bot
  @bot
end

#clockObject

Returns the value of attribute clock.



5
6
7
# File 'lib/brawl/bot_proxy.rb', line 5

def clock
  @clock
end

#last_tickObject

Returns the value of attribute last_tick.



5
6
7
# File 'lib/brawl/bot_proxy.rb', line 5

def last_tick
  @last_tick
end

#msg_logObject

Returns the value of attribute msg_log.



5
6
7
# File 'lib/brawl/bot_proxy.rb', line 5

def msg_log
  @msg_log
end

Instance Method Details

#add_proxy_methodsObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/brawl/bot_proxy.rb', line 22

def add_proxy_methods
  # I'm sure there's a better way of doing this
  combined_methods = @bot.public_methods(false)
  combined_methods.concat BasicArenaObject.instance_methods(false)

  combined_methods.each do |method|
    next if method.to_s[0] == "_"
    singleton_class.send :define_method, method do |*params, &block|
      sleep(0.01) while @clock.ticks <= @last_tick
      @last_tick = @clock.ticks
      @bot.send method, *params, &block
    end

  end
end

#code {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



55
56
57
# File 'lib/brawl/bot_proxy.rb', line 55

def code
  yield self
end

#nameObject



18
19
20
# File 'lib/brawl/bot_proxy.rb', line 18

def name
  @bot.name
end

#startObject



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/brawl/bot_proxy.rb', line 38

def start
  @run = true
  Thread.start {
    until @clock.state == :stopped || !@run || @health == 0
      sleep(0.01) if @clock.state == :wait
      if @clock.state == :running
        instance_eval("Thread.start{$SAFE=3;#{@code}}.join")
      end
    end
    @run = false
  }
end

#stopObject



51
52
53
# File 'lib/brawl/bot_proxy.rb', line 51

def stop
  @run = false
end