Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # encoding: utf-8
- # This plugin was developed for the K5 project by amigojapan
- # See files README.md and COPYING for copyright and licensing information.
- # Example plugin
- class String
- def contains_cjk?
- !!(self =~ /\p{Han}|\p{Katakana}|\p{Hiragana}|\p{Hangul}/)
- end
- end
- require_relative '../../IRCPlugin'
- require 'date'
- class Top3 < IRCPlugin
- Description = "top3 gives the top 3 Japanese writers of the month (made by amigojapan)"
- Commands = {
- :top3 => "displays the top 3 Japanese writers of the month (made by amigojapan)",
- :rank => "displays the rank of the user",
- }
- Dependencies = [ :StorageYAML ]
- def afterLoad
- @locked = false
- @storage = @plugin_manager.plugins[:StorageYAML]
- @top3 = @storage.read('Top3') || {}
- end
- def beforeUnload
- "Plugin is busy." if @locked
- @storage = nil
- @top3 = nil
- end
- def top3(msg)
- out=""
- unsorted=Array.new
- @top3.each{|data|
- #puts data
- if data[1][1].to_s == Date.today.mon.to_s #display only the entries for the current month
- unsorted.push([data[1][0],data[0]])
- end
- }
- sorted=unsorted.sort.reverse
- #puts sorted
- rank=0
- sorted.take(3).each{|data|
- rank=rank+1
- out=out+" #"+rank.to_s+" "+data[1]+" CJK chars:"+data[0].to_s
- }
- rank=0
- place=0
- sorted.each{|data|
- place=place+1
- if data[1] == msg.nick
- rank=place
- end
- }
- if @top3.include? msg.nick
- current_user = @top3[msg.nick][0]
- else
- current_user = 0
- end
- out=out+" | "
- out=out+msg.nick+"'s CJK count is: " + current_user.to_s
- if rank == 0
- out=out+" "+msg.nick+" has not typed any Japanese this month :("
- else
- out=out+", currently ranked #" + rank.to_s + " of " + place.to_s
- end
- msg.reply out
- #I added these just to make sure this is not causing the plugin to have a memory leak
- unsorted=nil
- sorted=nil
- end
- def rank(msg)
- person=msg.message.split(/ /)[1]# get parameter
- if person == nil
- person = msg.nick
- end
- out=""
- unsorted=Array.new
- @top3.each{|data|
- #puts data
- if data[1][1].to_s == Date.today.mon.to_s #display only the entries for the current month
- unsorted.push([data[1][0],data[0]])
- end
- }
- sorted=unsorted.sort.reverse
- rank=0
- rank=0
- place=0
- sorted.each{|data|
- place=place+1
- if data[1] == person
- rank=place
- end
- }
- if @top3.include? person
- current_user = @top3[person][0]
- else
- current_user = 0
- end
- out=out+person+"'s CJK count is: " + current_user.to_s
- if rank == 0
- out=out+" "+person+" has not typed any Japanese this month :("
- else
- out=out+", currently ranked #" + rank.to_s + " of " + place.to_s
- end
- msg.reply out
- #I added these just to make sure this is not causing the plugin to have a memory leak
- unsorted=nil
- sorted=nil
- end
- def count(msg)
- s2=msg.message.split(//)
- chars=0
- s2.each{|s|
- if s.contains_cjk? == true
- chars=chars+1
- end
- }
- if chars > 0
- if @top3.include? msg.nick
- @top3[msg.nick] = [@top3[msg.nick][0]+chars,Date.today.mon]
- else
- @top3[msg.nick] = [chars,Date.today.mon]
- end
- @storage.write('Top3', @top3)
- end
- end
- def on_privmsg(msg)
- if msg.bot_command == :top3
- top3(msg)
- elsif msg.bot_command == :rank
- rank(msg)
- elsif !msg.private? and !msg.bot_command
- count(msg)
- end
- end
- end
- #todo:
- #Add year tracking
- #add anual top3
- #Add .rank command so we can see what rank other people have(done)
Advertisement
Add Comment
Please, Sign In to add comment