]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/karma.rb
ad5f57a27dc9c1d151f32915fad2a8603eae66fc
[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     oldkarma = @bot.path 'karma.rbot'
18     if File.exist? oldkarma
19       log "importing old karma data"
20       IO.foreach(oldkarma) do |line|
21         if(line =~ /^(\S+)<=>([\d-]+)$/)
22           item = $1
23           karma = $2.to_i
24           @registry[item] = karma
25         end
26       end
27       File.delete oldkarma
28     end
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   def setkarma(m, params)
54     thing = (params[:key] || m.sourcenick).to_s
55     @registry[thing] = params[:val].to_i
56     karma(m, params)
57   end
58   
59   def help(plugin, topic="")
60     "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."
61   end
62
63   def message(m)
64     return unless m.public? && m.message.match(/\+\+|--/)
65     arg = nil
66     op = nil
67     ac = Hash.new
68     m.message.split.each_with_index do |tok, i|
69       tok.sub!(/[:,]$/, '') if i == 0
70       catch :me_if_you_can do
71         if m.channel.users[tok].nil?
72           if (tok =~ /^(?:--)(.*[^-].*)$/) || (tok =~ /^(.*[^-].*)(?:--)$/)
73             op, arg = '--', $1
74             next
75           elsif (tok =~ /^(?:\+\+)(.*[^+].*)$/)||(tok =~ /^(.*[^+].*)(?:\+\+)$/)
76             op, arg = '++', $1
77             next
78           end
79         end
80
81         if (tok =~ /^--+$/) || (tok =~ /^\+\++$/)
82           op = tok.slice(0, 2)
83         else
84           arg = tok
85         end
86       end # catch
87
88       if op && arg
89         ac[arg] ||= 0
90         ac[arg] += (op == '--' ? -1 : 1)
91         op = arg = nil
92       end
93     end
94
95     ac.each do |k, v|
96       next if v == 0
97       @registry[k] += (v > 0 ? 1 : -1)
98       m.reply @bot.lang.get("thanks") if k == @bot.nick && v > 0
99     end
100   end
101 end
102
103 plugin = KarmaPlugin.new
104
105 plugin.default_auth( 'edit', false )
106
107 plugin.map 'karmastats', :action => 'stats'
108 plugin.map 'karma :key', :defaults => {:key => false}
109 plugin.map 'setkarma :key :val', :defaults => {:key => false}, :requirements => {:val => /^-?\d+$/}, :auth_path => 'edit::set!'
110 plugin.map 'karma for :key'