lib/alex_codebreaker/session.rb in alex_codebreaker-0.1.6 vs lib/alex_codebreaker/session.rb in alex_codebreaker-0.1.7
- old
+ new
@@ -1,34 +1,47 @@
-class Session
- INITIAL_ATTEMPTS_USED = 0
- INITIAL_HINTS_USED = 0
+module AlexCodebreaker
+ class Session
+ include AlexCodebreaker::Modules::ArgumentsValidation
- attr_accessor :player_name, :attempts_used, :hints_used, :hints_total,
+ INITIAL_ATTEMPTS_USED = 0
+ INITIAL_HINTS_USED = 0
+
+ attr_reader :hints_used, :attempts_used, :player_name, :hints_total,
:difficulty_name, :attempts_total, :difficulty_level
- def initialize
- @attempts_used = INITIAL_ATTEMPTS_USED
- @hints_used = INITIAL_HINTS_USED
- end
+ def initialize
+ @attempts_used = INITIAL_ATTEMPTS_USED
+ @hints_used = INITIAL_HINTS_USED
+ end
- def difficulty_manager(difficulty)
- if DifficultyLevels::DIFFICULTY_LEVELS.include?(difficulty)
- add_difficulty(DifficultyLevels::DIFFICULTY_LEVELS[difficulty])
+ def add_name(given_name)
+ @player_name = given_name if name_validation(given_name)
end
- return false unless DifficultyLevels::DIFFICULTY_LEVELS.include?(difficulty)
- end
- def save_session_statistic
- File.open(Files::STATS_FILE, 'a') do |file|
- file.write(to_yaml)
+ def add_difficulty(difficulty)
+ return unless AlexCodebreaker::Modules::DifficultyLevels::DIFFICULTY_LEVELS.include?(difficulty.downcase.to_sym)
+
+ difficulty(AlexCodebreaker::Modules::DifficultyLevels::DIFFICULTY_LEVELS[difficulty.downcase.to_sym])
end
- end
- private
+ def check_hints
+ @hints_used += 1 if @hints_used < @hints_total
+ end
- def add_difficulty(difficulty)
- @difficulty_name = difficulty[:name]
- @difficulty_level = difficulty[:level]
- @attempts_total = difficulty[:attempts_total]
- @hints_total = difficulty[:hints_total]
+ def check_attempts
+ @attempts_used += 1 if @attempts_used < @attempts_total - 1
+ end
+
+ def save_statistic
+ File.open(AlexCodebreaker::Modules::Files::STATS_FILE, 'a') { |file| file.write(to_yaml) }
+ end
+
+ private
+
+ def difficulty(difficulty)
+ @difficulty_name = difficulty[:name]
+ @difficulty_level = difficulty[:level]
+ @attempts_total = difficulty[:attempts_total]
+ @hints_total = difficulty[:hints_total]
+ end
end
end