]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/chucknorris.rb
Fixed chucknorris.rb debug output
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / chucknorris.rb
1 require 'yaml'
2 require 'zlib'
3
4 MIN_RATING = 6.0
5 MIN_VOTES = 25
6
7 # the plugin
8 class ChuckNorrisPlugin < Plugin
9
10   # Loadez les factes
11   def initialize
12     if path = find_facts_file('chucknorris.yml.gz')
13       fyml = Zlib::GzipReader.open(path)
14     elsif path = find_facts_File('chucknorris.yml')
15       fyml = open(path)
16     else
17       raise "Error: Couldn't find chucknorris.yml[.gz]"
18     end
19     
20     debug "+ [chucknorris] Loading #{path}..."
21     
22     @@facts = YAML.load(fyml).map{|fact,(score,votes)| votes >= MIN_VOTES ? [score,fact] : nil}.compact
23     debug "+ [chucknorris] #{@@facts.length} Chuck Norris facts loaded..."
24     debug "  Random fact: #{@@facts[rand(@@facts.size)].inspect}"
25     
26     super
27   end
28   
29   # Just a little helper for the initialize method...
30   def find_facts_file(name)
31     full_path = File.join Config::datadir, "plugins", name
32     found_files = Dir[full_path]
33     if found_files.empty?
34       nil
35     else
36       found_files[0]
37     end
38   end
39   
40   # HELP!
41   def help(plugin, topic="chuck")
42     "fact|chuck|norris|chucknorris [min_rating] => \"fact\" shows a random Chuck Norris fact (optional minimum rating from 1-10, default=6.0)."
43     #\"fact [person]\" shows a fact about someone in the channel. 
44   end
45
46   # The meat.
47   def fact(m, params)
48     min = params[:minrating].to_f
49     debug "+ Getting Chuck Norris fact (rating > #{min})..."
50
51     viable_facts = @@facts.select {|rating, fact| rating >= min}
52     if viable_facts.empty?
53       debug "  - no facts found with rating >= #{min}"
54       m.reply "Are you nuts?!? There are no facts better than #{min}!!!"
55       return
56     end
57
58     rating, fact = viable_facts[rand(viable_facts.length)]
59     m.reply "#{fact} [score=#{rating}]"
60   end
61
62 end
63
64 plugin = ChuckNorrisPlugin.new
65
66 plugin.map 'fact :minrating', :action => 'fact', :defaults => {:minrating=>MIN_RATING}
67 plugin.map 'chucknorris :minrating', :action => 'fact', :defaults => {:minrating=>MIN_RATING}
68 plugin.map 'chuck :minrating', :action => 'fact', :defaults => {:minrating=>MIN_RATING}
69 plugin.map 'norris :minrating', :action => 'fact', :defaults => {:minrating=>MIN_RATING}
70