lib/codebreaker_first.rb in codebreaker_first-0.2.0 vs lib/codebreaker_first.rb in codebreaker_first-0.3.0

- old
+ new

@@ -43,12 +43,14 @@ def loose @game_result = false end - def save_results(name) - Result.save name + def save_result(name) + raise Exception, 'To save, you must finish the game!' if @game_result.nil? + + Result.save name, @user_tries, @game_result end def results Result.results end @@ -66,19 +68,17 @@ # Rubocop class Result SCORES_FILE = File.expand_path('scores.json', __dir__) - def self.save(name) - raise Exception, 'To save, you must finish the game!' if @game_result.nil? + def self.save(name, tries, result) + scores = results.to_a - results = results.to_a + scores << { name: name, tries: tries, result: result } - results << { name: name, tries: @user_tries, result: @game_result } + File.write SCORES_FILE, scores.to_json - File.write SCORES_FILE, results.to_json - - results + scores end def self.results file = File.read SCORES_FILE