From bc7efe2d4b360da0276287e6cc7f6a401609c162 Mon Sep 17 00:00:00 2001 From: Eric Sorenson Date: Tue, 15 Dec 2015 16:32:45 -0800 Subject: [PATCH] Improvements to the points plugin This commit makes a couple of improvements the points plugin: - added a 'pointsdump' command to show everything in the registry, not just the best and worst - fixed Hash#index deprecation warnings under ruby >1.9 - made nicer messages when displaying stats on an empty registry --- data/rbot/plugins/points.rb | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/data/rbot/plugins/points.rb b/data/rbot/plugins/points.rb index 1a1464e5..21157f3d 100644 --- a/data/rbot/plugins/points.rb +++ b/data/rbot/plugins/points.rb @@ -30,14 +30,26 @@ class PointsPlugin < Plugin def stats(m, params) if (@registry.length) - max = @registry.values.max - min = @registry.values.min - best = @registry.to_hash.index(max) - worst = @registry.to_hash.index(min) + max = @registry.values.max || "zero" + min = @registry.values.min || "zero" + best = @registry.to_hash.key(max) || "nobody" + worst = @registry.to_hash.key(min) || "nobody" m.reply "#{@registry.length} items. Best: #{best} (#{max}); Worst: #{worst} (#{min})" end end + def dump(m, params) + if (@registry.length) + msg = "Points dump: " + msg << @registry.to_hash.sort_by { |k, v| v }.reverse. + map { |k,v| "#{k}: #{v}" }. + join(", ") + m.reply msg + else + m.reply "nobody has any points yet!" + end + end + def points(m, params) thing = params[:key] thing = m.sourcenick unless thing @@ -57,7 +69,7 @@ class PointsPlugin < Plugin end def help(plugin, topic="") - "points module: Keeps track of internet points, infusing your pointless life with meaning. Listens to everyone's chat. ++/-- => increase/decrease points for , points for ? => show points for , pointstats => show stats. Points are a community rating system - only in-channel messages can affect points and you cannot adjust your own." + "points module: Keeps track of internet points, infusing your pointless life with meaning. Listens to everyone's chat. ++/-- => increase/decrease points for , points for ? => show points for , pointstats => show best/worst, pointsdump => show everyone's points. Points are a community rating system - only in-channel messages can affect points and you cannot adjust your own." end def message(m) @@ -96,6 +108,7 @@ class PointsPlugin < Plugin next if v == 0 @registry[k] += (v > 0 ? 1 : -1) m.reply @bot.lang.get("thanks") if k == @bot.nick && v > 0 + m.reply "#{k} now has #{@registry[k]} points!" end end end @@ -108,3 +121,4 @@ plugin.map 'pointstats', :action => 'stats' plugin.map 'points :key', :defaults => {:key => false} plugin.map 'setpoints :key :val', :defaults => {:key => false}, :requirements => {:val => /^-?\d+$/}, :auth_path => 'edit::set!' plugin.map 'points for :key' +plugin.map 'pointsdump', :action => 'dump' -- 2.39.2