MonkeyEngine 1.1.0 → 2.0.1

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.
Files changed (72) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +19 -16
  3. data/.rspec +3 -0
  4. data/.rubocop.yml +192 -0
  5. data/.ruby-version +1 -1
  6. data/CHANGELOG.md +10 -2
  7. data/Gemfile +3 -1
  8. data/Gemfile.lock +70 -0
  9. data/Rakefile +3 -2
  10. data/dictionaries/en-US.txt +466554 -0
  11. data/lib/Action/action.rb +5 -6
  12. data/lib/Monkey/monkey.rb +17 -13
  13. data/lib/Monkey.rb +3 -1
  14. data/lib/MonkeyAction/monkey_action.rb +3 -3
  15. data/lib/MonkeyAction/monkey_action_dead.rb +8 -8
  16. data/lib/MonkeyAction/monkey_action_eat.rb +6 -6
  17. data/lib/MonkeyAction/monkey_action_pause.rb +6 -7
  18. data/lib/MonkeyAction/monkey_action_sleep.rb +6 -6
  19. data/lib/MonkeyAction/monkey_action_type.rb +15 -12
  20. data/lib/MonkeyAction/monkey_action_wake.rb +8 -8
  21. data/lib/MonkeyAction/monkey_timed_action.rb +6 -6
  22. data/lib/MonkeyActions.rb +2 -0
  23. data/lib/MonkeyEngine/action_rules.rb +20 -18
  24. data/lib/MonkeyEngine/exceptions.rb +7 -7
  25. data/lib/MonkeyEngine/monkey_engine.rb +11 -14
  26. data/lib/MonkeyEngine/version.rb +3 -1
  27. data/lib/MonkeyEngine.rb +3 -1
  28. data/lib/MonkeyFactory/monkey_factory.rb +3 -4
  29. data/lib/MonkeyFactory.rb +3 -1
  30. data/lib/MonkeyKeyboard/keyboard_char.rb +3 -3
  31. data/lib/MonkeyKeyboard/keyboard_input.rb +4 -3
  32. data/lib/MonkeyKeyboard/keyboard_key.rb +12 -10
  33. data/lib/MonkeyKeyboard/keyboard_key_evaluator.rb +6 -8
  34. data/lib/MonkeyKeyboard/monkey_keyboard_en_us.rb +100 -80
  35. data/lib/MonkeyKeyboardEnUs.rb +3 -1
  36. data/lib/MonkeyManager/monkey_manager.rb +17 -15
  37. data/lib/MonkeyManager.rb +3 -1
  38. data/lib/MonkeyService/monkey_service.rb +8 -9
  39. data/lib/MonkeyService.rb +3 -1
  40. data/lib/tasks/engine.rb +63 -54
  41. data/monkeyengine.gemspec +26 -20
  42. data/spec/action_rules_spec.rb +17 -23
  43. data/spec/engine_spec.rb +15 -20
  44. data/spec/keyboard_char_spec.rb +4 -5
  45. data/spec/keyboard_key_spec.rb +8 -9
  46. data/spec/monkey_action_eat_spec.rb +17 -18
  47. data/spec/monkey_action_pause_spec.rb +19 -20
  48. data/spec/monkey_action_sleep_spec.rb +19 -20
  49. data/spec/monkey_action_type_spec.rb +25 -24
  50. data/spec/monkey_action_wake_spec.rb +10 -11
  51. data/spec/monkey_factory_spec.rb +7 -15
  52. data/spec/monkey_keyboard_en_us_spec.rb +10 -19
  53. data/spec/monkey_manager_spec.rb +3 -2
  54. data/spec/monkey_service_spec.rb +26 -29
  55. data/spec/monkey_spec.rb +3 -2
  56. data/spec/{spec_helpers.rb → spec_helper.rb} +7 -5
  57. data/spec/support/shared_examples.rb +16 -16
  58. metadata +69 -55
  59. data/.idea/.name +0 -1
  60. data/.idea/.rakeTasks +0 -7
  61. data/.idea/MonkeyEngine.iml +0 -133
  62. data/.idea/codeStyleSettings.xml +0 -13
  63. data/.idea/encodings.xml +0 -5
  64. data/.idea/misc.xml +0 -5
  65. data/.idea/modules.xml +0 -9
  66. data/.idea/runConfigurations/All_specs_in_test__MonkeyEngine.xml +0 -38
  67. data/.idea/runConfigurations/IRB_console.xml +0 -25
  68. data/.idea/runConfigurations/Start_Yard_Server.xml +0 -26
  69. data/.idea/runConfigurations/monkey_run.xml +0 -26
  70. data/.idea/scopes/scope_settings.xml +0 -5
  71. data/.idea/vcs.xml +0 -7
  72. data/.idea/workspace.xml +0 -886
