Skip to content

Instantly share code, notes, and snippets.

@mmasaki
Created January 27, 2011 06:20
Show Gist options
  • Save mmasaki/798152 to your computer and use it in GitHub Desktop.
Save mmasaki/798152 to your computer and use it in GitHub Desktop.
TC:Eのサーバの稼働状況を発言するbot
#encoding: utf-8
require 'nokogiri'
require 'net/irc'
Process.daemon
BOTNICK = "Glass_bot"
trap(:INT){
exit
}
class TCEBot < Net::IRC::Client
def initialize(*args)
super
@active = []
@yabashi = ""
@nonpb = ""
Thread.start do
loop do
active
sleep 300
end
end
end
def on_rpl_welcome(m)
["#pasratce","#NonPB_OBJ","#stepup","#pasrasan"].each do |c|
post JOIN, c
end
end
def on_ping(m)
post PONG, BOTNICK
end
def active
puts "active"
active_hash = []
Nokogiri(`qstat -xml -u -q3m,84 etmaster.idsoftware.com:27950`).search("server").each do |data|
if data[:type] == "Q3S" && data.at("gametype").inner_text == "tcetest"
case data.at("name").inner_text
when /yabasi/io
@yabashi = data.at("hostname").inner_text
when /nonpb/io
@nonpb = data.at("hostname").inner_text
end
if data.at("numplayers").inner_text.to_i.nonzero?
active_hash << {
:name => data.at("name").inner_text,
:addr => data.at("hostname").inner_text,
:map => data.at("map").inner_text,
:users => data.at("numplayers").inner_text,
:max => data.at("maxplayers").inner_text
}
end
end
end
@active = active_hash.sort{|b,a| a[:users] <=> b[:users]}.map do |server|
"#{server[:name]} /connect #{server[:addr]} @ #{server[:map]} (#{server[:users]}/#{server[:max]}人)".encode!("iso-2022-jp")
end
@active = ["TC is dead."] if @active.empty?
puts "active completed."
return @active
end
def srv(name,m,ip)
data = Nokogiri(`qstat -xml -u -q3s #{ip}`)
if data.at("server")
post NOTICE, m[0],"#{data.at("name").inner_text} /connect #{ip} @#{data.at("map").inner_text} (#{data.at("numplayers").inner_text}/#{data.at("maxplayers").inner_text}人)".encode!("iso-2022-jp")
else
post NOTICE, m[0],"鯖落ち: #{name} ( #{ip} )".encode!("iso-2022-jp")
end
end
def on_privmsg(m)
begin
m[1].encode!("utf-8","iso-2022-jp")
case m[1]
when "tce"
srv("CTF for All People",m,"113.151.8.227:27960")
srv("CTF for All People 2 [sv_fps 40, PB]",m,"113.151.8.227:27962")
if m[0] != "#NonPB_OBJ"
srv("NonPB[official]OBJ",m,"202.231.146.178:27961")
srv("NonPB[official]BC",m,"202.231.146.178:27962")
end
srv("AsunoSekaiwoMegassamoriageru [DEVELOP]TCE_Custom 0.49b! fixed",m,"113.151.8.227:27961")
when "ctf","CTF"
srv("CTF for All People",m,"113.151.8.227:27960")
when "ctf-pb","CTF-PB"
srv("CTF for All People 2 [sv_fps 40, PB]",m,"113.151.8.227:27962")
when /^non(pb)?$/io
puts "\nnonpb: #{@nonpb}"
if m[0] != "#NonPB_OBJ" && [email protected]?
srv("NonPB[official]OBJ",m,"#{@nonpb}")
bc = @nonpb
bc[-1] = "2"
srv("NonPB[official]BC",m,"#{@nonpb}")
end
when /^asuno[a-z]*$/io
srv("AsunoSekaiwoMegassamoriageru [DEVELOP]TCE_Custom 0.49b! fixed",m,"113.151.8.227:27961")
when "active"
@active.each do |i|
post NOTICE, m[0],i
sleep 0.5
end
when "active\.new"
post NOTICE, m[0],"しばらくお待ちください".encode!("iso-2022-jp")
active.each do |i|
post NOTICE, m[0],i
sleep 0.5
end
when /^びゃっこ鯖$/o
srv("びゃっこ鯖",m,"59.143.50.237:27960")
when /^まっち2$/o
srv("まっち2",m,"58.190.88.141:27960")
when "やばし鯖"
t = @yabashi.dup
t[-1] = '2'
puts "\nyabashi: #{t}"
srv("やばし鯖 5on5",m,"#{t}") unless @yabashi.empty?
when "sheen"
post NOTICE, m[0],"http://sheen.esel-project.net/add/"
when "privs"
post NOTICE, m[0],"http://tc.ajunk.org/add"
end
rescue => ex
puts ex.message
end
end
end
TCEBot.new("irc.friend-chat.jp", "6667", { :nick => BOTNICK,:user => BOTNICK,:real => BOTNICK}).start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment