Skip to content

Commit

Permalink
MapParse parses powers and initial state.
Browse files Browse the repository at this point in the history
  • Loading branch information
NamelessOne committed Jun 19, 2013
1 parent 1c71885 commit cc4ab4b
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 11 deletions.
2 changes: 1 addition & 1 deletion features/step_definitions/adjudication_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

Given /^current state "([^"]*)"$/ do |currentstate|
sp = Diplomacy::StateParser.new gamestate
sp.parse_units(currentstate)
sp.parse_units_by_power(currentstate)

adjudicator.map.areas[:Tri].should_not be_nil
end
Expand Down
7 changes: 7 additions & 0 deletions lib/adjudicator/state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ def initialize(owner = nil, unit = nil)
@owner = owner
@unit = unit
end

def to_s
out = ""
out << "#{@owner}"
out << ", #{@unit.type_to_s} (#{@unit.nationality})" if @unit
out
end
end

class GameState < Hash
Expand Down
31 changes: 29 additions & 2 deletions lib/graph/graph.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ class Area
attr_accessor :supply_center
attr_accessor :coasts

def initialize(name, abbrv)
def initialize(name, abbrv, supply_center = false)
@name = name
@abbrv = abbrv
@borders = {LAND_BORDER => [], SEA_BORDER => []}
@coasts = []
@supply_center = supply_center
end

def add_border(area, border_type)
Expand All @@ -37,9 +38,14 @@ def to_s
end

class Map
attr_accessor :areas
attr_accessor :areas, :powers
def initialize
@areas = {}
@powers = {}
end

def add_power(name, starting_areas)
@powers[name] = starting_areas
end

def add_border_by_object(area1, area2, type)
Expand All @@ -54,5 +60,26 @@ def add_border(name1, name2, type)
def neighbours?(area1, area2, type)
@areas[area1].borders[type].member? @areas[area2]
end

def supply_centers
@areas.select {|area| area.is_supply? }
end

def add_supply_center(abbr)
@areas[abbr.to_sym].supply_center = true
end

def to_s
out = ["Areas:"]
@areas.each do |abbrv, area|
out << area.to_s
end
out << "Powers:"
@powers.each do |power, starting_state|
out << "#{power}:"
out << starting_state
end
out.join "\n"
end
end
end
20 changes: 20 additions & 0 deletions lib/graph/map_reader.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
require 'yaml'
require 'graph/graph'
require 'parser/state_parser'

module Diplomacy
class MapReader
attr_accessor :maps

def initialize(map_path = nil)
@logger = Diplomacy.logger
@maps = {}

map_path ||= File.expand_path('../maps/', File.dirname(__FILE__))
Expand Down Expand Up @@ -36,6 +38,24 @@ def read_map_file(yaml_file)
map.add_border(border[0].to_sym, border[1].to_sym, Area::SEA_BORDER)
end
end

yamlmap['SCs'].each do |sc|
map.add_supply_center(sc)
end

yamlmap['Powers'].each do |power, starting_state|
sp = StateParser.new
gamestate = sp.parse_units_of_power starting_state[0], power # the first entry contains the power's units

starting_state[1..-1].each do |area| # the rest are single areas belonging to their state
@logger.debug "Adding #{area} for #{power}"
gamestate[area] = AreaState.new power
end

map.add_power(power, gamestate)
end

@logger.debug "Parsed map #{mapname}: #{map}"

@maps[mapname] = map
end
Expand Down
35 changes: 35 additions & 0 deletions lib/maps/standard.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -341,3 +341,38 @@ Standard:
- Lon
- Liv
- Edi
Powers:
Russia:
- FStP,AMos,FSev,AWar
- Ukr
- Lvn
- Fin
Turkey:
- FAnk,ASmy,ACon
- Syr
- Arm
Austria:
- ABud,AVie,FTri
- Tyr
- Boh
- Gal
Italy:
- FNap,ARom,AVen
- Pie
- Tus
- Apu
France:
- APar,AMar,FBre
- Gas
- Pic
- Bur
England:
- FLon,ALiv,FEdi
- Cly
- Yor
- Wal
Germany:
- ABer,AMun,FKie
- Ruh
- Pru
- Sil
21 changes: 13 additions & 8 deletions lib/parser/state_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,32 @@ def initialize(gamestate = nil)
@gamestate = gamestate || GameState.new
end

def parse_units(unitblob)
def parse_units_by_power(unitblob)
units_by_power = unitblob.split
units_by_power.each do |string|
power, units = string.split(":")
if power and units
unit_array = units.scan(/[AF]\w{3}/)

unit_array.each do |unit|
type, area = parse_single_unit(unit)
@gamestate[area.to_sym] = AreaState.new(nil, Unit.new(power, unit_type(type)))
end
parse_units_of_power(units, power)
end
end
@gamestate
end

def parse_units_of_power(unitblob, power)
unit_array = unitblob.scan(/[AF]\w{3}/)

unit_array.each do |unit|
type, area = parse_single_unit(unit)
@gamestate[area.to_sym] = AreaState.new(power, Unit.new(power, unit_type(type)))
end
@gamestate
end

def parse_single_unit(unitblob)
m = /([AF])(\w{3})/.match(unitblob)
return m[1],m[2]
end

def unit_type(abbrv)
return Diplomacy::Unit::ARMY if abbrv == "A"
return Diplomacy::Unit::FLEET if abbrv == "F"
Expand Down

0 comments on commit cc4ab4b

Please sign in to comment.