#!/usr/bin/perl 

#    ETLive.pl 1.21: Shows whos playing on your et server etc...
#    Copyright (C) 2003,2004,2005 Chris Bajumpaa
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

# This program works similar to wolflive.asp
# Since wolflive is the inspration/basis for this script, credit is due here.  
# ''WolfLive code by Lt.Christy @ http://www.force137.com/wolflive.asp''
# christy@force137.com

# If you want to change colors, bgcolors, fonts, etc..., look in the themes directory.
# read thru the options below


############################
#Adjust below for your server
############################

# Theme See your theme directory...
my $theme = "etlive";
#my $theme = "rust";

# Location of plain vanilla qstat
my $qstat = "/usr/bin/qstat";

# Server Address ip/hostname:port
my $serveraddr = "games.or8.net:27960";
#my $serveraddr = "202.12.147.106:27960";

#Path to title picture
my $titlepicture = "q3titletr.gif";

#Title Picture Position
my $titlepictureposition = "left";

#Our Url
my $siteurl = "http://games.or8.net";

#Link Name
my $sitename = "games.or8.net";

#Path to map folder
my $mapimages = "maps/";     		

# Various Messages

# Msg for when noone is playing.
my $nobodyhome = "NOBODY IS PLAYING RIGHT NOW"; 

# Intermission break out the llamas!!!
# This comes up between maps sometimes.
my $intermission = "INTERMISSION!!!!";

# Stats URL
my $statsurl = "http://systats.or8.net/php/player.php?name=";
#my $statsurl = "";

# Admin/Reserved Slots
# This will hide $reservedslots number of slots from public view
# Just change this to 0 if you don't want them to be hidden...
my $reservedslots = 0;

#########################################################
#	No need to edit anything below this line	#
#########################################################

use CGI qw(:standard -no_xhtml);
use CGI::Carp qw/fatalsToBrowser/;
use XML::Simple;
use IO::Pipe;

# qstat options...
# Don't change these..
my $qstatopts = "-hpn -xml -P -R -rws";

# Buildling of our command line;
my $prog = "$qstat $qstatopts $serveraddr";

# Pipe in...
my $pipe = new IO::Pipe;
$pipe->reader($prog);

# Create the hash for our dataset
my $live = XMLin($pipe,KeyAttr => ['player','rule'], ForceArray => ['player','rule']); 

# Theme...
$theme = "themes/" . $theme . ".xml";
my $themexml = XMLin($theme);

# ...
my $alliexptotal = 0;
my $alliepingtotal = 0;
my $alliemembers = 0;
my $axisxptotal = 0;
my $axispingtotal = 0;
my $axismembers = 0;
my $specmembers = 0;
my $connmembers = 0;

# Pretty Print our dates
sub now {
	my $WeekDay;
	my $DayOfYear; 
	my $IsDST;

	($Second, $Minute, $Hour, $Day, $Month, $Year, $WeekDay, $DayOfYear, $IsDST) = localtime(time);
	my $RealMonth = $Month + 1;

	if($RealMonth < 10) {
		$RealMonth = "0" . $RealMonth; # add a leading zero to one-digit months
	}

	if($Day < 10) {
		$Day = "0" . $Day; # add a leading zero to one-digit days
	}

	if($Hour < 10) {
		$Hour = "0" . $Hour; 
	}
	if($Minute < 10) {
		$Minute = "0" . $Minute; 
	}
	if($Second < 10) {
		$Second = "0" . $Second; 
	}

	$Fixed_Year = $Year + 1900;
	
	$date = join(".",$Fixed_Year,$RealMonth,$Day); 
	$time = join(":",$Hour,$Minute,$Second);
	$now = join(" ",$date,$time);
	return $now;

}

# Return and <IMG> tag refering to the correct map image 
# for the current map.
sub map_pic {

        my $map = $mapimages . $live->{server}->{map} . ".gif";

        unless(-e $map) {$map = $mapimages . "unknownmap.gif";}

        return " <img src=\"$map\" width=\"256\" height=\"256\" alt=\"mappic\" />";

}

# Print out our list of rule names

sub print_rules {

	$tmpstr = "";
	foreach $x (@{$live->{server}->{rules}->{rule}}) {

		$tmpstr = join("",$tmpstr,
				"<tr class=rules>",
				"<td nowrap>",
				"$x->{name}",
				"</td>",
				"<td nowrap>",
				"$x->{content}",
				"</td>",
				"</tr>"
				);

	}

	return $tmpstr;
}

