Add regexp support to name_lookup#find_entities

This commit is contained in:
Gary Tierney
2016-01-03 21:13:17 +00:00
parent a2864d6318
commit f80ea82ce7
+7 -2
View File
@@ -35,11 +35,16 @@ end
# Finds entities with the specified type (e.g. npc) and name, returning possible ids as an array. # Finds entities with the specified type (e.g. npc) and name, returning possible ids as an array.
def find_entities(type, name, limit = 5) def find_entities(type, name, limit = 5)
ids = [] ids = []
name.downcase!
Kernel.const_get("#{type.capitalize}Definition").definitions.each do |definition| Kernel.const_get("#{type.capitalize}Definition").definitions.each do |definition|
break if (ids.length == limit) break if (ids.length == limit)
ids << definition.id.to_i if definition.name.to_s.downcase == name if name.is_a? Regexp
ids << definition.id.to_i if definition.name.to_s.downcase =~ name
elsif name.is_a? Symbol
ids << definition.id.to_i if definition.name.to_s.downcase == name.to_s.downcase.gsub(/_/, ' ')
else
ids << definition.id.to_i if definition.name.to_s.downcase == name.downcase
end
end end
ids ids