Sha256: 879faee58648ebad9ddb7157cd1a2963f44cadc7836d22d85672e9f4e2778380

Contents?: true

Size: 1.06 KB

Versions: 4

Compression:

Stored size: 1.06 KB

Contents

# frozen_string_literal: true

module Codebreaker
  module Storage
    STORAGE_FILE = 'storage.yml'

    def store_game
      saved_games = load_games_history
      saved_games << self
      File.write(STORAGE_FILE, saved_games.to_yaml)
    end

    def statistics_with_raiting
      raiting = 0
      sort_data_for_statistics.map do |row|
        raiting += 1
        Hash[:raiting, raiting].merge!(row)
      end
    end

    def show_statistics
      tp(statistics_with_raiting).class
    end

    private

    def load_games_history
      File.empty?(STORAGE_FILE) ? [] : YAML.load_file(STORAGE_FILE)
    end

    def data_for_statistics
      load_games_history.map do |game|
        { name: game.user.name, level: game.level, attempts_total: game.total_attempts,
          attempts_used: game.total_attempts - game.attempts, hints_total: game.total_hints,
          hints_used: game.total_hints - game.hints }
      end
    end

    def sort_data_for_statistics
      data_for_statistics.sort_by { |row| [row[:level], row[:attempts_used], row[:hints_used]] }
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
codebreaker_al-0.1.2 lib/modules/storage.rb
codebreaker_al-0.1.1 lib/modules/storage.rb
codebreaker_al_rg-0.1.0 lib/modules/storage.rb
codebreaker_al-0.1.0 lib/modules/storage.rb