Sha256: b4d86414f4c9123d20f2ebcfce458e1de0ffa0aeac6b91eedd74a02404a32593

Contents?: true

Size: 1007 Bytes

Versions: 2

Compression:

Stored size: 1007 Bytes

Contents

# frozen_string_literal: true

require 'yaml'
require_relative 'user'

class Stats
  def self.save_game(user)
    @data = File.exist?('stats.yml') ? YAML.load_file('stats.yml') : []
    @data << user
    File.new('stats.yml', 'w') unless File.exist?('stats.yml')
    File.open('stats.yml', 'w') { |file| file.write(@data.to_yaml) }
  end

  def self.show_stats
    @data = File.exist?('stats.yml') ? YAML.load_file('stats.yml') : []
    @data.sort_by! { |user| [user.attempts[:all], user.attempts[:used], user.hints[:used]] }
    @data.each_with_index do |user, index|
      stats_format(user, index)
    end
  end

  def self.stats_format(user, index)
    index += 1
    puts "Rating: #{index}", "Name: #{user.name}",
         "Difficulty: #{user.difficulty}",
         "Available Attempts: #{user.attempts[:all]}",
         "Used Attempts: #{user.attempts[:used]}",
         "Available Hints: #{user.hints[:all]}",
         "Used Hints: #{user.hints[:used]}",
         '-----------------------'
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
codebreaker_kirill-1.1.1 lib/codebreaker_kirill/stats.rb
codebreaker_kirill-1.1.0 lib/codebreaker_kirill/stats.rb