Sha256: f184aaaebf2af6444a05e744797462249df9f1b86638557f5d432f25509875b9

Contents?: true

Size: 1.07 KB

Versions: 1

Compression:

Stored size: 1.07 KB

Contents

# frozen_string_literal: true

module CodebrackerLogicGame
  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 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

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
codebracker_logic_game-0.1.0 lib/modules/storage.rb