X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=data%2Frbot%2Fplugins%2Fkarma.rb;h=93d2118996eb787343e3159687baef268b0cc988;hb=052217de30c59206d7025b582d4604557a747470;hp=148427a53cd15f36872f8c5abd2ba2ec7fc08bb4;hpb=21949774b91eaec6ecde4eaa8ad121e2c0a36b87;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/data/rbot/plugins/karma.rb b/data/rbot/plugins/karma.rb index 148427a5..93d21189 100644 --- a/data/rbot/plugins/karma.rb +++ b/data/rbot/plugins/karma.rb @@ -14,18 +14,18 @@ class KarmaPlugin < Plugin @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| + oldkarma = @bot.path 'karma.rbot' + if File.exist? oldkarma + log "importing old karma data" + IO.foreach(oldkarma) do |line| if(line =~ /^(\S+)<=>([\d-]+)$/) item = $1 karma = $2.to_i @registry[item] = karma end end - File.delete("#{@bot.botclass}/karma.rbot") + File.delete oldkarma end - end def stats(m, params) @@ -49,37 +49,62 @@ class KarmaPlugin < Plugin m.reply "#{thing} has neutral karma" end end - - + + def setkarma(m, params) + thing = (params[:key] || m.sourcenick).to_s + @registry[thing] = params[:val].to_i + karma(m, params) + end + def help(plugin, topic="") - "karma module: ++/-- => increase/decrease karma for , karma for ? => show karma for , karmastats => show stats. Karma is a community rating system - only in-channel messages can affect karma and you cannot adjust your own." + "karma module: Listens to everyone's chat. ++/-- => increase/decrease karma for , karma for ? => show karma for , 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 + def message(m) + return unless m.public? && m.message.match(/\+\+|--/) + arg = nil + op = nil + ac = Hash.new + m.message.split.each_with_index do |tok, i| + tok.sub!(/[:,]$/, '') if i == 0 + catch :me_if_you_can do + if m.channel.users[tok].nil? + if (tok =~ /^(?:--)(.*[^-].*)$/) || (tok =~ /^(.*[^-].*)(?:--)$/) + op, arg = '--', $1 + next + elsif (tok =~ /^(?:\+\+)(.*[^+].*)$/)||(tok =~ /^(.*[^+].*)(?:\+\+)$/) + op, arg = '++', $1 + next + end + end + + if (tok =~ /^--+$/) || (tok =~ /^\+\++$/) + op = tok.slice(0, 2) + else + arg = tok end + end # catch + + if op && arg + ac[arg] ||= 0 + ac[arg] += (op == '--' ? -1 : 1) unless arg.downcase == m.sourcenick.downcase + op = arg = nil end end + + ac.each do |k, v| + next if v == 0 + @registry[k] += (v > 0 ? 1 : -1) + m.reply @bot.lang.get("thanks") if k == @bot.nick && v > 0 + end end end + plugin = KarmaPlugin.new + +plugin.default_auth( 'edit', false ) + plugin.map 'karmastats', :action => 'stats' plugin.map 'karma :key', :defaults => {:key => false} +plugin.map 'setkarma :key :val', :defaults => {:key => false}, :requirements => {:val => /^-?\d+$/}, :auth_path => 'edit::set!' plugin.map 'karma for :key'