Browse Source

alter xp and begin collecting message length statistics

Kylie Jo Swistak 5 years ago
parent
commit
4da6d9f58b
2 changed files with 24 additions and 7 deletions
  1. 15 3
      app/models/users.rb
  2. 9 4
      bot.rb

+ 15 - 3
app/models/users.rb

@@ -39,9 +39,8 @@ class User < ActiveRecord::Base
   end
 
   def update_xp(msg_length, member=nil)
-    xp =
+    old_xp =
       case msg_length
-      when 0..39 then 0
       when 40..149 then 1
       when 150..299 then 2
       when 300..599 then 3
@@ -49,9 +48,22 @@ class User < ActiveRecord::Base
       else 5
       end
 
+    multiplier =
+      case msg_length
+      when 50..299 then 1
+      when 300..649 then 1.5
+      when 650..999 then 1
+      else 0.5
+      end
+
+    xp = (msg_length/50 * multiplier).to_i
+
     self.update(
       boosted_xp: boosted_xp + xp,
-      unboosted_xp: unboosted_xp + xp
+      unboosted_xp: unboosted_xp + xp,
+      additional_xp: additional_xp + (xp - old_xp),
+      post_length: post_length + msg_length,
+      post_count: post_count + 1
     )
 
     self.reload

+ 9 - 4
bot.rb

@@ -95,19 +95,24 @@ bot.message do |event|
   # Apply experience to non-bot users
   elsif !author.bot_account? && !author.webhook?
     # Replace any urls with 150 'x' chars
-    message = URL.match(content) ? content.gsub(URL, "x" * 150) : content
+    message = URL.match(content) ? content.gsub(URL, "x" * 100) : content
 
     # Add 40 to the message length if there's a file
     msg_length = event.message.attachments.map(&:filename).count > 0 ?
       40 + message.length : message.length
 
-    if msg_length >= 40
-      # Wait until now to find user, so DB isn't touched for every message
-      user = User.find(author.id)
+    # Record post lengths and count
+    user = User.find(author.id)
 
+    if msg_length >= 40
       # Update User and reply with image, if there is one
       reply =  user.update_xp(msg_length, author)
       bot.send_file(event.channel.id, File.open(reply, 'r')) if reply
+    else
+      user.update(
+        short_post_length: user.short_post_length + msg_length,
+        short_post_count: user.short_post_count + 1
+      )
     end
   end