amigojapan

amigojapan top3 plugin for K5 bot

Apr 30th, 2015
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.73 KB | None | 0 0
  1. # encoding: utf-8
  2. # This plugin was developed for the K5 project by amigojapan
  3. # See files README.md and COPYING for copyright and licensing information.
  4.  
  5. # Example plugin
  6. class String
  7.   def contains_cjk?
  8.     !!(self =~ /\p{Han}|\p{Katakana}|\p{Hiragana}|\p{Hangul}/)
  9.   end
  10. end
  11.  
  12. require_relative '../../IRCPlugin'
  13. require 'date'
  14.  
  15. class Top3 < IRCPlugin
  16.   Description = "top3 gives the top 3 Japanese writers of the month (made by amigojapan)"
  17.   Commands = {
  18.     :top3 => "displays the top 3 Japanese writers of the month (made by amigojapan)",
  19.     :rank => "displays the rank of the user",
  20.   }
  21.   Dependencies = [ :StorageYAML ]
  22.  
  23.   def afterLoad
  24.     @locked = false
  25.     @storage = @plugin_manager.plugins[:StorageYAML]
  26.     @top3 = @storage.read('Top3') || {}
  27.   end
  28.  
  29.   def beforeUnload
  30.     "Plugin is busy." if @locked
  31.     @storage = nil
  32.     @top3 = nil
  33.   end
  34.  
  35.   def top3(msg)
  36.     out=""
  37.     unsorted=Array.new
  38.     @top3.each{|data|
  39.       #puts data
  40.       if data[1][1].to_s == Date.today.mon.to_s #display only the entries for the current month
  41.         unsorted.push([data[1][0],data[0]])
  42.       end
  43.     }
  44.     sorted=unsorted.sort.reverse
  45.     #puts sorted
  46.     rank=0
  47.     sorted.take(3).each{|data|
  48.       rank=rank+1
  49.       out=out+" #"+rank.to_s+" "+data[1]+" CJK chars:"+data[0].to_s
  50.     }
  51.     rank=0
  52.     place=0
  53.     sorted.each{|data|
  54.       place=place+1
  55.       if data[1] == msg.nick
  56.         rank=place
  57.       end
  58.     }
  59.     if @top3.include? msg.nick
  60.       current_user = @top3[msg.nick][0]
  61.     else
  62.       current_user = 0          
  63.     end      
  64.     out=out+" | "
  65.     out=out+msg.nick+"'s CJK count is: " + current_user.to_s
  66.     if rank == 0
  67.       out=out+" "+msg.nick+" has not typed any Japanese this month :("
  68.     else
  69.       out=out+", currently ranked #" + rank.to_s + " of " + place.to_s
  70.     end
  71.     msg.reply  out
  72.     #I added these just to make sure this is not causing the plugin to have a memory leak
  73.     unsorted=nil
  74.     sorted=nil
  75.   end
  76.  
  77.   def rank(msg)
  78.     person=msg.message.split(/ /)[1]# get parameter
  79.     if person == nil
  80.       person = msg.nick
  81.     end
  82.     out=""
  83.     unsorted=Array.new
  84.     @top3.each{|data|
  85.       #puts data
  86.       if data[1][1].to_s == Date.today.mon.to_s #display only the entries for the current month
  87.         unsorted.push([data[1][0],data[0]])
  88.       end
  89.     }
  90.     sorted=unsorted.sort.reverse
  91.     rank=0
  92.     rank=0
  93.     place=0
  94.     sorted.each{|data|
  95.       place=place+1
  96.       if data[1] == person
  97.         rank=place
  98.       end
  99.     }
  100.     if @top3.include? person
  101.       current_user = @top3[person][0]
  102.     else
  103.       current_user = 0          
  104.     end      
  105.     out=out+person+"'s CJK count is: " + current_user.to_s
  106.     if rank == 0
  107.       out=out+" "+person+" has not typed any Japanese this month :("
  108.     else
  109.       out=out+", currently ranked #" + rank.to_s + " of " + place.to_s
  110.     end
  111.     msg.reply  out
  112.     #I added these just to make sure this is not causing the plugin to have a memory leak
  113.     unsorted=nil
  114.     sorted=nil
  115.   end
  116.  
  117.   def count(msg)
  118.     s2=msg.message.split(//)
  119.     chars=0
  120.     s2.each{|s|
  121.         if s.contains_cjk? == true
  122.             chars=chars+1  
  123.         end
  124.     }
  125.     if chars > 0
  126.       if @top3.include? msg.nick
  127.         @top3[msg.nick] = [@top3[msg.nick][0]+chars,Date.today.mon]
  128.       else
  129.         @top3[msg.nick] = [chars,Date.today.mon]          
  130.       end
  131.       @storage.write('Top3', @top3)
  132.     end
  133.   end
  134.  
  135.   def on_privmsg(msg)
  136.     if msg.bot_command == :top3
  137.       top3(msg)
  138.     elsif msg.bot_command == :rank
  139.       rank(msg)
  140.     elsif !msg.private? and !msg.bot_command
  141.       count(msg)
  142.     end
  143.   end
  144.  
  145. end
  146. #todo:
  147. #Add year tracking
  148. #add anual top3
  149. #Add .rank command so we can see what rank other people have(done)
Advertisement
Add Comment
Please, Sign In to add comment