]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/hl2.rb
Plugin header boilerplating.
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / hl2.rb
1 #-- vim:sw=2:et\r
2 #++\r
3 #\r
4 # :title: Half-Life 2 plugin for rbot\r
5 #\r
6 # Author:: Ole Christian Rynning <oc@rynning.no>\r
7 # Copyright:: (C) 2006 Ole Christian Rynning\r
8 # License:: GPL v2\r
9 #\r
10 # Simple Half-Life 2 (Source Engine) plugin to query online\r
11 # servers to see if its online and kicking and how many users.\r
12 #\r
13 # Added 2 seconds timeout to the response. And sockets are now\r
14 # closing properly.\r
15 \r
16 require 'socket'\r
17 require 'timeout'\r
18 \r
19 class HL2Plugin < Plugin\r
20 \r
21   A2S_INFO = "\xFF\xFF\xFF\xFF\x54\x53\x6F\x75\x72\x63\x65\x20\x45\x6E\x67\x69\x6E\x65\x20\x51\x75\x65\x72\x79\x00"\r
22 \r
23   TIMEOUT = 2\r
24 \r
25   def a2s_info(addr, port)\r
26     socket = UDPSocket.new()\r
27     socket.send(A2S_INFO, 0, addr, port.to_i)\r
28     response = nil\r
29 \r
30     begin\r
31       timeout(TIMEOUT) do\r
32         response = socket.recvfrom(1400,0)\r
33       end\r
34     rescue Exception\r
35     end\r
36 \r
37     socket.close()\r
38     response ? response.first.unpack("iACZ*Z*Z*Z*sCCCaaCCZ*") : nil\r
39   end\r
40 \r
41   def help(plugin, topic="")\r
42     "hl2 'server:port' => show basic information about the given server"\r
43   end\r
44 \r
45   def hl2(m, params)\r
46     addr, port = params[:conn_str].split(':')\r
47     Thread.start do\r
48       info = a2s_info(addr, port)\r
49       if info != nil\r
50         m.reply "#{info[3]} is online with #{info[8]}/#{info[9]} players."\r
51       else\r
52         m.reply "Couldn't connect to #{params[:conn_str]}"\r
53       end\r
54     end\r
55   end\r
56 \r
57 end\r
58 \r
59 plugin = HL2Plugin.new\r
60 plugin.map 'hl2 :conn_str'\r
61 \r