]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/karma.rb
Plugin header boilerplating.
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / karma.rb
1 class KarmaPlugin < Plugin
2   def initialize
3     super
4
5     # this plugin only wants to store ints!
6     class << @registry
7       def store(val)
8         val.to_i
9       end
10       def restore(val)
11         val.to_i
12       end
13     end
14     @registry.set_default(0)
15
16     # import if old file format found
17     if(File.exist?("#{@bot.botclass}/karma.rbot"))
18       log "importing old karma data"
19       IO.foreach("#{@bot.botclass}/karma.rbot") do |line|
20         if(line =~ /^(\S+)<=>([\d-]+)$/)
21           item = $1
22           karma = $2.to_i
23           @registry[item] = karma
24         end
25       end
26       File.delete("#{@bot.botclass}/karma.rbot")
27     end
28
29   end
30
31   def stats(m, params)
32     if (@registry.length)
33       max = @registry.values.max
34       min = @registry.values.min
35       best = @registry.to_hash.index(max)
36       worst = @registry.to_hash.index(min)
37       m.reply "#{@registry.length} items. Best: #{best} (#{max}); Worst: #{worst} (#{min})"
38     end
39   end
40
41   def karma(m, params)
42     thing = params[:key]
43     thing = m.sourcenick unless thing
44     thing = thing.to_s
45     karma = @registry[thing]
46     if(karma != 0)
47       m.reply "karma for #{thing}: #{@registry[thing]}"
48     else
49       m.reply "#{thing} has neutral karma"
50     end
51   end
52   
53   
54   def help(plugin, topic="")
55     "karma module: Listens to everyone's chat. <thing>++/<thing>-- => increase/decrease karma for <thing>, karma for <thing>? => show karma for <thing>, karmastats => show stats. Karma is a community rating system - only in-channel messages can affect karma and you cannot adjust your own."
56   end
57   def listen(m)
58     return unless m.kind_of?(PrivMessage) && m.public?
59     # in channel message, the kind we are interested in
60     if(m.message =~ /(\+\+|--)/)
61       string = m.message.sub(/\W(--|\+\+)(\(.*?\)|[^(++)(\-\-)\s]+)/, "\2\1")
62       seen = Hash.new
63       while(string.sub!(/(\(.*?\)|[^(++)(\-\-)\s]+)(\+\+|--)/, ""))
64         key = $1
65         change = $2
66         next if seen[key]
67         seen[key] = true
68
69         key.sub!(/^\((.*)\)$/, "\1")
70         key.gsub!(/\s+/, " ")
71         next unless(key.length > 0)
72         next if(key.downcase == m.sourcenick.downcase)
73         if(change == "++")
74           @registry[key] += 1
75           if key =~ /^#{@bot.nick}$/i
76             @bot.say m.replyto, @bot.lang.get("thanks")
77           end
78         elsif(change == "--")
79           @registry[key] -= 1
80         end
81       end
82     end
83   end
84 end
85
86 plugin = KarmaPlugin.new
87 plugin.map 'karmastats', :action => 'stats'
88 plugin.map 'karma :key', :defaults => {:key => false}
89 plugin.map 'karma for :key'