Skip to content

Commit

Permalink
[eloot] v2.1.0 added option to output debug information to file
Browse files Browse the repository at this point in the history
  • Loading branch information
Deysh committed Jan 14, 2025
1 parent 0d81a65 commit 1d666dc
Showing 1 changed file with 201 additions and 50 deletions.
251 changes: 201 additions & 50 deletions scripts/eloot.lic
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
wiki: https://gswiki.play.net/Lich:Script_Eloot
game: Gemstone
tags: loot
version: 2.0.10
version: 2.1.0
Improvements:
Major_change.feature_addition.bugfix
v2.1.0 (2025-01-13)
- added option to output debug information to file
v2.0.10 (2025-01-11)
- bugfix for box inhand and full locksmith pool
v2.0.9 (2025-01-01)
Expand Down Expand Up @@ -778,7 +780,7 @@ module ELoot
# color options - set type to use
# yellow, orange, teal, green, plain

return if type == "debug" && !ELoot.data.settings[:debug]
return if type == "debug" && (!ELoot.data.settings[:debug] && !ELoot.data.settings[:debug_file])

if text.class == Hash
text = text.inspect.gsub("#<", "#")
Expand All @@ -789,16 +791,20 @@ module ELoot
end

respond '' if space
if type == 'debug'
if type == 'debug' && ELoot.data.settings[:debug]
begin
Lich::Messaging.mono(Lich::Messaging.xml_encode(text))
rescue NoMethodError
respond(text)
end
else
elsif type != 'debug'
Lich::Messaging.msg(type, text)
end
respond '' if space

if ELoot.data.settings[:debug_file]
ELoot.data.debug_logger.log(text)
end
end

def self.fwi?(room)
Expand Down Expand Up @@ -826,56 +832,89 @@ module ELoot
end
end

def self.test
echo " Eloot Version: #{ELoot.get_script_version}"
echo ""
echo "--------------- Settings ---------------"
echo(*ELoot.data.settings)
echo ""
echo "--------------- Sacks ------------------"
echo(*ELoot.data.sacks)
echo ""
echo "--------------- Disk -------------------"
echo ELoot.data.disk.inspect
echo ""
echo "--------------- Full Disk/Sack Check ---"
echo "Disk Full: #{ELoot.data.disk_full.inspect}"
echo "Sacks Full: #{ELoot.data.sacks_full.inspect}"
echo ""
echo "--------------- Contents ---------------"

