Sha256: 44f25a7124f398a5abdaa25508d105d5c8b400417a49812e012df80b87773d65

Contents?: true

Size: 1.24 KB

Versions: 2

Compression:

Stored size: 1.24 KB

Contents

# frozen_string_literal: true

class Statistic
  include DataHandler

  attr_reader :headers

  def initialize(db = __dir__ + '/storage.yml')
    @db = db
    @headers = %w[ratign name difficulty attempts_total attempts_used hints_total hints_used]
    @items = load || []
  end

  def statistic
    sort
    @items.each_with_index.map do |item, index|
      [index.next, item[:name], item[:difficulty], item[:attempts_total],
       item[:attempts_used], item[:hints_total], item[:hints_used]]
    end
  end

  def add_item(game, username)
    @items << { name: username,
                difficulty: game.current_difficulty[:name],
                difficulty_id: game.current_difficulty[:id],
                attempts_total: game.current_difficulty[:attempts],
                attempts_used: game.attempts_counter,
                hints_total: game.current_difficulty[:hints],
                hints_used: hints_used(game) }
    save
  end

  private

  def hints_used(game)
    game.current_difficulty[:hints] - game.hinted_numbers.size
  end

  def load
    DataHandler.load(@db)
  end

  def save
    DataHandler.save(@db, @items)
  end

  def sort
    @items.sort_by! do |item|
      [-item[:difficulty_id], item[:attempts_used], item[:hints_used]]
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
codebreaker_gapdn-0.1.1 lib/database/statistic.rb
codebreaker_gapdn-0.1.0 lib/database/statistic.rb