X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=data%2Frbot%2Fplugins%2Fchucknorris.rb;h=bf9d70f46827816a7f57941c1d9d365c2c85a8d6;hb=6b8cca66628db3634296e5cb7e065f84555cf7cd;hp=b929b6adb1da9fb9e9cf44e8065c050fc5eb980c;hpb=22b2382a8a4b817b3b8b0d7812bfddafe65b73b4;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git
diff --git a/data/rbot/plugins/chucknorris.rb b/data/rbot/plugins/chucknorris.rb
index b929b6ad..bf9d70f4 100644
--- a/data/rbot/plugins/chucknorris.rb
+++ b/data/rbot/plugins/chucknorris.rb
@@ -1,50 +1,74 @@
-require 'uri/common'
-require 'cgi'
-
-FACTMAP = { "mrt" => "Mr\. T",
- "vin" => "Vin Diesel",
- "chuck" => "Chuck Norris" }
+require 'yaml'
+require 'zlib'
+MIN_RATING = 6.0
+MIN_VOTES = 25
+
+# the plugin
class ChuckNorrisPlugin < Plugin
- def help(plugin, topic="")
- "getfact => show a random Chuck Norris or Vin Diesel or Mr. T fact || chucknorris => show a random Chuck Norris quote || vindiesel => show a random Vin Diesel quote || mrt => I pity the foo who can't figure this one out."
- end
-
- def getfact(m, params)
- who = params[:who]
- m.reply "Errorn!!!" unless who
-
- if who == 'random'
- who = FACTMAP.keys[rand(FACTMAP.length)]
- end
-
- longwho = FACTMAP[who]
- unless longwho
- m.reply "Who the crap is #{who}?!?!"
- return
- end
-
- matcher = %r{
And now a random fact about #{longwho}...
(.+?)
}
-
- factdata = @bot.httputil.get(URI.parse("http://www.4q.cc/index.php?pid=fact&person=#{who}"))
- unless factdata
- m.reply "This #{longwho} fact punched my teeth in. (HTTP error)"
+ # Loadez les factes
+ def initialize
+ if path = find_facts_file('chucknorris.yml.gz')
+ fyml = Zlib::GzipReader.open(path)
+ elsif path = find_facts_File('chucknorris.yml')
+ fyml = open(path)
+ 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
- if factdata =~ matcher
- m.reply(CGI::unescapeHTML($1))
+ def name
+ "chucknorris"
+ end
+
+ # 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
- m.reply "This #{longwho} fact made my brain explode. (Parse error)"
+ 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
+
+ # 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
+
+ rating, fact = viable_facts[rand(viable_facts.length)]
+ m.reply "#{fact} [score=#{rating}]"
end
end
-plugin = ChuckNorrisPlugin.new
-plugin.map 'getfact :who', :action => 'getfact',
- :defaults => {:who => 'random'}
-plugin.map 'chucknorris :who', :action => 'getfact', :defaults => {:who => "chuck"}
-plugin.map 'mrt :who', :action => 'getfact', :defaults => {:who => "mrt"}
-plugin.map 'vindiesel :who', :action => 'getfact', :defaults => {:who => "vin"}
+plugin = ChuckNorrisPlugin.new
+
+# 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}
+