Class: Codebreaker::Statistic

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

Constant Summary collapse

DATE_FORMAT =
'%Y.%m.%d - %T'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DataHandler

load, save

Constructor Details

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



11
12
13
14
15
# File 'lib/database/statistic.rb', line 11

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.



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

def headers
  @headers
end

#itemsObject (readonly)

Returns the value of attribute items.



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

def items
  @items
end

Instance Method Details

#add_item(game, username, date_format: DATE_FORMAT) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/database/statistic.rb', line 27

def add_item(game, username, date_format: DATE_FORMAT)
  @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),
              time: Time.now.strftime(date_format) }
  save
end

#statisticObject



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

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