Class: GoshrineBot::Client
- Inherits:
-
Object
- Object
- GoshrineBot::Client
- Defined in:
- lib/goshrine_bot/client.rb
Instance Method Summary collapse
- #add_game(game) ⇒ Object
- #gtp_cmd_line ⇒ Object
- #handle_match_accept(token) ⇒ Object
- #handle_match_request(request) ⇒ Object
-
#initialize(options) ⇒ Client
constructor
A new instance of Client.
- #load_existing_games(&blk) ⇒ Object
- #login(&blk) ⇒ Object
- #my_user_id ⇒ Object
- #run ⇒ Object
- #subscribe ⇒ Object
Constructor Details
#initialize(options) ⇒ Client
Returns a new instance of Client.
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/goshrine_bot/client.rb', line 7 def initialize() @base_url = URI::parse([:server_url]) GoshrineRequest.base_url = @base_url @options = @password = [:password] @login = [:login] @gtp_cmd_line = [:gtp_cmd_line] @connected = false @games = {} end |
Instance Method Details
#add_game(game) ⇒ Object
111 112 113 114 115 116 117 118 119 120 |
# File 'lib/goshrine_bot/client.rb', line 111 def add_game(game) @games[game.game_id] = game game.make_move # does nothing if its not our turn, or the game is not started @faye_client.subscribe("/game/private/" + game.token + '/' + @queue_id) do |m| game.(m) end @faye_client.subscribe("/game/play/" + game.token) do |m| game.(m) end end |
#gtp_cmd_line ⇒ Object
18 19 20 |
# File 'lib/goshrine_bot/client.rb', line 18 def gtp_cmd_line @gtp_cmd_line end |
#handle_match_accept(token) ⇒ Object
122 123 124 125 126 127 128 129 130 |
# File 'lib/goshrine_bot/client.rb', line 122 def handle_match_accept(token) game = GameInProgress.new(self) request = GoshrineRequest.new("/g/#{token}") request.callback { |http| attrs = JSON.parse(http.response) game.update_from_game_list(attrs) add_game(game) } end |
#handle_match_request(request) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/goshrine_bot/client.rb', line 132 def handle_match_request(request) active_games = @games.select { |key, game| game.state == "in-play" } max_games = @options[:maximum_concurrent_games] game = GameInProgress.new(self) game.update_from_match_request(request) if max_games.nil? || active_games.count < max_games request = GoshrineRequest.new("/match/accept?id=#{game.challenge_id}").get request.callback { |http| attrs = JSON.parse(http.response) game.update_from_game_list(attrs) add_game(game) } else count = max_games > 1 ? "#{max_games} games" : "1 game" reason = "#{@login} only plays #{count} at a time." puts "Rejecting match: #{reason}" GoshrineRequest.new("/match/reject?id=#{game.challenge_id}&reason=#{URI.escape(reason)}").get end end |
#load_existing_games(&blk) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/goshrine_bot/client.rb', line 92 def load_existing_games(&blk) request = GoshrineRequest.new('/game/active').get request.callback { |http| #puts "Got #{http.inspect}" games = JSON.parse(http.response) puts "#{games.count} game(s) in progress" games.each do |game_attrs| game = GameInProgress.new(self) game.update_from_game_list(game_attrs) if game.move_number != game.moves.size puts "Only #{game.moves.size} available! Expected #{game.move_number} in game #{game.token}" else add_game(game) end end blk.call } end |
#login(&blk) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/goshrine_bot/client.rb', line 26 def login(&blk) # login request = GoshrineRequest.new('/sessions/create').post(:body => {'login' => @login, 'password' => @password}) request.callback { |http| if http.response_header.status == 401 puts "Invalid Login or Password" EventMachine::stop else user = JSON.parse(http.response) puts "Got #{user.inspect}" @queue_id = user['queue_id'] @my_user_id = user['id'] @faye_token = user['faye_token'] if user['user_type'] != 'bot' puts "Account #{user['login']} is not registered as a robot!" EventMachine::stop else puts "Login successful" blk.call end end } request.errback { |http| puts "Login failed (network issue?)"; } end |
#my_user_id ⇒ Object
22 23 24 |
# File 'lib/goshrine_bot/client.rb', line 22 def my_user_id @my_user_id end |
#run ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/goshrine_bot/client.rb', line 72 def run login { puts "Starting faye client" @faye_client = Faye::Client.new((@base_url + '/events').to_s, :cookie => @cookie) if @faye_token @faye_client.add_extension(FayeAuthExt.new(@my_user_id, @faye_token)) end subscribe load_existing_games { if @options[:idle_shutdown_timeout] > 0 EM::add_periodic_timer( @options[:idle_shutdown_timeout] ) { @games.each do |token, game| game.idle_check(@options[:idle_shutdown_timeout]) end } end } } end |
#subscribe ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/goshrine_bot/client.rb', line 53 def subscribe @faye_client.subscribe("/user/private/" + @queue_id) do |m| puts "msg" msg_type = m["type"] case msg_type when 'match_requested' handle_match_request(m["match_request"]) when 'match_accepted' handle_match_accept(m["game_token"]) else puts "Unsupported private message type: #{msg_type}" end #@msg_queue_in.push(doc) end @faye_client.subscribe("/room/1") do |m| #@msg_queue_in.push(doc) end end |