Class: 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.



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

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.



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

def headers
  @headers
end

Instance Method Details

#add_item(game, username) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/database/statistic.rb', line 22

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



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

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