items_opened = Array.new
ELoot.data.sacks.keys.each { |item|
next if items_opened.include?(ELoot.data.sacks[item].id)

items_opened.push(ELoot.data.sacks[item].id)
Inventory.open_single_container(ELoot.data.sacks[item])
ELoot.data.sacks[item].contents.each { |thing|
echo "Container: #{ELoot.data.sacks[item]} | Item: #{thing.name} | Type: #{thing.type} | Sellable: #{thing.sellable}"
}
}
def self.word_wrap(text, width = 100)
text.gsub(/(.{1,#{width}})(\s+|\Z)/, "\\1\n").strip
end

echo ""
echo "--------------- Ready List -------------"
def self.test(debug = false)
rows = []
rows << :separator
rows << [{ value: "*** Settings ***", colspan: 7, alignment: :center }]
rows << :separator
ELoot.data.settings.each do |item|
rows << [{ value: "#{item[0]}: #{ELoot.word_wrap(item[1].to_s)}", colspan: 7 }]
end

ELoot.data.ready_list.each { |item|
echo item
}
rows << :separator
rows << [{ value: "*** Sacks ***", colspan: 7, alignment: :center }]
rows << :separator
rows << ["Type", "ID", { value: "Name", colspan: 5 }]
rows << :separator
ELoot.data.sacks.each do |key, value|
rows << [key, value.id, { value: value.name, colspan: 5 }]
end

echo ""
echo "--------------- Skinning Weapons -------------"
rows << :separator
rows << [{ value: "*** Disk ***", colspan: 7, alignment: :center }]
rows << [{ value: ELoot.data.disk.inspect, colspan: 7 }]

echo "Edged Skinner: #{ELoot.data.skin_edged.inspect}"
echo "Blunt Skinner: #{ELoot.data.skin_blunt.inspect}"
rows << :separator
rows << [{ value: "*** Full Disk/Sack Check ***", colspan: 7, alignment: :center }]
rows << [{ value: "Disk Full: #{ELoot.data.disk_full.inspect}", colspan: 7 }]
rows << [{ value: "Sacks Full: #{ELoot.data.sacks_full.inspect}", colspan: 7 }]

echo ""
echo "--------------- Coin Hand -------------"
rows << :separator
rows << [{ value: "*** Contents ***", colspan: 7, alignment: :center }]
rows << :separator
rows << ["Container", "Item", "Type", { value: "Sellable", colspan: 4 }]
rows << :separator
items_opened = []
ELoot.data.sacks.each_value do |sack|
next if items_opened.include?(sack.id)

echo "Coin Hand: #{ELoot.data.coin_hand.inspect}"
echo "Coin Container: #{ELoot.data.coin_container.inspect}"
items_opened << sack.id
Inventory.open_single_container(sack)

echo "--------------GameObj Types-------------"
echo GameObj.type_data.map { |k, _v| k }.sort.join(', ')
sack.contents.each do |thing|
rows << [sack.noun.capitalize, thing.name, thing.type, { value: thing.sellable, colspan: 4 }]
end
end

rows << :separator
rows << [{ value: "*** Ready List ***", colspan: 7, alignment: :center }]
rows << :separator
rows << ["Type", "ID", { value: "Name", colspan: 5 }]
rows << :separator
ELoot.data.ready_list.each do |key, value|
rows << [key, value.id, { value: value.name, colspan: 5 }]
end

rows << :separator
rows << [{ value: "*** Skinning Weapons ***", colspan: 7, alignment: :center }]
rows << :separator
rows << ["Edged Skinner", { value: ELoot.data.skin_edged.inspect, colspan: 6 }]
rows << ["Blunt Skinner", { value: ELoot.data.skin_blunt.inspect, colspan: 6 }]

rows << :separator
rows << [{ value: "*** Coin Hand ***", colspan: 7, alignment: :center }]
rows << :separator
rows << ["Coin Hand", { value: ELoot.data.coin_hand.inspect, colspan: 6 }]
rows << ["Coin Container", { value: ELoot.data.coin_container.inspect, colspan: 6 }]

rows << :separator
rows << [{ value: "*** GameObj Types ***", colspan: 7, alignment: :center }]
rows << :separator
rows << [{ value: "#{ELoot.word_wrap(GameObj.type_data.map { |k, _v| k }.sort.join(', '))}", colspan: 7 }]

table = Terminal::Table.new :title => "#{Script.current.name.capitalize} v#{ELoot.get_script_version}", :rows => rows

if debug
return table
else
respond
respond table
respond
end
end

def self.validate_setup
Expand Down Expand Up @@ -1320,6 +1359,7 @@ module ELoot
rows << [{ value: 'Troubleshooting', colspan: 2, alignment: :center }]
rows << :separator
rows << ["#{$lich_char}#{Script.current.name} debug", "Toggles debugging on or off"]
rows << ["#{$lich_char}#{Script.current.name} debug file", "Toggles logging to a file on or off"]
rows << ["#{$lich_char}#{Script.current.name} list", "Lists script settings"]
rows << ["#{$lich_char}#{Script.current.name} test", "Lists variables and their values"]
rows << :separator
Expand Down Expand Up @@ -1556,6 +1596,100 @@ module ELoot
sleep 0.2
end

# Writes debug out put to a file
class DebugLogger
attr_accessor :logger, :log_file, :script_name

def initialize
@script_name = Script.current.name
# Ensure the debug folder exists
debug_dir = File.join(LOG_DIR, 'debug', "#{XMLData.game}-#{XMLData.name}")
FileUtils.mkdir_p(debug_dir) unless Dir.exist?(debug_dir)

# Set up logger
@log_writer = Queue.new
@log_file = File.join(debug_dir, "#{@script_name}.log")
@logger = Logger.new(@log_file, 'daily')
@logger.formatter = proc do |_, datetime, _, msg|
formatted_time = datetime.strftime("%Y-%m-%d %H:%M:%S") # Excludes timezone
"[#{formatted_time}]: #{msg}\n"
end

# Start logging automatically after initialization
start_logging
end

def start_logging
# Startup Message
eloot_details = ELoot.test(true)

if eloot_details.to_s == ELoot.data.details_check
log("Starting up!")
else
ELoot.data.details_check = eloot_details.to_s
log("Starting up!\n#{eloot_details}")
end

@worker_thread = Thread.new {
UpstreamHook.add("debug_upstream_hook", proc { |data|
@log_writer << ">#{data.sub(/^<c>/, '')}".strip
data
})

DownstreamHook.add("debug_downstream_hook", proc { |data|
@log_writer << data.strip
data
})

loop do
current_last = $_LASTUPSTREAM_
@log_writer.push current_last.sub(/^<c>/, '').strip
while current_last == $_LASTUPSTREAM_
sleep 0.1
end
end
}

@writer_thread = Thread.new do
loop do
line = @log_writer.pop
@logger.info(@script_name) { line } unless line.to_s.strip.empty?
end
end

before_dying {
@logger.info(@script_name) { "Closing up!" }

respond("")
Lich::Messaging.msg("bold", "You've started #{Script.current.name} with debug to file on.")
Lich::Messaging.msg("bold", "Please remember to turn this option off once done needing to debug.")
Lich::Messaging.msg("bold", "Currently debugging to #{@log_file}")
Lich::Messaging.msg("bold", "To turn off, execute #{$clean_lich_char}#{Script.current.name} debug file")
respond("")

UpstreamHook.remove("debug_upstream_hook")
DownstreamHook.remove("debug_downstream_hook")
@worker_thread.kill
@writer_thread.kill
@logger.close
ELoot.data.debug_logger = nil
}

respond("")
Lich::Messaging.msg("bold", "You've started #{Script.current.name} with debug to file on.")
Lich::Messaging.msg("bold", "Please remember to turn this option off once done needing to debug.")
Lich::Messaging.msg("bold", "Currently debugging to #{@log_file}")
Lich::Messaging.msg("bold", "To turn off, execute #{$clean_lich_char}#{Script.current.name} debug file")
respond("")

end

# Method to log a custom message
def log(message)
@log_writer << message
end
end

# Global data used by ELoot
class Data
attr_accessor :disk, :disk_full, :sacks_full, :ready_list, :ready_method, :sacks, :settings, :skinners, :skinsheath, :silent_open, :checked_bags,
Expand All @@ -1564,13 +1698,15 @@ module ELoot
:charm, :gauntlet, :coin_bag, :reagent_horde_inv, :account_type, :gem_regex, :locker, :container_settings, :everything_list, :everything,
:only_list, :only, :inventory, :gem_inventory, :alchemy_inventory, :horde_type, :cache, :locker_city, :use_hording, :stash,
:items_to_horde, :horde_deposit, :inv_save, :deposit_regex, :withdraw_regex, :disk_nouns_regex, :sigil_determination_on_fail, :start_room, :last_called,
:urchin_msg, :gemshop_first, :reject_loot_names, :reject_loot_nouns, :version
:urchin_msg, :gemshop_first, :reject_loot_names, :reject_loot_nouns, :version, :debug_logger, :details_check

def initialize(settings)
@settings = settings
@start_room = nil
@last_called = {}
@version = ELoot.get_script_version
@debug_logger = nil
@details_check = nil

# inventory type
@sacks = {}
Expand Down Expand Up @@ -2188,6 +2324,7 @@ module ELoot
internal: {
silence: { default: false },
debug: { default: false },
debug_file: { default: false },
unskinnable: { default: [] },
unlootable: { default: [] },
crumbly: { default: [] },
Expand Down Expand Up @@ -2828,7 +2965,10 @@ module ELoot
value = value.to_i
end

respond
ELoot.msg(type: "yellow", text: " #{key.capitalize} has been set to #{value}")
respond

@settings[key] = value
end
end
Expand Down Expand Up @@ -6183,6 +6323,11 @@ ELoot.manage_sorter
# Default to loot
if Script.current.vars[1].nil?
Thread.new { loop { Script.self.kill if dead?; sleep(5); } }

if ELoot.data.settings[:debug_file]
ELoot.data.debug_logger = ELoot::DebugLogger.new
end

ELoot.disk_usage
ELoot.loot
ELoot::Inventory.close_sell_containers if ELoot.data.settings[:keep_closed]
Expand All @@ -6202,8 +6347,14 @@ ELoot.data.silver_breakdown = Hash.new

case Script.current.vars[0]
when /debug/
ELoot::Setup.update_setting(:debug, !ELoot.data.settings[:debug])
ELoot.data.settings[:debug] = !ELoot.data.settings[:debug]
if Script.current.vars[0].split(/[\s,|]+/).last =~ /file/
ELoot::Setup.update_setting(:debug_file, !ELoot.data.settings[:debug_file])
ELoot.data.settings[:debug_file] = !ELoot.data.settings[:debug_file]
else
ELoot::Setup.update_setting(:debug, !ELoot.data.settings[:debug])
ELoot.data.settings[:debug] = !ELoot.data.settings[:debug]
end

ELoot.save_profile(silent: true)
exit
when 'start'
Expand Down

0 comments on commit 1d666dc

Please sign in to comment.