X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=data%2Frbot%2Fplugins%2Fchucknorris.rb;h=42385ff303a2b0a109835c5c7f2f002a05abf596;hb=0ddf77c9d54236bbc75c2b603871fc0c9e072873;hp=07421fdafa93d61c5928fbad6d26ac843ff23cc6;hpb=ea52592ea75e50c3c094fab87b02a462d4437575;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/data/rbot/plugins/chucknorris.rb b/data/rbot/plugins/chucknorris.rb index 07421fda..42385ff3 100644 --- a/data/rbot/plugins/chucknorris.rb +++ b/data/rbot/plugins/chucknorris.rb @@ -1,151 +1,74 @@ -require 'uri/common' -require 'cgi' - -# the 4q.cc "id => full name" mapping -FACTMAP = { "mrt" => "Mr\. T", - "vin" => "Vin Diesel", - "chuck" => "Chuck Norris" } +require 'yaml' +require 'zlib' MIN_RATING = 6.0 - -PISSED_EXPRESSIONS = [ - "fuck this, i'm going to go get toed up.", - "screw this, i'm going to get hammered.", - "forget this, i'm going to iron some shirts.", - "disregard this, i'm going out to kill me some prostitutes.", -] - -# exceptions -class HTTPError < Exception; end -class ParseError < Exception; end +MIN_VOTES = 25 # the plugin class ChuckNorrisPlugin < Plugin - def help(plugin, topic="") - "fact [person] => \"fact\" shows a random Chuck Norris, Vin Diesel, or Mr. T fact. \"fact [person]\" shows a fact about someone in the channel. || chucknorris, chuck, norris => random Chuck Norris fact || vindiesel, vin, diesel => random Vin Diesel fact || mrt => I pity the foo who can't figure this one out." + # Loadez les factes + def initialize + if path = find_facts_file('chucknorris.yml.gz') + fyml = Zlib::GzipReader.open(path).read + elsif path = find_facts_file('chucknorris.yml') + fyml = open(path).read + else + raise "Error: Couldn't find chucknorris.yml[.gz]" + end + + debug "+ [chucknorris] Loading #{path}..." + + @@facts = YAML.load(fyml).map{|fact,(score,votes)| votes >= MIN_VOTES ? [score,fact] : nil}.compact + debug "+ [chucknorris] #{@@facts.length} Chuck Norris facts loaded..." + debug " Random fact: #{@@facts[rand(@@facts.size)].inspect}" + + super end - def getfact(who) - raise "Unknown name: #{who}" unless FACTMAP.keys.include? who - # get the fact - factdata = @bot.httputil.get(URI.parse("http://www.4q.cc/index.php?pid=fact&person=#{who}")) - unless factdata - raise HTTPError - end - - longwho = FACTMAP[who] - - # regexes - fact_matcher = %r{

And now a random fact about #{longwho}...

(.+?)
} - rating_matcher = %r{Current Rating: (\d+\.\d+)} - - # parse the fact - if factdata =~ fact_matcher - fact = CGI::unescapeHTML($1) - if factdata =~ rating_matcher - rating = $1.to_f - puts "fact=[#{fact}], rating=[#{rating}]" - return [fact, rating] - end - end - - raise ParseError - + def name + "chucknorris" end - def fact(m, params) - who = params[:who] - max_tries = (params[:tries] or "10").to_i - - valid_people = FACTMAP.keys + ["random"] - - # if the person wants a fact about themselves, then it'll substitute the name. - if valid_people.include? who - substitute_name = nil + # Just a little helper for the initialize method... + def find_facts_file(name) + full_path = File.join Config::datadir, "plugins", name + found_files = Dir[full_path] + if found_files.empty? + nil else - substitute_name = who - who = 'random' - end - - # pick a random person - if who == 'random' - if substitute_name - # take out the Mr. T facts if you're inserting someone's name - # beacuse tons of them suck, and most of them revolve around - # "pitying" someone or something. - people = FACTMAP.keys - ["mrt"] - who = people[rand(people.length)] - else - who = FACTMAP.keys[rand(FACTMAP.length)] - end - end - - # get the long name - longwho = FACTMAP[who] - unless longwho - m.reply "Who the crap is #{who}?!?!" - return - end - - # get the fact - - m.reply "alright, let's see if I can find a good one..." - - tries = 0 - results = [] - loop do - - begin - - puts "[chucknorris] Try number #{tries}/#{max_tries}..." - - tries += 1 - fact, rating = getfact(who) - - if rating >= MIN_RATING - fact.gsub!(longwho, substitute_name) if substitute_name - m.reply "#{results.join(', ') + "... "}hrm, this one's not bad:" - m.reply "#{fact} [rating: #{rating}]" - return - else - results << "lame" - end - - if tries > max_tries - m.reply "#{results.join(', ')}... these all suck. #{PISSED_EXPRESSIONS[rand(PISSED_EXPRESSIONS.length)]}" - return - end - - rescue HTTPError - #m.reply "This #{longwho} fact punched my teeth in. (HTTP error)" - results << "DOH!" - tries += 1 - rescue ParseError - #m.reply "This #{longwho} fact made my brain explode. (Parse error)" - results << "wtf?" - tries += 1 - end - + found_files[0] end - end + # HELP! + def help(plugin, topic="chuck") + "chuck|norris|chucknorris [min_rating] => show a random Chuck Norris fact (optional minimum rating from 1-10, default=6.0)." + #\"fact [person]\" shows a fact about someone in the channel. + end -end - -plugin = ChuckNorrisPlugin.new + # The meat. + def fact(m, params) + min = params[:minrating].to_f + debug "+ Getting Chuck Norris fact (rating > #{min})..." + + viable_facts = @@facts.select {|rating, fact| rating >= min} + if viable_facts.empty? + debug " - no facts found with rating >= #{min}" + m.reply "Are you nuts?!? There are no facts better than #{min}!!!" + return + end -plugin.map 'fact :who :tries', :action => 'fact', - :defaults => {:who => 'random', :tries=>10} + rating, fact = viable_facts[rand(viable_facts.length)] + m.reply "#{fact} [score=#{rating}]" + end -plugin.map 'chucknorris :who', :action => 'fact', :defaults => {:who => "chuck"} -plugin.map 'chuck :who', :action => 'fact', :defaults => {:who => "chuck"} -plugin.map 'norris :who', :action => 'fact', :defaults => {:who => "chuck"} +end -plugin.map 'vindiesel :who', :action => 'fact', :defaults => {:who => "vin"} -plugin.map 'diesel :who', :action => 'fact', :defaults => {:who => "vin"} -plugin.map 'vin :who', :action => 'fact', :defaults => {:who => "vin"} +plugin = ChuckNorrisPlugin.new -plugin.map 'mrt :who', :action => 'fact', :defaults => {:who => "mrt"} +# plugin.map 'fact :minrating', :action => 'fact', :defaults => {:minrating=>MIN_RATING} +plugin.map 'chucknorris :minrating', :action => 'fact', :defaults => {:minrating=>MIN_RATING} +plugin.map 'chuck :minrating', :action => 'fact', :defaults => {:minrating=>MIN_RATING} +plugin.map 'norris :minrating', :action => 'fact', :defaults => {:minrating=>MIN_RATING}