Skip to content

Commit

Permalink
rubocop cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mrhoribu authored Jan 9, 2025
1 parent 5d8880a commit 62968f3
Showing 1 changed file with 40 additions and 36 deletions.
76 changes: 40 additions & 36 deletions scripts/hud_bounty.lic
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
=begin
This script formats the output of the bounty verb in a better HUD
Includes:
1. tracking for vouchers used recently
2. human readable expiry time for waivers
3. bounty points per hour
4. table for tasks
author: Ondreian
tags: cosmetic, bounty
author: Ondreian
tags: cosmetic, bounty
version: 0.4
changelog:
0.1: initial release
0.2: fix for fmt_time
0.3: fix monospaced output for SF/Wiz FE, fix rescue for missing gem
0.4: fix for Lich refactor 5.11+ namescope issue
rubocop cleanup
=end

Expand All @@ -34,7 +35,7 @@ end
module BountyHUD
require "date"
begin
require "terminal-table"
require "terminal-table"
rescue LoadError => _exception
_respond "<b>This script needs the `terminal-table` gem.</b>"
_respond "<b>Install by running: gem install terminal-table</b>"
Expand All @@ -54,7 +55,7 @@ module BountyHUD
START = %[noun="#{Char.name}">#{Char.name}</a>, your Adventurer's Guild information is as follows:]
PROMPT = %[<prompt]

TASK_INFO = %r[^You have succeeded at the (?<task>.*?) task (?<total>[\d,]+) times?( and failed (?<failures>[\d,]+) times?)?\.]
TASK_INFO = %r[^You have succeeded at the (?<task>.*?) task (?<total>[\d,]+) times?(?: and failed (?<failures>[\d,]+) times?)?\.]
TOTAL_POINTS = %r[^You have accumulated a total of (?<points>[\d,]+) lifetime bounty points\.]
VOUCHERS = %r[^You have (?<expedites>[\d,]+) expedited task reassignment vouchers remaining.]
EXEMPTION = %r[You are currently exempt from being assigned the (?<task>.*?) task. Your exemption period will last until (?<expiry>.*?)\.]
Expand All @@ -64,13 +65,13 @@ module BountyHUD

def self.with_commas(str)
str.to_s.reverse
.gsub(/(\d{3})(?=\d)/, '\\1,')
.reverse
.gsub(/(\d{3})(?=\d)/, '\\1,')
.reverse
end

def self.points_context()
{ total: 0,
unspent: 0}
{ total: 0,
unspent: 0 }
end

def self.int(str)
Expand All @@ -82,10 +83,10 @@ module BountyHUD
end

def self.context(**kv)
kv.merge({
kv.merge({
points: points_context(),
totals: totals_context(),
tasks: []
tasks: []
})
end

Expand All @@ -103,44 +104,45 @@ module BountyHUD
recent = completions - @session[task]

ctx[:totals] = ctx[:totals].zip([
"",
completions,
recent,
int(info[:failures] || 0)
]).map do |pairs| pairs.reduce(&:+) end
"",
completions,
recent,
int(info[:failures] || 0)
]).map do |pairs| pairs.reduce(&:+) end

ctx[:tasks].push [
ctx[:tasks].push [
task,
with_commas(completions),
recent > 0 ? recent : "",
fails ]
fails
]
end

def self.parse_exemption(ctx, message)
info = message.match(Messages::EXEMPTION)
$exemption_expiry = DateTime.strptime(info[:expiry], "%m/%d/%Y %H:%M:%S %z")
Hash.put(ctx, %i[exemption],
{ task: info[:task].downcase,
expiry: ($exemption_expiry.to_time - Time.now) })
Hash.put(ctx, %i[exemption],
{ task: info[:task].downcase,
expiry: ($exemption_expiry.to_time - Time.now) })
end

def self.parse_vouchers(ctx, message)
info = message.match(Messages::VOUCHERS)
remaining = int(info[:expedites])
@session[:expedites] ||= remaining
used = @session[:expedites] - remaining
Hash.put(ctx, %i[expedites],
{total: remaining,
used: used })
Hash.put(ctx, %i[expedites],
{ total: remaining,
used: used })
end

def self.parse_total_points(ctx, message)
earned = int(message.match(Messages::TOTAL_POINTS)[:points])
@session[:starting_earned] ||= earned

Hash.put(ctx, :earned,
{ total: earned,
recent: earned - @session[:starting_earned] })
Hash.put(ctx, :earned,
{ total: earned,
recent: earned - @session[:starting_earned] })
end

def self.parse_unspent(ctx, message)
Expand Down Expand Up @@ -186,7 +188,7 @@ module BountyHUD
[days, hours, minutes, seconds]
.zip(%w(d h m s))
.select { |f| f.first > 0 }
.map {|f| f.first.to_s.rjust(2, "0") + f.last }
.map { |f| f.first.to_s.rjust(2, "0") + f.last }
.reduce("") { |acc, col| acc + " " + col }
.strip
end
Expand All @@ -198,7 +200,8 @@ module BountyHUD
def self.per_hour(metadata)
hours = (Time.now - @session[:start]) / (60 * 60)
with_commas(
(metadata.dig(:earned, :recent) / hours).to_i)
(metadata.dig(:earned, :recent) / hours).to_i
)
end

@metadata = {}
Expand Down Expand Up @@ -233,8 +236,9 @@ module BountyHUD

table = Terminal::Table.new(
headings: headers,
rows: metadata.fetch(:tasks, []) + [metadata.fetch(:totals, [])
.map do |col| with_commas(col) end])
rows: metadata.fetch(:tasks, []) + [metadata.fetch(:totals, [])
.map do |col| with_commas(col) end]
)

headers.each_with_index do |_, i|
table.align_column(i, :right)
Expand All @@ -246,22 +250,22 @@ module BountyHUD

return [hud, "\n", metadata[:task], prompt].join("\n")
end

def self.attach()
DownstreamHook.add(self.name, -> incoming {
DownstreamHook.add(self.name, ->incoming {
begin
if incoming =~ Messages::REWARDS
parse_rewards(incoming)
return nil
end
#echo([incoming, incoming.include?(Messages::START)])
# echo([incoming, incoming.include?(Messages::START)])
@buffer.push(incoming) if incoming.include?(Messages::START) or @buffer.size > 0
# to silence or not
return incoming if @buffer.empty?
# handle when we should consume the buffer
return BountyHUD.show_hud(incoming) if incoming.start_with?(Messages::PROMPT)
# silence the game output
return nil
return nil
rescue => exception
pp exception
pp exception.backtrace
Expand Down

0 comments on commit 62968f3

Please sign in to comment.