Sha256: 9f5cf5f4b250e88809975c6ba08b61298ffa9c9ff3725c4c18e793f202501c6b

Contents?: true

Size: 1.62 KB

Versions: 2

Compression:

Stored size: 1.62 KB

Contents

module AlexCodebreaker
  class Session
    include AlexCodebreaker::Modules::ArgumentsValidation

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

    attr_reader :hints_used, :attempts_used, :player_name, :hints_total,
                :difficulty_name, :attempts_total, :difficulty_level, :time

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

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

    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

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

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

      true
    end

    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

    private

    def check_folder_existence
      return if File.directory?(AlexCodebreaker.configuration.winners_folder_path)

      FileUtils.mkdir_p(AlexCodebreaker.configuration.winners_folder_path)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
alex_codebreaker-0.2.1 lib/alex_codebreaker/session.rb
alex_codebreaker-0.2.0 lib/alex_codebreaker/session.rb