codebreaker_ap 0.1.0 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: deb22669c587e91d496671d56eef7eaa54a719125b85b929437aef5645ce9204
4
- data.tar.gz: 0121d1d3dfc5e74ec0308522aa6184e467beac87e377a20a58d58f3ab50451dc
3
+ metadata.gz: 8ad9f28ef5499d36023b3252ec0edf5693a7a3f6dcc661d1c292a7fbf5355442
4
+ data.tar.gz: 7787374c62312877f4f4e22fd0a8255c2958800e2a90cc51b03ecdd163deac77
5
5
  SHA512:
6
- metadata.gz: 8499e8a4cdc334447501406f10b18870f633cb0d45a6a89f805a2838e32e525ca514b42f5ca7cf7f0bdd6e90f9f4e591563ba6c75acb8de8abcbc1a0774b42e0
7
- data.tar.gz: ed5f75a77537b91c6839a60af2e9a776513d5263126f88b0b21ea477b2d3ca9f8184cfa2c67a58fa1228ef913ea3755ea74afd49b45b71921b573646099fbdf8
6
+ metadata.gz: 37ef4c2ec01b2964979df2523546a456e9e82b42207cfa04d78714ae67674c33681bb01828bd58c0d442c8898600eaad14215e776e3d8395be6af16676b472c7
7
+ data.tar.gz: f75be4ac1d0b31d19fecc2c6768ebee081eea20724d07e106e9088e7a7526f23216fea4535fa8d75d6c3ea809e06360750a80f172455f7ea84282591b4bc1fc0
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- codebreaker_ap (0.1.0)
4
+ codebreaker_ap (0.1.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -11,7 +11,7 @@ The codebreaker gets some number of chances to break the code (depends on chosen
11
11
  Include to Gemfile:
12
12
 
13
13
  ```ruby
14
- gem 'game_codebreaker_ap'
14
+ gem 'codebreaker_ap'
15
15
  ```
16
16
 
17
17
  And then execute:
@@ -20,7 +20,7 @@ And then execute:
20
20
 
21
21
  Or install it yourself as:
22
22
 
23
- $ gem install game_codebreaker_ap
23
+ $ gem install codebreaker_ap
24
24
 
25
25
  ## Usage
26
26
 
@@ -4,9 +4,10 @@ I18n.available_locales = :en, :ru
4
4
  require 'yaml'
5
5
  require 'codebreaker_ap/modules/storage'
6
6
  require 'codebreaker_ap/modules/statistic'
7
+ require 'codebreaker_ap/entities/message'
7
8
  require 'codebreaker_ap/modules/errors'
8
9
  require 'codebreaker_ap/modules/validation'
9
- require 'codebreaker_ap/entities/base_entity'
10
+ require 'codebreaker_ap/entities/validator.rb'
10
11
  require 'codebreaker_ap/entities/player'
11
12
  require 'codebreaker_ap/entities/difficulty'
12
13
  require 'codebreaker_ap/game'
@@ -1,5 +1,5 @@
1
1
  module CodebreakerAp
2
- class Difficulty < BaseEntity
2
+ class Difficulty
3
3
  DIFFICULTY = {
4
4
  easy: {
5
5
  hints: 2,
@@ -9,7 +9,7 @@ module CodebreakerAp
9
9
  hints: 1,
10
10
  attempts: 10
11
11
  },
12
- hell: {
12
+ difficult: {
13
13
  hints: 1,
14
14
  attempts: 5
15
15
  }
@@ -22,17 +22,19 @@ module CodebreakerAp
22
22
  @level = nil
23
23
  @hints = nil
24
24
  @attempts = nil
25
- super()
26
25
  end
27
26
 
28
27
  def initialize_difficulty(level)
28
+ check = Validator.new
29
+ check.validate_difficulty(level, DIFFICULTY.keys)
30
+ return check.errors unless check.errors.empty?
31
+
29
32
  @level = level
30
- valid?
31
- setup_difficulty if validated
33
+ setup_difficulty
32
34
  end
33
35
 
34
36
  def hint(hints_code)
35
- return I18n.t(:no_hint) if @hints.zero?
37
+ return Message.new.no_hint if @hints.zero?
36
38
 
37
39
  @hints -= 1
38
40
  hints_code.pop
@@ -45,11 +47,5 @@ module CodebreakerAp
45
47
  @hints = difficulty_options[:hints]
46
48
  @attempts = difficulty_options[:attempts]
47
49
  end
48
-
49
- def validate
50
- check_difficulty(@level.to_sym, DIFFICULTY.keys)
51
- rescue WrongDifficultyError => e
52
- @errors.push(e)
53
- end
54
50
  end
55
51
  end
@@ -0,0 +1,23 @@
1
+ module CodebreakerAp
2
+ class Message
3
+ def file_rules
4
+ I18n.t(:file_rules)
5
+ end
6
+
7
+ def wrong_length
8
+ I18n.t(:wrong_length)
9
+ end
10
+
11
+ def wrong_difficulty
12
+ I18n.t(:wrong_difficulty)
13
+ end
14
+
15
+ def wrong_chars
16
+ I18n.t(:wrong_chars)
17
+ end
18
+
19
+ def no_hint
20
+ I18n.t(:no_hint)
21
+ end
22
+ end
23
+ end
@@ -1,5 +1,5 @@
1
1
  module CodebreakerAp
2
- class Player < BaseEntity
2
+ class Player
3
3
  attr_reader :name
4
4
  attr_accessor :answer, :validated
5
5
 
@@ -9,44 +9,22 @@ module CodebreakerAp
9
9
  def initialize
10
10
  @name = nil
11
11
  @answer = nil
12
- super()
13
12
  end
14
13
 
15
14
  def setup_name(player_name)
16
- @name = player_name
17
- valid_instance?
18
- end
19
-
20
- def setup_answer(answer)
21
- @answer = answer
22
- valid_instance?
23
- end
24
-
25
- private
15
+ check = Validator.new
16
+ check.validate_length(player_name, NAME_LENGTH)
17
+ return check.errors unless check.errors.empty?
26
18
 
27
- def valid_instance?
28
- valid?
29
- return if validated
30
-
31
- puts errors
32
- end
33
-
34
- def validate
35
- @errors << validate_name if name
36
- @errors << validate_answer if answer
37
- rescue WrongLengthError => e
38
- @errors.push(e)
39
- rescue WrongNumbersError => e
40
- @errors.push(e)
19
+ @name = player_name.capitalize
41
20
  end
42
21
 
43
- def validate_name
44
- check_length(@name, NAME_LENGTH)
45
- end
22
+ def setup_answer(answer)
23
+ check = Validator.new
24
+ check.validate_player_answer(answer, ANSWER_LENGTH, ANSWER_CHARS_RANGE)
25
+ return check.errors unless check.errors.empty?
46
26
 
47
- def validate_answer
48
- check_length(@answer, ANSWER_LENGTH)
49
- check_chars_range(@answer, ANSWER_CHARS_RANGE)
27
+ @answer = answer
50
28
  end
51
29
  end
52
30
  end
@@ -0,0 +1,30 @@
1
+ module CodebreakerAp
2
+ class Validator
3
+ include Validation
4
+
5
+ attr_reader :errors
6
+
7
+ def initialize
8
+ @errors = []
9
+ end
10
+
11
+ def validate_difficulty(level, difficulty_keys)
12
+ check_difficulty(level.to_sym, difficulty_keys)
13
+ rescue WrongDifficultyError => e
14
+ @errors.push(e)
15
+ end
16
+
17
+ def validate_length(name, name_length)
18
+ check_length(name, name_length)
19
+ rescue WrongLengthError => e
20
+ @errors.push(e)
21
+ end
22
+
23
+ def validate_player_answer(answer, answer_length, answer_chars_range)
24
+ validate_length(answer, answer_length)
25
+ check_chars_range(answer, answer_chars_range)
26
+ rescue WrongNumbersError => e
27
+ @errors.push(e)
28
+ end
29
+ end
30
+ end
@@ -37,10 +37,10 @@ module CodebreakerAp
37
37
  end
38
38
 
39
39
  def show_rules
40
- rules = read_from_file("#{LOCALES_FOLDER}/#{I18n.t(:file_rules)}")
40
+ rules = read_from_file("#{LOCALES_FOLDER}/#{Message.new.file_rules}")
41
41
  return unless rules
42
42
 
43
- puts rules
43
+ rules
44
44
  end
45
45
 
46
46
  private
@@ -1,17 +1,17 @@
1
1
  module CodebreakerAp
2
2
  class WrongLengthError < StandardError
3
3
  def initialize
4
- super(I18n.t(:wrong_length))
4
+ super(Message.new.wrong_length)
5
5
  end
6
6
  end
7
7
  class WrongDifficultyError < StandardError
8
8
  def initialize
9
- super(I18n.t(:wrong_difficulty))
9
+ super(Message.new.wrong_difficulty)
10
10
  end
11
11
  end
12
12
  class WrongNumbersError < StandardError
13
13
  def initialize
14
- super(I18n.t(:wrong_chars))
14
+ super(Message.new.wrong_chars)
15
15
  end
16
16
  end
17
17
  end
@@ -5,9 +5,15 @@ module CodebreakerAp
5
5
  save_to_file(create_statistic_data(player_name, difficulty), STATISTIC_FILE)
6
6
  end
7
7
 
8
+ def load_statistic
9
+ data = YAML.load_stream(File.open(STATISTIC_FILE, 'a+'))
10
+ return unless data
11
+
12
+ data.sort_by { |players| [-players[:difficulty].length, players[:used_attempts], players[:used_hints]] }
13
+ end
14
+
8
15
  def show_stats
9
- statistic = load_statistic
10
- statistic.each_with_index do |value, index|
16
+ load_statistic.each_with_index do |value, index|
11
17
  puts statistic(value, index + 1)
12
18
  end
13
19
  end
@@ -22,7 +28,8 @@ module CodebreakerAp
22
28
  total_attempts: difficulty_total[:attempts],
23
29
  total_hints: difficulty_total[:hints],
24
30
  used_attempts: difficulty_total[:attempts] - difficulty.attempts,
25
- used_hints: difficulty_total[:hints] - difficulty.hints
31
+ used_hints: difficulty_total[:hints] - difficulty.hints,
32
+ date: Time.now.strftime('%d/%m/%Y %H:%M')
26
33
  }
27
34
  end
28
35
 
@@ -33,14 +40,8 @@ module CodebreakerAp
33
40
  "Total attempts: #{value[:total_attempts]}\n"\
34
41
  "Total hints: #{value[:total_hints]}\n"\
35
42
  "Used attempts: #{value[:used_attempts]}\n"\
36
- "Used hints: #{value[:used_hints]}\n\n"
37
- end
38
-
39
- def load_statistic
40
- data = YAML.load_stream(File.open(STATISTIC_FILE, 'a+'))
41
- return unless data
42
-
43
- data.sort_by { |players| [players[:difficulty], players[:used_attempts]] }
43
+ "Used hints: #{value[:used_hints]}\n"\
44
+ "Date: #{value[:date]}\n\n"
44
45
  end
45
46
  end
46
47
  end
@@ -1,3 +1,3 @@
1
1
  module CodebreakerAp
2
- VERSION = '0.1.0'.freeze
2
+ VERSION = '0.1.3'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: codebreaker_ap
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - andrewpetrenko1
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-12-22 00:00:00.000000000 Z
11
+ date: 2020-04-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -131,9 +131,10 @@ files:
131
131
  - bin/setup
132
132
  - codebreaker_ap.gemspec
133
133
  - lib/codebreaker_ap.rb
134
- - lib/codebreaker_ap/entities/base_entity.rb
135
134
  - lib/codebreaker_ap/entities/difficulty.rb
135
+ - lib/codebreaker_ap/entities/message.rb
136
136
  - lib/codebreaker_ap/entities/player.rb
137
+ - lib/codebreaker_ap/entities/validator.rb
137
138
  - lib/codebreaker_ap/game.rb
138
139
  - lib/codebreaker_ap/modules/errors.rb
139
140
  - lib/codebreaker_ap/modules/statistic.rb
@@ -1,23 +0,0 @@
1
- module CodebreakerAp
2
- class BaseEntity
3
- include Validation
4
-
5
- attr_reader :errors, :validated
6
-
7
- def initialize
8
- @validated = false
9
- @errors = []
10
- end
11
-
12
- def valid?
13
- @errors.clear
14
- validate
15
- @errors.compact!
16
- @validated = true if @errors.empty?
17
- end
18
-
19
- private
20
-
21
- def validate; end
22
- end
23
- end