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].freeze
STAT_FILE_PATH =
'statistic.yml'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from YamlFile

load, save

Constructor Details

#initializeStatistic

Returns a new instance of Statistic.



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

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

Instance Attribute Details

#headingsObject (readonly)

Returns the value of attribute headings.



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

def headings
  @headings
end

#statisticObject (readonly)

Returns the value of attribute statistic.



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

def statistic
  @statistic
end

Instance Method Details

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



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

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 }
end

#statistic_getObject



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

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



32
33
34
# File 'lib/codebreaker/statistic.rb', line 32

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