]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/karma.rb
d8d378e332c223ef54ea0997892905610ab73d15
[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   end
29
30   def stats(m, params)
31     if (@registry.length)
32       max = @registry.values.max
33       min = @registry.values.min
34       best = @registry.to_hash.index(max)
35       worst = @registry.to_hash.index(min)
36       m.reply "#{@registry.length} items. Best: #{best} (#{max}); Worst: #{worst} (#{min})"
37     end
38   end
39
40   def karma(m, params)
41     thing = params[:key]
42     thing = m.sourcenick unless thing
43     thing = thing.to_s
44     karma = @registry[thing]
45     if(karma != 0)
46       m.reply "karma for #{thing}: #{@registry[thing]}"
47     else
48       m.reply "#{thing} has neutral karma"
49     end
50   end
51
52   def setkarma(m, params)
53     thing = (params[:key] || m.sourcenick).to_s
54     @registry[thing] = params[:val].to_i
55     karma(m, params)
56   end
57   
58   def help(plugin, topic="")
59     "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."
60   end
61
62   def message(m)
63     return unless m.public? && m.message.match(/\+\+|--/)
64     arg = nil
65     op = nil
66     ac = Hash.new
67     m.message.split.each_with_index do |tok, i|
68       tok.sub!(/[:,]$/, '') if i == 0
69       catch :me_if_you_can do
70         if m.channel.users[tok].nil?
71           if (tok =~ /^(?:--)(.*[^-].*)$/) || (tok =~ /^(.*[^-].*)(?:--)$/)
72             op, arg = '--', $1
73             next
74           elsif (tok =~ /^(?:\+\+)(.*[^+].*)$/)||(tok =~ /^(.*[^+].*)(?:\+\+)$/)
75             op, arg = '++', $1
76             next
77           end
78         end
79
80         if (tok =~ /^--+$/) || (tok =~ /^\+\++$/)
81           op = tok.slice(0, 2)
82         else
83           arg = tok
84         end
85       end # catch
86
87       if op && arg
88         ac[arg] ||= 0
89         ac[arg] += (op == '--' ? -1 : 1)
90         op = arg = nil
91       end
92     end
93
94     ac.each do |k, v|
95       next if v == 0
96       @registry[k] += (v > 0 ? 1 : -1)
97       m.reply @bot.lang.get("thanks") if k == @bot.nick && v > 0
98     end
99   end
100 end
101
102 plugin = KarmaPlugin.new
103
104 plugin.default_auth( 'edit', false )
105
106 plugin.map 'karmastats', :action => 'stats'
107 plugin.map 'karma :key', :defaults => {:key => false}
108 plugin.map 'setkarma :key :val', :defaults => {:key => false}, :requirements => {:val => /^-?\d+$/}, :auth_path => 'edit::set!'
109 plugin.map 'karma for :key'