@@ -1,9 +1,20 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'keyboard_char'
2
4
 
3
5
  # Represents a keyboard key.
4
6
  class KeyboardKey
5
7
  attr_reader :keyboard_char, :keyboard_shift_char, :keyboard_key_section, :keyboard_key_weight
6
8
 
9
+ def self.make_key(char, shift_char, keyboard_key_section, keyboard_key_weight)
10
+ # TODO: Check keyboard_key_section is :left or :right
11
+
12
+ keyboard_char = KeyboardChar.new char
13
+ keyboard_shift_char = KeyboardChar.new shift_char
14
+
15
+ KeyboardKey.new keyboard_char, keyboard_shift_char, keyboard_key_section, keyboard_key_weight
16
+ end
17
+
7
18
  def initialize(keyboard_char, keyboard_shift_char, keyboard_key_section, keyboard_key_weight)
8
19
  # TODO: Check keyboard_char is_a? KeyboardChar
9
20
  # TODO: Check keyboard_shift_char is_a? KeyboardChar
@@ -14,13 +25,4 @@ class KeyboardKey
14
25
  @keyboard_key_section = keyboard_key_section
15
26
  @keyboard_key_weight = keyboard_key_weight
16
27
  end
17
-
18
- def self.make_key(char, shift_char, keyboard_key_section, keyboard_key_weight)
19
- # TODO: Check keyboard_key_section is :left or :right
20
-
21
- keyboard_char = KeyboardChar.new char
22
- keyboard_shift_char = KeyboardChar.new shift_char
23
-
24
- KeyboardKey.new keyboard_char, keyboard_shift_char, keyboard_key_section, keyboard_key_weight
25
- end
26
- end
28
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class KeyboardKeyEvaluator
2
4
  def initialize
3
5
  @shift_on = false
@@ -8,18 +10,14 @@ class KeyboardKeyEvaluator
8
10
  @shift_on = !@shift_on if keyboard_key.keyboard_char.char == :shift
9
11
  @caps_on = !@shift_on if keyboard_key.keyboard_char.char == :caps
10
12
 
11
- return nil if keyboard_key.keyboard_char.char.is_a? Symbol
13
+ return if keyboard_key.keyboard_char.char.is_a? Symbol
12
14
 
13
- if @shift_on
14
- return keyboard_key.keyboard_shift_char.char
15
- end
15
+ return keyboard_key.keyboard_shift_char.char if @shift_on
16
16
 
17
17
  char = keyboard_key.keyboard_char.char
18
18
 
19
- if @caps_on
20
- return char.upcase
21
- end
19
+ return char.upcase if @caps_on
22
20
 
23
21
  char
24
22
  end
25
- end
23
+ end
@@ -1,3 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pry-byebug'
1
4
  require 'singleton'
2
5
  require 'LittleWeasel'
3
6
 
@@ -6,101 +9,106 @@ require_relative 'keyboard_key'
6
9
  require_relative 'keyboard_input'
7
10
  require_relative 'keyboard_key_evaluator'
8
11
 
9
-
10
12
  module MonkeyEngine
11
-
12
13
  class MonkeyKeyboardEnUs
13
14
  include Singleton
14
15
 
15
16
  attr_reader :keys, :left_keys, :right_keys
16
17
 
17
18
  def initialize
19
+ LittleWeasel.configure { |config| }
18
20
 
