search: show long definitions with 'google define:stuff'
[rbot] / data / rbot / plugins / wow.rb
1 #-- vim:sw=2:et
2 #++
3 #
4 # :title: World of Warcraft Realm Status plugin for rbot
5 #
6 # Author:: MrChucho (mrchucho@mrchucho.net)
7 # Copyright:: (C) 2006 Ralph M. Churchill
8 #
9 # Requires:: insatiable appetite for World of Warcraft
10
11 require 'rexml/document'
12
13 class Realm
14     attr_accessor :name,:status,:type,:pop
15     def initialize(name,status,type,pop)
16         self.name = pretty_realm(name)
17         self.status = pretty_status(status)
18         self.type = pretty_type(type)
19         self.pop = pretty_pop(pop)
20     end
21     def to_s
22         "#{name} (#{type}) Status: #{status} Population: #{pop}"
23     end
24     # just a longer, tabluar format
25     # might be good if displaying multiple realms
26     def _to_s
27         sprintf("%-8s %-20s %-8s %-9s\n%-11s %-22s %-8s %-9s",
28             "Status","Realm","Type","Population",
29             status,name,type,pop)
30     end
31 private
32     def pretty_status(status)
33         case status
34         when 1
35             "\ 33Up\ 3"
36         when 2
37             "\ 35Down\ 3"
38         end
39     end
40     def pretty_pop(pop)
41         case pop
42         when 1
43             "\ 33Low\ 3"
44         when 2
45             "\ 37Medium\ 3"
46         when 3
47             "\ 34High\ 3"
48         when 4
49             "\ 35Max(Queued)\ 3"
50         end
51     end
52     def pretty_realm(realm)
53         "\ 2#{realm}\ 2"
54     end
55     def pretty_type(type)
56         case type
57         when 0
58             'RP-PVP'
59         when 1
60             'Normal'
61         when 2
62             'PVP'
63         when 3
64             'RP'
65         end
66     end
67 end
68
69 class RealmPlugin < Plugin
70     USAGE="realm <realm> => determine the status of a Warcraft realm"
71     def initialize
72         super
73         class << @registry
74             def store(val)
75                 val
76             end
77             def restore(val)
78                 val
79             end
80         end
81     end
82     def help(plugin,topic="")
83         USAGE
84     end
85     def usage(m,params={})
86         m.reply USAGE
87     end
88     def get_realm_status(realm_name)
89         begin
90           xmldoc = @bot.httputil.get("http://www.worldofwarcraft.com/realmstatus/status.xml", :cache => false)
91           raise "unable to retrieve realm status" unless xmldoc
92           realm_list = (REXML::Document.new xmldoc).root
93           realm_data = realm_list.get_elements("//r[@n=\"#{realm_name}\"]").first
94           if realm_data and realm_data.attributes.any? then
95             realm = Realm.new(
96               realm_data.attributes['n'],
97               realm_data.attributes['s'].to_i,
98               realm_data.attributes['t'].to_i,
99               realm_data.attributes['l'].to_i)
100             realm.to_s
101           else
102             "realm #{realm_name} not found."
103           end
104         rescue => err
105           "error retrieving realm status: #{err}"
106         end
107     end
108     def realm(m,params)
109       if params[:realm_name] and params[:realm_name].any?
110         realm_name = params[:realm_name].collect{|tok|
111           tok.capitalize
112         }.join(' ')
113         @registry[m.sourcenick] = realm_name
114         m.reply get_realm_status(realm_name)
115       else
116         if @registry.has_key?(m.sourcenick)
117           realm_name = @registry[m.sourcenick]
118           m.reply get_realm_status(realm_name)
119         else
120           m.reply "I don't know which realm you want.\n#{USAGE}"
121         end
122       end
123     end
124 end
125 plugin = RealmPlugin.new
126 plugin.map 'realm *realm_name',
127   :defaults => {:realm_name => false}, :thread => true