Class: Report

Inherits:
Section show all
Includes:
Gamedata
Defined in:
lib/galaxy/report.rb

Overview

Describes Galaxy Plus Report (as received from server) data structures and provides ‘parse’ method for extracting data it. Initially, Report “contains” extracted data (through included module Gamedata), but later on these data containers should be moved to new Game class

Constant Summary

Constants included from Regexen

Regexen::Battle_groups_header, Regexen::Battle_groups_record, Regexen::Battle_planets_header, Regexen::Battle_planets_record, Regexen::Bombings_header, Regexen::Bombings_record, Regexen::Default_footer, Regexen::Default_header_proc, Regexen::Fint, Regexen::Fleets_header, Regexen::Fleets_record, Regexen::Fname, Regexen::Fnum, Regexen::Group, Regexen::Groups_header, Regexen::Groups_record, Regexen::Header, Regexen::Incoming_groups_header, Regexen::Incoming_groups_record, Regexen::Int, Regexen::Line, Regexen::Maps_header, Regexen::Name, Regexen::Num, Regexen::Planets_header, Regexen::Planets_record, Regexen::Production_planets_header, Regexen::Production_planets_record, Regexen::Races_header, Regexen::Races_record, Regexen::Reports_header, Regexen::Routes_header, Regexen::Routes_record, Regexen::Scargo, Regexen::Science_products_header, Regexen::Science_products_record, Regexen::Ship_products_header, Regexen::Ship_products_record, Regexen::Sint, Regexen::Sname, Regexen::Snum, Regexen::Sstatus, Regexen::Unidentified_groups_header, Regexen::Unidentified_groups_record, Regexen::Unidentified_planets_header, Regexen::Unidentified_planets_record, Regexen::Uninhabited_planets_header, Regexen::Uninhabited_planets_record, Regexen::Your_groups_header, Regexen::Your_groups_record, Regexen::Your_planets_header, Regexen::Your_planets_record

Instance Attribute Summary

Attributes included from Gamedata

#battles, #bombings, #fleets, #game, #groups, #order, #owner, #planets, #products, #races, #routes, #server, #time, #turn

Attributes inherited from Section

#footer, #footer_proc, #header, #header_proc, #mult, #name, #record, #record_proc, #sections, #skip, #text

Instance Method Summary collapse

Methods included from Gamedata

#battle_groups, #incoming_groups, #sciences, #ships, #unidentified_groups, #unidentified_planets, #uninhabited_planets, #your_active_groups, #your_groups, #your_planets

Methods inherited from Section

#copy, #find_text, #parse, #scan_text

Constructor Details

#initialize(*args) ⇒ Report

Describes G+ Report data structure, opens report file if given a valid file name



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/galaxy/report.rb', line 104

def initialize (*args)
  
  # Define Proc for Report Header processing TODO 
  report_proc = lambda do |match, state|
    @owner = state[:owner] = match[1]
    @game = state[:game] = match[2]
    @turn = state[:turn] = match[3].to_i
    @time = state[:time] = match[4]
    @server = state[:server] = match[5]
  end
  
  # Define G+ Report Sections
  @sections = [ 
  :races,
  {:name=>:science_products, :mult=>true}, 
  {:name=>:ship_products, :mult=>true}, 
  {:name=>:battle_planets, :footer => 'Battle Protocol', 
    :sections => [Section.new(:name=>:battle_groups, :mult=>true)], :mult=>true },
  :bombings, 
  {:header => 'Maps_header', :skip=>true},
  :incoming_groups,
  :your_planets,
  :production_planets, 
  :routes,
  {:name=>:planets, :mult=>true},
  :uninhabited_planets,
  :unidentified_planets,
  :fleets,
  :your_groups,
  {:name=>:groups, :mult=>true},  
  :unidentified_groups
  ].map do |init| Section.new init end
  
  # Initialize main Section and data Collections (from module Gamedata)
  super :name=>:reports, :header_proc=>report_proc, :sections => @sections
  
  # Checking arguments
  case args.size 
    when 0 then # Do nothing
    when 1 then open *args
  else  # Wrong number of arguments, initializer failed
    puts "Usage: Report.new or Report.new(file_name)"
    raise ArgumentError, "Wrong number of arguments in Report initialization"
  end
end

Instance Method Details

#open(file_name) ⇒ Object

Validates file name and reads everything from file into Report’s @text property



151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/galaxy/report.rb', line 151

def open file_name
  if file_name =~ /\A[[:punct:]\w\d]+.rep\z/ and File.exists? file_name
    # This is a valid and existing rep file name
  elsif file_name += '.rep' and file_name =~ /\A[[:punct:]\w\d]+.rep\z/ and File.exists? file_name
    # This is a valid and existing rep file (without rep suffix)
  else
    raise ArgumentError, "Can't open: Invalid Report file #{file_name}"
  end
  # puts "Initializing Report from rep file: " + file_name
  # Open file and pass file stream into block to read from it into Report's @text property
  # File closing is automatic upon execution of the block
  File.open(file_name, "r") do |f| @text = f.read end 
end

#statusObject

Method returns status string of the Report



166
167
168
169
170
171
172
173
174
# File 'lib/galaxy/report.rb', line 166

def status
  return "
Report: #@owner #@game #@turn #@time #@server 
Races: #{@races.size} Sciences: #{sciences.size} Types: #{designs.size} BattleGroups: #{battle_groups.size} \
Bombings: #{@bombings.size} Incomings: #{incoming_groups.size} Your Planets: #{your_planets.size} \
Ships in Production: #{@productions} Routes: #{@routes.size}
Planets: #{@planets.size} Uninhabited Planets: #{uninhabited_planets.size} Unidentified Planets: #{unidentified_planets.size} \
Fleets: #{@fleets.size} Your Groups: #{your_groups.size} Groups: #{@groups.size} Unidentified Groups: #{unidentified_groups.size}"  
end