]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/commitdiff
plugin(points): new message parser, see #34
authorMatthias Hecker <mail@apoc.cc>
Mon, 13 Apr 2020 18:40:11 +0000 (20:40 +0200)
committerMatthias Hecker <mail@apoc.cc>
Mon, 13 Apr 2020 18:40:11 +0000 (20:40 +0200)
data/rbot/plugins/points.rb
test/plugins/test_points.rb

index 34af9b5090d1ad4fd7bb076e7e0c8a3aced1fcfe..976594ee59e8cdcef9b950e0b3e2b7828b4aa1b9 100644 (file)
@@ -73,49 +73,35 @@ class PointsPlugin < Plugin
   end
 
   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|
-      # ignore preceeding +/-
-      if op && arg.nil?
-        op = nil
-      end
-      tok.sub!(/[:,]$/, '') if i == 0
-      catch :me_if_you_can do
-        if m.channel.users[tok].nil?
-          if tok =~ /^(.*[^-].*)(?:--)$/
-            op, arg = '--', $1
-            next
-          elsif tok =~ /^(.*[^+].*)(?:\+\+)$/
-            op, arg = '++', $1
-            next
-          end
-        end
+    return unless m.public? and m.message.match(/\+\+|--/)
 
-        if (tok =~ /^--+$/) || (tok =~ /^\+\++$/)
-          op = tok.slice(0, 2)
-        else
-          arg = tok
-        end
-      end # catch
+    votes = Hash.new { |h,k| h[k] = 0 }  # defaulting to zero
+    m.message.split(' ').each do |token|
+      # remove any color codes from the token
+      token = token.gsub(FormattingRx, '')
 
-      if op && arg
-        ac[arg] ||= 0
-        ac[arg] += (op == '--' ? -1 : 1) unless arg.downcase == m.sourcenick.downcase
-        op = arg = nil
-      end
+      # each token must end with ++ or --
+      next unless token.match(/^(.*)(\+\+|--)$/)
+      token = $1  # strip ++/-- from token
+      flag = $2  # remember ++/--
+
+      # each token must include at least one alphanumerical character
+      next unless token.match /[[:alnum:]]/
+
+      # ignore assigning points to oneself
+      next if token.downcase == m.sourcenick.downcase
+
+      votes[token] += flag == '++' ? 1 : -1
     end
 
-    ac.each do |k, v|
-      next if v == 0 or /--|\+\+/.match(k)
-      # strip invisible formatting characters like bold or color codes
-      k = k.gsub(FormattingRx, '')
-      next if k.downcase == m.sourcenick.downcase
-      @registry[k] += (v > 0 ? 1 : -1)
-      m.reply @bot.lang.get("thanks") if k == @bot.nick && v > 0
-      m.reply "#{k} now has #{@registry[k]} points!"
+    votes.each do |token, points|
+      @registry[token] += points
+
+      if token == @bot.nick and points > 0
+        m.thanks
+      end
+
+      m.reply "#{token} now has #{@registry[token]} points!"
     end
   end
 end
index 83018e2eb65131c02bafe17099ce2f493b573040..d29b6174e44a541ac7aca01d35d9d5d6ccbdb5c3 100644 (file)
@@ -49,6 +49,13 @@ class PointsPluginTest < Test::Unit::TestCase
     @plugin.message(m)
     assert_equal('alice now has 1 points!', m.replies.first)
 
+    # assign to multiple things
+    m = MockMessage.new('hello linux++ hello torvalds++', 'user')
+    @plugin.message(m)
+    assert_equal(m.replies.length, 2)
+    assert_equal('linux now has 3 points!', m.replies[0])
+    assert_equal('torvalds now has 1 points!', m.replies[1])
+
     ignored = [
       '++alice',
       '--alice',
@@ -56,6 +63,16 @@ class PointsPluginTest < Test::Unit::TestCase
       'ls --sort time',
       '-- foo',
       '++ foo',
+      'test ++',
+      'test --',
+      '<-- pointing',
+      'pointing -->',
+      '&++',
+      ' ++',
+      ' --',
+      '++ --',
+      '-- ++',
+      'https://linux.slashdot.org/story/20/04/12/2138205/how-red-hats-new-ceo-handles-life-under-ibm----and-a-global-pandemic'
     ]
     ignored.each do |ignore|
       m = MockMessage.new(ignore, 'user')
@@ -69,6 +86,6 @@ class PointsPluginTest < Test::Unit::TestCase
 
     m = MockMessage.new('bot++', 'user')
     @plugin.message(m)
-    assert_include(MockBot.new.lang.strings['thanks'], m.replies.first)
+    assert_equal('thanks :)', m.replies.first)
   end
 end