Module: CodenjoyConnection
- Defined in:
- lib/codenjoy_connection.rb,
lib/codenjoy_connection/player.rb,
lib/codenjoy_connection/version.rb,
lib/codenjoy_connection/exceptions.rb
Defined Under Namespace
Classes: GenericError, Player
Constant Summary
collapse
- VERSION =
"0.0.2"
Class Method Summary
collapse
Class Method Details
.play(player, opts) ⇒ Object
22
23
24
25
26
|
# File 'lib/codenjoy_connection.rb', line 22
def self.play(player, opts)
url = prepare_url(opts)
player = CodenjoyConnection::Player.new(player)
set_connection(player,url)
end
|
.prepare_url(opts = {}) ⇒ Object
8
9
10
11
12
13
14
15
16
17
18
19
20
|
# File 'lib/codenjoy_connection.rb', line 8
def self.prepare_url(opts = {})
raise CodenjoyConnection::GenericError.new("Please, specify connection params.") if opts == {}
host, port, username, player = opts[:host], opts[:port], opts[:username], opts[:player]
game_url = opts[:game_url] ||'tetris-contest/ws?'
raise CodenjoyConnection::GenericError.new("Please, specify a host for connection.") unless host
raise CodenjoyConnection::GenericError.new("Please, specify a port for connection.") unless port
raise CodenjoyConnection::GenericError.new("Please, specify a username for connection.") unless username
"ws://#{host}:#{port}/#{game_url}user=#{username}"
end
|
.set_connection(player, url) ⇒ Object
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/codenjoy_connection.rb', line 28
def self.set_connection(player,url)
EM.run do
ws = Faye::WebSocket::Client.new(url)
ws.on :message do |event|
p [:message, event.data]
player.process_data(event.data)
ws.send(player.make_step)
end
end
end
|