Class: Lekanmastermind::FileHandler
- Inherits:
-
Object
- Object
- Lekanmastermind::FileHandler
- Defined in:
- lib/lekanmastermind/filemanager.rb
Instance Method Summary collapse
- #print_top_scores(level) ⇒ Object
- #top_ten(level) ⇒ Object
- #writer(name, guess, time, chance, level) ⇒ Object
Instance Method Details
#print_top_scores(level) ⇒ Object
12 13 14 15 16 17 18 |
# File 'lib/lekanmastermind/filemanager.rb', line 12 def print_top_scores(level) if top_ten(level) top_ten(level).each_with_index { |player, index| "#{index + 1} #{player}" } else 'No Records yet!!' end end |
#top_ten(level) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/lekanmastermind/filemanager.rb', line 20 def top_ten(level) if File.file?('scores.yaml') scores = YAML.load_stream(File.open('scores.yaml','r')).select do |player| player.level == level end scores.sort do |player1, player2| by_guess = player1.time <=> player2.time by_guess == 0 ? player1.chances <=> player2.chances : by_guess end[0...10] end end |
#writer(name, guess, time, chance, level) ⇒ Object
4 5 6 7 8 9 10 |
# File 'lib/lekanmastermind/filemanager.rb', line 4 def writer(name, guess, time, chance, level) player_result = Lekanmastermind::PlayerResult.new( name, guess, time, chance, level) File.open('scores.yaml', 'a') do |file| file.write(YAML.dump(player_result)) end end |