Module: CodeBrkrGameTraining::Statistics

Included in:
Game
Defined in:
lib/code_brkr_game_training/modules/statistics.rb

Overview

Module for game stats

Constant Summary collapse

STATISTICS_FILE_PATH =
File.dirname(File.expand_path(__FILE__)) + '/../data/game_stats.yml'

Instance Method Summary collapse

Instance Method Details

#full_statisticsObject



21
22
23
24
25
26
27
28
29
# File 'lib/code_brkr_game_training/modules/statistics.rb', line 21

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

#save_game_results!(user, difficulty) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/code_brkr_game_training/modules/statistics.rb', line 9

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