Class: WeewarAI::AI

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

Overview

Begin creating your bot by subclassing the WeewarAI::AI class.

class MyBot < WeewarAI::AI
end

See examples/basic.rb for a simple working example.

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ AI

In your bot’s initialize method, call the AI superclass’s initialize method with these parameters (values are examples):

super(
  :server => 'test.weewar.com',
  :username => 'aiMyBot',
  :api_key => 'r0goujPhKJEM6udL3RNfBtcS9',
)

Retrieve your API key from test.weewar.com/apiToken .

Once constructed, your AI instance will have the following instance variables available:



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

def initialize( params )
  @username = params[ :username ]
  WeewarAI::API.init( params )
  refresh
end

Instance Method Details

#accept_all_invitationsObject

Accepts all game invitations.

bot.accept_all_invitations


73
74
75
76
77
78
79
# File 'lib/weewar-ai/ai.rb', line 73

def accept_all_invitations
  one_accepted = false
  @invitations.each do |invitation|
    one_accepted ||= accept_invitation( invitation )
  end
  one_accepted
end

#accept_invitation(game_id) ⇒ Object

Accepts the invitation to join the game identified by the game_id.

bot.accept_invitation 165


66
67
68
69
# File 'lib/weewar-ai/ai.rb', line 66

def accept_invitation( game_id )
  response = WeewarAI::API.accept_invitation( game_id )
  %r{<ok/>} === response
end

#refreshObject

Retrieves your AI’s headquarters data from the game server, and updates the instance variables @games, @needy_games and



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/weewar-ai/ai.rb', line 42

def refresh
  xml = XmlSimple.xml_in(
    WeewarAI::API.get( "/headquarters" ),
    { 'ForceArray' => [ 'game' ], }
  )
  gxml = xml[ 'game' ]
  @invitations = gxml.find_all { |g|
    g[ 'link' ] =~ /join/
  }.map { |g|
    g[ 'id' ].to_i
  }
  gxml = gxml.reject { |g|
    @invitations.include? g[ 'id' ].to_i
  }
  @games = gxml.map { |g|
    WeewarAI::Game.new( g[ 'id' ] )
  }
  @needy_games = @games.find_all { |g|
    g.current_player and g.current_player.name == @username
  }
end