Sha256: 06edcc80873dc6bdf19f2ac11fc7f6432ede4f38933e3f1e30239a0e7ceee506

Contents?: true

Size: 851 Bytes

Versions: 4

Compression:

Stored size: 851 Bytes

Contents

module Database
  class Statistics
    DB_FILE = 'seed.yaml'.freeze

    class << self
      def save(game_result)
        @stats = open
        @stats << game_result
        File.write(DB_FILE, @stats.to_yaml)
      end

      def open
        File.exist?(DB_FILE) ? load : []
      end

      def load
        Psych.safe_load(
          File.read(DB_FILE),
          [Symbol, Difficult],
          [],
          true
        )
      end

      def sort(stats)
        stats
          .sort_by { |stat| [stat[:difficult].to_i, stat[:attempts_used], stat[:hints_used]] }
          .each.with_index { |stat, index| stat[:rating] = index + 1 }
      end

      def show
        @stats = open
        return Output::Messages.stats_error if @stats.empty?

        sort(@stats).each { |stat| Output::Messages.show_stats(stat) }
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
codebreaker_dmitriev-0.2.2 lib/console_app/database.rb
codebreaker_dmitriev-0.2.1 lib/console_app/database.rb
codebreaker_dmitriev-0.2.0 lib/console_app/database.rb
codebreaker_dmitriev-0.1.0 lib/console_app/database.rb