Class: WeewarAI::Game

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

Overview

The Game class is your interface to a game on the weewar server. Game instances are used to do such things as finish turns, surrender, and abandon. Also, you access game maps and units through a Game instance.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ Game

Instantiate a new Game instance corresponding to the weewar game with the given id number.

game = WeewarAI::Game.new( 132 )


19
20
21
22
# File 'lib/weewar-ai/game.rb', line 19

def initialize( id )
  @id = id.to_i
  refresh
end

Instance Attribute Details

#credits_per_baseObject (readonly) Also known as: creditsPerBase

Returns the value of attribute credits_per_base.



11
12
13
# File 'lib/weewar-ai/game.rb', line 11

def credits_per_base
  @credits_per_base
end

#idObject (readonly)

Returns the value of attribute id.



11
12
13
# File 'lib/weewar-ai/game.rb', line 11

def id
  @id
end

#initial_creditsObject (readonly) Also known as: initialCredits

Returns the value of attribute initial_credits.



11
12
13
# File 'lib/weewar-ai/game.rb', line 11

def initial_credits
  @initial_credits
end

#last_attackedObject

Returns the value of attribute last_attacked.



14
15
16
# File 'lib/weewar-ai/game.rb', line 14

def last_attacked
  @last_attacked
end

#mapObject (readonly)

Returns the value of attribute map.



11
12
13
# File 'lib/weewar-ai/game.rb', line 11

def map
  @map
end

#map_urlObject (readonly) Also known as: mapUrl

Returns the value of attribute map_url.



11
12
13
# File 'lib/weewar-ai/game.rb', line 11

def map_url
  @map_url
end

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/weewar-ai/game.rb', line 11

def name
  @name
end

#paceObject (readonly)

Returns the value of attribute pace.



11
12
13
# File 'lib/weewar-ai/game.rb', line 11

def pace
  @pace
end

#pending_invitesObject (readonly) Also known as: pendingInvites

Returns the value of attribute pending_invites.



11
12
13
# File 'lib/weewar-ai/game.rb', line 11

def pending_invites
  @pending_invites
end

#playersObject (readonly)

Returns the value of attribute players.



11
12
13
# File 'lib/weewar-ai/game.rb', line 11

def players
  @players
end

#playing_sinceObject (readonly) Also known as: playingSince

Returns the value of attribute playing_since.



11
12
13
# File 'lib/weewar-ai/game.rb', line 11

def playing_since
  @playing_since
end

#roundObject (readonly)

Returns the value of attribute round.



11
12
13
# File 'lib/weewar-ai/game.rb', line 11

def round
  @round
end

#stateObject (readonly)

Returns the value of attribute state.



11
12
13
# File 'lib/weewar-ai/game.rb', line 11

def state
  @state
end

#typeObject (readonly)

Returns the value of attribute type.



11
12
13
# File 'lib/weewar-ai/game.rb', line 11

def type
  @type
end

#unitsObject (readonly)

Returns the value of attribute units.



11
12
13
# File 'lib/weewar-ai/game.rb', line 11

def units
  @units
end

#urlObject (readonly)

Returns the value of attribute url.



11
12
13
# File 'lib/weewar-ai/game.rb', line 11

def url
  @url
end

Instance Method Details

#abandonObject

Abandon this game.

game.abandon


108
109
110
# File 'lib/weewar-ai/game.rb', line 108

def abandon
  send "<abandon/>"
end

#basesObject

An Array of the base Hexes for this game.

bases = game.bases


150
151
152
# File 'lib/weewar-ai/game.rb', line 150

def bases
  @map.bases
end

#bases_of(faction) ⇒ Object

An Array of the base Hexes owned by the given faction.

their_bases = game.bases enemy_faction


156
157
158
# File 'lib/weewar-ai/game.rb', line 156

def bases_of( faction )
  @map.bases.find_all { |b| b.faction == faction }
end

#current_playerObject

The Player whose turn it is.

turn_taker = game.current_player


118
119
120
# File 'lib/weewar-ai/game.rb', line 118

def current_player
  @players.find { |p| p.current? }
end

#enemy_basesObject

