Class: Codebreaker::Statistic

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

Instance Method Summary collapse

Instance Method Details

#collect_statistic(hash) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/codebreaker/statistic.rb', line 5

def collect_statistic(hash)
  @stat = [{
    user_name: hash[:user_name],
    difficulty: hash[:difficulty],
    attempts_total: hash[:attempts_total],
    attempts_used: hash[:attempts_used],
    hints_total: hash[:hints_total],
    hints_used: hash[:hints_used]
  }]
  save_statistic
end

#save_statisticObject



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

def save_statistic
  yaml_data = YAML.load_file(Constants::PATH)
  yaml_data = [] if yaml_data.nil?
  yaml_data << @stat
  File.open(Constants::PATH, 'r+') { |file| file.write YAML.dump(yaml_data.flatten) }
end

#show_statisticObject



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

def show_statistic
  @items = YAML.load_file(Constants::PATH)
  @items.sort_by! { |game| [game[:difficulty], game[:attempts_used], game[:hints_used]] }
  @items.each_with_index.map do |stat, index|
    [index.next, stat[:user_name], stat[:difficulty], stat[:attempts_total],
     stat[:attempts_used], stat[:hints_total], stat[:hints_used]]
  end
end