MonkeyEngine 1.0.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +19 -16
  3. data/.rspec +3 -0
  4. data/.ruby-version +1 -1
  5. data/CHANGELOG.md +6 -0
  6. data/Gemfile +3 -1
  7. data/Gemfile.lock +71 -0
  8. data/README.md +5 -1
  9. data/Rakefile +3 -2
  10. data/dictionaries/en-US.txt +466554 -0
  11. data/lib/Action/action.rb +5 -4
  12. data/lib/Monkey/monkey.rb +17 -12
  13. data/lib/Monkey.rb +3 -1
  14. data/lib/MonkeyAction/monkey_action.rb +3 -1
  15. data/lib/MonkeyAction/monkey_action_dead.rb +8 -6
  16. data/lib/MonkeyAction/monkey_action_eat.rb +6 -4
  17. data/lib/MonkeyAction/monkey_action_pause.rb +6 -5
  18. data/lib/MonkeyAction/monkey_action_sleep.rb +6 -4
  19. data/lib/MonkeyAction/monkey_action_type.rb +15 -10
  20. data/lib/MonkeyAction/monkey_action_wake.rb +8 -6
  21. data/lib/MonkeyAction/monkey_timed_action.rb +6 -4
  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 +13 -10
  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 -2
  31. data/lib/MonkeyKeyboard/keyboard_input.rb +4 -3
  32. data/lib/MonkeyKeyboard/keyboard_key.rb +3 -1
  33. data/lib/MonkeyKeyboard/keyboard_key_evaluator.rb +5 -7
  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 +13 -11
  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 +64 -47
  41. data/monkeyengine.gemspec +22 -20
  42. data/spec/action_rules_spec.rb +9 -9
  43. data/spec/engine_spec.rb +5 -10
  44. data/spec/keyboard_char_spec.rb +3 -4
  45. data/spec/keyboard_key_spec.rb +3 -4
  46. data/spec/monkey_action_eat_spec.rb +14 -15
  47. data/spec/monkey_action_pause_spec.rb +15 -16
  48. data/spec/monkey_action_sleep_spec.rb +15 -16
  49. data/spec/monkey_action_type_spec.rb +22 -21
  50. data/spec/monkey_action_wake_spec.rb +6 -7
  51. data/spec/monkey_factory_spec.rb +5 -5
  52. data/spec/monkey_keyboard_en_us_spec.rb +8 -14
  53. data/spec/monkey_manager_spec.rb +2 -1
  54. data/spec/monkey_service_spec.rb +15 -18
  55. data/spec/monkey_spec.rb +2 -1
  56. data/spec/{spec_helpers.rb → spec_helper.rb} +6 -4
  57. data/spec/support/shared_examples.rb +12 -12
  58. metadata +72 -56
  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,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 do |config| end
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.collect { |key| keyboard_key_evaluator.get_char(key) }.compact
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*.
@@ -88,7 +89,8 @@ module MonkeyEngine
88
89
  return @monkeys.select { |m| m.monkey_symbol == monkey.to_sym }.first if monkey.is_a? String
89
90
  return @monkeys.select { |m| m.monkey_symbol == monkey.monkey_symbol }.first 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,91 +1,108 @@
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 do |config| end
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
- }
28
+ end
26
29
 
27
30
  self
28
31
  end
29
32
 
30
- #protected
31
-
32
33
  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)
34
+ @monkey_service.add(MonkeyFactory.create(:groucho))
35
+ @monkey_service.add(MonkeyFactory.create(:harpo))
36
+ @monkey_service.add(MonkeyFactory.create(:chico))
37
+ @monkey_service.add(MonkeyFactory.create(:zeppo))
37
38
  end
38
39
 
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
40
+ def update(_time, action, param)
41
+ return unless param.is_a?(Hash) && param.key?(:action)
42
+
43
+ if param[:action].is_a?(MonkeyActionType) && action == :action_completed
44
+ monkey = param[:action].monkey.monkey_symbol.to_s
45
+ is_word = param[:action].keyboard_input.is_word
46
+ word = param[:action].keyboard_input.input_to_s
47
+
48
+ message = "Monkey: [#{monkey.capitalize}] | Is Word: [#{is_word}] | Value: [#{word.capitalize}]"
49
+ if is_word
50
+ @monkey_words[monkey] = {} unless @monkey_words[monkey]
51
+ @monkey_words[monkey][word] = 0 unless @monkey_words.dig(monkey, word)
52
+ times = @monkey_words[monkey][word] += 1
53
+ puts "#{message} | Times: #{times} so far!".colorize(color: :green, mode: :bold)
54
+ return
57
55
  end
58
- rescue Exception => e
59
- puts "Exception: #{e}"
56
+
57
+ puts message
60
58
  end
59
+ rescue StandardError => e
60
+ puts "StandardError: #{e}"
61
61
  end
62
62
  end
63
63
  end
64
64
 
65
-
66
65
  namespace :engine do
67
- desc "Run the MonkeyEngine"
66
+ desc 'Run the MonkeyEngine'
68
67
  task :run do
69
- runtime = 10 #ARGV[0]
68
+ runtime = 60 * 2
70
69
 
71
70
  service = MonkeyEngine::MonkeyService.instance
72
71
 
