forked from bytebin/deepworld-gameserver
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gateway.rb
34 lines (27 loc) · 1.02 KB
/
gateway.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
require 'json'
class Gateway
ENVIRONMENTS = {
staging: {gateway: 'gateway-staging.deepworldgame.com', gateway_port: 80},
development: {gateway: '127.0.0.1', gateway_port: 5001}
}
HEADERS = {'User-Agent' => "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.83 Safari/535.11"}
attr_reader :auth_url
def initialize(environment)
env = ENVIRONMENTS[environment.to_sym]
@auth_url = "http://#{env[:gateway]}:#{env[:gateway_port]}/sessions"
end
def authenticate(player_name, password, &block)
http = EventMachine::HttpRequest.new(@auth_url).post body: {name: player_name, password: password}
http.errback do
puts "[Error] Gateway authentication failed for #{player_name}"
end
http.callback do
if http.response_header.status.to_s == "200"
response = JSON.parse(http.response)
yield response
else
puts "[Error] Gateway response of #{http.response_header.status} for #{player_name}"
end
end
end
end