Olib 0.1.2 → 2.0.0.pre.rc.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 (69) hide show
  1. checksums.yaml +5 -5
  2. data/Olib.gemspec +1 -1
  3. data/README.md +0 -0
  4. data/TODOS.md +0 -0
  5. data/lib/Olib.rb +6 -79
  6. data/lib/Olib/actor/actor.rb +0 -0
  7. data/lib/Olib/area.rb +22 -37
  8. data/lib/Olib/bounty.rb +8 -10
  9. data/lib/Olib/character/char.rb +64 -68
  10. data/lib/Olib/character/disk.rb +31 -9
  11. data/lib/Olib/character/group.rb +122 -40
  12. data/lib/Olib/character/inventory.rb +0 -0
  13. data/lib/Olib/character/mind.rb +0 -0
  14. data/lib/Olib/character/stance.rb +0 -0
  15. data/lib/Olib/combat/creature.rb +77 -128
  16. data/lib/Olib/combat/creatures.rb +52 -36
  17. data/lib/Olib/core/action.rb +8 -0
  18. data/lib/Olib/core/container.rb +32 -236
  19. data/lib/Olib/core/containers.rb +42 -0
  20. data/lib/Olib/core/errors.rb +69 -71
  21. data/lib/Olib/core/exist.rb +88 -0
  22. data/lib/Olib/core/item.rb +43 -598
  23. data/lib/Olib/core/kinds.rb +6 -0
  24. data/lib/Olib/core/rummage.rb +42 -0
  25. data/lib/Olib/core/transaction.rb +53 -0
  26. data/lib/Olib/core/use.rb +2 -5
  27. data/lib/Olib/core/utils.rb +25 -123
  28. data/lib/Olib/core/verbs.rb +304 -0
  29. data/lib/Olib/dictionary/dictionary.rb +150 -150
  30. data/lib/Olib/ext/hash.rb +7 -0
  31. data/lib/Olib/ext/matchdata.rb +14 -0
  32. data/lib/Olib/ext/string.rb +9 -0
  33. data/lib/Olib/ext/symbol.rb +13 -0
  34. data/lib/Olib/go2.rb +48 -112
  35. data/lib/Olib/loot.rb +44 -0
  36. data/lib/Olib/npcs/npc.rb +4 -0
  37. data/lib/Olib/npcs/npcs.rb +45 -0
  38. data/lib/Olib/objects/box.rb +1 -1
  39. data/lib/Olib/objects/clothing.rb +1 -1
  40. data/lib/Olib/objects/herb.rb +1 -1
  41. data/lib/Olib/objects/jar.rb +0 -0
  42. data/lib/Olib/objects/jewel.rb +7 -7
  43. data/lib/Olib/objects/jewelry.rb +1 -1
  44. data/lib/Olib/objects/scroll.rb +1 -1
  45. data/lib/Olib/objects/uncommon.rb +1 -1
  46. data/lib/Olib/objects/wand.rb +1 -1
  47. data/lib/Olib/pattern_matching/any.rb +11 -0
  48. data/lib/Olib/pattern_matching/err.rb +4 -0
  49. data/lib/Olib/pattern_matching/ok.rb +4 -0
  50. data/lib/Olib/pattern_matching/outcome.rb +35 -0
  51. data/lib/Olib/pattern_matching/pattern_matching.rb +5 -0
  52. data/lib/Olib/pattern_matching/result.rb +80 -0
  53. data/lib/Olib/pattern_matching/rill.rb +43 -0
  54. data/lib/Olib/pattern_matching/where.rb +4 -0
  55. data/lib/Olib/shops.rb +147 -155
  56. data/lib/Olib/supervisor/supervisor.rb +0 -0
  57. data/lib/Olib/version.rb +1 -1
  58. data/lib/Olib/xml.rb +43 -0
  59. metadata +28 -15
  60. data/lib/Olib/core/extender.rb +0 -29
  61. data/lib/Olib/interface/queryable.rb +0 -50
  62. data/lib/Olib/npcs.rb +0 -5
  63. data/lib/Olib/pattern.rb +0 -34
  64. data/lib/Olib/storage/app_data.rb +0 -32
  65. data/lib/Olib/try/try.rb +0 -58
  66. data/lib/Olib/utils/cli.rb +0 -81
  67. data/lib/Olib/utils/help_menu.rb +0 -166
  68. data/lib/Olib/utils/monsterbold.rb +0 -5
  69. data/lib/Olib/utils/vbulletin.rb +0 -101