19
21
  @keys = [
20
- # Row 1 of 5
21
- KeyboardKey::make_key('`', '~', :left, 1),
22
- KeyboardKey::make_key('1', '!', :left, 1),
23
- KeyboardKey::make_key('2', '@', :left, 1),
24
- KeyboardKey::make_key('3', '#', :left, 1),
25
- KeyboardKey::make_key('4', '$', :left, 1),
26
- KeyboardKey::make_key('5', '%', :left, 1),
27
- KeyboardKey::make_key('6', '^', :left, 1),
28
- KeyboardKey::make_key('7', '&', :right, 1),
29
- KeyboardKey::make_key('8', '*', :right, 1),
30
- KeyboardKey::make_key('9', '(', :right, 1),
31
- KeyboardKey::make_key('0', ')', :right, 1),
32
- KeyboardKey::make_key('-', '_', :right, 1),
33
- KeyboardKey::make_key('=', '+', :right, 1),
34
- KeyboardKey::make_key(:del, :del, :right, 1),
35
-
36
- # Row 2 of 5
37
- KeyboardKey::make_key(:tab, :tab, :left, 2),
38
- KeyboardKey::make_key('q', 'Q', :left, 1),
39
- KeyboardKey::make_key('w', 'W', :left, 1),
40
- KeyboardKey::make_key('e', 'E', :left, 1),
41
- KeyboardKey::make_key('r', 'R', :left, 1),
42
- KeyboardKey::make_key('t', 'T', :left, 1),
43
- KeyboardKey::make_key('y', 'Y', :left, 1),
44
- KeyboardKey::make_key('u', 'U', :right, 1),
45
- KeyboardKey::make_key('i', 'I', :right, 1),
46
- KeyboardKey::make_key('o', 'O', :right, 1),
47
- KeyboardKey::make_key('p', 'P', :right, 1),
48
- KeyboardKey::make_key('', '{', :right, 1),
49
- KeyboardKey::make_key('', '}', :right, 1),
50
- KeyboardKey::make_key('\\', '|', :right, 1),
51
-
52
- # Row 3 of 5
53
- KeyboardKey::make_key(:caps, :caps, :left, 3),
54
- KeyboardKey::make_key('a', 'A', :left, 1),
55
- KeyboardKey::make_key('s', 'S', :left, 1),
56
- KeyboardKey::make_key('d', 'D', :left, 1),
57
- KeyboardKey::make_key('f', 'F', :left, 1),
58
- KeyboardKey::make_key('g', 'G', :left, 1),
59
- KeyboardKey::make_key('h', 'H', :right, 1),
60
- KeyboardKey::make_key('j', 'J', :right, 1),
61
- KeyboardKey::make_key('k', 'K', :right, 1),
62
- KeyboardKey::make_key('l', 'L', :right, 1),
63
- KeyboardKey::make_key(';', ':', :right, 1),
64
- KeyboardKey::make_key('\'', '"', :right, 1),
65
- KeyboardKey::make_key(:enter, :enter, :right, 2),
66
-
67
- # Row 4 of 5
68
- KeyboardKey::make_key(:shift, :shift, :left, 5),
69
- KeyboardKey::make_key('z', 'Z', :left, 1),
70
- KeyboardKey::make_key('x', 'X', :left, 1),
71
- KeyboardKey::make_key('c', 'C', :left, 1),
72
- KeyboardKey::make_key('v', 'V', :left, 1),
73
- KeyboardKey::make_key('b', 'B', :left, 1),
74
- KeyboardKey::make_key('n', 'N', :right, 1),
75
- KeyboardKey::make_key('m', 'M', :right, 1),
76
- KeyboardKey::make_key(',', '<', :right, 1),
77
- KeyboardKey::make_key('.', '>', :right, 1),
78
- KeyboardKey::make_key('/', '?', :right, 1),
79
- KeyboardKey::make_key(:shift, :shift, :right, 5),
80
-
81
- # Row 5 of 5
82
- KeyboardKey::make_key(:space, :space, :left, 2),
83
- KeyboardKey::make_key(:space, :space, :right, 2)
22
+ # Row 1 of 5
23
+ KeyboardKey.make_key('`', '~', :left, 1),
24
+ KeyboardKey.make_key('1', '!', :left, 1),
25
+ KeyboardKey.make_key('2', '@', :left, 1),
26
+ KeyboardKey.make_key('3', '#', :left, 1),
27
+ KeyboardKey.make_key('4', '$', :left, 1),
28
+ KeyboardKey.make_key('5', '%', :left, 1),
29
+ KeyboardKey.make_key('6', '^', :left, 1),
30
+ KeyboardKey.make_key('7', '&', :right, 1),
31
+ KeyboardKey.make_key('8', '*', :right, 1),
32
+ KeyboardKey.make_key('9', '(', :right, 1),
33
+ KeyboardKey.make_key('0', ')', :right, 1),
34
+ KeyboardKey.make_key('-', '_', :right, 1),
35
+ KeyboardKey.make_key('=', '+', :right, 1),
36
+ KeyboardKey.make_key(:del, :del, :right, 1),
37
+
38
+ # Row 2 of 5
39
+ KeyboardKey.make_key(:tab, :tab, :left, 2),
40
+ KeyboardKey.make_key('q', 'Q', :left, 1),
41
+ KeyboardKey.make_key('w', 'W', :left, 1),
42
+ KeyboardKey.make_key('e', 'E', :left, 1),
43
+ KeyboardKey.make_key('r', 'R', :left, 1),
44
+ KeyboardKey.make_key('t', 'T', :left, 1),
45
+ KeyboardKey.make_key('y', 'Y', :left, 1),
46
+ KeyboardKey.make_key('u', 'U', :right, 1),
47
+ KeyboardKey.make_key('i', 'I', :right, 1),
48
+ KeyboardKey.make_key('o', 'O', :right, 1),
49
+ KeyboardKey.make_key('p', 'P', :right, 1),
50
+ KeyboardKey.make_key('', '{', :right, 1),
51
+ KeyboardKey.make_key('', '}', :right, 1),
52
+ KeyboardKey.make_key('\\', '|', :right, 1),
53
+
54
+ # Row 3 of 5
55
+ KeyboardKey.make_key(:caps, :caps, :left, 3),
56
+ KeyboardKey.make_key('a', 'A', :left, 1),
57
+ KeyboardKey.make_key('s', 'S', :left, 1),
58
+ KeyboardKey.make_key('d', 'D', :left, 1),
59
+ KeyboardKey.make_key('f', 'F', :left, 1),
60
+ KeyboardKey.make_key('g', 'G', :left, 1),
61
+ KeyboardKey.make_key('h', 'H', :right, 1),
62
+ KeyboardKey.make_key('j', 'J', :right, 1),
63
+ KeyboardKey.make_key('k', 'K', :right, 1),
64
+ KeyboardKey.make_key('l', 'L', :right, 1),
65
+ KeyboardKey.make_key(';', ':', :right, 1),
66
+ KeyboardKey.make_key('\'', '"', :right, 1),
67
+ KeyboardKey.make_key(:enter, :enter, :right, 2),
68
+
69
+ # Row 4 of 5
70
+ KeyboardKey.make_key(:shift, :shift, :left, 5),
71
+ KeyboardKey.make_key('z', 'Z', :left, 1),
72
+ KeyboardKey.make_key('x', 'X', :left, 1),
73
+ KeyboardKey.make_key('c', 'C', :left, 1),
74
+ KeyboardKey.make_key('v', 'V', :left, 1),
75
+ KeyboardKey.make_key('b', 'B', :left, 1),
76
+ KeyboardKey.make_key('n', 'N', :right, 1),
77
+ KeyboardKey.make_key('m', 'M', :right, 1),
78
+ KeyboardKey.make_key(',', '<', :right, 1),
79
+ KeyboardKey.make_key('.', '>', :right, 1),
80
+ KeyboardKey.make_key('/', '?', :right, 1),
81
+ KeyboardKey.make_key(:shift, :shift, :right, 5),
82
+
83
+ # Row 5 of 5
84
+ KeyboardKey.make_key(:space, :space, :left, 2),
85
+ KeyboardKey.make_key(:space, :space, :right, 2)
84
86
  ]
