Class: Database::Statistics

Inherits:
Object
  • Object
show all
Defined in:
lib/console_app/database.rb

Constant Summary collapse

DB_FILE =
'seed.yaml'.freeze

Class Method Summary collapse

Class Method Details

.loadObject



16
17
18
19
20
21
22
23
# File 'lib/console_app/database.rb', line 16

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

.openObject



12
13
14
# File 'lib/console_app/database.rb', line 12

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

.save(game_result) ⇒ Object



6
7
8
9
10
# File 'lib/console_app/database.rb', line 6

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

.showObject



31
32
33
34
35
36
# File 'lib/console_app/database.rb', line 31

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

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

.sort(stats) ⇒ Object



25
26
27
28
29
# File 'lib/console_app/database.rb', line 25

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