Olib 0.0.3 → 0.0.4

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.
data/lib/Olib/shops.rb ADDED
@@ -0,0 +1,175 @@
1
+ module Olib
2
+ module Shop
3
+
4
+ @@containers = []
5
+
6
+ def Shop.items
7
+ Shop.containers.map { |container| container.contents }.flatten
8
+ end
9
+
10
+ def Shop.containers
11
+ #fput "look"
12
+ @@containers = [
13
+ GameObj.loot,
14
+ GameObj.room_desc
15
+ ]
16
+ .flatten
17
+ .compact
18
+ .map { |container| Shop::Container.new(container) }
19
+
20
+ @@containers
21
+
22
+ end
23
+
24
+ def Shop.cache
25
+ @@containers
26
+ end
27
+
28
+ class Container < Olib::Gameobj_Extender
29
+ attr_accessor :show, :nested, :containers, :id, :cache, :props
30
+
31
+ def to_s
32
+ info = {}
33
+ info[:name] = @name
34
+ info[:noun] = @noun
35
+ info[:props] = @props
36
+ info[:cache] = @cache
37
+ info.to_s
38
+ end
39
+
40
+ def initialize(obj)
41
+ @cache = Hash.new
42
+ @props = Hash.new
43
+ super(obj)
44
+ end
45
+
46
+ def action(verb)
47
+ "#{verb} ##{@id}"
48
+ end
49
+
50
+ def look
51
+ self
52
+ end
53
+
54
+ # detect nested containers
55
+ def at
56
+
57
+ Olib.wrap_stream(action 'look at') { |line|
58
+
59
+ raise Olib::Errors::Mundane if line =~ /You see nothing unusual/
60
+
61
+ if line =~ /Looking at the (.*?), you see (?<nested>.*)/
62
+ @nested = true
63
+
64
+ @containers = line.match(/Looking at the (.*?), you see (?<nested>.*)/)[:nested].scan(/<a exist="(?<id>.*?)" noun="(?<noun>.*?)">(?<name>.*?)<\/a>/).map {|matches|
65
+ Container.new GameObj.new *matches
66
+ }
67
+ raise Olib::Errors::Prempt
68
+ end
69
+
70
+ }
71
+
72
+ self
73
+ end
74
+
75
+ def nested?
76
+ @nested || false
77
+ end
78
+
79
+ def in
80
+ return self unless @id
81
+ Olib.wrap_stream(action 'look in') { |line|
82
+ raise Olib::Errors::Mundane if line=~ /^There is nothing in there|^That is closed|filled with a variety of garbage|Total items:/
83
+ raise Olib::Errors::Prempt if line =~ /^In the (.*?) you see/
84
+ }
85
+ self
86
+ end
87
+
88
+ def on
89
+ return self unless @id
90
+ Olib.wrap_stream(action 'look on') { |line|
91
+ raise Olib::Errors::Mundane if line =~ /^There is nothing on there/
92
+ raise Olib::Errors::Prempt if line =~ /^On the (.*?) you see/
93
+ }
94
+ self
95
+ end
96
+
97
+ def contents
98
+ look.in.on unless GameObj[@id].contents
99
+ GameObj[@id].contents.map {|i| Item.new(i).define('container', @id) }
100
+ end
101
+
102
+ def containers
103
+ @containers
104
+ end
105
+ end
106
+
107
+ class Playershop
108
+ @@noncontainers = [ "wall", "ceiling", "permit", "floor", "helmet", "snowshoes",
109
+ "candelabrum", "flowerpot", "Hearthstone", "bear", "candelabra",
110
+ "sculpture", "anvil", "tapestry", "portrait", "Wehnimer", "spiderweb",
111
+ "rug", "fountain", "longsword", "ship", "panel", "painting", "armor",
112
+ "flowers", "head", "plate", "vase", "pillows", "mask", "skeleton", "fan",
113
+ "flag", "statue", "mat", "plaque", "mandolin", "plant", "sign" ]
114
+
115
+ def Playershop.containers
116
+ Shop.containers.reject { |container|
117
+ @@noncontainers.include? container.noun
118
+ }
119
+ end
120
+
121
+ def Playershop.balance
122
+ balance = 0
123
+ Olib.wrap_stream('shop withdraw') { |line|
124
+ next if line =~ /^Usage: SHOP WITHDRAW <amount>/
125
+ raise Olib::Errors::Prempt if line =~ /^You must be in your shop to do that.$/
126
+
127
+ if line =~ /Your shop's bank account is currently ([\d]+)/
128
+ balance = $1.to_i
129
+ raise Olib::Errors::Prempt
130
+ end
131
+
132
+ }
133
+ return balance
134
+ end
135
+
136
+ def Playershop.where(conditions)
137
+ Playershop.items.select { |item|
138
+ conditions.keys.map { |key|
139
+ item.respond_to?(key) ? item.send(key) == conditions[key] : false
140
+
141
+ }.include? false
142
+
143
+ }
144
+
145
+ end
146
+
147
+ def Playershop.find_by_tags(*tags)
148
+
149
+ Playershop.items.select { |item|
150
+ !tags.map {|tag| item.is?(tag) }.include? false
151
+ }
152
+ end
153
+
154
+ def Playershop.sign
155
+ Shop.containers.select { |container|
156
+ container.noun == 'sign'
157
+ }[0]
158
+ end
159
+
160
+ def Playershop.items
161
+ Playershop.containers.map { |container|
162
+ container.contents
163
+ }.flatten
164
+ end
165
+ end
166
+ end
167
+
168
+ def Olib.Playershop
169
+ Olib::Shop::Playershop
170
+ end
171
+
172
+ def Olib.Shop
173
+ Olib::Shop
174
+ end
175
+ end
@@ -2,27 +2,47 @@ module Olib
2
2
 
3
3
  class Transport
4
4
 
5
- attr_accessor :origin, :silvers, :teleporter
5
+ @@silvers = 0
6
+ @@teleporter = {}
7
+ @@routines = {}
6
8
 
7
- def initialize
8
- rebase
9
- @teleporter = {}
10
- self.__constructor__
11
- return self
9
+ def Transport.__extend__
10
+ singleton = (class << self; self end)
11
+ [
12
+ 'bank', 'gemshop', 'pawnshop', 'advguild', 'forge', 'inn', 'npchealer',
13
+ 'chronomage', 'town', 'furrier', 'herbalist', 'locksmith', 'alchemist',
14
+ 'fletcher', 'sunfist', 'movers', 'consignment', 'advguard','advguard2',
15
+ 'clericshop', 'warriorguild'
16
+ ].each do |target|
17
+ singleton.send :define_method, "go2_#{target}".to_sym do
18
+
19
+ unhide if hidden?
20
+ unless Room.current.tags.include?(target)
21
+ start_script 'go2', [target, '_disable_confirm_']
22
+ wait_while { running? "go2" };
23
+ end
24
+ return self
25
+ end
26
+ end
12
27
  end
13
28
 
14
- def rebase
15
- @origin = {}
16
- @origin[:roomid] = Room.current.id
17
- @origin[:hidden] = hiding?
18
- @origin[:location] = Room.current.location
29
+ Transport.__extend__
30
+
31
+ def Transport.rebase
32
+ @@origin = {}
33
+ @@origin[:roomid] = Room.current.id
34
+ @@origin[:hidden] = hiding?
35
+ @@origin[:location] = Room.current.location
19
36
  self
20
37
  end
21
38
 
39
+ Transport.rebase
40
+
41
+
22
42
  # Thanks Tillmen
23
- def cost(to)
43
+ def Transport.cost(to)
24
44
  cost = 0
