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
16
17
# File 'lib/codebreaker/statistic.rb', line 5

def collect_statistic(hash)
  game_date = Time.new.strftime '%Y/%m/%d'
  @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],
    date: game_date
  }]
  save_statistic
end

#save_statisticObject



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

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



19
20
21
22
23
24
25
26
27
28
# File 'lib/codebreaker/statistic.rb', line 19

def show_statistic
  @items = YAML.load_file(Constants::PATH)
  return if @items.nil?

  @items.sort_by! { |game| [game[:difficulty].reverse, 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], stat[:date]]
  end
end