An Array of bases not owned by your AI (including neutral bases).

capturable_bases = game.enemy_bases


168
169
170
# File 'lib/weewar-ai/game.rb', line 168

def enemy_bases
  @map.bases.find_all { |b| b.faction != my_faction }
end

#enemy_unitsObject

An Array of the Units not belonging to your AI.

bad_guys = game.enemy_units


144
145
146
# File 'lib/weewar-ai/game.rb', line 144

def enemy_units
  @units.find_all { |u| u.faction != my_faction }
end

#faction_for_player(player_name) ⇒ Object

The Faction of the given player.

pistos_faction = game.faction_for_player 'Pistos'


128
129
130
# File 'lib/weewar-ai/game.rb', line 128

def faction_for_player( player_name )
  @factions.find { |f| f.player_name == player_name }
end

#finish_turnObject Also known as: finishTurn

End turn in this game.

game.finish_turn


95
96
97
# File 'lib/weewar-ai/game.rb', line 95

def finish_turn
  send "<finishTurn/>"
end

#my_basesObject

Your AI’s bases in this game.

good_bases = game.my_bases


162
163
164
# File 'lib/weewar-ai/game.rb', line 162

def my_bases
  bases_of my_faction
end

#my_factionObject

Your AI’s Faction in this game.

me = my = i = game.my_faction
puts "My name is #{my.player_name}."
if i.can_afford? :htank
  my_base.build :htank
end


138
139
140
# File 'lib/weewar-ai/game.rb', line 138

def my_faction
  faction_for_player WeewarAI::API.username
end

#refreshObject

Hits the weewar server for all the game state data as it sees it. All internal variables are updated to match.

my_game.refresh


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/weewar-ai/game.rb', line 32

def refresh
  xml = XmlSimple.xml_in(
    WeewarAI::API.get( "/gamestate/#{id}" ),
    { 'ForceArray' => [ 'faction', 'player', 'terrain', 'unit' ], }
  )
  #$stderr.puts xml.nice_inspect
  @name = xml[ 'name' ]
  @round = xml[ 'round' ].to_i
  @state = xml[ 'state' ]
  @pending_invites = ( xml[ 'pendingInvites' ] == 'true' )
  @pace = xml[ 'pace' ].to_i
  @type = xml[ 'type' ]
  @url = xml[ 'url' ]
  @players = xml[ 'players' ][ 'player' ].map { |p| WeewarAI::Player.new( p ) }
  @map = WeewarAI::Map.new( self, xml[ 'map' ].to_i )
  @map_url = xml[ 'mapUrl' ]
  @credits_per_base = xml[ 'creditsPerBase' ]
  @initial_credits = xml[ 'initialCredits' ]
  @playing_since = Time.parse( xml[ 'playingSince' ] )
  
  @units = Array.new
  @factions = Array.new
  xml[ 'factions' ][ 'faction' ].each_with_index do |faction_xml,ordinal|
    faction = Faction.new( self, faction_xml, ordinal )
    @factions << faction
    
    faction_xml[ 'unit' ].each do |u|
      hex = @map[ u[ 'x' ], u[ 'y' ] ]
      unit = Unit.new(
        self,
        hex,
        faction,
        u[ 'type' ],
        u[ 'quantity' ].to_i,
        u[ 'finished' ] == 'true',
        u[ 'capturing' ] == 'true'
      )
      @units << unit
      hex.unit = unit
    end
    
    faction_xml[ 'terrain' ].each do |terrain|
      hex = @map[ terrain[ 'x' ], terrain[ 'y' ] ]
      if hex.type == :base
        hex.faction = faction
      end
    end
  end
end

#send(command_xml) ⇒ Object

Sends some command XML for this game to the server. You should generally never need to call this method directly; it is used internally by the Game class.



85
86
87
# File 'lib/weewar-ai/game.rb', line 85

def send( command_xml )
  WeewarAI::API.send "<weewar game='#{@id}'>#{command_xml}</weewar>"
end

#surrenderObject

Surrender in this game.

game.surrender


102
103
104
# File 'lib/weewar-ai/game.rb', line 102

def surrender
  send "<surrender/>"
end