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