73
- LittleWeasel::Checker.instance.options = {exclude_alphabet: true, strip_whitespace: false, ignore_numeric: false}
74
-
75
72
  runner = Runner::MonkeyRun.new service, runtime
76
73
  runner.thread.join
77
74
 
78
75
  service.kill_all!
79
76
  service.join_all(10)
77
+ sleep(3)
80
78
 
81
- puts "Total valid words: #{runner.words.count}"
79
+ monkey_words_sorted = runner.monkey_words.sort_by { |key, _value| key }
82
80
 
83
- runner.words.sort!{|a,b| a[:monkey]<=>b[:monkey]}
81
+ puts '-----------------------------------------'
82
+ puts 'Valid words:'
84
83
 
85
- runner.words.each { |word|
86
- puts "Monkey [#{word[:monkey].capitalize}] typed [#{word[:word].capitalize}]"
87
- }
84
+ word_index = 0
85
+
86
+ monkey_words_sorted.each do |monkey_word_info|
87
+ monkey = monkey_word_info[0]
88
+ monkey_word_info[1].each do |monkey_word|
89
+ word_index += 1
90
+ word = monkey_word[0]
91
+ times = monkey_word[1]
92
+ if word.length > 1
93
+ puts "#{word_index}. Monkey [#{monkey.capitalize}] typed [#{word.capitalize}] [#{times}] time(s) <=== #{word.length}-letter word!".colorize(color: :green, mode: :bold)
94
+ else
95
+ puts "#{word_index}. Monkey [#{monkey.capitalize}] typed [#{word.capitalize}] [#{times}] time(s)".colorize(color: :green)
96
+ end
97
+ end
98
+ end
99
+ total_valid_words_count = runner.monkey_words.inject(0) { |sum, monkey_word_info| sum + monkey_word_info[1].keys.count }
100
+
101
+ puts '-----------------------------------------'
102
+ puts "Total valid words: #{total_valid_words_count}"
103
+ puts 'These monkeys can type!' if total_valid_words_count > 0
104
+ puts 'Done.'
105
+ puts
106
+ puts
88
107
  end
89
108
  end
90
-
91
-
data/monkeyengine.gemspec CHANGED
@@ -1,32 +1,34 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
1
+ # frozen_string_literal: true
2
+
3
+ require 'English'
4
+
5
+ lib = File.expand_path('lib', __dir__)
3
6
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
7
  require 'MonkeyEngine/version'
5
8
 
6
9
  Gem::Specification.new do |spec|
7
- spec.name = "MonkeyEngine"
10
+ spec.name = 'MonkeyEngine'
8
11
  spec.version = MonkeyEngine::VERSION
9
- spec.authors = ["Gene M. Angelo, Jr."]
10
- spec.email = ["public.gma@gmail.com"]
11
- spec.description = %q{MonkeyEngine - FUN!}
12
- spec.summary = %q{The engine that drives my monkeys!}
13
- spec.homepage = "http://www.geneangelo.com"
14
- spec.license = "MIT"
12
+ spec.authors = ['Gene M. Angelo, Jr.']
13
+ spec.email = ['public.gma@gmail.com']
14
+ spec.description = "Have some fun! MonkeyEngine is a gem that allows virtual monkeys to tap away on a virtual keyboard! Can any of them complete a sentence? write a book? The sky's the limit! Add your own AI! Publish the results! Go...BANANAS!"
15
+ spec.summary = 'The engine that drives my monkeys!'
16
+ spec.homepage = 'https://github.com/gangelo/monkeyengine'
17
+ spec.license = 'MIT'
15
18
 
16
- spec.files = `git ls-files`.split($/)
19
+ spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
17
20
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
21
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
- spec.require_paths = ["lib"]
20
-
21
- spec.required_ruby_version = '~> 2.1'
22
+ spec.require_paths = ['lib']
22
23
 
23
- spec.add_development_dependency 'bundler', '~> 1.3'
24
- spec.add_development_dependency 'rake', '~> 0'
25
- spec.add_development_dependency 'rspec', '~> 3.0', '>= 3.0.0'
26
- spec.add_development_dependency 'yard', '0.8.6.2'
24
+ spec.add_development_dependency 'bundler', '~> 2.3', '>= 2.3.20'
25
+ spec.add_development_dependency 'colorize', '~> 0.8.1'
26
+ spec.add_development_dependency 'pry-byebug', '~> 3.9'
27
+ spec.add_development_dependency 'rake', '~> 13.0', '>= 13.0.6'
27
28
  spec.add_development_dependency 'redcarpet', '~> 2.3', '>= 2.3.0'
28
- spec.add_development_dependency 'pry'
29
+ spec.add_development_dependency 'rspec', '~> 3.10'
30
+ spec.add_development_dependency 'yard', '~> 0.9.28'
29
31
 
30
- spec.add_runtime_dependency 'LittleWeasel', '~> 3.0'
31
- spec.add_runtime_dependency 'ProtectedConstructor', '~> 2.0', '>=2.0.0'
32
+ spec.add_runtime_dependency 'LittleWeasel', '~> 5.0', '>= 5.0.3'
33
+ spec.add_runtime_dependency 'ProtectedConstructor', '~> 2.0', '>= 2.0.3'
32
34
  end