Class: Typingtutor::Stats
- Inherits:
-
Object
- Object
- Typingtutor::Stats
- Defined in:
- lib/typingtutor/stats.rb
Constant Summary collapse
- FILE =
File.('~/.typingtutor')
Instance Method Summary collapse
-
#initialize ⇒ Stats
constructor
A new instance of Stats.
- #print ⇒ Object
- #print_full ⇒ Object
- #record_exercise(exercise) ⇒ Object
- #record_letter(letter, ok) ⇒ Object
- #record_word(word) ⇒ Object
- #save ⇒ Object
- #worst_letters ⇒ Object
Constructor Details
#initialize ⇒ Stats
Returns a new instance of Stats.
5 6 7 8 9 10 11 12 13 |
# File 'lib/typingtutor/stats.rb', line 5 def initialize @stats = {} @stats.merge!(YAML.load(IO.read(FILE))) if File.exists?(FILE) @stats[:created_at] ||= Time.now @stats[:total] ||= {} @stats[:exercises] ||= {} @stats[:words] ||= {} @stats[:letters] ||= {} end |
Instance Method Details
#print ⇒ Object
56 57 58 59 60 |
# File 'lib/typingtutor/stats.rb', line 56 def print puts "------------------------" puts "Your avg speed: #{@stats[:total][:avg_wpm].round} wpm" puts "Your max speed: #{@stats[:total][:max_wpm].round} wpm" end |
#print_full ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/typingtutor/stats.rb', line 62 def print_full puts "Accuracy per letter:" worst_letters.each {|letter, accuracy| puts "#{letter}: #{accuracy.round}%"} if @stats[:exercises].size > 0 puts puts "Exercises:" @stats[:exercises].each do |name, data| puts "- #{name}: #{data[:runs]} runs, #{data[:last_run_results][:gross_wpm].round} wpm" end end puts puts "Exercises played: #{@stats[:total][:runs] || 0}" puts "Time played: #{(@stats[:total][:time] || 0).round.divmod(60).join('m ')}s" puts "Avg speed: #{(@stats[:total][:avg_wpm] || 0).round} wpm" puts "Max speed: #{(@stats[:total][:max_wpm] || 0).round} wpm" end |
#record_exercise(exercise) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/typingtutor/stats.rb', line 20 def record_exercise(exercise) @stats[:exercises][exercise.name] ||= {} @stats[:exercises][exercise.name][:runs] ||= 0 @stats[:exercises][exercise.name][:runs] += 1 @stats[:exercises][exercise.name][:last_run] = Time.now @stats[:exercises][exercise.name][:last_run_results] = exercise.results # update totals @stats[:total][:runs] ||= 0 @stats[:total][:runs] += 1 [:time, :chars, :correct_chars, :words, :keystrokes].each do |metric| @stats[:total][metric] ||= 0 @stats[:total][metric] += exercise.results[metric] end @stats[:total][:max_wpm] ||= 0 @stats[:total][:max_wpm] = [@stats[:total][:max_wpm], exercise.results[:gross_wpm]].max @stats[:total][:avg_wpm] = @stats[:total][:words] / (@stats[:total][:time] / 60) end |
#record_letter(letter, ok) ⇒ Object
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/typingtutor/stats.rb', line 45 def record_letter(letter, ok) return if letter == ' ' @stats[:letters][letter] ||= {} @stats[:letters][letter][:total] ||= {} @stats[:letters][letter][:total][:count] ||= 0 @stats[:letters][letter][:total][:count] += 1 @stats[:letters][letter][:total][:correct] ||= 0 @stats[:letters][letter][:total][:correct] += 1 if ok @stats[:letters][letter][:total][:accuracy] = (@stats[:letters][letter][:total][:correct].to_f / @stats[:letters][letter][:total][:count].to_f * 100).round end |
#record_word(word) ⇒ Object
41 42 43 |
# File 'lib/typingtutor/stats.rb', line 41 def record_word(word) # TODO end |
#save ⇒ Object
15 16 17 18 |
# File 'lib/typingtutor/stats.rb', line 15 def save @stats[:updated_at] = Time.now IO.write(FILE, YAML.dump(@stats)) end |
#worst_letters ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/typingtutor/stats.rb', line 80 def worst_letters accuracy = @stats[:letters].map {|letter, data| [letter, data[:total][:accuracy]]}.to_h # { "a" => 100, "b" => 98 } accuracy.reject! {|key, value| value == 100} accuracy.reject! {|key, value| key !~ /\w/ } accuracy = accuracy.sort_by {|key, value| value }.to_h accuracy end |