]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/points.rb
plugin(points): forgot one special case, see #34
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / points.rb
1 class PointsPlugin < 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     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-]+)$/)
22           item = $1
23           points = $2.to_i
24           @registry[item] = points
25         end
26       end
27       File.delete oldpoints
28     end
29   end
30
31   def stats(m, params)
32     if (@registry.length)
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})"
38     end
39   end
40
41   def dump(m, params)
42     if (@registry.length)
43       msg = "Points dump: "
44       msg << @registry.to_hash.sort_by { |k, v| v }.reverse.
45                        map { |k,v| "#{k}: #{v}" }.
46                        join(", ")
47       m.reply msg
48     else
49       m.reply "nobody has any points yet!"
50     end
51   end
52
53   def points(m, params)
54     thing = params[:key]
55     thing = m.sourcenick unless thing
56     thing = thing.to_s
57     points = @registry[thing]
58     if(points != 0)
59       m.reply "points for #{thing}: #{@registry[thing]}"
60     else
61       m.reply "#{thing} has zero points"
62     end
63   end
64
65   def setpoints(m, params)
66     thing = (params[:key] || m.sourcenick).to_s
67     @registry[thing] = params[:val].to_i
68     points(m, params)
69   end
70
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."
73   end
74
75   def message(m)
76     return unless m.public? and m.message.match(/\+\+|--/)
77
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, '')
82
83       # each token must end with ++ or --
84       next unless token.match(/^(.*)(\+\+|--)$/)
85       token = $1  # strip ++/-- from token
86       flag = $2  # remember ++/--
87
88       # token must not have more than one ++/--
89       next if token.match(/(\+\+|--)/)
90
91       # each token must include at least one alphanumerical character
92       next unless token.match /[[:alnum:]]/
93
94       # ignore assigning points to oneself
95       next if token.downcase == m.sourcenick.downcase
96
97       votes[token] += flag == '++' ? 1 : -1
98     end
99
100     votes.each do |token, points|
101       @registry[token] += points
102
103       if token == @bot.nick and points > 0
104         m.thanks
105       end
106
107       m.reply "#{token} now has #{@registry[token]} points!"
108     end
109   end
110 end
111
112 plugin = PointsPlugin.new
113
114 plugin.default_auth( 'edit', false )
115
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'