Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "[script] [update sorter] Fix for Ethun's issue" #7029

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 56 additions & 1 deletion sorter.lic
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ 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 @@ -53,6 +64,7 @@ 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 @@ -66,42 +78,85 @@ 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 |_item, data|
items.each do |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
Loading