85
87
 
86
88
  @left_keys = []
87
89
  @right_keys = []
88
90
 
89
- @keys.each { |key|
91
+ @keys.each do |key|
90
92
  # Separate the keys on the left side of the keyboard...
91
- key.keyboard_key_weight.times do
92
- left_keys.push(key)
93
- end if key.keyboard_key_section == :left
93
+ if key.keyboard_key_section == :left
94
+ key.keyboard_key_weight.times do
95
+ left_keys.push(key)
96
+ end
97
+ end
94
98
 
95
99
  # Separate the keys on the right side of the keyboard...
100
+ next unless key.keyboard_key_section == :right
101
+
96
102
  key.keyboard_key_weight.times do
97
103
  right_keys.push(key)
98
- end if key.keyboard_key_section == :right
99
- }
104
+ end
105
+ end
100
106
 
101
107
  @keys.freeze
102
108
  @keys_left.freeze
103
109
  @keys_right.freeze
110
+
111
+ @en_us_dictionary = create_dictionary
104
112
  end
105
113
 
106
114
  def get_keyboard_input
@@ -114,24 +122,36 @@ module MonkeyEngine
114
122
  keyboard_key_evaluator = KeyboardKeyEvaluator.new
115
123
 
116
124
  # Take the keys until we hit a key that terminates a word...
