1 class PointsPlugin < Plugin
5 # this plugin only wants to store ints!
14 @registry.set_default(0)
16 # import if old file format found
17 oldpoints = @bot.path 'points.rbot'
18 if File.exist? oldpoints
19 log "importing old points data"
20 IO.foreach(oldpoints) do |line|
21 if(line =~ /^(\S+)<=>([\d-]+)$/)
24 @registry[item] = points
33 max = @registry.values.max || "zero"
34 min = @registry.values.min || "zero"
35 best = @registry.to_hash.key(max) || "nobody"
36 worst = @registry.to_hash.key(min) || "nobody"
37 m.reply "#{@registry.length} items. Best: #{best} (#{max}); Worst: #{worst} (#{min})"
44 msg << @registry.to_hash.sort_by { |k, v| v }.reverse.
45 map { |k,v| "#{k}: #{v}" }.
49 m.reply "nobody has any points yet!"
55 thing = m.sourcenick unless thing
57 points = @registry[thing]
59 m.reply "points for #{thing}: #{@registry[thing]}"
61 m.reply "#{thing} has zero points"
65 def setpoints(m, params)
66 thing = (params[:key] || m.sourcenick).to_s
67 @registry[thing] = params[:val].to_i
71 def help(plugin, topic="")
72 "points module: Keeps track of internet points, infusing your pointless life with meaning. Listens to everyone's chat. <thing>++/<thing>-- => increase/decrease points for <thing>, points for <thing>? => show points for <thing>, 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."
76 return unless m.public? && m.message.match(/\+\+|--/)
80 m.message.split.each_with_index do |tok, i|
81 # ignore preceeding +/-
85 tok.sub!(/[:,]$/, '') if i == 0
86 catch :me_if_you_can do
87 if m.channel.users[tok].nil?
88 if tok =~ /^(.*[^-].*)(?:--)$/
91 elsif tok =~ /^(.*[^+].*)(?:\+\+)$/
97 if (tok =~ /^--+$/) || (tok =~ /^\+\++$/)
106 ac[arg] += (op == '--' ? -1 : 1) unless arg.downcase == m.sourcenick.downcase
112 next if v == 0 or /--|\+\+/.match(k)
113 # strip invisible formatting characters like bold or color codes
114 k = k.gsub(FormattingRx, '')
115 @registry[k] += (v > 0 ? 1 : -1)
116 m.reply @bot.lang.get("thanks") if k == @bot.nick && v > 0
117 m.reply "#{k} now has #{@registry[k]} points!"
122 plugin = PointsPlugin.new
124 plugin.default_auth( 'edit', false )
126 plugin.map 'pointstats', :action => 'stats'
127 plugin.map 'points :key', :defaults => {:key => false}
128 plugin.map 'setpoints :key :val', :defaults => {:key => false}, :requirements => {:val => /^-?\d+$/}, :auth_path => 'edit::set!'
129 plugin.map 'points for :key'
130 plugin.map 'pointsdump', :action => 'dump'