]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/wow.rb
e1c7a4a4ee20ab6bab338272f52a91c5abb2f8c4
[user/henk/code/ruby/rbot.git] / 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.elements["r[@n=\"#{realm_name}\"]"]
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           else
101             "realm #{realm_name} not found."
102           end
103         rescue => err
104           "error retrieving realm status: #{err}"
105         end
106     end
107     def realm(m,params)
108       if params[:realm_name] and params[:realm_name].any?
109         realm_name = params[:realm_name].collect{|tok|
110           tok.capitalize
111         }.join(' ')
112         @registry[m.sourcenick] = realm_name
113         m.reply get_realm_status(realm_name)
114       else
115         if @registry.has_key?(m.sourcenick)
116           realm_name = @registry[m.sourcenick]
117           m.reply get_realm_status(realm_name)
118         else
119           m.reply "I don't know which realm you want.\n#{USAGE}"
120         end
121       end
122     end
123 end
124 plugin = RealmPlugin.new
125 plugin.map 'realm *realm_name',
126   :defaults => {:realm_name => false}, :thread => true