chucknorris: typo
[rbot] / 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   def name
30     "chucknorris"
31   end
32
33   # Just a little helper for the initialize method...
34   def find_facts_file(name)
35     full_path = File.join Config::datadir, "plugins", name
36     found_files = Dir[full_path]
37     if found_files.empty?
38       nil
39     else
40       found_files[0]
41     end
42   end
43
44   # HELP!
45   def help(plugin, topic="chuck")
46     "chuck|norris|chucknorris [min_rating] => show a random Chuck Norris fact (optional minimum rating from 1-10, default=6.0)."
47     #\"fact [person]\" shows a fact about someone in the channel.
48   end
49
50   # The meat.
51   def fact(m, params)
52     min = params[:minrating].to_f
53     debug "+ Getting Chuck Norris fact (rating > #{min})..."
54
55     viable_facts = @@facts.select {|rating, fact| rating >= min}
56     if viable_facts.empty?
57       debug "  - no facts found with rating >= #{min}"
58       m.reply "Are you nuts?!? There are no facts better than #{min}!!!"
59       return
60     end
61
62     rating, fact = viable_facts[rand(viable_facts.length)]
63     m.reply "#{fact} [score=#{rating}]"
64   end
65
66 end
67
68 plugin = ChuckNorrisPlugin.new
69
70 # plugin.map 'fact :minrating', :action => 'fact', :defaults => {:minrating=>MIN_RATING}
71 plugin.map 'chucknorris :minrating', :action => 'fact', :defaults => {:minrating=>MIN_RATING}
72 plugin.map 'chuck :minrating', :action => 'fact', :defaults => {:minrating=>MIN_RATING}
73 plugin.map 'norris :minrating', :action => 'fact', :defaults => {:minrating=>MIN_RATING}
74