-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7026 from ThatRasputin/script/new-rummage
[Script] [rummage] Adding new standalone rummage sorter
- Loading branch information
Showing
1 changed file
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
# Rummage Parser | ||
|
||
custom_require.call(%w[common common-items]) | ||
require 'terminal-table' | ||
|
||
class Rummage | ||
def run(container) | ||
container_name, items = get_container_contents(container) | ||
sorted_items = sort_items(items) | ||
display_table(sorted_items, container_name) | ||
end | ||
|
||
def get_container_contents(container) | ||
full_contents = Lich::Util.issue_command("rummage #{container}", /^You rummage .* but there is nothing in there\.$|^You rummage .* and see (.*)\./, quiet: true) | ||
|
||
rummage_line = full_contents.find { |line| line.start_with?('You rummage') } | ||
|
||
if rummage_line =~ /but there is nothing in there/ | ||
container_name = rummage_line.match(/^You rummage (.*?) but/)[1] | ||
return [container_name, []] | ||
else | ||
container_name = rummage_line.match(/^You rummage (.*?) and see/)[1] | ||
contents = rummage_line.sub(/^You rummage through .*? and see /, '') | ||
return [container_name, DRC.list_to_array(contents)] | ||
end | ||
end | ||
|
||
def sort_items(items) | ||
sorted = {} | ||
items.each do |item| | ||
clean_item = item.sub(/^\s*?\b(?:a|an|some|and|the)\b\s/, '').chomp('.') | ||
noun = DRC.get_noun(clean_item) | ||
type = get_item_type(clean_item, noun) | ||
|
||
if sorted[type] | ||
if sorted[type][clean_item] | ||
sorted[type][clean_item][:qty] += 1 | ||
else | ||
sorted[type][clean_item] = { noun: noun, qty: 1, full_description: item.chomp('.') } | ||
end | ||
else | ||
sorted[type] = { clean_item => { noun: noun, qty: 1, full_description: item.chomp('.') } } | ||
end | ||
end | ||
sorted | ||
end | ||
|
||
def get_item_type(item, noun) | ||
item_data = get_data('sorting').to_h.merge(get_data('items').to_h) | ||
category = 'Other' | ||
|
||
item_data.each do |key, value| | ||
if noun =~ /#{value.join('$|').concat('$')}/i || item =~ /(?:#{value.join('$|').concat('$')})/i | ||
category = key.to_s.sub(/_nouns|_types/, '').capitalize | ||
break | ||
end | ||
end | ||
|
||
category | ||
end | ||
|
||
def display_table(sorted_items, container_name) | ||
table = Terminal::Table.new do |t| | ||
t.title = "Contents of #{container_name.strip}" | ||
t.style = { border_x: "-", border_i: "+", border_y: "|" } | ||
|
||
if sorted_items.empty? | ||
t << [{ value: "This container is empty", alignment: :center, colspan: 2 }] | ||
else | ||
t.headings = ['Item', 'Qty.'] | ||
sorted_types = sorted_items.keys.sort_by { |type| type == "Other" ? [1, type] : [0, type] } | ||
|
||
sorted_types.each do |type| | ||
items = sorted_items[type] | ||
t << :separator | ||
t << [{ value: type.strip, alignment: :center, colspan: 2 }] | ||
t << :separator | ||
items.each do |_item, data| | ||
t << [data[:full_description].strip, data[:qty]] | ||
end | ||
end | ||
end | ||
end | ||
Lich::Messaging.mono(table.to_s) | ||
end | ||
end | ||
|
||
container = script.vars[1] | ||
if container.nil? || container.empty? | ||
respond "Usage: ;rummage <container>" | ||
exit | ||
end | ||
Rummage.new.run(container) |