Skip to content

Commit

Permalink
Merge pull request #7028 from ThatRasputin/script/update-sorter
Browse files Browse the repository at this point in the history
  • Loading branch information
MahtraDR authored Dec 18, 2024
2 parents 76796c3 + 3def6f9 commit 4488a91
Showing 1 changed file with 1 addition and 56 deletions.
57 changes: 1 addition & 56 deletions sorter.lic
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,6 @@ class Sorter
sorted_items = sort_items(items)
@use_table ? display_table(sorted_items, container_name) : display_default(sorted_items, container_name)
nil # Suppress the original line output
elsif line =~ /^On (?:an?|the) (.*?), you see:/
container_name = $1.strip
shop_items = []
while (item_line = get) !~ /\[Type SHOP \[GOOD\] or click an item to see some details about it\.\]/
if item_line =~ /<d cmd='shop #\d+ on #\d+'>(.*?)<\/d>/
shop_items << $1.strip
end
end
sorted_items = sort_shop_items(shop_items)
display_shop_table(sorted_items, container_name)
nil
else
line # Allow non-matching lines to pass through
end
Expand All @@ -64,7 +53,6 @@ class Sorter
clean_item = item.sub(/^\s*?\b(?:a|an|some|and|the)\b\s/, '').chomp('.').strip
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
Expand All @@ -78,85 +66,42 @@ class Sorter
sorted
end

def sort_shop_items(items)
sorted = {}
items.each do |item|
if item =~ /(.*) for (.*?) (.*?) Kronars$/
full_description = $1.strip
price = $2
currency = $3
clean_item = full_description.sub(/^\s*?\b(?:a|an|some|and|the)\b\s/, '')
noun = DRC.get_noun(clean_item)
type = get_item_type(clean_item, noun)
sorted[type] ||= {}
sorted[type][clean_item] = {
noun: noun,
price: "#{price} #{currency}",
full_description: full_description
}
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 |data|
items.each do |_item, data|
t << [data[:full_description].strip, data[:qty]]
end
end
end
end

table_string = table.to_s.gsub('[[MONSTERBOLD]]', monsterbold_start).gsub('[[/MONSTERBOLD]]', monsterbold_end)
Lich::Messaging.mono(table_string)
end

def display_shop_table(sorted_items, container_name)
table = Terminal::Table.new do |t|
t.title = "Items for sale on #{container_name}"
t.headings = ['Item', 'Type', 'Price']
sorted_items.each do |type, items|
t << :separator
t << [{ value: type, alignment: :center, colspan: 3 }]
t << :separator
items.each do |data|
t << [data[:full_description], data[:noun], data[:price]]
end
end
end

Lich::Messaging.mono(table.to_s)
end

def display_default(sorted_items, container_name)
output = "#{container_name}:\n"
sorted_items.sort.each do |category_name, category_contents|
Expand Down

0 comments on commit 4488a91

Please sign in to comment.