Class: Olib::Container

Inherits:
Gameobj_Extender show all
Defined in:
lib/Olib/core/container.rb

Direct Known Subclasses

Area, Container

Instance Attribute Summary collapse

Attributes inherited from Gameobj_Extender

#type

Instance Method Summary collapse

Methods inherited from Gameobj_Extender

#__extend__, #echo

Constructor Details

#initialize(id = nil) ⇒ Container

Returns a new instance of Container.


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/Olib/core/container.rb', line 43

def initialize(id=nil)
  # extract the class name to attempt to lookup the item by your settings
  # ex: class Lootsack
  # ex: class Gemsack
  name       = if self.class.name.include?("::") then self.class.name.downcase.split("::").last.strip else self.class.name.downcase end 
  candidates = Inventory[Vars[name]]
  raise Olib::Errors::DoesntExist.new("#{name} could not be initialized are you sure you:\n ;var set #{name}=<something>") if candidates.empty? && id.nil?
  @id    = id
  @ref   = GameObj[id] || candidates.first
  @ontop = Array.new
  
  unless GameObj[@ref.id].contents
    tops = [
      "table"
    ]

    action = tops.include?(@ref.noun) ? "look on ##{@ref.id}" : "look in ##{@ref.id}"
     
    fput action
  end
  
  super @ref
        
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object


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

def method_missing(name, *args)
  where(noun: name.to_s)
end

Instance Attribute Details

#containersObject

Returns the value of attribute containers


26
27
28
# File 'lib/Olib/core/container.rb', line 26

def containers
  @containers
end

#nestedObject

Returns the value of attribute nested


26
27
28
# File 'lib/Olib/core/container.rb', line 26

def nested
  @nested
end

#ontopObject

Returns the value of attribute ontop


26
27
28
# File 'lib/Olib/core/container.rb', line 26

def ontop
  @ontop
end

#refObject

Returns the value of attribute ref


26
27
28
# File 'lib/Olib/core/container.rb', line 26

def ref
  @ref
end

Instance Method Details

#[](query) ⇒ Object


93
94
95
96
97
# File 'lib/Olib/core/container.rb', line 93

def [](query)
  return contents.select do |item| 
    item if (item.type =~ query || item.noun =~ query || item.name =~ query)
  end
end

#add(*items) ⇒ Object


168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/Olib/core/container.rb', line 168

def add(*items)
  _id = @id
  items.each { |item|

    result = Olib.do "_drag ##{item.class == String ? item : item.id} ##{_id}", /#{[Olib::Dictionary.put[:success], Olib::Dictionary.put[:failure].values].flatten.join("|")}/
    if result =~ /won"t fit in the/
      tag "full"
      raise Errors::ContainerFull
    end
  }
  self
end

#atObject


112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/Olib/core/container.rb', line 112

def at
  Olib.wrap_stream("look at ##{@id}") { |line|
    if line =~ /You see nothing unusual|prompt time|You gaze through (.*?) and see...|written/
      raise Olib::Errors::Mundane
    end

    if line =~ /Looking at the (.*?), you see (?<nested>.*)/
      @nested = true

      @containers = line
        .match(/Looking at the (.*?), you see (?<nested>.*)/)[:nested]
        .scan(/<a exist="(?<id>.*?)" noun="(?<noun>.*?)">(?<name>.*?)<\/a>/)
        .map {|matches| Container.new GameObj.new *matches }
      raise Olib::Errors::Mundane
    end
    
  }
  self
end

#contentsObject


68
69
70
71
72
# File 'lib/Olib/core/container.rb', line 68

def contents
  [ @ontop,
    GameObj[@ref.id].contents.map do |item| Item.new(item, self) end
  ].flatten
end

#def(__verbs__) ⇒ Object


99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/Olib/core/container.rb', line 99

def 

def __verbs__
  @verbs = "open close analyze inspect weigh".split(" ").map(&:to_sym)
  singleton = (class << self; self end)
  @verbs.each do |verb|
    singleton.send :define_method, verb do
      fput "#{verb.to_s} ##{@id}"
      self
    end
  end
end

#find_by_tags(*tags) ⇒ Object


86
87
88
89
90
# File 'lib/Olib/core/container.rb', line 86

def find_by_tags(*tags)  
  contents.select { |item|
    !tags.map {|tag| item.is?(tag) }.include?(false)
  }
end

#full?Boolean

Returns:

  • (Boolean)

164
165
166
# File 'lib/Olib/core/container.rb', line 164

def full?
  is? "full"
end

#inObject


151
152
153
154
# File 'lib/Olib/core/container.rb', line 151

def in
  fput "look in ##{@id}"
  self
end

#lookObject


132
133
134
# File 'lib/Olib/core/container.rb', line 132

def look
  self
end

#nested?Boolean

Returns:

  • (Boolean)

160
161
162
# File 'lib/Olib/core/container.rb', line 160

def nested?
  @nested
end

#onObject


136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/Olib/core/container.rb', line 136

def on
  return self unless @id
  Olib.wrap_stream("look on ##{@id}") { |line|
    raise Olib::Errors::Mundane if line =~ /There is nothing on there|prompt time/
    if line =~ /On the (.*?) you see/
      @ontop << line.match(Dictionary.contents)[:items]
        .scan(Dictionary.tag)
        .map {|matches| Item.new GameObj.new *matches }
      raise Olib::Errors::Mundane
    end
    next
  }
  self
end

#rummageObject


156
157
158
# File 'lib/Olib/core/container.rb', line 156

def rummage
  Rummage.new(self)
end

#to_sObject


185
186
187
# File 'lib/Olib/core/container.rb', line 185

def to_s
  "<Container:#{@id} @name=#{@name} @contents=[#{contents}]>"
end

#where(conditions) ⇒ Object


74
75
76
77
78
79
80
81
82
83
84
# File 'lib/Olib/core/container.rb', line 74

def where(conditions)
  contents.select { |item|
    !conditions.keys.map { |key|
      if conditions[key].class == Array
        item.props[key].class == Array && !conditions[key].map { |ele| item.props[key].include? ele }.include?(false)
      else
        item.props[key] == conditions[key]
      end
    }.include?(false)
  }
end