alex_codebreaker 0.1.12 → 0.1.13

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fee977f0016bb5f1331a26061deee8d4b3fa92ac505d37a4cd37f88bc4d7f1bf
4
- data.tar.gz: 76f042f3ad1d24cd11c6f8c6d0dbf86b3263927af47206b6880885d34c0cd8fd
3
+ metadata.gz: c5a6d38c548011653e19a27a4104ea2d1c489e4da6b40a668eb8f65692113f5e
4
+ data.tar.gz: 3196886d889cc891d472944d7e8f27373c4466835a1a87222266faa575917996
5
5
  SHA512:
6
- metadata.gz: 2e4b08ab3a7386c94ec7a6e7ca17f25137ad0228d45f5a067da6bbe58b9016f6ef57831a02d605f60e025a4d8c5cebdac6c6175e79ca70d8a0fd0e22f4e7f345
7
- data.tar.gz: b77b9df5ccbda1f13e3c2ccc347a7c651b07f643b5d02127359d5b401ed7190b3ac3fbc6086c3b2d15723c283d7b5e84f02875a92eecca0817bed1b3b9e91558
6
+ metadata.gz: bd9759580e561f62ef281b33038c12dad441f38f2f57b5034fc79e676e3e738f4ef0715a4a5f5407c14dbecda581e35fe6027a22b5d74c99371f1d917ea89aca
7
+ data.tar.gz: 55344c2fc1a93e3b8487982a7e01775be6062107d685ce2866cd816cc03223f5143179cc02ffec4178f823bedd9c55475c607734310c105d5c51bd6ebb5a3a81
data/README.md CHANGED
@@ -20,8 +20,14 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
- To config storage path add this to your app config
24
- `AlexCodebreaker.configure { |config| config.storage_path = 'your/path.yml' }`
23
+ To config winners folder path add this to your app config:
24
+
25
+ `AlexCodebreaker.configure { |config| config.winners_folder_path = 'your/path/' }`
26
+
27
+ To config save games folder path add this to your app config:
28
+
29
+ `AlexCodebreaker.configure { |config| config.games_folder_path = 'your/folder/' }`
30
+
25
31
 
26
32
  Use this gem for creating amazing version of codebreaker game!
27
33
 
@@ -6,8 +6,8 @@ require_relative 'alex_codebreaker/modules/settings'
6
6
  require_relative 'alex_codebreaker/modules/difficulty_levels'
7
7
  require_relative 'alex_codebreaker/modules/validators'
8
8
  require_relative 'alex_codebreaker/modules/arguments_validation'
9
- require_relative 'alex_codebreaker/files_config'
10
9
  require_relative 'alex_codebreaker/secret_code_generator'
10
+ require_relative 'alex_codebreaker/id_generator'
11
11
  require_relative 'alex_codebreaker/comparison'
12
12
  require_relative 'alex_codebreaker/players_rating'
13
13
  require_relative 'alex_codebreaker/session'
@@ -1,11 +1,13 @@
1
1
  module AlexCodebreaker
2
2
  class Configuration
3
- DEFAULT_PATH = 'stats.yml'.freeze
3
+ DEFAULT_WINNERS_FOLDER_PATH = 'games/winners/'.freeze
4
+ DEFAULT_GAMES_FOLDER_PATH = 'games/'.freeze
4
5
 
5
- attr_accessor :storage_path
6
+ attr_accessor :winners_folder_path, :games_folder_path
6
7
 
7
8
  def initialize
8
- @storage_path = DEFAULT_PATH
9
+ @winners_folder_path = DEFAULT_WINNERS_FOLDER_PATH
10
+ @games_folder_path = DEFAULT_GAMES_FOLDER_PATH
9
11
  end
10
12
  end
11
13
 
@@ -1,13 +1,16 @@
1
1
  module AlexCodebreaker
2
2
  class Game
3
3
  include AlexCodebreaker::Modules::ArgumentsValidation
4
+ FILENAME_EXTENSION = '.yml'.freeze
4
5
 
5
- attr_reader :secret_code, :session
6
+ attr_reader :secret_code, :session, :game_id
6
7
 
7
- def initialize
8
+ def initialize(outer_id: nil)
9
+ @game_id = outer_id if outer_id
8
10
  @secret_code = AlexCodebreaker::SecretCodeGenerator.new.secret_code
9
11
  @secret_code_for_hint = @secret_code.clone.uniq
10
12
  @session = Session.new
13
+ save_game
11
14
  end
12
15
 
13
16
  def hint
@@ -19,15 +22,20 @@ module AlexCodebreaker
19
22
  def guess(user_input)
20
23
  return unless guess_validation(user_input) && @session.check_attempts
21
24
 
25
+ save_game
22
26
  compare_codes(user_input)
23
27
  end
24
28
 
25
29
  private
26
30
 
27
31
  def process_hint
28
- return @secret_code_for_hint.first if @secret_code_for_hint.one?
29
-
30
- @secret_code_for_hint.delete(@secret_code_for_hint.sample)
32
+ hint = if @secret_code_for_hint.one?
33
+ @secret_code_for_hint.first
34
+ else
35
+ @secret_code_for_hint.delete(@secret_code_for_hint.sample)
36
+ end
37
+ save_game
38
+ hint
31
39
  end
32
40
 
33
41
  def compare_codes(user_input)
@@ -39,5 +47,17 @@ module AlexCodebreaker
39
47
  def format_user_input(user_input)
40
48
  user_input.chars.map(&:to_i)
