Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Return to Castle Wolfenstein server status

by the_slycer (Chaplain)
on Jan 22, 2002 at 21:55 UTC ( #140695=CUFP: print w/replies, xml ) Need Help??

Query a wolfenstein (or probably q3 as well) server, and get a list of connected players, their ping, and their score.

The below link works best using IE, not sure how to make mozilla/netscape render a background the same way.

See an example at YFB Clan Server Status
#!/usr/bin/perl use warnings; use strict; use IO::Socket::INET; use HTML::Template; my $template = HTML::Template->new(filename => "html/rtcw_status.html" +); my @loop_data; #an array of hashes for each player row my $host = ""; #host that wolf is running on my $port = "27960" #port my $query = "\377\377\377\377getstatus\n"; #command string my $sock = IO::Socket::INET->new( #make the connection PeerAddr => $host, PeerPort => $port, Proto => "udp" ) || die "Could not connect to $host: $!"; #or tell us why not my $response; #this is what we will get back from the server send ($sock,$query,0) || die "Could not send: $!"; #send the query recv ($sock,$response,2048,0); #read the response my ($mapname) = $response =~ /.+\\mapname\\([^\\]+)/; #get the mapname my ($maxclients) = $response =~ /.+\\sv_maxclients\\([^\\]+)/; #and the max amount + of client connections my @status = split /\n/,$response; #now, split the response shift @status; #drop the first two lines (first line is getstatusre +sponse) shift @status; #(second line is the "header" info my $connected = "0";#default connected = 0 $connected = length(@status) if $status[0]; #if there is someone co +nnected, count how many foreach (@status) { my ($score,$ping,$name) = split; #split the score line into app +ropriate vars $name =~ s/^"|"$//g; #remove the quotes $name = parse_name($name); my %row = ( PLAYER => $name, SCORE => $score, PING => $ping ); +#create a hash for template loop push @loop_data,\%row; #push the hash into the template loop ar +ray } #fill the template $template->param( MAPNAME => $mapname, IMAGENAME => $mapname . ".jpg", MAXCONNECTS => $maxclients, CONNECTED => $connected, PLAYER_SCORE => \@loop_data, ); print "Content-type: text/html\n\n"; print $template->output; sub parse_name { #fun names -> HTML colors my $name = shift; $name =~ s!\^0([^^]+)!<font color="#000000">$1</font>!g; $name =~ s!\^1([^^]+)!<font color="#FF0000">$1</font>!g; $name =~ s!\^2([^^]+)!<font color="#00FF00">$1</font>!g; $name =~ s!\^3([^^]+)!<font color="#FFFF00">$1</font>!g; $name =~ s!\^4([^^]+)!<font color="#00FFFF">$1</font>!g; $name =~ s!\^5([^^]+)!<font color="#FF00FF">$1</font>!g; $name =~ s!\^6([^^]+)!<font color="#FF0000">$1</font>!g; $name =~ s!\^7([^^]+)!<font color="#FFFFFF">$1</font>!g; return ($name); }

Replies are listed 'Best First'.
Re: Return to Castle Wolfenstein server status
by Anonymous Monk on Jan 23, 2002 at 06:19 UTC
    I know that the topic is cool uses for Perl, but I thought I'd mention QStat anyway. You can find it at It does quite a bit of good stuff, and combined with some output formatting (most of which you seem to already have), then you'd wind up with a really nice set of tools.

    If you are just trying out new things in Perl for personal edification or some such, then please disregard my response.


      heh, this is just the start! I've already built yet another parser, with output to a mysql database, and am just finishing the web-front end to it. :) qstat is nice, I use it for other things, the idea of the above is to output to a webpage though.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://140695]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2020-06-03 10:38 GMT
Find Nodes?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?

    Results (22 votes). Check out past polls.