Olib 0.1.0 → 0.1.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.
@@ -1,4 +1,2 @@
1
- module Olib
2
- class Uncommon < Item
3
- end
1
+ class Uncommon < Olib::Item
4
2
  end
@@ -1,4 +1,2 @@
1
- module Olib
2
- class Wand < Item
3
- end
1
+ class Wand < Olib::Item
4
2
  end
@@ -0,0 +1,34 @@
1
+ ##
2
+ ## @brief pattern matching for Ruby
3
+ ##
4
+ class Pattern
5
+ attr_accessor :cases
6
+
7
+ def initialize(cases)
8
+ @cases = cases
9
+ end
10
+
11
+ def match(str)
12
+ found = @cases.each_pair.find do |exp, handler|
13
+ str =~ exp
14
+ end
15
+
16
+ if !found
17
+ raise Exception.new [
18
+ "Error: inexhaustive pattern",
19
+ "counterexample: #{str}",
20
+ ].join("\n")
21
+ end
22
+
23
+ exp, handler = found
24
+
25
+ handler.call exp.match(str).to_struct
26
+ end
27
+
28
+ def to_proc
29
+ patt = self
30
+ Proc.new do |str|
31
+ patt.match str
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,31 @@
1
+ require 'fileutils'
2
+
3
+ module Olib
4
+ class App
5
+ APP_DIR = Dir.home + "/." + self.name.downcase
6
+ ##
7
+ ## setup app dir
8
+ ##
9
+ FileUtils.mkdir_p APP_DIR
10
+
11
+ def self.app_file(path)
12
+ APP_DIR + "/" + path
13
+ end
14
+
15
+ def self.open(file)
16
+ File.open(app_file(file), 'a', &block)
17
+ end
18
+
19
+ def self.write(file, data)
20
+ open(file) do |f|
21
+ JSON.stringify(data)
22
+ end
23
+ end
24
+
25
+ def self.read(file, &block)
26
+ open(file) do |file|
27
+ yield
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,205 @@
1
+ require "ostruct"
2
+ require "fiber"
3
+ require "thread"
4
+ ##
5
+ ## @brief Class for Supervisor.
6
+ ##
7
+ class Supervisor
8
+ ##
9
+ ## the amount of time in seconds between each yield
10
+ ##
11
+ TICK = 0.1
12
+ TREE = {}
13
+
14
+ def self.register(supervisor)
15
+ TREE[supervisor.name] = supervisor
16
+ end
17
+
18
+ def self.fetch(name)
19
+ TREE.fetch(name)
20
+ end
21
+
22
+ def self.unregister(supervisor)
23
+ TREE.delete(supervisor.name)
24
+ end
25
+
26
+ def self.exists?(name)
27
+ TREE.has_key?(name)
28
+ end
29
+
30
+ attr_accessor :fibers, :cleanup, :name, :debug
31
+ ##
32
+ ## @brief creates a Supervisor instance
33
+ ##
34
+ ## @return self
35
+ ##
36
+ def initialize(name)
37
+ supervisor = self
38
+
39
+ if Supervisor.exists?(name)
40
+ raise Exception.new "a Supervisor with the name #{name} already exists. They are required to be unique"
41
+ end
42
+
43
+ @fibers = []
44
+ @pre_hooks = []
45
+ @post_hooks = []
46
+ @name = name
47
+ @debug = false
48
+ @cleanup = [Proc.new do
49
+ Supervisor.unregister(supervisor)
50
+ end]
51
+
52
+ Supervisor.register(supervisor)
53
+ before_dying { cleanup! }
54
+ end
55
+ ##
56
+ ## @brief returns if the Supervisor is in debug mode
57
+ ##
58
+ ## @return Boolean
59
+ ##
60
+ def debug?
61
+ @debug
62
+ end
63
+ ##
64
+ ## @brief turn on debug mode for a Supervisor
65
+ ##
66
+ ## @return self
67
+ ##
68
+ def debug!
69
+ @debug = true
70
+ self
71
+ end
72
+
73
+ def debug(msg)
74
+ if debug?
75
+ _respond(msg)
76
+ end
77
+ end
78
+ ##
79
+ ## @brief add a hook to the Supervisor tree
80
+ ##
81
+ ## @param name The name
82
+ ## @param hook The hook
83
+ ##
84
+ ## @return self
85
+ ##
86
+ def add(name, &hook)
87
+ @fibers << [name, Fiber.new do
88
+ loop do
89
+ Fiber.yield hook.call
90
+ end
91
+ end]
92
+ self
93
+ end
94
+ ##
95
+ ## @brief hooks that run before every tick
96
+ ##
97
+ ## @param name The name
98
+ ## @param hook The hook
99
+ ##
100
+ ## @return self
101
+ ##
102
+ def pre_hook(name, &hook)
103
+ @pre_hooks << [name, Fiber.new do
104
+ loop do
105
+ Fiber.yield hook.call
106
+ end
107
+ end]
108
+ self
109
+ end
110
+ ##
111
+ ## @brief hooks that run after every tick
112
+ ##
113
+ ## @param name The name
114
+ ## @param hook The hook
115
+ ##
116
+ ## @return self
117
+ ##
118
+ def post_hook(name, &hook)
119
+ @post_hooks << [name, Fiber.new do
120
+ loop do
121
+ Fiber.yield hook.call
122
+ end
123
+ end]
124
+ self
125
+ end
126
+ ##
127
+ ## @brief add a cleanup task to the Supervisor tree
128
+ ##
129
+ ## @param hook The hook
130
+ ##
131
+ ## @return self
132
+ ##
133
+ def cleanup(&hook)
134
+ debug "cleanup -> #{@fibers.size} fibers -> [" + @fibers.map(&:first).join(", ") + "]"
135
+ debug "cleanup -> #{@cleanup.size} procs"
136
+ @cleanup << hook
137
+ self
138
+ end
139
+ ##
140
+ ## @brief cleanup a Supervisor tree
141
+ ##
142
+ ## @return self
143
+ ##
144
+ def cleanup!
145
+ @cleanup.each(&:call)
146
+ self
147
+ end
148
+ ##
149
+ ## @brief attach a supervisor tree to the current Script
150
+ ##
151
+ ## @return nil
152
+ ##
153
+ def link!
154
+ loop do
155
+ run!
156
+ sleep TICK
157
+ end
158
+ nil
159
+ end
160
+ ##
161
+ ## @brief one event loop of the Supervisor
162
+ ##
163
+ ## @return nil
164
+ ##
165
+ def run!
166
+ @fibers.map(&:last).each do |fiber|
167
+ run_pre_hooks!
168
+ fiber.resume
169
+ run_post_hooks!
170
+ end
171
+ end
172
+ def run_post_hooks!
173
+ @post_hooks.map(&:last).map(&:resume)
174
+ end
175
+
176
+ def run_pre_hooks!
177
+ @pre_hooks.map(&:last).map(&:resume)
178
+ end
179
+ ##
180
+ ## @brief converts a Supervisor to a Proc, useful for building
181
+ ## trees from Named Supervisors
182
+ ##
183
+ ## @return Proc
184
+ ##
185
+ def to_proc
186
+ supervisor = self
187
+ return Proc.new do
188
+ debug "running child supervisor :#{supervisor.name}"
189
+ supervisor.run!
190
+ end
191
+ end
192
+ ##
193
+ ## @brief build a supervisor tree by joining two supervisors
194
+ ##
195
+ ## @param supervisor The supervisor
196
+ ##
197
+ ## @return self
198
+ ##
199
+ def join(*supervisors)
200
+ supervisors.each do |supervisor|
201
+ add supervisor.name, &supervisor.to_proc
202
+ end
203
+ self
204
+ end
205
+ end
@@ -0,0 +1,58 @@
1
+ class Try
2
+ attr_accessor :result, :task
3
+
4
+ def self.of(&task)
5
+ Proc.new do
6
+ Try.new task
7
+ end
8
+ end
9
+
10
+ def initialize(&task)
11
+ @task = task
12
+ run!
13
+ end
14
+
15
+ private def run!
16
+ begin
17
+ result = @task.call
18
+ # handle recursives
19
+ if result.is_a?(Try)
20
+ @result = result.result
21
+ else
22
+ @result = result
23
+ end
24
+ rescue Exception => e
25
+ @result = e
26
+ end
27
+ end
28
+
29
+ def failed?
30
+ @result.class.ancestors.include? Exception
31
+ end
32
+
33
+ def success?
34
+ !failed?
35
+ end
36
+
37
+ def recover
38
+ Try.new { yield @result } if failed?
39
+ end
40
+
41
+ def then
42
+ Try.new { yield @result } if success?
43
+ end
44
+
45
+ def match?(exp)
46
+ if failed?
47
+ @result.message.match(exp)
48
+ elsif @result.respond_to?(:match)
49
+ @result.match(exp)
50
+ else
51
+ raise Exception.new "cannot match class #{@result.class}"
52
+ end
53
+ end
54
+ end
55
+
56
+ def try(&block)
57
+ Try.new &block
58
+ end
@@ -1,3 +1,3 @@
1
1
  module Olib
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: Olib
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ondreian Shamsiel
@@ -21,22 +21,26 @@ files:
21
21
  - README.md
