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? and m.message.match(/\+\+|--/)
78 votes = Hash.new { |h,k| h[k] = 0 } # defaulting to zero
79 m.message.split(' ').each do |token|
80 # remove any color codes from the token
81 token = token.gsub(FormattingRx, '')
83 # each token must end with ++ or --
84 next unless token.match(/^(.*)(\+\+|--)$/)
85 token = $1 # strip ++/-- from token
86 flag = $2 # remember ++/--
88 # token must not have more than one ++/--
89 next if token.match(/(\+\+|--)/)
91 # each token must include at least one alphanumerical character
92 next unless token.match /[[:alnum:]]/
94 # ignore assigning points to oneself
95 next if token.downcase == m.sourcenick.downcase
97 votes[token] += flag == '++' ? 1 : -1
100 votes.each do |token, points|
101 @registry[token] += points
103 if token == @bot.nick and points > 0
107 m.reply "#{token} now has #{@registry[token]} points!"
112 plugin = PointsPlugin.new
114 plugin.default_auth( 'edit', false )
116 plugin.map 'pointstats', :action => 'stats'
117 plugin.map 'points :key', :defaults => {:key => false}
118 plugin.map 'setpoints :key :val', :defaults => {:key => false}, :requirements => {:val => /^-?\d+$/}, :auth_path => 'edit::set!'
119 plugin.map 'points for :key'
120 plugin.map 'pointsdump', :action => 'dump'