Class: MonkeyMusic::UI::Browser

Inherits:
Object
  • Object
show all
Defined in:
lib/monkey_music/ui/browser.rb

Instance Method Summary collapse

Constructor Details

#initialize(level, players, delay, clear) ⇒ Browser

Returns a new instance of Browser.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/monkey_music/ui/browser.rb', line 6

def initialize(level, players, delay, clear)
  @delay = delay
  @players = players
  @level = level
  puts "Initializing websockets..."
  Thread.new {
    EM.run {
      EM::WebSocket.run(:host => "0.0.0.0", :port => 3000) do |ws|
        @ws = ws
        ws.onopen { update }
      end
    }
  }
  print "Using browser UI. Press the enter key to start game."
  gets
  puts "Starting game..."
  puts "Game started!"
end

Instance Method Details

#update(turn = 0, turn_time = 0) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/monkey_music/ui/browser.rb', line 25

def update(turn = 0, turn_time = 0)
  @ws.send({
    :width => @level.width,
    :height => @level.height,
    :units => @level.units,
    :players => @level.players.map {|p| {
      :boost_cooldown => p.boost_cooldown,
      :remaining_time => p.remaining_time,
      :score => p.monkey.score,
      :capacity => p.monkey.capacity,
      :id => p.monkey.id,
      :name => p.monkey.name,
    }},
    :turn_limit => @level.turn_limit,
    :time_limit => @level.time_limit,
    :turn => turn,
  }.to_json) if @ws
  sleep 0.50
end