Advertisement
amigojapan

.top3 exclude user1 user2

Jul 26th, 2015
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 7.08 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. optional .top3 exclude user1 user2... (made by amigojapan)",
  19.     :rank => "displays the rank of the user(made by amigojapan)",
  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.     splitmsg=msg.message.split#we need this later to get the people to exclude
  39.     if splitmsg.include?("exclude")
  40.       exclude_array=splitmsg.drop(splitmsg.index("exclude")+1) #make exclude list
  41.     end
  42.     @top3.each{|data|
  43.       #puts data
  44.       #msg.reply data.to_s
  45.       years=JSON.parse(data[1])
  46.       #msg.reply years.to_s
  47.       if not years[Date.today.year.to_s].nil? #year not found
  48.         if not years[Date.today.year.to_s][Date.today.mon.to_s].nil? #display only the entries for the current month
  49.           #msg.reply "not nil"
  50.           if not exclude_array.nil?
  51.             if not exclude_array.include?(data[0])#data[0] is the nickname
  52.               unsorted.push([years[Date.today.year.to_s][Date.today.mon.to_s],data[0]])
  53.               #msg.reply  "here1"+data[0]
  54.             end
  55.           else
  56.             unsorted.push([years[Date.today.year.to_s][Date.today.mon.to_s],data[0]])
  57.             #msg.reply  "here2"
  58.           end
  59.         end
  60.       end
  61.       years=nil
  62.       #msg.reply unsorted.to_s
  63.     }
  64.     sorted=unsorted.sort.reverse
  65.     #puts sorted
  66.     rank=0
  67.     sorted.take(3).each{|data|
  68.       rank=rank+1
  69.       out=out+" #"+rank.to_s+" "+data[1]+" CJK chars:"+data[0].to_s
  70.     }
  71.     rank=0
  72.     place=0
  73.     sorted.each{|data|
  74.       place=place+1
  75.       if data[1] == msg.nick
  76.         rank=place
  77.       end
  78.     }
  79.     if @top3.include? msg.nick
  80.       #current_user = @top3[msg.nick][0]
  81.       years=JSON.parse(@top3[msg.nick])
  82.       if years[Date.today.year.to_s].nil?
  83.           current_user = 0
  84.         else
  85.           if years[Date.today.year.to_s][Date.today.mon.to_s].nil?
  86.             current_user = 0
  87.           else
  88.             current_user = years[Date.today.year.to_s][Date.today.mon.to_s]
  89.         end
  90.       end
  91.       years=nil
  92.     else
  93.       current_user = 0          
  94.     end      
  95.     out=out+" | "
  96.     out=out+msg.nick+"'s CJK count is: " + current_user.to_s
  97.     if rank == 0
  98.       out=out+" "+msg.nick+" has not typed any Japanese this month :("
  99.     else
  100.       out=out+", currently ranked #" + rank.to_s + " of " + place.to_s
  101.     end
  102.     msg.reply  out
  103.     #I added these just to make sure this is not causing the plugin to have a memory leak
  104.     unsorted=nil
  105.     sorted=nil
  106.   end
  107.  
  108.   def rank(msg)
  109.     person=msg.message.split(/ /)[1]# get parameter
  110.     if person == nil
  111.       person = msg.nick
  112.     end
  113.     out=""
  114.     unsorted=Array.new
  115.     @top3.each{|data|
  116.       #puts data
  117.       #if data[1][1].to_s == Date.today.mon.to_s #display only the entries for the current month
  118.       #  unsorted.push([data[1][0],data[0]])
  119.       #end
  120.       years=JSON.parse(data[1])
  121.       #msg.reply years.to_s
  122.       if not years[Date.today.year.to_s].nil? #year not found
  123.         if not years[Date.today.year.to_s][Date.today.mon.to_s].nil? #display only the entries for the current month
  124.           #msg.reply "not nil"
  125.           unsorted.push([years[Date.today.year.to_s][Date.today.mon.to_s],data[0]])
  126.         end
  127.       end
  128.       years=nil
  129.     }
  130.     sorted=unsorted.sort.reverse
  131.     rank=0
  132.     place=0
  133.     sorted.each{|data|
  134.       place=place+1
  135.       if data[1] == person
  136.         rank=place
  137.       end
  138.     }
  139.     if @top3.include? person
  140.       #current_user = @top3[person][0]
  141.       years=JSON.parse(@top3[person])
  142.       if not years[Date.today.year.to_s].nil? #year not found
  143.         if not years[Date.today.year.to_s][Date.today.mon.to_s].nil? #
  144.           current_user = years[Date.today.year.to_s][Date.today.mon.to_s]
  145.         else
  146.           current_user = 0
  147.         end
  148.       else
  149.         current_user = 0
  150.       end
  151.       years=nil
  152.     else
  153.       current_user = 0          
  154.     end      
  155.     out=out+person+"'s CJK count is: " + current_user.to_s
  156.     if current_user == 0
  157.       out=out+" "+person+" has not typed any Japanese this month :("
  158.     else
  159.       out=out+", currently ranked #" + rank.to_s + " of " + place.to_s
  160.     end
  161.     msg.reply  out
  162.     #I added these just to make sure this is not causing the plugin to have a memory leak
  163.     unsorted=nil
  164.     sorted=nil
  165.   end
  166.  
  167.   def count(msg)
  168.     s2=msg.message.split(//)
  169.     chars=0
  170.     s2.each{|s|
  171.         if s.contains_cjk? == true
  172.             chars=chars+1  
  173.         end
  174.     }
  175.     #msg.reply @top3[msg.nick].nil?.to_s
  176.     if @top3[msg.nick].nil? #no data add yearly and monthly arrays
  177.       years={}
  178.       years[Date.today.year.to_s]={}
  179.       years[Date.today.year.to_s][Date.today.mon.to_s]=chars
  180.       @top3[msg.nick] =years.to_json
  181.       @storage.write('Top3', @top3)
  182.       years=nil
  183.       return
  184.     end
  185.     #if years[Date.today.year].nil? #new year
  186.     #  years[Date.today.year]={}
  187.     #end
  188.     #if years[Date.today.year][Date.today.mon].nil? #new month
  189.     #  years[Date.today.year][Date.today.mon]=
  190.     #end    
  191.     if chars > 0
  192.       years=JSON.parse(@top3[msg.nick])
  193.       if years[Date.today.year.to_s].nil?
  194.         years[Date.today.year.to_s]={}
  195.       end
  196.       if years[Date.today.year.to_s][Date.today.mon.to_s].nil?
  197.           years[Date.today.year.to_s][Date.today.mon.to_s]=0
  198.       end
  199.       years[Date.today.year.to_s][Date.today.mon.to_s]+=chars
  200.       @top3[msg.nick] =years.to_json
  201.       #msg.reply years[Date.today.year.to_s][Date.today.mon.to_s]
  202.       #if @top3.include? msg.nick
  203.       #  @top3[msg.nick] = [@top3[msg.nick][0]+chars,Date.today.mon]
  204.       #else
  205.       #  @top3[msg.nick] = [chars,Date.today.mon]          
  206.       #end
  207.       @storage.write('Top3', @top3)
  208.     end
  209.   end
  210.  
  211.   def on_privmsg(msg)
  212.     if msg.bot_command == :top3
  213.       top3(msg)
  214.     elsif msg.bot_command == :rank
  215.       rank(msg)
  216.     elsif !msg.private? and !msg.bot_command
  217.       count(msg)
  218.     end
  219.   end
  220.   rescue Exception => e
  221.     @top3["error message"] =  e.message  
  222.     @top3["error backtrace"] =  e.backtrace.inspect  
  223.     @storage.write('Top3', @top3)
  224. end
  225. #Add year tracking
  226. #add anual top3
  227. #Add .rank command so we can see what rank other people have(done)
  228. #request to keep track of nicks even if they change nick
  229. #nick = msg.tail || msg.nick
  230. #user = msg.bot.find_user_by_nick(nick)
  231. #futoshi: .top3 without futoshi みたいのはどう?w
  232. #corelax: btw how about this command -> .top3at 201507
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement