Module: Olib

Defined in:
lib/Olib/core/extender.rb,
lib/Olib.rb,
lib/Olib/go2.rb,
lib/Olib/shops.rb,
lib/Olib/bounty.rb,
lib/Olib/version.rb,
lib/Olib/core/item.rb,
lib/Olib/utils/cli.rb,
lib/Olib/utils/cli.rb,
lib/Olib/core/utils.rb,
lib/Olib/core/errors.rb,
lib/Olib/objects/box.rb,
lib/Olib/objects/jar.rb,
lib/Olib/objects/herb.rb,
lib/Olib/objects/wand.rb,
lib/Olib/objects/jewel.rb,
lib/Olib/core/container.rb,
lib/Olib/objects/scroll.rb,
lib/Olib/character/group.rb,
lib/Olib/combat/creature.rb,
lib/Olib/objects/jewelry.rb,
lib/Olib/objects/unknown.rb,
lib/Olib/utils/help_menu.rb,
lib/Olib/combat/creatures.rb,
lib/Olib/objects/clothing.rb,
lib/Olib/objects/uncommon.rb,
lib/Olib/utils/monsterbold.rb,
lib/Olib/character/inventory.rb,
lib/Olib/dictionary/dictionary.rb

Overview

a collection for managing all of the creatures in a room

Defined Under Namespace

Modules: Errors, Inventory, Shop Classes: Bounty, Box, Clothing, Container, Creature, Creatures, Dictionary, Gameobj_Extender, Go2, Group, HelpMenu, Herb, Item, Jar, Jewel, Jewelry, Lootsack, ScriptVars, Scroll, Transport, Uncommon, Unknown, Wand

Constant Summary collapse

VERSION =
"0.1.0"
@@debug =
false
@@xml =
false

Class Method Summary collapse

Class Method Details

.CLIObject



78
79
80
# File 'lib/Olib/utils/cli.rb', line 78

def Olib.CLI
  Olib::ScriptVars.new
end

.debug(msg) ⇒ Object



83
84
85
86
# File 'lib/Olib/core/utils.rb', line 83

def Olib.debug(msg)
  return unless @@debug
  echo "Olib.debug> #{msg}"
end

.do(action, re) ⇒ Object



37
38
39
# File 'lib/Olib.rb', line 37

def Olib.do(action, re)
  dothistimeout action, 5, re
end

.exitObject



190
191
192
# File 'lib/Olib/core/utils.rb', line 190

def Olib.exit
  raise Olib::Errors::Mundane
end

.Go2Object



138
139
140
# File 'lib/Olib/go2.rb', line 138

def Olib.Go2
  Olib::Go2
end

.install(g, v = nil) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/Olib.rb', line 46

def Olib.install(g, v=nil)
  if !which("gem") then
    echo "Olib could not detect the `gem` executable in your $PATH"
    echo "when you installed Ruby did you forget to click the `modify $PATH` box?"
    raise Exception
  end

  begin
    unless v.nil?
      # make it a true instance of Gem::Version so we can compare
      raise UpdateGemError if Gem.loaded_specs[g].version <  Gem::Version.new(v)
      gem g, v
    end

    Olib.reload g

  ##
  ## rescue missing gem and reload after install
  ##
  rescue LoadError
    echo "installing #{g}..."
    version = "--version '#{v}'" unless v.nil?
    worked = system("gem install #{g} #{version} --no-ri --no-rdoc")
    unless worked then raise "Could not install #{g} gem" end
    Olib.reload g
    echo "... installed #{g}!"

  ##
  ## rescue from too old of a gem version for a Ruby environment
  ##
  rescue UpdateGemError
    echo "updating #{g}@#{Gem.loaded_specs[g].version} => #{v}..."
    version = "--version '#{v}'" unless v.nil?
    worked = system("gem install #{g} #{version} --no-ri --no-rdoc")
    unless worked then raise "Could not install #{g} gem" end
    Olib.reload g
    echo "... updated #{g} to #{v}!"
  end
end

.InventoryObject



77
78
79
# File 'lib/Olib/character/inventory.rb', line 77

def Olib.Inventory
  Olib::Inventory
end

.LootsackObject



180
181
182
183
184
# File 'lib/Olib/core/container.rb', line 180

def Olib.Lootsack
  return @@lootsack if @@lootsack
  @@lootsack = Lootsack.new
  @@lootsack
end

.methodize(str) ⇒ Object



20
21
22
# File 'lib/Olib.rb', line 20

