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