Class: Olib::Container
Instance Attribute Summary collapse
#type
Instance Method Summary
collapse
#__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)
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
#containers ⇒ Object
Returns the value of attribute containers.
26
27
28
|
# File 'lib/Olib/core/container.rb', line 26
def containers
@containers
end
|
#nested ⇒ Object
Returns the value of attribute nested.
26
27
28
|
# File 'lib/Olib/core/container.rb', line 26
def nested
@nested
end
|
#ontop ⇒ Object
Returns the value of attribute ontop.
26
27
28
|
# File 'lib/Olib/core/container.rb', line 26
def ontop
@ontop
end
|
#ref ⇒ Object
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
|
#at ⇒ Object
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
|
#contents ⇒ Object
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
|
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
164
165
166
|
# File 'lib/Olib/core/container.rb', line 164
def full?
is? "full"
end
|
#in ⇒ Object
151
152
153
154
|
# File 'lib/Olib/core/container.rb', line 151
def in
fput "look in ##{@id}"
self
end
|
#look ⇒ Object
132
133
134
|
# File 'lib/Olib/core/container.rb', line 132
def look
self
end
|
#nested? ⇒ Boolean
160
161
162
|
# File 'lib/Olib/core/container.rb', line 160
def nested?
@nested
end
|
#rummage ⇒ Object
156
157
158
|
# File 'lib/Olib/core/container.rb', line 156
def rummage
Rummage.new(self)
end
|
#to_s ⇒ Object
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
|