Sha256: 916b933499773e10f8659679ad0ca6ef75b9d97aa67216e31a3b4fc37bb087d3

Contents?: true

Size: 1.12 KB

Versions: 2

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

# Main Gem module
module CodeBrkrGameTraining
  # Module for game stats
  module Statistics
    STATISTICS_FILE_PATH = File.dirname(File.expand_path(__FILE__)) + '/../data/game_stats.yml'

    def save_game_results!(user, difficulty)
      difficulty_init = difficulty.init_values
      difficulty_actual = difficulty.actual_values
      game_session = {
        name: user.name, difficulty: difficulty_init[:name],
        attempts_total: difficulty_init[:attempts], hints_total: difficulty_init[:hints]
      }
      game_session[:attempts_used] = game_session[:attempts_total] - difficulty_actual[:attempts]
      game_session[:hints_used] = game_session[:hints_total] - difficulty_actual[:hints]
      save_to_file(game_session, STATISTICS_FILE_PATH)
    end

    def full_statistics
      games_array = load_from_file(STATISTICS_FILE_PATH)
      return games_array if games_array.empty?

      difficulty_order = DifficultyController.difficulty_levels_order
      games_array.sort_by do |game|
        [difficulty_order.index(game[:difficulty]), game[:attempts_used], game[:hints_used]]
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
code_brkr_game_training-0.7.5 lib/code_brkr_game_training/modules/statistics.rb
code_brkr_game_training-0.7.2 lib/code_brkr_game_training/modules/statistics.rb