25
- Map.findpath(Room.current.id, @to).each { |id|
45
+ Map.findpath(Room.current.id, to).each { |id|
26
46
  Room[id].tags.each { |tag|
27
47
  if tag =~ /^silver-cost:#{id-1}:(.*)$/
28
48
  cost_string = $1
@@ -39,58 +59,62 @@ module Olib
39
59
 
40
60
  # use the teleporter variable to locate your teleporter and teleport
41
61
  # naive of where you are
42
- def fwi_teleport
43
- unless @teleporter[:item]
44
- setting = 'teleporter'
45
- if UserVars.send(setting).nil? or UserVars.send(setting).empty?
46
- echo "error: #{setting.to_s} is not set. (;vars set #{setting.to_s}=<teleporter>)"
47
- else
48
- #echo "locating teleporter..."
49
- GameObj.containers.map do |container, contents|
50
- contents.each do |item|
51
- if item.full_name =~ /#{UserVars.send(setting)}/
52
- respond @item
53
- @teleporter[:container] = container
54
- @teleporter[:item] = item
55
- end
56
- end
57
- end
58
- end
62
+ def Transport.fwi_teleport
63
+ unless Vars.teleporter
64
+ echo "Error: No teleport defined ;var set teleporter=<teleporter>"
65
+ exit
66
+ end
67
+
68
+ unless Inventory.teleporter
69
+ echo "Error: Your teleporter could not be found #{Vars.teleporter}"
70
+ exit
71
+ end
72
+
73
+ last = Room.current.id
74
+ teleporter = Inventory.teleporter
75
+ if teleporter.worn?
76
+ teleporter.turn
77
+ else
78
+ teleporter.take.turn.stash
79
+ end
80
+
81
+ if Room.current.id == last
82
+ echo "Error: You tried to teleport to FWI in a room that does not allow that"
59
83
  end
60
- multifput "get ##{@teleporter[:item].id}", "turn ##{@teleporter[:item].id}", "_drag ##{@teleporter[:item].id} ##{@teleporter[:container]}" if @teleporter[:item]
84
+
61
85
  self
62
86
  end
63
87
 
64
- def wealth
88
+ def Transport.wealth
65
89
  fput "info"
66
90
  while(line=get)
67
91
  next if line =~ /^\s*Name\:|^\s*Gender\:|^\s*Normal \(Bonus\)|^\s*Strength \(STR\)\:|^\s*Constitution \(CON\)\:|^\s*Dexterity \(DEX\)\:|^\s*Agility \(AGI\)\:|^\s*Discipline \(DIS\)\:|^\s*Aura \(AUR\)\:|^\s*Logic \(LOG\)\:|^\s*Intuition \(INT\)\:|^\s*Wisdom \(WIS\)\:|^\s*Influence \(INF\)\:/
68
92
  if line =~ /^\s*Mana\:\s+\-?[0-9]+\s+Silver\:\s+([0-9]+)/
69
- @silvers= $1.to_i
93
+ @@silvers= $1.to_i
70
94
  break
71
95
  end
72
96
  sleep 0.1
73
97
  end
74
- @silvers
98
+ @@silvers
75
99
  end
76
100
 
77
- def deplete(silvers)
78
- @silvers = @silvers - silvers
101
+ def Transport.deplete(silvers)
102
+ @@silvers = @@silvers - silvers
79
103
  end
80
104
 
81
- def smart_wealth
82
- return @silvers if @silvers
83
- @wealth
105
+ def Transport.smart_wealth
106
+ return @@silvers if @@silvers
107
+ @@wealth
84
108
  end
85
109
 
86
- def unhide
87
- fput 'unhide' if Spell[9003].active? or hidden?
110
+ def Transport.unhide
111
+ fput 'unhide' if Spell[916].active? or hidden?
88
112
  self
89
113
  end
90
114
 
91
- def withdraw(amount)
115
+ def Transport.withdraw(amount)
92
116
  go2_bank
93
- result = Olib.do "withdraw #{amount} silvers", /I'm sorry|then hands you/
117
+ result = Olib.do "withdraw #{amount} silvers", /I'm sorry|hands you/
94
118
  if result =~ /I'm sorry/
95
119
  go2_origin
96
120
  echo "Unable to withdraw the amount requested for this script to run from your bank account"
@@ -99,14 +123,14 @@ module Olib
99
123
  return self
100
124
  end
101
125
 
102
- def deposit_all
126
+ def Transport.deposit_all
103
127
  self.go2_bank
104
128
  fput "unhide" if invisible? || hidden?
105
129
  fput "deposit all"
106
130
  return self
107
131
  end
108
132
 
109
- def deposit(amt)
133
+ def Transport.deposit(amt)
110
134
  self.go2_bank
111
135
  fput "unhide" if invisible? || hidden?
112
136
  fput "deposit #{amt}"
@@ -115,13 +139,13 @@ module Olib
115
139
 
116
140
  # naive share
117
141
  # does not check if you're actually in a group or not
118
- def share
142
+ def Transport.share
119
143
  wealth
120
144
  fput "share #{@silvers}"
121
145
  self
122
146
  end
123
147
 
124
- def go2(roomid)
148
+ def Transport.go2(roomid)
125
149
  unhide if hidden
126
150
  unless Room.current.id == roomid
127
151
  start_script 'go2', [roomid, '_disable_confirm_']
@@ -130,49 +154,26 @@ module Olib
130
154
  return self
131
155
  end
132
156
 
133
- def hide
134
- while not hiding?
135
- waitrt?
136
- fput 'hide'
137
- end
138
- end
139
-
140
- def __constructor__
141
- singleton = (class << self; self end)
142
- [
143
- 'bank', 'gemshop', 'pawnshop', 'advguild', 'forge', 'inn', 'npchealer',
144
- 'chronomage', 'town', 'furrier', 'herbalist', 'locksmith', 'alchemist',
145
- 'fletcher', 'sunfist', 'movers', 'consignment', 'advguard','advguard2',
146
- 'clericshop', 'warriorguild'
147
- ].each do |target|
148
- singleton.send :define_method, "go2_#{target}".to_sym do
149
-
150
- unhide if hidden?
151
- unless Room.current.tags.include?(target)
152
- start_script 'go2', [target, '_disable_confirm_']
153
- wait_while { running? "go2" };
154
- end
155
- return self
156
- end
157
- end
158
- end
159
-
160
157
  # TODO
161
158
  # create a dictionary of house lockers and the logic to enter a locker
162
159
  # insure locker is closed before scripting away from it
163
- def go2_locker
160
+ def Transport.go2_locker
164
161
  echo "the go2_locker method currently does not function properly..."
165
162
  self
166
163
  end
167
164
 
168
- def go2_origin
169
- if Room.current.id != @origin[:roomid]
165
+ def Transport.go2_origin
166
+ if Room.current.id != @@origin[:roomid]
170
167
  start_script 'go2', [@origin[:roomid]]
171
168
  wait_while { running? "go2" };
172
169
  end
173
- hide if @origin[:hidden]
170
+ hide if @@origin[:hidden]
174
171
  return self
175
172
  end
176
173
 
177
174
  end
175
+
176
+ def Olib.Transport
177
+ Olib::Transport
178
+ end
178
179
  end
data/lib/Olib/utils.rb ADDED
@@ -0,0 +1,223 @@
1
+ require "Olib"
2
+
3
+ module Olib
4
+ @@debug = false
5
+ @@xml = false
6
+
7
+ class ScriptVars
8
+ attr_accessor :opts
9
+ def initialize
10
+ opts = {}
11
+ opts[:flags] = {}
12
+ return opts if Script.current.vars.empty?
13
+ list = Script.current.vars.map(&:downcase).last(Script.current.vars.length-1)
14
+ unless list.first.start_with?('--')
15
+ opts[:cmd] = list.shift
16
+ end
17
+ # iterate over list for flag values
18
+
19
+ list.each.with_index {|ele, i|
20
+ if ele.start_with?('--')
21
+ opts[:flags][ symbolize(ele) ] = ''
22
+ else
23
+ # cast to Number if is number
24
+ ele = ele.to_i if ele =~ /^([\d\.]+$)/
25
+ # look back to previous flag and set it to it's value
26
+ opts[:flags][ symbolize(list[i-1]) ] = ele
27
+ end
28
+ }
29
+
30
+ @opts = opts
31
+ self
32
+ end
33
+
34
+ def cmd
35
+ @opts[:cmd]
36
+ end
37
+
38
+ def empty?(flag)
39
+ opts[:flags][flag].class == TrueClass || opts[:flags][flag].class == NilClass
40
+ end
41
+
42
+ def cmd?(action)
43
+ cmd == action
44
+ end
45
+
46
+ def symbolize(flag)
47
+ flag.gsub('--', '').gsub('-', '_').to_sym
48
+ end
49
+
50
+ def help?
51
+ cmd =~ /help/
52
+ end
53
+
54
+ def flags
55
+ opts[:flags].keys
56
+ end
57
+
58
+ def to_s
59
+ @opts.to_s
60
+ end
61
+
62
+ def flag
63
+ self
64
+ end
65
+
66
+ def flag?(f)
67
+ opts[:flags][ symbolize(f) ]
68
+ end
69
+
70
+ def method_missing(arg1, arg2=nil)
71
+ @opts[:flags][arg1]
72
+ end
73
+
74
+ end
75
+
76
+ def Olib.vars
77
+ ScriptVars.new
78
+ end
79
+
80
+
81
+ def Olib.toggle_debug
82
+ @@debug = @@debug ? false : true
83
+ end
84
+
85
+ def Olib.debug(msg)
86
+ return unless @@debug
87
+ echo "Olib.debug> #{msg}"
88
+ end
89
+
90
+ def Olib.timeout(sec) #:yield: +sec+
91
+ return yield(sec) if sec == nil or sec.zero?
92
+
93
+ begin
94
+ current_thread = Thread.current
95
+ x = Thread.start{
96
+ begin
97
+ yield(sec)
98
+ rescue => e
99
+ current_thread.raise e
100
+ end
101
+ }
102
+ y = Thread.start {
103
+ begin
104
+ sleep sec
105
+ rescue => e
106
+ x.raise e
107
+ else
108
+ x.kill
109
+ current_thread.raise Olib::Errors::TimedOut
110
+ end
111
+ }
112
+ x.value
113
+ ensure
114
+ if y
115
+ y.kill
116
+ y.join # make sure y is dead.
117
+ end
118
+ end
119
+
120
+ end
121
+
122
+ def Olib.script
123
+ Script.current
124
+ end
125
+
126
+ def Olib.turn_on_xml
127
+ if not @@xml
128
+ @@xml = true
129
+ Script.current.want_downstream_xml = @@xml
130
+ end
131
+ self
132
+ end
133
+
134
+ def Olib.turn_off_xml
135
+ if @@xml
136
+ @@xml = false
137
+ Script.current.want_downstream_xml = @@xml
138
+ end
139
+ self
140
+ end
141
+
142
+ def Olib.xml?
143
+ @@xml
144
+ end
145
+
146
+ def Olib.wrap(action)
147
+
148
+ begin
149
+ Olib.timeout(3) {
150
+ put action
151
+ while (line=get)
152
+ next if Dictionary.ignorable?(line)
153
+ # attempt at removing PC action that turned out to be more harmful than good
154
+ # next if not GameObj.pcs.nil? and line =~ /#{GameObj.pcs.join('|')}/
155
+ yield line
156
+ end
157
+ }
158
+
159
+ rescue Olib::Errors::TimedOut
160
+ Olib.debug "timeout... "
161
+ # Silent
162
+ rescue Olib::Errors::Mundane => e
163
+
164
+ rescue Olib::Errors::Prempt => e
165
+
166
+ end
167
+
168
+ end
169
+
170
+ def Olib.wrap_greedy(action)
171
+
172
+ begin
173
+ Olib.timeout(3) {
174
+ put action
175
+ while (line=get)
176
+ #next if not GameObj.pcs.nil? and line =~ /#{GameObj.pcs.join('|')}/
177
+ yield line
178
+ end
179
+ }
180
+
181
+ rescue Olib::Errors::TimedOut
182
+ Olib.debug "timeout... "
183
+ # Silent
184
+ rescue Olib::Errors::Mundane => e
185
+
186
+ rescue Olib::Errors::Prempt => e
187
+
188
+ end
189
+
190
+ end
191
+
192
+ def Olib.exit
193
+ raise Olib::Errors::Mundane
194
+ end
195
+
196
+ def Olib.wrap_stream(action, seconds=3)
197
+ begin
198
+ Olib.turn_on_xml
199
+
200
+ Olib.timeout(seconds) {
201
+ put action
202
+ while (line=get)
203
+ next if Olib::Dictionary.ignorable?(line)
204
+ # next if not GameObj.pcs.nil? and line =~ /#{GameObj.pcs.join('|')}/
205
+ yield line
206
+ end
207
+ }
208
+
209
+ rescue Olib::Errors::TimedOut
210
+ Olib.debug "timeout... "
211
+ # Silent
212
+
213
+ rescue Olib::Errors::Mundane => e
214
+ Olib.debug "mundane..."
215
+
216
+ rescue Olib::Errors::Prempt => e
217
+ Olib.debug "waiting prempted..."
218
+
219
+ ensure
220
+ Olib.turn_off_xml
221
+ end
222
+ end
223
+ end