117
- keyboard_input.input = keys.take_while { |key|
125
+ keyboard_input.input = keys.take_while do |key|
118
126
  !keyboard_char_ends_word?(key.keyboard_char.char)
119
- }.collect { |key| keyboard_key_evaluator.get_char(key) }.compact
127
+ end.filter_map { |key| keyboard_key_evaluator.get_char(key) }
120
128
 
121
129
  # Keep going until we get something...
122
130
  keyboard_input = get_keyboard_input if keyboard_input.input.empty?
123
-
124
- keyboard_input.is_word = LittleWeasel::Checker.instance.exists?(keyboard_input.input_to_s,
125
- exclude_alphabet: true, strip_whitespace: true, ignore_numeric: false, single_word_mode: true)
131
+ keyboard_input.is_word = en_us_dictionary.word_results(keyboard_input.input_to_s).success?
126
132
 
127
133
  keyboard_input
128
134
  end
129
135
 
130
136
  # Returns true if the character causes a word termination.
131
137
  def keyboard_char_ends_word?(char)
132
- return char == :tab || char == :enter || char == :space || char == '.'
138
+ [:tab, :enter, :space, '.'].include?(char)
133
139
  end
134
140
 
135
- end
141
+ private
142
+
143
+ attr_accessor :en_us_dictionary
136
144
 
137
- end
145
+ def create_dictionary
146
+ dictionary_manager = LittleWeasel::DictionaryManager.new
147
+ en_us_key = LittleWeasel::DictionaryKey.new(language: :en, region: :us)
148
+ en_us_dictionary = dictionary_manager.create_dictionary_from_file(
149
+ dictionary_key: en_us_key, file: "dictionaries/#{en_us_key}.txt"
150
+ )
151
+ en_us_dictionary.add_preprocessors(word_preprocessors: [LittleWeasel::Preprocessors::EnUs::CapitalizePreprocessor.new])
152
+ en_us_dictionary.add_filters(word_filters: [LittleWeasel::Filters::EnUs::SingleCharacterWordFilter.new])
153
+
154
+ en_us_dictionary
155
+ end
156
+ end
157
+ end
@@ -1 +1,3 @@
1
- require_relative 'MonkeyKeyboard/monkey_keyboard_en_us'
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'MonkeyKeyboard/monkey_keyboard_en_us'
@@ -1,8 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'singleton'
2
4
  require 'forwardable'
3
5
 
4
6
  module MonkeyEngine
5
-
6
7
  # The MonkeyManager.
7
8
  #
8
9
  # This class provides the functionality needed to manage Monkeys (See Monkey, MonkeyService)
@@ -18,11 +19,9 @@ module MonkeyEngine
18
19
  # The constructor
19
20
  #
20
21
  def initialize
21
- @monkeys = Array.new
22
+ @monkeys = []
22
23
  end
23
24
 
24
- public
25
-
26
25
  # Adds the Monkey to the list of Monkeys to be managed.
27
26
  #
28
27
  # @param (Monkey, #read) monkey The Monkey to add.
@@ -33,8 +32,10 @@ module MonkeyEngine
33
32
  # @raise [MonkeyEngine::Exceptions::UniqueObjectException] if monkey already exists.
34
33
  #
35
34
  def add(monkey)
36
- raise MonkeyEngine::Exceptions::InvalidArgumentTypeException.new "Parameter 'monkey' is not Monkey object" unless monkey.is_a? Monkey
37
- raise MonkeyEngine::Exceptions::UniqueObjectException.new "Monkeys must be unique" if exists? monkey
35
+ unless monkey.is_a? Monkey
36
+ raise MonkeyEngine::Exceptions::InvalidArgumentTypeException, "Parameter 'monkey' is not Monkey object"
37
+ end
38
+ raise MonkeyEngine::Exceptions::UniqueObjectException, 'Monkeys must be unique' if exists? monkey
38
39
 
