summaryrefslogtreecommitdiff
path: root/lib/rbot/plugins/karma.rb
blob: 148427a53cd15f36872f8c5abd2ba2ec7fc08bb4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
class KarmaPlugin < Plugin
  def initialize
    super

    # this plugin only wants to store ints!
    class << @registry
      def store(val)
        val.to_i
      end
      def restore(val)
        val.to_i
      end
    end
    @registry.set_default(0)

    # import if old file format found
    if(File.exist?("#{@bot.botclass}/karma.rbot"))
      puts "importing old karma data"
      IO.foreach("#{@bot.botclass}/karma.rbot") do |line|
        if(line =~ /^(\S+)<=>([\d-]+)$/)
          item = $1
          karma = $2.to_i
          @registry[item] = karma
        end
      end
      File.delete("#{@bot.botclass}/karma.rbot")
    end

  end

  def stats(m, params)
    if (@registry.length)
      max = @registry.values.max
      min = @registry.values.min
      best = @registry.to_hash.index(max)
      worst = @registry.to_hash.index(min)
      m.reply "#{@registry.length} items. Best: #{best} (#{max}); Worst: #{worst} (#{min})"
    end
  end

  def karma(m, params)
    thing = params[:key]
    thing = m.sourcenick unless thing
    thing = thing.to_s
    karma = @registry[thing]
    if(karma != 0)
      m.reply "karma for #{thing}: #{@registry[thing]}"
    else
      m.reply "#{thing} has neutral karma"
    end
  end
  
  
  def help(plugin, topic="")
    "karma module: <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."
  end
  def listen(m)
    return unless m.kind_of?(PrivMessage) && m.public?
    # in channel message, the kind we are interested in
    if(m.message =~ /(\+\+|--)/)
      string = m.message.sub(/\W(--|\+\+)(\(.*?\)|[^(++)(\-\-)\s]+)/, "\2\1")
      seen = Hash.new
      while(string.sub!(/(\(.*?\)|[^(++)(\-\-)\s]+)(\+\+|--)/, ""))
        key = $1
        change = $2
        next if seen[key]
        seen[key] = true

        key.sub!(/^\((.*)\)$/, "\1")
        key.gsub!(/\s+/, " ")
        next unless(key.length > 0)
        next if(key == m.sourcenick)
        if(change == "++")
          @registry[key] += 1
        elsif(change == "--")
          @registry[key] -= 1
        end
      end
    end
  end
end
plugin = KarmaPlugin.new
plugin.map 'karmastats', :action => 'stats'
plugin.map 'karma :key', :defaults => {:key => false}
plugin.map 'karma for :key'