# frozen_string_literal: true module Codebreaker class Statistic def collect_statistic(hash) @stat = [{ user_name: hash[:user_name], difficulty: hash[:difficulty], attempts_total: hash[:attempts_total], attempts_used: hash[:attempts_used], hints_total: hash[:hints_total], hints_used: hash[:hints_used] }] save_statistic end def show_statistic @items = YAML.load_file(Constants::PATH) @items.sort_by! { |game| [game[:difficulty], game[:attempts_used], game[:hints_used]] } @items.each_with_index.map do |stat, index| [index.next, stat[:user_name], stat[:difficulty], stat[:attempts_total], stat[:attempts_used], stat[:hints_total], stat[:hints_used]] end end def save_statistic yaml_data = YAML.load_file(Constants::PATH) yaml_data = [] if yaml_data.nil? yaml_data << @stat File.open(Constants::PATH, 'r+') { |file| file.write YAML.dump(yaml_data.flatten) } end end end