39
40
  @monkeys.push(monkey)
40
41
  monkey
@@ -66,7 +67,7 @@ module MonkeyEngine
66
67
  #
67
68
  def alive?(monkey)
68
69
  monkey = get(monkey)
69
- monkey.alive? unless monkey.nil?
70
+ monkey&.alive?
70
71
  end
71
72
 
72
73
  # Returns the Monkey indicated by *monkey*.
@@ -81,14 +82,15 @@ module MonkeyEngine
81
82
  # @raise [MonkeyEngine::Exceptions::InvalidArgumentTypeException] if parameter monkey is not a Monkey, Symbol, or String object.
82
83
  #
83
84
  def get(monkey)
84
- return nil if @monkeys.empty?
85
+ return if @monkeys.empty?
85
86
 
86
87
  # TODO: This seems inefficient.
87
- return @monkeys.select { |m| m.monkey_symbol == monkey }.first if monkey.is_a? Symbol
88
- return @monkeys.select { |m| m.monkey_symbol == monkey.to_sym }.first if monkey.is_a? String
89
- return @monkeys.select { |m| m.monkey_symbol == monkey.monkey_symbol }.first if monkey.is_a? Monkey
88
+ return @monkeys.find { |m| m.monkey_symbol == monkey } if monkey.is_a? Symbol
89
+ return @monkeys.find { |m| m.monkey_symbol == monkey.to_sym } if monkey.is_a? String
90
+ return @monkeys.find { |m| m.monkey_symbol == monkey.monkey_symbol } if monkey.is_a? Monkey
90
91
 
91
- raise MonkeyEngine::Exceptions::InvalidArgumentTypeException.new "Parameter 'monkey' is not a Symbol, String or Monkey object"
92
+ raise MonkeyEngine::Exceptions::InvalidArgumentTypeException,
93
+ "Parameter 'monkey' is not a Symbol, String or Monkey object"
92
94
  end
93
95
 
94
96
  # Returns a duplicate Array of Monkey objects managed by this MonkeyManager.
@@ -120,7 +122,7 @@ module MonkeyEngine
120
122
  # @return [Array, nil] Returns an [Array] of Monkey objects, or, nil, if no Monkey objects are being managed by
121
123
  # this MonkeyManager.
122
124
  #
123
- def join_all(limit=nil)
125
+ def join_all(limit = nil)
124
126
  limit = 0 unless limit.nil?
125
127
  @monkeys.each { |monkey| monkey.join limit } unless @monkeys.empty?
126
128
  end
@@ -152,11 +154,11 @@ module MonkeyEngine
152
154
  # @return [Array] an Array of Monkey objects that were killed.
153
155
  #
154
156
  def kill_all!
155
- @monkeys.each { |monkey| monkey.kill } unless @monkeys.empty?
157
+ @monkeys.each(&:kill) unless @monkeys.empty?
156
158
  monkeys = @monkeys.dup
157
159
  @monkeys.clear
158
160
 
159
161
  monkeys
160
162
  end
161
163
  end
162
- end
164
+ end
data/lib/MonkeyManager.rb CHANGED
@@ -1 +1,3 @@
1
- require_relative 'MonkeyManager/monkey_manager'
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'MonkeyManager/monkey_manager'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'singleton'
2
4
  require 'observer'
3
5
  require 'forwardable'
@@ -7,7 +9,6 @@ require 'MonkeyEngine'
7
9
  require 'MonkeyManager'
8
10
 
9
11
  module MonkeyEngine
10
-
11
12
  # The monkey service.
12
13
  #
13
14
  # Provides the main interface for all functionality relating to
@@ -38,7 +39,7 @@ module MonkeyEngine
38
39
  #
39
40
  def add(monkey)
40
41
  @monkey_manager.add(monkey).start
41
- do_notify_observers(:add, {monkey: monkey})
42
+ do_notify_observers(:add, { monkey: monkey })
42
43
  end
43
44
 
44
45
  # Determines if any Monkeys that are being managed by the
@@ -49,13 +50,13 @@ module MonkeyEngine
49
50
  # @return [Boolean] true if any Monkeys are alive, false otherwise.
50
51
  #
51
52
  def any_alive?
52
- return false if @monkey_manager.count == 0
53
+ return false if @monkey_manager.count.zero?
53
54
 
54
55
  alive_count = 0
