Advertisement
amigojapan

new Top3 no monthly bug, keeps track of all data

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