#!/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 = "1.1.1.1"; #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 getstatusresponse)
shift @status; #(second line is the "header" info
my $connected = "0";#default connected = 0
$connected = length(@status) if $status[0]; #if there is someone connected, count how many
foreach (@status) {
my ($score,$ping,$name) = split; #split the score line into appropriate 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 array
}
#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([^^]+)!$1!g;
$name =~ s!\^1([^^]+)!$1!g;
$name =~ s!\^2([^^]+)!$1!g;
$name =~ s!\^3([^^]+)!$1!g;
$name =~ s!\^4([^^]+)!$1!g;
$name =~ s!\^5([^^]+)!$1!g;
$name =~ s!\^6([^^]+)!$1!g;
$name =~ s!\^7([^^]+)!$1!g;
return ($name);
}