Class: Codebreaker::Statistic

Inherits:
Object
  • Object
show all
Includes:
DataHandler
Defined in:
lib/database/statistic.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DataHandler

load, save

Constructor Details

#initialize(db = __dir__ + '/storage.yml') ⇒ Statistic

Returns a new instance of Statistic.



9
10
11
12
13
# File 'lib/database/statistic.rb', line 9

def initialize(db = __dir__ + '/storage.yml')
  @db = db
  @headers = %w[ratign name difficulty attempts_total attempts_used hints_total hints_used]
  @items = load || []
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



7
8
9
# File 'lib/database/statistic.rb', line 7

def headers
  @headers
end

Instance Method Details

#add_item(game, username) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/database/statistic.rb', line 25

def add_item(game, username)
  @items << { name: username,
              difficulty: game.current_difficulty[:name],
              difficulty_id: game.current_difficulty[:id],
              attempts_total: game.current_difficulty[:attempts],
              attempts_used: game.attempts_counter,
              hints_total: game.current_difficulty[:hints],
              hints_used: hints_used(game) }
  save
end

#statisticObject



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

def statistic
  sort
  @items.each_with_index.map do |item, index|
    [
      index.next, item[:name], item[:difficulty], item[:attempts_total],
      item[:attempts_used], item[:hints_total], item[:hints_used]
    ]
  end
end