From 78e22654c232992598ef1db3bf29dfe69f1ade36 Mon Sep 17 00:00:00 2001 From: Mike Klobutcher <19937263+ThatRasputin@users.noreply.github.com> Date: Mon, 16 Dec 2024 17:54:38 -0500 Subject: [PATCH 1/3] Adding new standalone rummage sorter --- rummage.lic | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 rummage.lic diff --git a/rummage.lic b/rummage.lic new file mode 100644 index 000000000..8830cddb6 --- /dev/null +++ b/rummage.lic @@ -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 " + exit +end +Rummage.new.run(container) \ No newline at end of file From 1671b695a959d664a905051b5effb50e3d812b6b Mon Sep 17 00:00:00 2001 From: Mahtra <93822896+MahtraDR@users.noreply.github.com> Date: Tue, 17 Dec 2024 13:24:18 +0800 Subject: [PATCH 2/3] Rubocop --- rummage.lic | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/rummage.lic b/rummage.lic index 8830cddb6..b49cb6443 100644 --- a/rummage.lic +++ b/rummage.lic @@ -12,9 +12,9 @@ class Rummage 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, []] @@ -24,14 +24,14 @@ class Rummage 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 @@ -48,14 +48,14 @@ class Rummage 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 @@ -63,19 +63,19 @@ class Rummage 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| + items.each do |_item, data| t << [data[:full_description].strip, data[:qty]] end end @@ -90,4 +90,4 @@ if container.nil? || container.empty? respond "Usage: ;rummage " exit end -Rummage.new.run(container) \ No newline at end of file +Rummage.new.run(container) From dbe8ddc5956fd7ee888fa57070e39d116a1c0dfc Mon Sep 17 00:00:00 2001 From: Mahtra <93822896+MahtraDR@users.noreply.github.com> Date: Tue, 17 Dec 2024 13:28:19 +0800 Subject: [PATCH 3/3] Rubocop again... --- rummage.lic | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/rummage.lic b/rummage.lic index b49cb6443..6a00e8401 100644 --- a/rummage.lic +++ b/rummage.lic @@ -12,9 +12,9 @@ class Rummage 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, []] @@ -24,14 +24,14 @@ class Rummage 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 @@ -48,14 +48,14 @@ class Rummage 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 @@ -63,13 +63,13 @@ class Rummage 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