Module: CodeBrkrGameTraining::Statistics

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

Overview

Module for game stats

Constant Summary collapse

SAVE_DIRECTORY =

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

'data'
SAVE_FILE =
'game_stats.yml'

Instance Method Summary collapse

Instance Method Details

#full_statisticsObject



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

def full_statistics
  games_array = load_from_file(SAVE_DIRECTORY, SAVE_FILE)
  return games_array if games_array.empty?

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

#save_game_results!(user, difficulty) ⇒ Object



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

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(data: game_session, directory: SAVE_DIRECTORY, file: SAVE_FILE)
end