41
49
  end
50
+
51
+ def save_game
52
+ @game_id ||= AlexCodebreaker::IDGenerator.new.unique_id
53
+ unless File.directory?(AlexCodebreaker.configuration.games_folder_path)
54
+ FileUtils.mkdir_p(AlexCodebreaker.configuration.games_folder_path)
55
+ end
56
+ File.open(save_game_path, 'w') { |file| file.write(to_yaml) }
57
+ end
58
+
59
+ def save_game_path
60
+ "#{AlexCodebreaker.configuration.games_folder_path}#{@game_id}#{FILENAME_EXTENSION}"
61
+ end
42
62
  end
43
63
  end
@@ -0,0 +1,18 @@
1
+ module AlexCodebreaker
2
+ class IDGenerator
3
+ MIN_NUMBER = 1
4
+ MAX_NUMBER = 9_999_999_999
5
+
6
+ attr_reader :unique_id
7
+
8
+ def initialize
9
+ @unique_id = generate_id
10
+ end
11
+
12
+ private
13
+
14
+ def generate_id
15
+ rand(MIN_NUMBER..MAX_NUMBER)
16
+ end
17
+ end
18
+ end
@@ -10,14 +10,14 @@ module AlexCodebreaker
10
10
  private
11
11
 
12
12
  def load_and_sort_stats
13
- return unless File.exist?(AlexCodebreaker.configuration.storage_path)
13
+ return unless File.exist?(winners_path)
14
14
 
15
15
  load_stats
16
16
  sort_stats
17
17
  end
18
18
 
19
19
  def load_stats
20
- File.open(AlexCodebreaker.configuration.storage_path) do |file|
20
+ File.open(winners_path) do |file|
21
21
  @stats = Array.new(YAML.load_stream(file))
22
22
  end
23
23
  end
@@ -25,5 +25,9 @@ module AlexCodebreaker
25
25
  def sort_stats
26
26
  @stats.sort_by! { |value| [-value.difficulty_level, value.attempts_used, value.hints_used] }
27
27
  end
28
+
29
+ def winners_path
30
+ "#{AlexCodebreaker.configuration.winners_folder_path}#{AlexCodebreaker::Session::WINNERS_FILE_NAME}"
31
+ end
28
32
  end
29
33
  end
@@ -4,6 +4,7 @@ module AlexCodebreaker
4
4
 
5
5
  INITIAL_ATTEMPTS_USED = 0
6
6
  INITIAL_HINTS_USED = 0
7
+ WINNERS_FILE_NAME = 'winners.yml'.freeze
7
8
 
8
9
  attr_reader :hints_used, :attempts_used, :player_name, :hints_total,
9
10
  :difficulty_name, :attempts_total, :difficulty_level, :time
@@ -38,9 +39,17 @@ module AlexCodebreaker
38
39
  @attempts_used
39
40
  end
40
41
 
41
- def save_statistic
42
+ def save_winner_statistic
42
43
  @time = Time.new
43
- File.open(AlexCodebreaker.configuration.storage_path, 'a') { |file| file.write(to_yaml) }
44
+ check_folder_existence
45
+ path = "#{AlexCodebreaker.configuration.winners_folder_path}#{WINNERS_FILE_NAME}"
46
+ File.open(path, 'a') { |file| file.write(to_yaml) }
47
+ end
48
+
49
+ def check_folder_existence
50
+ return if File.directory?(AlexCodebreaker.configuration.winners_folder_path)
51
+
52
+ FileUtils.mkdir_p(AlexCodebreaker.configuration.winners_folder_path)
44
53
  end
45
54
  end
46
55
  end
@@ -1,5 +1,5 @@
1
1
  module AlexCodebreaker
2
2
  module Version
3
- VERSION = '0.1.12'.freeze
3
+ VERSION = '0.1.13'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alex_codebreaker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.12
4
+ version: 0.1.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oleksandr Loza
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-03-26 00:00:00.000000000 Z
11
+ date: 2020-03-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -162,11 +162,10 @@ files:
162
162
  - lib/alex_codebreaker.rb
163
163
  - lib/alex_codebreaker/comparison.rb
164
164
  - lib/alex_codebreaker/configuration.rb
165
- - lib/alex_codebreaker/files_config.rb
166
165
  - lib/alex_codebreaker/game.rb
166
+ - lib/alex_codebreaker/id_generator.rb
167
167
  - lib/alex_codebreaker/modules/arguments_validation.rb
168
168
  - lib/alex_codebreaker/modules/difficulty_levels.rb
169
- - lib/alex_codebreaker/modules/files.rb
170
169
  - lib/alex_codebreaker/modules/settings.rb
171
170
  - lib/alex_codebreaker/modules/validators.rb
172
171
  - lib/alex_codebreaker/players_rating.rb
@@ -1,15 +0,0 @@
1
- module AlexCodebreaker
2
- class FilesConfig
3
- # rubocop:disable Style/ClassVars
4
- DEFAULT_PATH = 'stats.yml'.freeze
5
-
6
- def default_path
7
- @@default_path ||= DEFAULT_PATH
8
- end
9
-
10
- def default_path=(path)
11
- @@default_path = path
12
- end
13
- # rubocop:enable Style/ClassVars
14
- end
15
- end
@@ -1,7 +0,0 @@
1
- module AlexCodebreaker
2
- module Modules
3
- module Files
4
- STATS_FILE = 'stats.yml'.freeze
5
- end
6
- end
7
- end