Class: D3MPQ::Analyzer

Inherits:
Object
  • Object
show all
Defined in:
lib/d3_mpq/analyzer.rb

Constant Summary collapse

SEPERATOR =
","

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parser, files, fields = :content) ⇒ Analyzer

Returns a new instance of Analyzer.



9
10
11
12
13
14
# File 'lib/d3_mpq/analyzer.rb', line 9

def initialize(parser, files, fields = :content)
  @parser     = parser.new
  @files      = [*files]
  @field      = fields
#      @fields     = [*fields]
end

Instance Attribute Details

#attributesObject (readonly)

Return analyzed attributes



109
110
111
# File 'lib/d3_mpq/analyzer.rb', line 109

def attributes
  @attributes
end

Instance Method Details

#parser_nameObject

Example: d3mpq_stringlist



58
59
60
# File 'lib/d3_mpq/analyzer.rb', line 58

def parser_name
  @parser_name ||= @parser.class.name.gsub("::", "_").downcase
end

#writeObject

Just write analyzed data to disc. (within /analyze)



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/d3_mpq/analyzer.rb', line 17

def write
  case parser_name
  when "d3mpq_stringlist"
    write_stringlist
  when "d3mpq_recipe"
    write_recipe
  when "d3mpq_coredata_gamebalance_setitembonuses"
    @field = nil
    write_recipe
  when "d3mpq_attributes"
    write_single_file("analyze")
  when "d3mpq_coredata_actor"
    write_single_file("analyze")
  else
    write_game_balance
  end
end

#write_analyzed(dir) ⇒ Object

Writing multiple files to given dir.



98
99
100
101
102
103
104
105
106
# File 'lib/d3_mpq/analyzer.rb', line 98

def write_analyzed(dir)
  FileUtils.mkdir_p(dir)

  attributes.each do |a, v|
    path = File.join(dir, a.to_s)
    s = "Count|Value\n" + v.map { |e| "#{e[:count]}|#{e[:value]}" }.join("\n")
    File.open("#{path}.csv", 'w') { |f| f.write(s) }
  end
end

#write_game_balanceObject

Writing if GameBalance



49
50
51
52
53
54
55
# File 'lib/d3_mpq/analyzer.rb', line 49

def write_game_balance
  write_single_file("analyze")

  dir = File.join("analyze", parser_name)
  dir = File.join(dir, @field.to_s) if @field
  write_analyzed(dir)
end

#write_recipeObject

Writing if StringList



42
43
44
45
46
# File 'lib/d3_mpq/analyzer.rb', line 42

def write_recipe
  @field = nil
  dir = File.join("analyze", "Recipe")
  write_single_file(dir, @files.first.split("/").last)
end

#write_single_file(dir, filename = nil) ⇒ Object

Write output to a single file. (dir/filename) Using parser as filename if none is given.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/d3_mpq/analyzer.rb', line 64

def write_single_file(dir, filename = nil)
  FileUtils.mkdir_p File.join(dir)

  # HACKY: NEED TO FIX
  keys = snapshots.first.keys
  keys = snapshots.first[@field].first.keys if @field

  s = []
  s << keys.join(SEPERATOR)
  snapshots.each do |snapshot|
    # HACKY: NEED TO FIX
    if @field
      snapshot[@field].each_with_index do |e, i|
        s << e.values.map do |v|
          if v.is_a?(String)
            "\"#{v}\""
          else
            "#{v}"
          end
        end.join(SEPERATOR)
      end
    else
      s << [*snapshot.values].map { |e|
        e.is_a?(String) ? "\"#{e}\""  : "#{e}"
      } .join(SEPERATOR)
    end
  end

  filename ||= @parser.class.name.split("::").last
  path = File.join(dir, filename)
  File.open("#{path}.csv", 'w') { |f| f.write(s.join("\n")) }
end

#write_stringlistObject

Writing if StringList



36
37
38
39
# File 'lib/d3_mpq/analyzer.rb', line 36

def write_stringlist
  dir = File.join("analyze", "StringList")
  write_single_file(dir, @files.first.split("/").last)
end