sub print_players {

	my @allie;
	my @axis;
	my @teamindex;
	
	my $tmpstr = "";

	my $axisheader = 	TR(
				{
				-CLASS=>"playersheader"
				},

				td(
					{
					-ALIGN=>"left",
					},

					"<div class=axis>AXIS</div>"
				),

				td(
					{
					-ALIGN=>"right",
					},

					"XP"
				),

				td(

					{
					-ALIGN=>"right",
					},
					"Ping"

				),

			);

	my $allieheader = 	TR(
				{
				-CLASS=>"playersheader"
				},

				td(
					{
					-ALIGN=>"left",
					},

					"<div class=allies>ALLIES</div>"
				),

				td(
					{
					-ALIGN=>"right",
					},

					"XP"
				),

				td(

					{
					-ALIGN=>"right",
					},
					"Ping"

				),

			);

	my $specheader = 	TR(
				{
				-CLASS=>"playersheader"
				},

				td(
					{
					-ALIGN=>"left",
					},

					"<div class=spectator>SPECTATORS</div>"
				),

				td(
					{
					-ALIGN=>"right",
					},

					"XP"
				),

				td(

					{
					-ALIGN=>"right",
					},
					"Ping"

				),

			);

	my $connheader = 	TR(
				{
				-CLASS=>"playersheader"
				},

				td(
					{
					-ALIGN=>"left",
					},

					"<div class=spectator>CONNECTING</div>"
				),

				td(
					{
					-ALIGN=>"right",
					},

					"XP"
				),

				td(

					{
					-ALIGN=>"right",
					},
					"Ping"

				),

			);

	my $footer =	TR(
				{
				-CLASS=>"playersfooter",
				},
	
				td(
					{
					-COLSPAN=>"3",
					-ALIGN=>"CENTER",
					},

						"Click on a players name to see their stats."	
				),
					
			);

	my $blank = 	TR(
				{
				-CLASS=>"playersempty",
				},
	
				td(
					{
					-COLSPAN=>"3",
					},
						"&nbsp;"	
				),
					
			); 


			


	if ($live->{server}->{rules}->{rule}->[0]->{name} eq "") { 

						return join("",
							"<tr class=players valign=top><td colspan=4>",
							"<div align=center class=intermission>$intermission",
							"</div>",
							"</td></tr>"
							);
						}

	if ($live->{server}->{numplayers} == 0) { 

						return join("",
							"<tr class=players valign=top><td colspan=4>",
							"<div align=center class=nobodyhome>$nobodyhome",
							"</div>",
							"</td></tr>"
							);
				
						}

	for ($x=0;$x<$live->{server}->{numplayers};$x++) {

		#fill the team index up with the default team which is spectator
		$teamindex[$x] = "<div class=spectator>SPECTATOR</div>";
		$specmembers = $live->{server}->{numplayers};

	}

	foreach $x (@{$live->{server}->{rules}->{rule}}) {

		if ($x->{name} eq "P") {

			$specmembers = 0;

			@members = split("",$x->{content});			

			$j = 0;

			foreach $i (@members) {

				#ETPro P style team data.
				#        TEAM_FREE	0
				#        TEAM_AXIS	1
				#        TEAM_ALLIES	2
				#        TEAM_SPECTATOR	3

				if ($i eq "3") {

					$teamindex[$j] = "<div class=spectator>SPECTATOR</div>";
					$j++;
					$specmembers++;

				}

				if ($i eq "2") {

					#das uber allies
					$teamindex[$j] = "<div class=allies>ALLIES</div>";
					$alliexptotal+= $live->{server}->{players}->{player}->[$j]->{score};
					$alliepingtotal+= $live->{server}->{players}->{player}->[$j]->{ping};
					$alliemembers++;
					$j++;
				}

				if ($i eq "1") {

					#das uber axis
					$teamindex[$j] = "<div class=axis>AXIS</div>";
					$axisxptotal+= $live->{server}->{players}->{player}->[$j]->{score};
					$axispingtotal+= $live->{server}->{players}->{player}->[$j]->{ping};
					$axismembers++;
					$j++;

				}

				if ($i eq "0") {

					#connecting
					$teamindex[$j] = "CONNECTING";
					$j++;
					$connmembers++;

				}

				if ($1 eq "-") {
					#empty slot.
				} 

			}


		}

		if ($x->{name} eq "Players_Allies") {


			@allie = split(/ /,$x->{content});			

			foreach $i (@allie) {

				if ($i eq "(None)") {} else {

					$specmembers--;

					#das uber allies
					$teamindex[$i-1] = "<div class=allies>ALLIES</div>";
					$alliexptotal+= $live->{server}->{players}->{player}->[$i]->{score};
					$alliepingtotal+= $live->{server}->{players}->{player}->[$i]->{ping};
					$alliemembers++;
				}

			}


		}


		if ($x->{name} eq "Players_Axis") {


			@axis = split(/ /,$x->{content});			
			
			foreach $i (@axis) {

				if ($i eq "(None)") {} else {

					$specmembers--;

					#das uber axis
					$teamindex[$i-1] = "<div class=axis>AXIS</div>";
					$axisxptotal+= $live->{server}->{players}->{player}->[$i]->{score};
					$axispingtotal+= $live->{server}->{players}->{player}->[$i]->{ping};
					$axismembers++;
				}

			}


		}

	}
		
	for ($i = 0; $i < $live->{server}->{numplayers}; $i++) {	

		$live->{server}->{players}->{player}->[$i]->{name} = 
			&convert_name($live->{server}->{players}->{player}->[$i]->{name});

		$playersping = $live->{server}->{players}->{player}->[$i]->{ping};
		
		$color = &graduatecolor($playersping);

		$playersping = join ("","<div style=\"color:$color;\">",
					"$playersping","</div>");

		$playersxp = $live->{server}->{players}->{player}->[$i]->{score};
		
		$extra = "";

		$color = &graduatecolor($playersxp);

		if ($playersxp > 1020) { $extra = "yes"; }

		if ($extra) {
			$playersxp = join ("",
				"<div style=\"color:$color;,font-weight:bold;\">","$playersxp",
				"</div>");
		} else {
			$playersxp = join ("",
				"<div style=\"color:$color;\">",
				"$playersxp","</div>");
		}

		$name =	&color_name($live->{server}->{players}->{player}->[$i]->{name});

		if ( $teamindex[$i] =~ m/ALLIES/ ) {

			$alliestr = join("",$alliestr,
				"<tr class=players valign=top>",
				"<td nowrap align=left>$name</td>",
				#"<td>$teamindex[$i]</td>",
				"<td nowrap align=right>$playersxp</td>",
				"<td nowrap align=right>$playersping</td>",
				"</tr>",
				);

		} elsif ( $teamindex[$i] =~ m/AXIS/ ) {

			$axisstr = join("",$axisstr,
				"<tr class=players valign=top>",
				"<td nowrap align=left>$name</td>",
				#"<td>$teamindex[$i]</td>",
				"<td nowrap align=right>$playersxp</td>",
				"<td nowrap align=right>$playersping</td>",
				"</tr>",
				);

		} elsif ( $teamindex[$i] =~ m/CONNECTING/ ) {
	
			$connstr = join("",$connstr,
				"<tr class=players valign=top>",
				"<td nowrap align=left>$name</td>",
				#"<td>$teamindex[$i]</td>",
				"<td nowrap align=right>$playersxp</td>",
				"<td nowrap align=right>$playersping</td>",
				"</tr>",
				);

		} elsif ( $teamindex[$i] =~ m/SPECTATOR/ ) {
			#spectators
			$specstr = join("",$specstr,
				"<tr class=players valign=top>",
				"<td nowrap align=left>$name</td>",
				#"<td>$teamindex[$i]</td>",
				"<td nowrap align=right>$playersxp</td>",
				"<td nowrap align=right>$playersping</td>",
				"</tr>",
				);
		}

	}

	if($alliemembers) { $allieline =  $allieheader . $alliestr . $footer . $blank } else {
		$allieline = "";
	}

	if($axismembers) { $axisline = $axisheader . $axisstr . $footer . $blank } else {
		$axisline = "";
	}

	if($connmembers) { $conline = $connheader . $connstr . $footer . $blank } else {
		$conline = "";
	}
	
	if($specmembers) { $specline = $specheader . $specstr . $footer . $blank } else {
		$specline = "";
	}

	return	$allieline . 
		$axisline .
		$specline . 
		$conline .
		$blank;

}