22
22
  - TODOS.md
23
23
  - lib/Olib.rb
24
+ - lib/Olib/actor/actor.rb
24
25
  - lib/Olib/area.rb
25
26
  - lib/Olib/bounty.rb
26
27
  - lib/Olib/character/char.rb
28
+ - lib/Olib/character/disk.rb
27
29
  - lib/Olib/character/group.rb
28
30
  - lib/Olib/character/inventory.rb
29
31
  - lib/Olib/character/mind.rb
32
+ - lib/Olib/character/stance.rb
30
33
  - lib/Olib/combat/creature.rb
31
34
  - lib/Olib/combat/creatures.rb
32
35
  - lib/Olib/core/container.rb
33
36
  - lib/Olib/core/errors.rb
34
37
  - lib/Olib/core/extender.rb
35
38
  - lib/Olib/core/item.rb
39
+ - lib/Olib/core/use.rb
36
40
  - lib/Olib/core/utils.rb
37
41
  - lib/Olib/dictionary/dictionary.rb
38
- - lib/Olib/events/emitter.rb
39
42
  - lib/Olib/go2.rb
43
+ - lib/Olib/interface/queryable.rb
40
44
  - lib/Olib/npcs.rb
41
45
  - lib/Olib/objects/box.rb
42
46
  - lib/Olib/objects/clothing.rb
@@ -46,9 +50,12 @@ files:
46
50
  - lib/Olib/objects/jewelry.rb
47
51
  - lib/Olib/objects/scroll.rb
48
52
  - lib/Olib/objects/uncommon.rb
49
- - lib/Olib/objects/unknown.rb
50
53
  - lib/Olib/objects/wand.rb
54
+ - lib/Olib/pattern.rb
51
55
  - lib/Olib/shops.rb
56
+ - lib/Olib/storage/app_data.rb
57
+ - lib/Olib/supervisor/supervisor.rb
58
+ - lib/Olib/try/try.rb
52
59
  - lib/Olib/utils/cli.rb
53
60
  - lib/Olib/utils/help_menu.rb
54
61
  - lib/Olib/utils/monsterbold.rb
@@ -1,7 +0,0 @@
1
- class Emitter
2
- @@channels = Hash.new
3
-
4
- def Emitter.run
5
-
6
- end
7
- end
@@ -1,4 +0,0 @@
1
- module Olib
2
- class Unknown < Item
3
- end
4
- end