Class: WeewarAI::API

Inherits:
Object show all
Defined in:
lib/weewar-ai/api.rb

Overview

The API class contains some lower-level methods. You should not normally need to use them yourself. Instead, use the methods of your AI instance.

Class Method Summary collapse

Class Method Details

.accept_invitation(game_id) ⇒ Object Also known as: acceptInvitation

Accepts an invitation to a game.



79
80
81
# File 'lib/weewar-ai/api.rb', line 79

def self.accept_invitation( game_id )
  send "<weewar game='#{game_id}'><acceptInvitation/></weewar>"
end

.agentObject



30
31
32
# File 'lib/weewar-ai/api.rb', line 30

def self.agent
  trait[ :agent ]
end

.decline_invitation(game_id) ⇒ Object Also known as: declineInvitation

Declines an invitation to a game.



84
85
86
# File 'lib/weewar-ai/api.rb', line 84

def self.decline_invitation( game_id )
  send "<weewar game='#{game_id}'><declineInvitation/></weewar>"
end

.get(path) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/weewar-ai/api.rb', line 37

def self.get( path )
  result = nil
  retries = 0
  while GOD_LOVES_YOU
    url = "http://#{server}/api1/#{path}"
    begin
      result = agent.get( url ).body
      break
    rescue EOFError, Errno::EPIPE => e
      if retries < 10
        $stderr.puts "Communications error fetching '#{url}'.  Retrying (#{retries})..."
        sleep retries + 3
        retries += 1
      else
        break
      end
    end
  end
  if $debug
    $stderr.puts "XML RECEIVE: #{result}"
  end
  result
end

.init(params) ⇒ Object

Initializes the connection to the weewar.com API. You do not need to call this yourself; it is called for you when you subclass AI.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/weewar-ai/api.rb', line 12

def self.init( params )
  [ :server, :username, :api_key ].each do |required_param|
    if params[ required_param ].nil? or params[ required_param ].strip.empty?
      raise "Missing #{required_param}."
    end
  end
  
  trait[ :agent ] = agent = WWW::Mechanize.new
  trait[ :username ], trait[ :api_key ] = params[ :username ], params[ :api_key ]
  agent.basic_auth( params[ :username ], params[ :api_key ] )
  trait[ :server ] = params[ :server ]
  
  Hex.initialize_specs
end

.remove_game(game_id) ⇒ Object Also known as: removeGame

Removes a game from your AI’s headquarters.



89
90
91
# File 'lib/weewar-ai/api.rb', line 89

def self.remove_game( game_id )
  send "<weewar game='#{game_id}'><removeGame/></weewar>"
end

.send(xml) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/weewar-ai/api.rb', line 61

def self.send( xml )
  url = URI.parse( "http://#{server}/api1/eliza" )
  req = Net::HTTP::Post.new( url.path )
  req.basic_auth( trait[ :username ], trait[ :api_key ] )
  req[ 'Content-Type' ] = 'application/xml'
  result = Net::HTTP.new( url.host, url.port ).start { |http|
    if $debug
      $stderr.puts "XML SEND: #{xml}"
    end
    http.request( req, xml )
  }.body
  if $debug
    $stderr.puts "XML RECEIVE: #{result}"
  end
  result
end

.serverObject



33
34
35
# File 'lib/weewar-ai/api.rb', line 33

def self.server
  trait[ :server ]
end

.usernameObject



27
28
29
# File 'lib/weewar-ai/api.rb', line 27

def self.username
  trait[ :username ]
end