def Olib.methodize(str)
  str.downcase.strip.gsub(/-|\s+|'|"/, "_")
end

.monsterbold(str) ⇒ Object



2
3
4
# File 'lib/Olib/utils/monsterbold.rb', line 2

def Olib.monsterbold(str)
  "<pushBold/>#{str}<popBold/>"
end

.PlayershopObject



170
171
172
# File 'lib/Olib/shops.rb', line 170

def Olib.Playershop
  Olib::Shop::Playershop
end

.run(script, *args) ⇒ Object



41
42
43
44
# File 'lib/Olib.rb', line 41

def Olib.run(script, *args)
  start_script script, args
  wait_while { running? script }
end

.scriptObject



120
121
122
# File 'lib/Olib/core/utils.rb', line 120

def Olib.script
  Script.current
end

.ShopObject



174
175
176
# File 'lib/Olib/shops.rb', line 174

def Olib.Shop
  Olib::Shop
end

.timeout(sec) ⇒ Object

:yield: sec



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/Olib/core/utils.rb', line 88

def Olib.timeout(sec)   #:yield: +sec+
  return yield(sec) if sec == nil or sec.zero?

  begin
    current_thread = Thread.current
    x = Thread.start{ 
      begin
        yield(sec)
      rescue => e 
        current_thread.raise e
      end
    }
    y = Thread.start {
      begin
        sleep sec
      rescue => e
        x.raise e
      else
        x.kill
        current_thread.raise Olib::Errors::TimedOut
      end
    }
    x.value
  ensure
    if y
      y.kill
      y.join # make sure y is dead.
    end
  end
  
end

.toggle_debugObject



79
80
81
# File 'lib/Olib/core/utils.rb', line 79

def Olib.toggle_debug
  @@debug = @@debug ? false : true 
end

.turn_off_xmlObject



132
133
134
135
136
137
138
# File 'lib/Olib/core/utils.rb', line 132

def Olib.turn_off_xml
  if @@xml
    @@xml = false
    Script.current.want_downstream_xml = @@xml
  end
  self
end

.turn_on_xmlObject



124
125
126
127
128
129
130
# File 'lib/Olib/core/utils.rb', line 124

def Olib.turn_on_xml
  if not @@xml
    @@xml = true
    Script.current.want_downstream_xml = @@xml
  end
  self
end

.update_notifierObject



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/Olib.rb', line 6

def Olib.update_notifier
  begin
      response  = JSON.parse Net::HTTP.get URI('https://rubygems.org/api/v1/gems/Olib.json')
      # check version
      if Gem.loaded_specs["Olib"].version < Gem::Version.new(response['version'])
        puts "<pushBold/>You need to update the Olib gem with a `gem install Olib`<popBold/>"
      end
      
    rescue
      echo $!
      puts $!.backtrace[0..1]
    end
end

.varsObject



74
75
76
# File 'lib/Olib/core/utils.rb', line 74

def Olib.vars
  ScriptVars.new
end

.wrap(action = nil) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/Olib/core/utils.rb', line 144

def Olib.wrap(action = nil)
  
  begin
    Olib.timeout(3) {
      put action if action
      while (line=get)
        next if Dictionary.ignorable?(line)
        # attempt at removing PC action that turned out to be more harmful than good
        # next if not GameObj.pcs.nil? and line =~ /#{GameObj.pcs.join('|')}/
        yield line
      end
    }

  rescue Olib::Errors::TimedOut
    Olib.debug "timeout... "
    # Silent
  rescue Olib::Errors::Mundane => e

  rescue Olib::Errors::Prempt => e
    
  end
  
end

.wrap_greedy(action) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/Olib/core/utils.rb', line 168

def Olib.wrap_greedy(action)
  
  begin
    Olib.timeout(3) {
      put action
      while (line=get)
        #next if not GameObj.pcs.nil? and line =~ /#{GameObj.pcs.join('|')}/
        yield line
      end
    }

  rescue Olib::Errors::TimedOut
    Olib.debug "timeout... "
    # Silent
  rescue Olib::Errors::Mundane => e

  rescue Olib::Errors::Prempt => e
    
  end
  
end

.wrap_stream(action = nil) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/Olib/core/utils.rb', line 194

def Olib.wrap_stream(action = nil)
  begin
    Olib.turn_on_xml

    Olib.timeout(3) {
      if action then fput action end
      while (line=get)
        next if     Olib::Dictionary.ignorable?(line)
        # next if not GameObj.pcs.nil? and line =~ /#{GameObj.pcs.join('|')}/
        yield line
      end
    }

  rescue Olib::Errors::TimedOut
    Olib.debug "timeout... "
    # Silent

  rescue Olib::Errors::Mundane => e
    Olib.debug "mundane..."

  rescue Olib::Errors::Prempt => e
    Olib.debug "waiting prempted..."

  ensure
    Olib.turn_off_xml
  end
end

.xml?Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/Olib/core/utils.rb', line 140

def Olib.xml?
  @@xml
end