Sha256: 5eb58bb669a40fc281c9ddbbd89c2799f4cc3a1142829dce1716f27308123b15

Contents?: true

Size: 787 Bytes

Versions: 1

Compression:

Stored size: 787 Bytes

Contents

module Codebreaker
  class Stats
    PATH = './db/data.yml'.freeze

    def initialize
      @storage = FileStorage.new(PATH)
    end

    def load
      @storage.load || []
    end

    def save_game(game)
      validate_game_is_win(game)
      data = load
      data << game_data(game)
      @storage.save(data)
    end

    private

    def game_data(game)
      {
        name: game.player.name,
        difficulty: game.difficulty.type,
        attempts_total: game.difficulty.attempts,
        attempts_used: game.difficulty.attempts - game.attempts_left,
        hints_total: game.difficulty.hints,
        hints_used: game.difficulty.hints - game.hints_left
      }
    end

    def validate_game_is_win(game)
      raise Errors::GameSaveError unless game.win?
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cb-core-0.1.6 lib/codebreaker/stats.rb