# Pretty Print the hostname
sub color_hostname ($) {

	my $foo; 
	my $rawname = $_[0];

	$foo = $rawname;

	foreach $x (@{$live->{server}->{rules}->{rule}}) {

		if ($x->{name} eq "sv_hostname") {

			$foo = $x->{content};
			$foo = &parse_color($foo);
			$x->{name} = "sv_hostname";
			$x->{content} = "$foo";
		}

	}

	
	return $foo;

}

# Pretty Print the player names
sub color_name ($) {

	my $foo; 
	my $rawname = $_[0];

	$foo = &parse_color($_[0]);

	$foo = join("","<a style=\"text-decoration: none\" href=\"$statsurl$rawname\">",$foo,"</a>");
	return $foo;

}

# Parse the color codes in hostsname, player names etc
# This could probably be better
# Beware this is a fucking mess....
# Attempt to sanizitize players names. (Prevent sneaky bastards from embedding there own html)

sub parse_color ($) {

	my $foo; 
	my $rawname = $_[0];
	my $counter=0;
	my @list;

	#eek	
	@list = split(/(\^[0-9a-zA-Z\!\@\#\$\&\*\(\)\_\-\=\+\|\\\/\\,\.\<\>\?\:\'\[\]\{\}\"\`\^])||([<>\/\\\#!-])/, $_[0]);

	foreach $i (@list) {

	 #==============================#
	 #  This list isn't 100% right	#
	 #     But it's good enough	#
	 #==============================#
	 # Basic Colors...		#
	 #==============================#
	 # command	# result	#
	 #==============#===============#
	 # ^0		# black(default)#
	 # ^1		# red		#
	 # ^2		# green		#
	 # ^3		# yellow	#
	 # ^4		# blue		#
	 # ^5		# light blue	#
	 # ^6		# pink		#
	 # ^7		# white		#
	 # ^8		# orange	#
	 # ^9		# grey		#
	 #==============================#
	 # Extended/OSP Colors...	#
	 #==============================#
	 # command	# result	#
	 #==============#===============#
	 # ^A or ^a	# #DF984A	#
	 # ^B or ^b	# 15,115,113	#
	 # ^C or ^c	# 78,20,79	#
	 # ^D or ^d	# 43,77,112	#
	 # ^E or ^e	# 117,11,197	#
	 # ^F or ^f	# 53,147,185	#
	 # ^G or ^g	# 181,206,186	#
	 # ^H or ^h	# 25,88,59	#
	 # ^I or ^i	# 138,39,59	#
	 # ^J or ^j	# 121,43,41	#
	 # ^K or ^k	# 142,54,8	#
	 # ^L or ^l	# 78,20,79	#
	 # ^M or ^m	# 107,110,67	#
	 # ^N or ^n	# 227,229,207	#
	 # ^O or ^o	# 78,20,79	#
	 # ^P or ^p	# 0,0,0		#
	 # ^Q or ^q	# 188,25,30	#
	 # ^R or ^r	# 78,20,79	#
	 # ^S or ^s	# 225,235,61	#
	 # ^T or ^t	# 37,32,132	#
	 # ^U or ^u	# 82,177,181	#
	 # ^V or ^v	# 191,43,191	#
	 # ^W or ^w	# 255,255,255	#
	 # ^X or ^x	# 217,137,38	#
	 # ^Y or ^y	# 116,119,124	#
	 # ^Z or ^z	# 185,189,192	#
	 #--------------#---------------#
	 # ^:		# 175,175,175	#
	 # ^\		# 28,100,26	#
	 # ^,		# 178,159,116	#
	 # ^.		# #FFFFFF	#
	 # ^?		# 84,15,10	#
	 # ^/		# 226,231,173	#
	 # ^+		# 130,59,17	#
	 # ^-		# 110,119,70	#
	 # ^#		# ?		#
	 # ^!		# ?		#
	 # many 	# more		#
	 #==============================#

		if ($i eq "^0") {

			$counter++;
			$i = "<span style=\"color:#000000;\">";
			

		}

		if ($i eq "^1") {

			$counter++;
			$i = "<span style=\"color:#FF0000;\">";
			

		}

		if ($i eq "^2") {

			$counter++;
			$i = "<span style=\"color:#00FF00;\">";
			

		}

		if ($i eq "^3") {

			$counter++;
			$i = "<span style=\"color:#FFFF00;\">";
			

		}

		if ($i eq "^4") {

			$counter++;
			$i = "<span style=\"color:#0000FF;\">";
			

		}


		if ($i eq "^5") {

			$counter++;
			$i = "<span style=\"color:#33CCFF;\">";
			

		}


		if ($i eq "^6") {

			$counter++;
			$i = "<span style=\"color:#FF66CC;\">";
			

		}

		if ($i eq "^7") {

			$counter++;
			$i = "<span style=\"color:#FFFFFF;\">";
			

		}

		if ($i eq "^8") {

			$counter++;
			$i = "<span style=\"color:#FFA000;\">";
			

		}

		if ($i eq "^9") {

			$counter++;
			$i = "<span style=\"color:#C0C0C0;\">";
			

		}

		#OSP Colors....
		if ($i eq "^A" || $i eq "^a") {
			$counter++;
			$i = "<span style=\"color:#DF9E4A;\">";
			
		}
		if ($i eq "^B" || $i eq "^b") {
			$counter++;
			$i = "<span style=\"color:#0F7371;\">";
			
		}
		if ($i eq "^C" || $i eq "^c") {
			$counter++;
			$i = "<span style=\"color:#4E144F;\">";
			
		}
		if ($i eq "^D" || $i eq "^d") {
			$counter++;
			$i = "<span style=\"color:#2b4d70;\">";
			
		}
		if ($i eq "^E" || $i eq "^e") {
			$counter++;
			$i = "<span style=\"color:#7508C5;\">";
			
		}
		if ($i eq "^F" || $i eq "^f") {
			$counter++;
			$i = "<span style=\"color:#3593B9;\">";
			
		}
		if ($i eq "^G" || $i eq "^g") {
			$counter++;
			$i = "<span style=\"color:#BBCEBA;\">";
			
		}
		if ($i eq "^H" || $i eq "^h") {
			$counter++;
			$i = "<span style=\"color:#19583B;\">";
			
		}
		if ($i eq "^I" || $i eq "^i") {
			$counter++;
			$i = "<span style=\"color:#8A273B;\">";
			
		}
		if ($i eq "^J" || $i eq "^j") {
			$counter++;
			$i = "<span style=\"color:#792B29;\">";
			
		}
		if ($i eq "^K" || $i eq "^k") {
			$counter++;
			$i = "<span style=\"color:#8E3608;\">";
			
		}
		if ($i eq "^L" || $i eq "^l") {
			$counter++;
			$i = "<span style=\"color:#BA9A61;\">";
			
		}
		if ($i eq "^M" || $i eq "^m") {
			$counter++;
			$i = "<span style=\"color:#6B6E43;\">";
			
		}
		if ($i eq "^N" || $i eq "^n") {
			$counter++;
			$i = "<span style=\"color:#E3E5CF;\">";
			
		}
		if ($i eq "^O" || $i eq "^o") {
			$counter++;
			$i = "<span style=\"color:#CFD1A2;\">";
			
		}
		if ($i eq "^P" || $i eq "^p") {
			$counter++;
			$i = "<span style=\"color:#000000;\">";
			
		}
		if ($i eq "^Q" || $i eq "^q") {
			$counter++;
			$i = "<span style=\"color:#BC191E;\">";
			
		}
		if ($i eq "^R" || $i eq "^r") {
			$counter++;
			$i = "<span style=\"color:#41B747;\">";
			
		}
		if ($i eq "^S" || $i eq "^s") {
			$counter++;
			$i = "<span style=\"color:#E1EB3D;\">";
			
		}
		if ($i eq "^T" || $i eq "^t") {
			$counter++;
			$i = "<span style=\"color:#252084;\">";
			
		}
		if ($i eq "^U" || $i eq "^u") {
			$counter++;
			$i = "<span style=\"color:#52B1B5;\">";
			
		}
		if ($i eq "^V" || $i eq "^v") {
			$counter++;
			$i = "<span style=\"color:#BF2BBF;\">";
			
		}
		if ($i eq "^W" || $i eq "^w") {
			$counter++;
			$i = "<span style=\"color:#FFFFFF;\">"	;
			
		}
		if ($i eq "^X" || $i eq "^x") {
			$counter++;
			$i = "<span style=\"color:#D98926;\">";
			
		}
		if ($i eq "^Y" || $i eq "^y") {
			$counter++;
			$i = "<span style=\"color:#74777C;\">";
			
		}
		if ($i eq "^Z" || $i eq "^z") {
			$counter++;
			$i = "<span style=\"color:#B9BDC0;\">";
			
		}
		if ($i eq "^!") {
			$counter++;
			$i = "<span style=\"color:#DF9E4A;\">";
			
		}
		if ($i eq "^@") {
			$counter++;
			$i = "<span style=\"color:#773C08;\">";
			
		}
		if ($i eq "^#") {
			$counter++;
			$i = "<span style=\"color:#4E144F;\">";
			
		}
		if ($i eq "^\$") {
			$counter++;
			$i = "<span style=\"color:#0277F2;\">";
			
		}
		if ($i eq "^\&") {
			$counter++;
			$i = "<span style=\"color:#318FC3;\">";
			
		}
		if ($i eq "^\*") {
			$counter++;
			$i = "<span style=\"color:#A6191F;\">";
			
		}
		if ($i eq "^\(") {
			$counter++;
			$i = "<span style=\"color:#026037;\">";
			
		}
		if ($i eq "^\)") {
			$counter++;
			$i = "<span style=\"color:#EB0237;\">";
			
		}
		if ($i eq "^_") {
			$counter++;
			$i = "<span style=\"color:#720208;\">";
			
		}
		if ($i eq "^-") {
			$counter++;
			$i = "<span style=\"color:#696908;\">";
			
		}
		if ($i eq "^=") {
			$counter++;
			$i = "<span style=\"color:#696908;\">";
			
		}
		if ($i eq "^+") {
			$counter++;
			$i = "<span style=\"color:#823B11;\">";
			
		}
		if ($i eq "^|") {
			$counter++;
			$i = "<span style=\"color:#027708;\">";
			
		}
		if ($i eq "^\\") {
			$counter++;
			$i = "<span style=\"color:#027708;\">";
			
		}
		if ($i eq "^/") {
			$counter++;
			$i = "<span style=\"color:#E2E7AD;\">";
			
		}
		if ($i eq "^.") {
			$counter++;
			$i = "<span style=\"color:#FFFFFF;\">";
			
		}
		if ($i eq "^,") {
			$counter++;
			$i = "<span style=\"color:#B29F74;\">";
			
		}
		if ($i eq "^<") {
			$counter++;
			$i = "<span style=\"color:#027708;\">";
			
		}
		if ($i eq "^>") {
			$counter++;
			$i = "<span style=\"color:#02027D;\">";
			
		}
		if ($i eq "^?") {
			$counter++;
			$i = "<span style=\"color:#540F0A;\">";
			
		}
		if ($i eq "^:") {
			$counter++;
			$i = "<span style=\"color:#B1B1B6;\">";
			
		}
		if ($i eq "^\'") {
			$counter++;
			$i = "<span style=\"color:#BEECC3;\">";
			
		}
		if ($i eq "^[") {
			$counter++;
			$i = "<span style=\"color:#B2B2B7;\">";
			
		}
		if ($i eq "^]") {
			$counter++;
			$i = "<span style=\"color:#767608;\">";
			
		}
		if ($i eq "^{") {
			$counter++;
			$i = "<span style=\"color:#B2B2B7;\">";
			
		}
		if ($i eq "^}") {
			$counter++;
			$i = "<span style=\"color:#767608;\">";
			
		}
		if ($i eq "^\"") {
			$counter++;
			$i = "<span style=\"color:#020279;\">";
			
		}
		if ($i eq "^\`") {
			$counter++;
			$i = "<span style=\"color:#733A07;\">";
			
		}

		if ($i eq "^\^") {
			#escape ^ grrr
			$counter++;
			$i = "<span>^";
			
		}

                if ($i eq "<") {

                        $i = "&#60;";
			

                }

                if ($i eq ">") {

                        $i = "&#62;";
			

                }

                if ($i eq "\\") {

                        $i = "&#92;";
			

                }

                if ($i eq "/") {

                        $i = "&#47;";
			

                }

                if ($i eq "#") {

                        $i = "&#35;";
			

                }

                if ($i eq "&") {

                        $i = "&#38;";
			

                }


                if ($i eq "!") {

                        $i = "&#33;";
			

                }

                if ($i eq "-") {

                        $i = "&#45;";
			
                }

	}

	
	$foo = join("","<span style=\"color:#FFFFFF;\">",@list);

	for($x=0; $x < $counter; $x++) {

		$foo = join("","$foo","</span>");			

	}

	$foo = join("","$foo","</span>");

	return $foo;

}

# Convert players name from ascii representation for hexadecimal bytes (like 4D6F6E676F)
# to a scalar string like "Mongo", these are not null terminated.
sub convert_name ($) {

	my $foo = pack("H*",$_[0]);

	return $foo;
} 


sub graduatecolor ($) {

	#Graduate from blue to red;
	my $value = $_[0];

	#Set our color start point
	my $rcolor = 0;
	my $gcolor = 0;
	my $bcolor = 255;
	
	#What the color scale looks like in decimal
	#scale from 0 to 1000
	# 0 	= 0000FF
	# 255 	= 4000BF
	# 510 	= 800080
	# 765 	= BF0040
	# 1020 	= FF0000

	#Maximal (255 * 4)
	if ($value > 1020) {$value=1020;}
	#Minimal (255 * 0)
	if ($value < 0) {$value=0;}

	# Divide the index by 4 so we can fit in to 8 bits per pixel.
	# In perl numbers are always stored as floating point
	# So when you take two numbers you think that are integers you are
	# going to get back a floating point result. 
	# You must strip out the fractional portion using int
	my $index = int($value / 4);
	
	$rcolor = $rcolor + $index;
	$bcolor = $bcolor - $index;

	# Silly perl data conversions :-(
	# Convert from integer to string formated as hexadecimal
	# for html color components
	$rcolor = sprintf("%x", $rcolor);
	$gcolor = sprintf("%x", $gcolor);
	$bcolor = sprintf("%x", $bcolor);
	
	# Add a leading 0 if less then ten.	
	my $tmphex = 15;
	if (hex($rcolor) < 16) { $rcolor="0" . $rcolor };
	if (hex($gcolor) < 16) { $gcolor="0" . $gcolor };
	if (hex($bcolor) < 16) { $bcolor="0" . $bcolor };

	# Make our color from the components.
	return join("","#",$rcolor,$gcolor,$bcolor);

}

sub calc_winner {

	my $allieweight = 0;
	my $axisweight = 0;
	my $sigma = 0.01;
	my $allieavgxp 		= int( $alliexptotal 	/ ( $alliemembers + $sigma ) );
	my $axisavgxp 		= int( $axisxptotal 	/ ( $axismembers  + $sigma ) );
	my $allieavgping	= int( $alliepingtotal 	/ ( $alliemembers + $sigma ) );
	my $axisavgping 	= int( $axispingtotal 	/ ( $axismembers  + $sigma ) );

	if ($alliemembers > $axismembers) {

		$allieweight++;

	} elsif ($axismembers > $alliemembers) {

		$axisweight++;

	}

	if ($allieavgxp > $axisavgxp) {

		$allieweight++;

	} elsif ($axisavgxp > $allieavgxp) {

		$axisweight++;
		
	}

	if ($allieavgping > $axisavgping) {

		$axisweight++;

	} elsif ($axisavgping > $allieavgping) {

		$allieweight++;

	}

	if ($allieweight > $axisweight) {

		return "Allied Victory!";
		#return $axisweight . " " . $allieweight;

	} elsif ($axisweight > $allieweight) {

		return "Axis Sieg!";
		#return $axisweight . " " . $allieweight;

	} elsif ($axisweight == $allieweight) {

		return "Tie!";
		#return $axisweight . " " . $allieweight;
	}


}

sub print_mapinfo {

	return td(	
			{
			-WIDTH=>"256",
			-HEIGHT=>"40%",
			-VALIGN=>"TOP",
			-CLASS=>"main",
			},

		table(
			{
			-WIDTH=>"100%",
			#-HEIGHT=>"100%",
			-ALIGN=>"CENTER",
			-CLASS=>"sub",
			},
				TR(
					td(
						{
						-VALIGN=>"TOP",
						-ALIGN=>"CENTER",
						-HEIGHT=>"1",
						-CLASS=>"mapheaders"
						},

							"<span class=mapbold>", $live->{server}->{numplayers}, 
							"</span>out of<span class=mapbold>",
							$live->{server}->{maxplayers} - $reservedslots, 
							"</span>players<br/> map:<span class=mapbold>",
							$live->{server}->{map}, "</span><br/>",&now,

					),

				),
						

				TR(
					{
					-VALIGN=>"TOP",
					-ALIGN=>"CENTER",
					},
						td(
							br(),
							&map_pic,
						),
				),
			),		
	);
		
}


sub print_playerteaminfo {

	return td(
		{
		-WIDTH=>"256",
		-ALIGN=>"left",
		-VALIGN=>"top",
		-CLASS=>"main"
		},

		table(
				{
				-WIDTH=>"100%",
				-CLASS=>"sub",
				},

			&print_players,
		
		),

		table(
			{
			-WIDTH=>"100%",
			-CLASS=>"sub",
			},

			TR(
				{
				-CLASS=>"teamsheader",
				},

				td(
					{
					-ALIGN=>"CENTER",
					-COLSPAN=>"2",
					-CLASS=>"allies"
					},
							"ALLIES",
				),

				td(
					{
					-ALIGN=>"CENTER",
					-COLSPAN=>"2",
					-CLASS=>"axis"
					},
							"AXIS",
				),

			),

			TR( 
				{
				-CLASS=>"teams",
				},

				td(
					{
					-ALIGN=>"LEFT",
					},

						"Members: ",
				),

				td(
					{
					-ALIGN=>"LEFT",
					},

						$alliemembers,
				),

				td(
					{
					-ALIGN=>"LEFT",
					},

						"Members:",
				),

				td(
					{
					-ALIGN=>"LEFT",
					},

						$axismembers,
				),


			),

			TR( 
				{
				-CLASS=>"teams",
				},

				td(
					{
					-ALIGN=>"LEFT",
					},

						"Avg XP:",
				),

				td(
					{
					-ALIGN=>"LEFT",
					},

						int($alliexptotal / ($alliemembers + 0.01)),
				),

				td(
					{
					-ALIGN=>"LEFT",
					},

						"Avg XP:",
				),

				td(
					{
					-ALIGN=>"LEFT",
					},
						int($axisxptotal / ($axismembers + 0.01)),
				),

			),

			TR( 
				{
				-CLASS=>"teams",
				},

				td(
					{
					-ALIGN=>"LEFT",
					},

						"Avg Ping:",
				),

				td(
					{
					-ALIGN=>"LEFT",
					},

						int($alliepingtotal / ($alliemembers + 0.01)),
				),

				td(
					{
					-ALIGN=>"LEFT",
					},

						"Avg Ping:",
				),

				td(
					{
					-ALIGN=>"LEFT",
					},

						int($axispingtotal / ($axismembers + 0.01)), 
				),

			),

			TR(
				{
				-CLASS=>"teamsfooter",
				},

				td(
					{
					-ALIGN=>"CENTER",
					-COLSPAN=>"4",
					},

					"Likely Outcome: ",
					&calc_winner,
				),
			),
		),

	);
}

sub print_ruleset {
	return td( 
		{ 
		-width=>"40%",
		-align=>"left", 
		-valign=>"top",  
		-class=>"main"
		},

		table( 
			{
			-border=>"0", 
			-width=>"100%", 
			-class=>"sub"
			},

			TR(
				{
				-class=>"rulesheader"
				},


				td(
					"Rules"
				),	
									
				td(
					"Settings"
				),

			),
			&print_rules
								
		),
	);

}

# Actually print stuff to the browser.

# print the standard text/html mime type header
print header;

# Open up the HTML and BODY Tags
print start_html(
			{

				-HEAD=>meta( {-http_equiv=>'REFRESH',-content=>'30;'}),
				-title=>"(" . $live->{server}->{numplayers} . "/" . $live->{server}->{maxplayers} . ") " . $live->{server}->{name},
				-style=>{-src=>"$themexml->{theme}->{css}"},
			}, 

	);

# Start the magic
print	div({-ALIGN=>"center"},

		table( 
			{
			-WIDTH=>"856",
			#-HEIGHT=>"230", 
			-CLASS=>"main",
			},

			TR( {-CLASS=>"main"},
				#link to wherevever
				td(
					{
					-WIDTH=>"100%",
					-ALIGN=>"LEFT",
					-VALIGN=>"TOP",
					-COLSPAN=>"3",
					-CLASS=>"headers"
					},

						a(
							{
							-href=>"$siteurl"	
							},
							"Back to: ",$sitename,
						),

				),

			),

			TR(
				{
				#-WIDTH=>"100%",
				#-BORDER=>"0",
				-CLASS=>"main"
				},
				td(
					{
					-WIDTH=>"100%",
					-ALIGN=>$titlepictureposition,
					-COLSPAN=>"3",
					-CLASS=>"title"
					},

					img( 
						{ -SRC=>$titlepicture,
						  -ALT=>"titlepicture",
						},
					),	
				),

			),

			TR( {-CLASS=>"main"},


				td(
					{
					-WIDTH=>"100%",
					-COLSPAN=>"3",
					-ALIGN=>"left",
					-CLASS=>"headers"
					},

	
							"&nbsp;",&color_hostname($live->{server}->{name}), 
						"  ($live->{server}->{hostname})  ",
				),
			),

			TR(

				&print_mapinfo,

				&print_playerteaminfo,
		
				&print_ruleset,
			),

			TR( {-CLASS=>"main"},


                                td(
                                        {
                                        -ALIGN=>"left",
                                        -CLASS=>"headers",
                                        },

                                        "Theme: $themexml->{theme}->{name}",

                                ),
                                td(
                                        {
                                        -ALIGN=>"left",
                                        -CLASS=>"headers",
                                        },

                                        "&nbsp;",

                                ),
                                td(
                                        {
                                        -ALIGN=>"right",
                                        -CLASS=>"headers",
                                        },

                                        "$themexml->{theme}->{copyright}->{cnote}",
                                        a( {-href=>$themexml->{theme}->{copyright}->{clink}},
                                        "$themexml->{theme}->{copyright}->{cname}",
					),
				),

			),



		),
		
			span(
				{
				-CLASS=>"notice",
				},
				"ETLive 1.21",
				"(C) 2003 cbajumpa\@or8.net ",
				a( {-href=>"http://systats.or8.net/etlive/"},
				" Get it here. " ),
			),


			p(
				a(
					{
					 -HREF=>"http://validator.w3.org/check/referer",
					},

					img(
                                        	{
						-BORDER=>"0",
						-SRC=>"http://www.w3.org/Icons/valid-html401.gif",
						-ALT=>"Valid HTML 4.01!",
						-HEIGHT=>"31",
						-WIDTH=>"88",
						},
					),

				),

				a(
					{
					 -HREF=>"http://jigsaw.w3.org/css-validator/check/referer",
					},

					img(
                                        	{
						-BORDER=>"0",
						-SRC=>"http://jigsaw.w3.org/css-validator/images/vcss.gif",
						-ALT=>"Valid CSS!",
						-HEIGHT=>"31",
						-WIDTH=>"88",
						},
					),

				),

			),

	);


print end_html;


