module CodebreakerAp::Statistic

Constants

STATISTIC_FILE

Public Instance Methods

load_statistic() click to toggle source
# File lib/codebreaker_ap/modules/statistic.rb, line 8
def load_statistic
  data = YAML.load_stream(File.open(STATISTIC_FILE, 'a+'))
  return unless data

  data.sort_by { |players| [-players[:difficulty].length, players[:used_attempts], players[:used_hints]] }
end
save_stats(player_name, difficulty) click to toggle source
# File lib/codebreaker_ap/modules/statistic.rb, line 4
def save_stats(player_name, difficulty)
  save_to_file(create_statistic_data(player_name, difficulty), STATISTIC_FILE)
end
show_stats() click to toggle source
# File lib/codebreaker_ap/modules/statistic.rb, line 15
def show_stats
  load_statistic.each_with_index do |value, index|
    puts statistic(value, index + 1)
  end
end

Private Instance Methods

create_statistic_data(player_name, difficulty) click to toggle source
# File lib/codebreaker_ap/modules/statistic.rb, line 23
def create_statistic_data(player_name, difficulty)
  difficulty_total = Difficulty::DIFFICULTY[difficulty.level.to_sym]
  {
    player: player_name,
    difficulty: difficulty.level,
    total_attempts: difficulty_total[:attempts],
    total_hints: difficulty_total[:hints],
    used_attempts: difficulty_total[:attempts] - difficulty.attempts,
    used_hints: difficulty_total[:hints] - difficulty.hints,
    date: Time.now.strftime('%d/%m/%Y %H:%M')
  }
end
statistic(value, index) click to toggle source
# File lib/codebreaker_ap/modules/statistic.rb, line 36
def statistic(value, index)
  "Rating: ##{index}\n"\
  "Player name: #{value[:player]}\n"\
  "Difficulty: #{value[:difficulty]}\n"\
  "Total attempts: #{value[:total_attempts]}\n"\
  "Total hints: #{value[:total_hints]}\n"\
  "Used attempts: #{value[:used_attempts]}\n"\
  "Used hints: #{value[:used_hints]}\n"\
  "Date: #{value[:date]}\n\n"
end