Class: WeewarAI::Game
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
-
#credits_per_base ⇒ Object
(also: #creditsPerBase)
readonly
Returns the value of attribute credits_per_base.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#initial_credits ⇒ Object
(also: #initialCredits)
readonly
Returns the value of attribute initial_credits.
-
#last_attacked ⇒ Object
Returns the value of attribute last_attacked.
-
#map ⇒ Object
readonly
Returns the value of attribute map.
-
#map_url ⇒ Object
(also: #mapUrl)
readonly
Returns the value of attribute map_url.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#pace ⇒ Object
readonly
Returns the value of attribute pace.
-
#pending_invites ⇒ Object
(also: #pendingInvites)
readonly
Returns the value of attribute pending_invites.
-
#players ⇒ Object
readonly
Returns the value of attribute players.
-
#playing_since ⇒ Object
(also: #playingSince)
readonly
Returns the value of attribute playing_since.
-
#round ⇒ Object
readonly
Returns the value of attribute round.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#units ⇒ Object
readonly
Returns the value of attribute units.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
-
#abandon ⇒ Object
Abandon this game.
-
#bases ⇒ Object
An Array of the base Hexes for this game.
-
#bases_of(faction) ⇒ Object
An Array of the base Hexes owned by the given faction.
-
#current_player ⇒ Object
The Player whose turn it is.
-
#enemy_bases ⇒ Object
An Array of bases not owned by your AI (including neutral bases).
-
#enemy_units ⇒ Object
An Array of the Units not belonging to your AI.
-
#faction_for_player(player_name) ⇒ Object
The Faction of the given player.
-
#finish_turn ⇒ Object
(also: #finishTurn)
End turn in this game.
-
#initialize(id) ⇒ Game
constructor
Instantiate a new Game instance corresponding to the weewar game with the given id number.
-
#my_bases ⇒ Object
Your AI’s bases in this game.
-
#my_faction ⇒ Object
Your AI’s Faction in this game.
-
#refresh ⇒ Object
Hits the weewar server for all the game state data as it sees it.
-
#send(command_xml) ⇒ Object
Sends some command XML for this game to the server.
-
#surrender ⇒ Object
Surrender in this game.
Constructor Details
Instance Attribute Details
#credits_per_base ⇒ Object (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 |
#id ⇒ Object (readonly)
Returns the value of attribute id.
11 12 13 |
# File 'lib/weewar-ai/game.rb', line 11 def id @id end |
#initial_credits ⇒ Object (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_attacked ⇒ Object
Returns the value of attribute last_attacked.
14 15 16 |
# File 'lib/weewar-ai/game.rb', line 14 def last_attacked @last_attacked end |
#map ⇒ Object (readonly)
Returns the value of attribute map.
11 12 13 |
# File 'lib/weewar-ai/game.rb', line 11 def map @map end |
#map_url ⇒ Object (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 |
#name ⇒ Object (readonly)
Returns the value of attribute name.
11 12 13 |
# File 'lib/weewar-ai/game.rb', line 11 def name @name end |
#pace ⇒ Object (readonly)
Returns the value of attribute pace.
11 12 13 |
# File 'lib/weewar-ai/game.rb', line 11 def pace @pace end |
#pending_invites ⇒ Object (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 |
#players ⇒ Object (readonly)
Returns the value of attribute players.
11 12 13 |
# File 'lib/weewar-ai/game.rb', line 11 def players @players end |
#playing_since ⇒ Object (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 end |
#round ⇒ Object (readonly)
Returns the value of attribute round.
11 12 13 |
# File 'lib/weewar-ai/game.rb', line 11 def round @round end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
11 12 13 |
# File 'lib/weewar-ai/game.rb', line 11 def state @state end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
11 12 13 |
# File 'lib/weewar-ai/game.rb', line 11 def type @type end |
#units ⇒ Object (readonly)
Returns the value of attribute units.
11 12 13 |
# File 'lib/weewar-ai/game.rb', line 11 def units @units end |
#url ⇒ Object (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
#abandon ⇒ Object
Abandon this game.
game.abandon
108 109 110 |
# File 'lib/weewar-ai/game.rb', line 108 def abandon send "<abandon/>" end |
#bases ⇒ Object
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_player ⇒ Object
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_bases ⇒ Object
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_units ⇒ Object
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_turn ⇒ Object 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_bases ⇒ Object
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_faction ⇒ Object
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 |
#refresh ⇒ Object
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 |
#surrender ⇒ Object
Surrender in this game.
game.surrender
102 103 104 |
# File 'lib/weewar-ai/game.rb', line 102 def surrender send "<surrender/>" end |