Class: Codebreaker::Statistic

Inherits:
Object
  • Object
show all
Includes:
YamlFile
Defined in:
lib/codebreaker/statistic.rb

Constant Summary collapse

STAT_HEADER_LIST =
%w[rating name difficulty attempts_total attempts_used hints_total hints_used date_saved].freeze
STAT_FILE_PATH =
'statistic.yml'.freeze
DATETIME_FORMAT =
'%Y.%m.%d - %H:%M:%S'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from YamlFile

load, save

Constructor Details

#initializeStatistic

Returns a new instance of Statistic.



10
11
12
13
# File 'lib/codebreaker/statistic.rb', line 10

def initialize
  @headings = STAT_HEADER_LIST
  @statistic_items = []
end

Instance Attribute Details

#headingsObject (readonly)

Returns the value of attribute headings.



9
10
11
# File 'lib/codebreaker/statistic.rb', line 9

def headings
  @headings
end

#statisticObject (readonly)

Returns the value of attribute statistic.



9
10
11
# File 'lib/codebreaker/statistic.rb', line 9

def statistic
  @statistic
end

#statistic_itemsObject (readonly)

Returns the value of attribute statistic_items.



9
10
11
# File 'lib/codebreaker/statistic.rb', line 9

def statistic_items
  @statistic_items
end

Instance Method Details

#statistic_add_item(name:, difficulty:, game_stage:) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/codebreaker/statistic.rb', line 24

def statistic_add_item(name:, difficulty:, game_stage:)
  @statistic_items << { name: name,
                        difficulty: difficulty.name,
                        attempts_total: game_stage.attempts,
                        attempts_used: game_stage.step_number,
                        hints_total: difficulty.hints,
                        hints_used: game_stage.hint_used,
                        date_saved: Time.now.strftime(DATETIME_FORMAT) }
end

#statistic_getObject



15
16
17
18
19
20
21
22
# File 'lib/codebreaker/statistic.rb', line 15

def statistic_get
  statistic_load
  statistic_sort
  @statistic_items.each_with_index.map do |stat, index|
    [index.next, stat[:name], stat[:difficulty], stat[:attempts_total],
     stat[:attempts_used], stat[:hints_total], stat[:hints_used]]
  end
end

#statistic_saveObject



34
35
36
# File 'lib/codebreaker/statistic.rb', line 34

def statistic_save
  YamlFile.save(STAT_FILE_PATH, @statistic_items)
end