Module: Olib

Defined in:
lib/Olib/core/extender.rb,
lib/Olib.rb,
lib/Olib/go2.rb,
lib/Olib/shops.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/jar.rb,
lib/Olib/core/container.rb,
lib/Olib/utils/help_menu.rb,
lib/Olib/storage/app_data.rb,
lib/Olib/utils/monsterbold.rb,
lib/Olib/dictionary/dictionary.rb

Overview

TODO: this should be refactored to use a mixin pattern

Defined Under Namespace

Modules: Errors, Shop Classes: App, Container, Dictionary, Gameobj_Extender, Go2, HelpMenu, Item, Jar, ScriptVars

Constant Summary collapse

VERSION =
"0.1.2"
@@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

invoke update notifier immediately Olib.update_notifier


71
72
73
# File 'lib/Olib.rb', line 71

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


128
129
130
# File 'lib/Olib/go2.rb', line 128

def Olib.Go2
  Olib::Go2
end

.methodize(str) ⇒ Object


54
55
56
# File 'lib/Olib.rb', line 54

def Olib.methodize(str)
  str.to_s.downcase.strip.gsub(/-|\s+|'|"/, "_").to_sym
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


75
76
77
78
# File 'lib/Olib.rb', line 75

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


41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/Olib.rb', line 41

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


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

def Olib.xml?
  @@xml
end