55
56
 
56
57
  @monkey_manager.each { |monkey| alive_count += 1 if monkey.alive? }
57
58
 
58
- alive_count > 0
59
+ alive_count.positive?
59
60
  end
60
61
 
61
62
  # Kills the monkey.
@@ -72,7 +73,7 @@ module MonkeyEngine
72
73
  #
73
74
  def kill!(monkey)
74
75
  @monkey_manager.kill!(monkey)
75
- do_notify_observers(:kill!, {monkey: monkey})
76
+ do_notify_observers(:kill!, { monkey: monkey })
76
77
  end
77
78
 
78
79
  # Kills all monkeys managed by the underlying MonkeyManager.
@@ -109,7 +110,7 @@ module MonkeyEngine
109
110
  # @return (Boolean) true if the action performed has been completed (Action#action_completed?), false otherwise.
110
111
  #
111
112
  def monkey_do(action)
112
- do_notify_observers(:action_completed, {action: action}) if @engine.do_action action
113
+ do_notify_observers(:action_completed, { action: action }) if @engine.do_action action
113
114
  end
114
115
 
115
116
  # Evaluates the given action, to determine whether or not the action is completed (Action#action_completed?).
@@ -131,7 +132,5 @@ module MonkeyEngine
131
132
  changed
132
133
  notify_observers(Time.now, param1, param2)
133
134
  end
134
-
135
135
  end
136
-
137
- end
136
+ end
data/lib/MonkeyService.rb CHANGED
@@ -1 +1,3 @@
1
- require_relative 'MonkeyService/monkey_service'
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'MonkeyService/monkey_service'
data/lib/tasks/engine.rb CHANGED
@@ -1,77 +1,72 @@
1
- require 'pry'
1
+ # frozen_string_literal: true
2
2
 
3
+ require 'pry-byebug'
4
+ require 'colorize'
3
5
  require 'yaml'
4
- require "minitest/autorun"
6
+ require 'minitest/autorun'
5
7
  require 'LittleWeasel'
6
8
 
7
- require File.expand_path('../../MonkeyFactory', __FILE__)
8
- require File.expand_path('../../MonkeyEngine', __FILE__)
9
+ require File.expand_path('../MonkeyFactory', __dir__)
10
+ require File.expand_path('../MonkeyEngine', __dir__)
11
+
12
+ LittleWeasel.configure { |config| }
9
13
 
10
14
  module Runner
11
-
12
15
  class MonkeyRun
13
- attr_reader :monkey_service, :runtime, :thread, :words
16
+ attr_reader :monkey_service, :runtime, :thread, :monkey_words
14
17
 
15
18
  def initialize(monkey_service, runtime)
16
- @words = Array.new
19
+ @monkey_words = {}
17
20
  @monkey_service = monkey_service
18
21
  @runtime = runtime.to_i
19
22
 
20
23
  @monkey_service.add_observer self
21
24
 
22
- @thread = Thread.new {
25
+ @thread = Thread.new do
23
26
  go
24
27
  sleep @runtime
25
- }
26
-
27
- self
28
+ end
28
29
  end
29
30
 
30
- #protected
31
-
32
31
  def go
33
- @monkey_service.add(MonkeyFactory::create :groucho)
34
- @monkey_service.add(MonkeyFactory::create :harpo)
35
- @monkey_service.add(MonkeyFactory::create :chico)
36
- @monkey_service.add(MonkeyFactory::create :zeppo)
32
+ @monkey_service.add(MonkeyFactory.create(:groucho))
33
+ @monkey_service.add(MonkeyFactory.create(:harpo))
34
+ @monkey_service.add(MonkeyFactory.create(:chico))
35
+ @monkey_service.add(MonkeyFactory.create(:zeppo))
37
36
  end
38
37
 
