Class: AlexCodebreaker::Session

Inherits:
Object
  • Object
show all
Includes:
Modules::ArgumentsValidation
Defined in:
lib/alex_codebreaker/session.rb

Constant Summary collapse

INITIAL_ATTEMPTS_USED =
0
INITIAL_HINTS_USED =
0
WINNERS_FILE_NAME =
'/winners.yml'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Modules::ArgumentsValidation

#guess_validation, #name_validation

Methods included from Modules::Validators

#argument_length_check, #argument_max_length_check, #argument_min_length_check, #digits_check

Constructor Details

#initializeSession

Returns a new instance of Session.



12
13
14
15
16
# File 'lib/alex_codebreaker/session.rb', line 12

def initialize
  @attempts_used = INITIAL_ATTEMPTS_USED
  @hints_used = INITIAL_HINTS_USED
  @time = Time.new
end

Instance Attribute Details

#attempts_totalObject (readonly)

Returns the value of attribute attempts_total.



9
10
11
# File 'lib/alex_codebreaker/session.rb', line 9

def attempts_total
  @attempts_total
end

#attempts_usedObject (readonly)

Returns the value of attribute attempts_used.



9
10
11
# File 'lib/alex_codebreaker/session.rb', line 9

def attempts_used
  @attempts_used
end

#difficulty_levelObject (readonly)

Returns the value of attribute difficulty_level.



9
10
11
# File 'lib/alex_codebreaker/session.rb', line 9

def difficulty_level
  @difficulty_level
end

#difficulty_nameObject (readonly)

Returns the value of attribute difficulty_name.



9
10
11
# File 'lib/alex_codebreaker/session.rb', line 9

def difficulty_name
  @difficulty_name
end

#hints_totalObject (readonly)

Returns the value of attribute hints_total.



9
10
11
# File 'lib/alex_codebreaker/session.rb', line 9

def hints_total
  @hints_total
end

#hints_usedObject (readonly)

Returns the value of attribute hints_used.



9
10
11
# File 'lib/alex_codebreaker/session.rb', line 9

def hints_used
  @hints_used
end

#player_nameObject (readonly)

Returns the value of attribute player_name.



9
10
11
# File 'lib/alex_codebreaker/session.rb', line 9

def player_name
  @player_name
end

#timeObject (readonly)

Returns the value of attribute time.



9
10
11
# File 'lib/alex_codebreaker/session.rb', line 9

def time
  @time
end

Instance Method Details

#add_difficulty(difficulty) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/alex_codebreaker/session.rb', line 22

def add_difficulty(difficulty)
  difficulty_level = AlexCodebreaker::Modules::DifficultyLevels::DIFFICULTY_LEVELS[difficulty.downcase.to_sym]
  return unless difficulty_level

  @difficulty_name = difficulty_level[:name]
  @difficulty_level = difficulty_level[:level]
  @attempts_total = difficulty_level[:attempts_total]
  @hints_total = difficulty_level[:hints_total]
end

#add_name(given_name) ⇒ Object



18
19
20
# File 'lib/alex_codebreaker/session.rb', line 18

def add_name(given_name)
  @player_name = given_name if name_validation(given_name)
end

#check_attemptsObject



36
37
38
39
40
41
# File 'lib/alex_codebreaker/session.rb', line 36

def check_attempts
  @attempts_used += 1
  return if @attempts_used >= @attempts_total

  true
end

#check_hintsObject



32
33
34
# File 'lib/alex_codebreaker/session.rb', line 32

def check_hints
  @hints_used += 1 if @hints_used < @hints_total
end

#save_winner_statisticObject



43
44
45
46
47
48
# File 'lib/alex_codebreaker/session.rb', line 43

def save_winner_statistic
  @time = Time.new
  check_folder_existence
  path = "#{AlexCodebreaker.configuration.winners_folder_path}#{WINNERS_FILE_NAME}"
  File.open(path, 'a') { |file| file.write(to_yaml) }
end