@@ -1,25 +1,29 @@
1
- require 'Olib/interface/queryable'
2
1
  require 'net/http'
3
2
  require 'json'
4
3
  # a collection for managing all of the creatures in a room
5
4
 
6
- class Creatures < Interface::Queryable
5
+ class Creatures
6
+ include Enumerable
7
7
 
8
8
  METADATA_URL = "https://cdn.rawgit.com/ondreian/gemstone_data_project/c40a5dfb/creatures.json"
9
9
 
10
- ARCHETYPES = [
11
- :undead, :living, :weak, :grimswarm,
12
- :antimagic, :flying, :lowly, :bandit,
13
- :aggressive,
10
+ ARCHETYPES = %i[
11
+ undead living weak
12
+ grimswarm antimagic flying
13
+ lowly bandit aggressive
14
14
  ]
15
15
 
16
- STATES = [
17
- :dead, :sleeping, :webbed, :immobile,
18
- :stunned, :prone, :sitting,
19
- :kneeling, :flying
16
+ STATES = %i[
17
+ prone sitting kneeling
18
+ dead
19
+ sleeping webbed immobile
20
+ stunned
21
+ flying
20
22
  ]
21
23
 
22
- def Creatures.fetch_metadata!
24
+ KINDS = ARCHETYPES + STATES
25
+
26
+ def self.fetch_metadata()
23
27
  begin
24
28
  JSON.parse Net::HTTP.get URI METADATA_URL
25
29
  rescue
@@ -29,46 +33,58 @@ class Creatures < Interface::Queryable
29
33
  end
30
34
  end
31
35
 
32
- METADATA = fetch_metadata!
36
+ METADATA = fetch_metadata()
33
37
  BY_NAME = METADATA.reduce(Hash.new) do |by_name, record|
34
38
  by_name[record["name"]] = record
35
39
  by_name
36
40
  end
37
41
 
38
- def Creatures.unsafe
39
- (GameObj.npcs || [])
40
- .map do |obj| Creature.new obj end
41
- .reject do |creature| creature.gone? end
42
+ ALL = -> creature { true }
43
+
44
+ attr_reader :predicate
45
+
46
+ def initialize(&predicate)
47
+ @predicate = predicate
42
48
  end
43
49
 
44
- def Creatures.fetch
45
- unsafe.select do |creature|
46
- creature.aggressive?
47
- end.reject do |creature|
48
- creature.tags.include?(:companion) ||
49
- creature.tags.include?(:familiar) ||
50
- creature.gone? ||
51
- creature.name =~ /nest/
52
- end
50
+ def each()
51
+ GameObj.npcs.to_a.map do |obj| Creature.new(obj) end
52
+ .select(&@predicate)
53
+ .each do |creature| yield(creature) if GameObj[creature.id] and creature.aggressive? end
54
+ end
55
+
56
+ def respond_to_missing?(method, include_private = false)
57
+ to_a.respond_to?(method) or super
53
58
  end
54
59
 
55
- [ARCHETYPES, STATES].flatten.each do |state|
56
- Creatures.define_singleton_method(state) do
57
- select do |creature|
58
- [creature.tags, creature.status].flatten.include?(state)
59
- end
60
+ def method_missing(method, *args)
61
+ if to_a.respond_to?(method)
62
+ to_a.send(method, *args)
63
+ else
64
+ super(method, *args)
60
65
  end
61
66
  end
62
67
 
63
- def self.living
64
- reject do |creature|
65
- creature.undead?
68
+ KINDS.each do |kind|
69
+ define_method(kind) do
70
+ Creatures.new do |creature| [creature.tags, creature.status].flatten.include?(kind) end
66
71
  end
67
72
  end
68
73
 
69
- def self.bounty
70
- select do |creature|
71
- creature.name.include?(Bounty.creature)
74
+ def bounty
75
+ Creatures.new do |creature| creature.name.include?(Bounty.creature) end
76
+ end
77
+
78
+ def self.method_missing(method, *args, &block)
79
+ if respond_to?(method)
80
+ Creatures.new.send(method, *args, &block)
81
+ else
82
+ super(method, *args, &block)
72
83
  end
73
84
  end
85
+
86
+ def self.respond_to?(method)
87
+ return super(method) unless Creatures.new.respond_to?(method)
88
+ return true
89
+ end
74
90
  end
@@ -0,0 +1,8 @@
1
+ module Action
2
+ def self.try_or_fail(seconds: 5, command: nil)
3
+ fput(command)
4
+ expiry = Time.now + seconds
5
+ wait_until do yield or Time.now > expiry end
6
+ Err[command: command, seconds: seconds, reason: :not_found] if Time.now > expiry
7
+ end
8
+ end
@@ -1,266 +1,62 @@
1
1
  # for defining containers ala lootsack and using them across scripts
2
- require "Olib/core/extender"
2
+ require "Olib/core/exist"
3
3
  require "Olib/core/item"
4
-
5
- class Regexp
6
- def or(re)
7
- Regexp.new self.to_s + "|" + re.to_s
8
- end
9
- end
10
-
11
- def class_exists?(class_name)
12
- klass = Module.const_get(class_name)
13
- return klass.is_a?(Class)
14
- rescue NameError
15
- return false
16
- end
4
+ require "Olib/core/action"
17
5
 
18
6
  class GameObj
19
7
  def to_container
20
- Olib::Container.new self.id
21
- end
22
- end
23
-
24
- module Olib
25
- class Container < Gameobj_Extender
26
- attr_accessor :ref, :nested, :containers, :ontop
27
-
28
-
29
- Item.type_methods.each do |method, tag|
30
- type =tag.split.map(&:capitalize).join('_')
31
- if class_exists?(type)
32
- define_method(method.to_sym) do
33
- find_by_tags(tag).map do |item|
34
- Kernel.const_get(type).new item
35
- end
36
- end
37
- else
38
- define_method(method.to_sym) do find_by_tags(tag) end
39
- end
40
- end
41
-
42
-
43
- def initialize(id=nil)
44
- # extract the class name to attempt to lookup the item by your settings
45
- # ex: class Lootsack
46
- # ex: class Gemsack
47
- name = if self.class.name.include?("::") then self.class.name.downcase.split("::").last.strip else self.class.name.downcase end
48
- candidates = Inventory[Vars[name]]
49
- raise Olib::Errors::DoesntExist.new("#{name} could not be initialized are you sure you:\n ;var set #{name}=<something>") if candidates.empty? && id.nil?
50
- @id = id
51
- @ref = GameObj[id] || candidates.first
52
- @ontop = Array.new
53
-
54
- unless GameObj[@ref.id].contents
55
- tops = [
56
- "table"
57
- ]
58
-
59
- action = tops.include?(@ref.noun) ? "look on ##{@ref.id}" : "look in ##{@ref.id}"
60
-
61
- fput action
62
- end
63
-
64
- super @ref
65
-
66
- end
67
-
68
- def contents
69
- [ @ontop,
70
- GameObj[@ref.id].contents.map do |item| Item.new(item, self) end
71
- ].flatten
72
- end
73
-
74
- def where(conditions)
75
- contents.select { |item|
76
- !conditions.keys.map { |key|
77
- if conditions[key].class == Array
78
- item.props[key].class == Array && !conditions[key].map { |ele| item.props[key].include? ele }.include?(false)
79
- else
80
- item.props[key] == conditions[key]
81
- end
82
- }.include?(false)
83
- }
84
- end
85
-
86
- def find_by_tags(*tags)
87
- contents.select { |item|
88
- !tags.map {|tag| item.is?(tag) }.include?(false)
89
- }
90
- end
91
-
92
-
93
- def [](query)
94
- return contents.select do |item|
95
- item if (item.type =~ query || item.noun =~ query || item.name =~ query)
96
- end
97
- end
98
-
99
- def
100
-
101
- def __verbs__
102
- @verbs = "open close analyze inspect weigh".split(" ").map(&:to_sym)
103
- singleton = (class << self; self end)
104
- @verbs.each do |verb|
105
- singleton.send :define_method, verb do
106
- fput "#{verb.to_s} ##{@id}"
107
- self
108
- end
109
- end
110
- end
111
-
112
- def at
113
- Olib.wrap_stream("look at ##{@id}") { |line|
114
- if line =~ /You see nothing unusual|prompt time|You gaze through (.*?) and see...|written/
115
- raise Olib::Errors::Mundane
116
- end
117
-
118
- if line =~ /Looking at the (.*?), you see (?<nested>.*)/
119
- @nested = true
120
-
121
- @containers = line
122
- .match(/Looking at the (.*?), you see (?<nested>.*)/)[:nested]
123
- .scan(/<a exist="(?<id>.*?)" noun="(?<noun>.*?)">(?<name>.*?)<\/a>/)
124
- .map {|matches| Container.new GameObj.new *matches }
125
- raise Olib::Errors::Mundane
126
- end
127
-
128
- }
129
- self
130
- end
131
-
132
- def look
133
- self
134
- end
135
-
136
- def on
137
- return self unless @id
138
- Olib.wrap_stream("look on ##{@id}") { |line|
139
- raise Olib::Errors::Mundane if line =~ /There is nothing on there|prompt time/
140
- if line =~ /On the (.*?) you see/
141
- @ontop << line.match(Dictionary.contents)[:items]
142
- .scan(Dictionary.tag)
143
- .map {|matches| Item.new GameObj.new *matches }
144
- raise Olib::Errors::Mundane
145
- end
146
- next
147
- }
148
- self
149
- end
150
-
151
- def in
152
- fput "look in ##{@id}"
153
- self
154
- end
155
-
156
- def rummage
157
- Rummage.new(self)
158
- end
159
-
160
- def nested?
161
- @nested
162
- end
163
-
164
- def full?
165
- is? "full"
166
- end
167
-
168
- def add(*items)
169
- _id = @id
170
- items.each { |item|
171
-
172
- result = Olib.do "_drag ##{item.class == String ? item : item.id} ##{_id}", /#{[Olib::Dictionary.put[:success], Olib::Dictionary.put[:failure].values].flatten.join("|")}/
173
- if result =~ /won"t fit in the/
174
- tag "full"
175
- raise Errors::ContainerFull
176
- end
177
- }
178
- self
179
- end
180
-
181
- def method_missing(name, *args)
182
- where(noun: name.to_s)
183
- end
184
-
185
- def to_s
186
- "<Container:#{@id} @name=#{@name} @contents=[#{contents}]>"
187
- end
8
+ Container.new(self)
188
9
  end
189
10
  end
190
11
 
191
- class Rummage
192
- SUCCESS = /and remove/
193
- FAIL = /but can't seem|^But your hands are full|^You can only rummage for|^What/
194
-
195
- @@message = OpenStruct.new(
196
- success: SUCCESS,
197
- fail: FAIL,
198
- either: SUCCESS.or(FAIL)
199
- )
12
+ class Container < Exist
13
+ TOPS = %w(table)
14
+ include Enumerable
200
15
 
201
- def Rummage.message
202
- @@message
16
+ def initialize(obj)
17
+ super(obj)
18
+ fput "look in ##{obj.id}" unless GameObj.containers.fetch(id, false)
203
19
  end
204
20
 
205
- attr_accessor :container
206
-
207
- def initialize(container)
208
- @container = container
21
+ def check_contents
22
+ fput TOPS.include?(noun) ? "look on ##{id}" : "look in ##{id}"
209
23
  end
210
24
 
211
- def perform(mod, query)
212
- res = Olib.do "rummage ##{@container.id} #{mod} #{query}", Rummage.message.either
213
- [!res.match(FAIL), res]
25
+ def contents
26
+ GameObj.containers.fetch(id, []).map do |item| Item.new(item, self) end
214
27
  end
215
28
 
216
- def spell(number)
217
- perform "spell", number
29
+ def each(&block)
30
+ contents.each(&block)
218
31
  end
219
32
 
220
- def runestone(rune)
221
- perform "runestone", rune
33
+ def where(**query)
34
+ contents.select(&Where[**query])
222
35
  end
223
36
 
224
- def ingredient(str)
225
- perform "ingredient", str
37
+ def type(type = nil)
38
+ return super() if type.nil?
39
+ find_by_tags(type)
226
40
  end
227
41
 
228
- def holy(tier)
229
- perform "holy", tier
42
+ def find_by_tags(*tags)
43
+ tags = tags.map(&:to_sym)
44
+ contents.select do |item| (item.tags & tags).size.eql?(tags.size) end
230
45
  end
231
- end
232
46
 
233
- # globalize
234
- class Container < Olib::Container
235
- end
236
-
237
- module Containers
238
- @@containers = {}
239
-
240
- def Containers.define(name)
241
- container = Class.new(Olib::Container)
242
- @@containers[name] = Object.const_set(name.capitalize, container).new
243
- @@containers[name]
47
+ def rummage
48
+ Rummage.new(self)
244
49
  end
245
50
 
246
- def Containers.method_missing(name)
247
- return @@containers[name] if @@containers[name]
248
- return Containers.define(name)
51
+ def to_json(*args)
52
+ {id: id, name: name, noun: noun, contents: contents}.to_json(*args)
249
53
  end
250
54
 
251
- def Containers.[](name)
252
- begin
253
- Containers.define(name)
254
- rescue Exception => e
255
- nil
55
+ def add(*items)
56
+ items.flatten.each do |item|
57
+ Action.try_or_fail(command: "_drag ##{item.id} ##{id}") do
58
+ contents.map(&:id).include?(item.id.to_s)
59
+ end
256
60
  end
257
61
  end
258
-
259
- def Containers.right_hand
260
- Olib::Container.new(GameObj.right_hand.id)
261
- end
262
-
263
- def Containers.left_hand
264
- Olib::Container.new(GameObj.left_hand.id)
265
- end
266
62
  end
@@ -0,0 +1,42 @@
1
+ require "Olib/pattern_matching/pattern_matching"
2
+
3
+ module Containers
4
+ @@containers = {}
5
+
6
+ def self.find_game_obj!(name)
7
+ var = Vars[name.to_s] or fail Exception, "Var[#{name}] is not set\n\t;vars set #{name}=<whatever>"
8
+ pattern = %r[#{var}]
9
+ GameObj.inv.find(&Where[name: pattern]) or fail Exception, "#{name.capitalize}(#{var}) could not be found in GameObj.inv"
10
+ end
11
+
12
+ def self.define(name)
13
+ @@containers[name] = Container.new(
14
+ Containers.find_game_obj!(name))
15
+ @@containers[name]
16
+ end
17
+
18
+ def self.method_missing(name, *args)
19
+ return @@containers[name] if @@containers[name]
20
+ return self.define(name)
21
+ end
22
+
23
+ def self.[](name)
24
+ begin
25
+ self.define(name)
26
+ rescue Exception => err
27
+ Err(why: err.message, error: err)
28
+ end
29
+ end
30
+
31
+ def self.right_hand
32
+ Container.new(Char.right)
33
+ end
34
+
35
+ def self.left_hand
36
+ Container.new(Char.left)
37
+ end
38
+
39
+ def self.registry
40
+ @@containers
41
+ end
42
+ end