-
Notifications
You must be signed in to change notification settings - Fork 0
/
item.rb
237 lines (194 loc) · 7.54 KB
/
item.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
85
86
87
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
class Item
attr_reader :description, :name, :aliases, :location_description, :use_description, :read_description, :reveal_description, :state_descriptions, :applicable_commands, :command_restrictions, :use_on_doing_actions, :use_on_receiving_actions, :requires_time, :use_redirect, :custom_commands, :will_reveal_owned_items_when_looked_at
attr_accessor :state, :associated_location, :is_hidden, :belongs_to, :owns
def initialize(name, description = '', options = {})
@name = name.strip()
@aliases = options[:aliases] || []
@description = description.gsub(/\R+/, ' ').squeeze(' ').strip()
@location_description = options[:location_description] || (options.has_key?(:state_descriptions) ? options[:state_descriptions][options[:state]][:location] : '')
@read_description = options[:read_description]
@use_description = options[:use_description]
@associated_location = nil
# an item gets a 'reveal' description that adds to the location description when it becomes revealed after the open command
@reveal_description = options[:reveal_description]
@use_description = options[:use_description]
@use_redirect = options[:use_redirect]
@applicable_commands = options[:applicable_commands] || []
@custom_commands = options[:custom_commands] || {}
@state = options[:state] || nil
@state_descriptions = options[:state_descriptions] || {}
@command_restrictions = options[:command_restrictions] || {}
@state_actions = options[:state_actions] || {}
@ownership_actions = options[:ownership_actions] || {}
@use_action = options[:use_action] || nil
@use_on_doing_actions = options[:use_on_doing_actions] || {}
@use_on_receiving_actions = options[:use_on_receiving_actions] || {}
# for purposes of having a link to the thing that owns it so we can check statuses.
# e.g. letter should only display its description if its owner(mailbox)
# is open
@owns = options[:owns] || []
@owns.each {|item| item.belongs_to = self}
@belongs_to = options[:belongs_to] || nil
@requires_time = options[:requires_time] || false
@is_hidden = options[:is_hidden] || false
@will_reveal_owned_items_when_looked_at = options[:will_reveal_owned_items_when_looked_at] || false
@does_start_with_vowel_sound = options[:does_start_with_vowel_sound] || false
@is_plural = options[:is_plural] || false
end
def get_indefinite_article
return '' if @name == 'water'
return 'some' if @is_plural
@does_start_with_vowel_sound || @name =~ /^[aeiouAEIOU]/ ? 'an' : 'a'
end
def remove_owned_item(item)
@owns.delete(item)
item.belongs_to = nil
invoke_owns_ownership_action(item)
item.invoke_belongs_to_ownership_action(self)
end
def add_owned_item(item)
@owns << item
item.belongs_to = self
invoke_owns_ownership_action(item)
item.invoke_belongs_to_ownership_action(self)
end
def reveal_owned_items
# Item is in a Location
# if item.associated_location
# reveal items
revealed_items = []
get_flattened_nested_items.each do |item|
if item.is_hidden == true
item.is_hidden = false
revealed_items << item
end
end
revealed_items
# end
end
def update_location_description_due_to_state
@location_description = @state_descriptions[@state][:location]
end
def update_location_description_due_to_drop
indefinite_article = get_indefinite_article
@location_description = @belongs_to.nil? ? "#{indefinite_article.empty? ? @name.capitalize : "#{indefinite_article.capitalize} #{@name}"} is on the floor." : "#{indefinite_article.empty? ? @name.capitalize : "#{indefinite_article.capitalize} #{@name}"} is with the #{@belongs_to.name}."
end
# recursive
def find_nested_item(item_name, options = {})
found_item = nil
return self if self.has_name?(item_name) && (options[:include_hidden] == true || self.is_hidden == false)
self.owns.each do |owned_item|
result = owned_item.find_nested_item(item_name, options)
found_item = result unless result.nil?
end
found_item
end
# recursive
def get_flattened_nested_items(collection = [])
collection << self
self.owns.each {|owned_item| owned_item.get_flattened_nested_items(collection)}
collection
end
def command_restricted?(command)
!@command_restrictions[command].nil? && @command_restrictions[command][:restricted_states].include?(@state)
end
def use
if !@use_description.nil?
putsy @use_description
end
if !@use_action.nil?
invoke_use_action
end
end
def use_on(target_item)
was_doing_action_successful = target_item.invoke_use_on_receiving_action(self)
was_receiving_action_successful = invoke_use_on_doing_action(target_item)
if !was_doing_action_successful && !was_receiving_action_successful
putsy "Nothing happened when you applied the #{@name} to the #{target_item.name}."
end
nil
end
def invoke_use_action
if !@use_action.nil?
lamb = eval @use_action
lamb.call
return true
end
false
end
# what this item will do when it loses/gains an item
def invoke_owns_ownership_action(owned_item, *args)
if !@ownership_actions[:owns].nil?
if !@ownership_actions[:owns][:empty_array].nil? && @owns.empty?
lamb = eval @ownership_actions[:owns][:empty_array]
lamb.call(owned_item)
return true
end
end
false
end
# what this item will do when it becomes owned or no longer owned
def invoke_belongs_to_ownership_action(owner_item, *args)
if !@ownership_actions[:belongs_to].nil?
if !@ownership_actions[:belongs_to][:nil].nil? && @belongs_to.nil?
lamb = eval @ownership_actions[:belongs_to][:nil]
lamb.call(owner_item)
return true
end
end
false
end
# what this item will do when used ON a certain target item
def invoke_use_on_doing_action(receiving_item, *args)
if !@use_on_doing_actions[receiving_item.name.to_sym].nil?
lamb = eval @use_on_doing_actions[receiving_item.name.to_sym]
lamb.call(receiving_item)
return true
end
false
end
# what this item will do when used on BY a certain target item
def invoke_use_on_receiving_action(doing_item, *args)
if !@use_on_receiving_actions[doing_item.name.to_sym].nil?
lamb = eval @use_on_receiving_actions[doing_item.name.to_sym]
lamb.call(doing_item)
return true
end
false
end
def invoke_state_action(*args)
if !@state_actions[@state].nil?
lamb = eval @state_actions[@state]
lamb.call(*args)
return true
end
false
end
def invoke_custom_command(incoming_command, additional)
def find_custom_command_on_item(string_command)
tuple = @custom_commands.find do |key, value|
key_match = string_command.gsub(/ /, '_').to_sym
value[:aliases].include?(string_command) || key == key_match
end
command = tuple.nil? ? nil : tuple[0]
end
string_command = "#{incoming_command.to_s}#{additional.empty? ? '' : " #{additional}"}"
found_command = find_custom_command_on_item(string_command)
if !@custom_commands[found_command].nil?
lamb = eval @custom_commands[found_command][:action]
lamb.call
return true
end
false
end
def get_hidden_custom_commands
hidden_commands = @custom_commands.select { |k, v| v[:is_hidden] }.map{|k, v| k }
end
def has_name?(name)
@name == name || @aliases.include?(name)
end
def update_state(state)
@state = state
update_location_description_due_to_state
end
end