39
- def update(time, action, param)
40
- begin
41
-
42
- return unless param.is_a?(Hash) && param.has_key?(:action)
43
-
44
- if param[:action].is_a?(MonkeyActionType) && action == :action_completed
45
-
46
- monkey = param[:action].monkey.monkey_symbol.to_s
47
- is_word = param[:action].keyboard_input.is_word
48
- word = param[:action].keyboard_input.input_to_s
49
-
50
- #if param[:action].is_a?(MonkeyActionType) && action == :action_completed
51
- puts "Monkey: [#{monkey.capitalize}] | Is Word: [#{is_word}] | Value: [#{word.capitalize}]"
52
- #end
53
-
54
- if is_word
55
- @words << { word: word, monkey: monkey }
56
- end
38
+ def update(_time, action, param)
39
+ return unless param.is_a?(Hash) && param.key?(:action)
40
+
41
+ if param[:action].is_a?(MonkeyActionType) && action == :action_completed
42
+ monkey = param[:action].monkey.monkey_symbol.to_s
43
+ is_word = param[:action].keyboard_input.is_word
44
+ word = param[:action].keyboard_input.input_to_s
45
+
46
+ message = "Monkey: [#{monkey.capitalize}] | Is Word: [#{is_word}] | Value: [#{word.capitalize}]"
47
+ if is_word
48
+ @monkey_words[monkey] = {} unless @monkey_words[monkey]
49
+ @monkey_words[monkey][word] = 0 unless @monkey_words.dig(monkey, word)
50
+ times = @monkey_words[monkey][word] += 1
51
+ puts "#{message} | Times: #{times} so far!".colorize(color: :green, mode: :bold)
52
+ return
57
53
  end
58
- rescue Exception => e
59
- puts "Exception: #{e}"
54
+
55
+ puts message
60
56
  end
57
+ rescue StandardError => e
58
+ puts "StandardError: #{e}"
61
59
  end
62
60
  end
63
61
  end
64
62
 
65
-
66
63
  namespace :engine do
67
- desc "Run the MonkeyEngine"
64
+ desc 'Run the MonkeyEngine'
68
65
  task :run do
69
- runtime = 15 #ARGV[0]
66
+ runtime = 60 * 2
70
67
 
71
68
  service = MonkeyEngine::MonkeyService.instance
72
69
 
73
- LittleWeasel::Checker.instance.options = {exclude_alphabet: true, strip_whitespace: false, ignore_numeric: false}
74
-
75
70
  runner = Runner::MonkeyRun.new service, runtime
76
71
  runner.thread.join
77
72
 
@@ -79,25 +74,39 @@ namespace :engine do
79
74
  service.join_all(10)
80
75
  sleep(3)
81
76
 
82
- runner.words.sort!{|a,b| a[:monkey]<=>b[:monkey]}
77
+ monkey_words_sorted = runner.monkey_words.sort_by { |key, _value| key }
83
78
 
84
79
  puts '-----------------------------------------'
85
- puts "Valid words:"
86
-
87
- runner.words.each_with_index { |word, index|
88
- if word[:word].length > 2
89
- puts "#{index + 1}. Monkey [#{word[:monkey].capitalize}] typed [#{word[:word].capitalize}] <=== #{word[:word].length}-letter word!"
90
- else
91
- puts "#{index + 1}. Monkey [#{word[:monkey].capitalize}] typed [#{word[:word].capitalize}]"
80
+ puts 'Valid words:'
81
+
82
+ word_index = 0
83
+
84
+ monkey_words_sorted.each do |monkey_word_info|
85
+ monkey = monkey_word_info[0]
86
+ monkey_word_info[1].each do |monkey_word|
87
+ word_index += 1
88
+ word = monkey_word[0]
89
+ times = monkey_word[1]
90
+ if word.length > 1
91
+ puts "#{word_index}. Monkey [#{monkey.capitalize}] typed [#{word.capitalize}] [#{times}] time(s) " \
92
+ "<=== #{word.length}-letter word!".colorize(
93
+ color: :green, mode: :bold
94
+ )
95
+ else
96
+ puts "#{word_index}. Monkey [#{monkey.capitalize}] typed [#{word.capitalize}] [#{times}] time(s)"
97
+ .colorize(color: :green)
98
+ end
92
99
  end
93
- }
100
+ end
101
+ total_valid_words_count = runner.monkey_words.inject(0) do |sum, monkey_word_info|
102
+ sum + monkey_word_info[1].keys.count
103
+ end
94
104
 
95
105
  puts '-----------------------------------------'
96
- puts "Total valid words: #{runner.words.count}"
106
+ puts "Total valid words: #{total_valid_words_count}"
107
+ puts 'These monkeys can type!' if total_valid_words_count.positive?
97
108
  puts 'Done.'
98
109
  puts
99
110
  puts
100
111
  end
101
112
  end
102
-
103
-