How to track players


(Chuck) #21

Well, the .ht* files shouldn’t be chmodded per definition; rather, they should be protected by the webserver configuration if desirable of course.

Genert, about your ‘switch {}’ wouldn’t it be easier and cleaner to do something like this (this is how I do it though):


<?php
// A friend helped me make this a bit nicer. :)
// Sort the player array on team.
$teams		=	array(
	'axis'		=>	array( ),
	'allies'	=>	array( ),
	'spec'		=>	array( )
);

$team_array	=	array( '', 'axis', 'allies', 'spec' );

array_map(
	function ( $a )
	{
		// We require the two arrays.
		global $teams, $team_array;

		// Append the player to the proper team.
		$teams[ $team_array[ $a['team'] ] ][]	=	$a;
	},
	$et->player_info // full player array.
);

I am extremely interested in seeing your ‘fixOSPColors’ function, my own colorize function works like a charm - but I’m wondering how others did it, for the record:


function colorize( $nickname )
{
	// Get the quake colors.
	global $quake, $hex;

	// Replace quake colors with hex colors.
	$nickname = str_replace( $quake, $hex, $nickname );

	// Return the nickname.
	return $nickname;// . "</h4>";
}

Where ‘$quake’ is an array containing ^0-^9, ^aA-^zZ, plus some other chars like ? *, etc.
And $hex is an array containing html (hex) colours. :slight_smile:
Greetings,

Chuck.


(Indloon) #22

[QUOTE=Chuck;377380]Well, the .chmod files shouldn’t be chmodded per definition; rather, they should be protected by the webserver configuration if desirable of course.

Genert, about your ‘switch {}’ wouldn’t it be easier and cleaner to do something like this (this is how I do it though):


<?php
// A friend helped me make this a bit nicer. :)
// Sort the player array on team.
$teams		=	array(
	'axis'		=>	array( ),
	'allies'	=>	array( ),
	'spec'		=>	array( )
);

$team_array	=	array( '', 'axis', 'allies', 'spec' );

array_map(
	function ( $a )
	{
		// We require the two arrays.
		global $teams, $team_array;

		// Append the player to the proper team.
		$teams[ $team_array[ $a['team'] ] ][]	=	$a;
	},
	$et->player_info // full player array.
);

I am extremely interested in seeing your ‘fixOSPColors’ function, my own colorize function works like a charm - but I’m wondering how others did it, for the record:


function colorize( $nickname )
{
	// Get the quake colors.
	global $quake, $hex;

	// Replace quake colors with hex colors.
	$nickname = str_replace( $quake, $hex, $nickname );

	// Return the nickname.
	return $nickname;// . "</h4>";
}

Where ‘$quake’ is an array containing ^0-^9, ^aA-^zZ, plus some other chars like ? *, etc.
And $hex is an array containing html (hex) colours. :slight_smile:
Greetings,

Chuck.[/QUOTE]

I have got the team stuff already.=)

In color replacement you should use the color code with array.
As you asked about fixOSPcolors


	function fixOSPColors($str, $std = "white") {
		$str = ereg_replace("(\^)?$", "", $str);
		$str = ereg_replace("\^\^", "^", $str);
		$str = "<FONT COLOR=\"" . $std . "\">" . $str . "</FONT>";
		
		$color = array (
			0  => "black",		1  => "red",		2  => "lime",		3  => "yellow",		4  => "blue",		
			5  => "cyan",		6  => "magenta",	7  => "white",		8  => "orange",		9  => "gray", 	
			
			10 => "#CC9966",	11 => "teal", 		12 => "purple",		13 => "#0066CC",	14 => "#6600CC", 	
			15 => "#3399CC", 	16 => "#CCFFCC", 	17 => "#006633",	18 => "#990033", 	19 => "#993333", 	
			20 => "#993300", 	21 => "#CC9966",	22 => "#999966", 	23 => "#CCCC99", 	24 => "#CCCC66",
			25 => "#00007F",
			
			"black"		=> array (0 => "0", 1 => "p", 2 => "P"											),
			"red"		=> array (0 => "1", 1 => "q", 2 => "Q"											),
			"lime"		=> array (0 => "2", 1 => "r", 2 => "R"											),
			"yellow"	=> array (0 => "3", 1 => "s", 2 => "S"											),
			"blue"		=> array (0 => "4", 1 => "t", 2 => "T", 3 => ">"								),
			"cyan"		=> array (0 => "5", 1 => "u", 2 => "U"											),
			"magenta"	=> array (0 => "6", 1 => "v", 2 => "V"											),
			"white"		=> array (0 => "7", 1 => "w", 2 => "W", 3 => "{", 4 => "["	                    ),
			"orange"	=> array (0 => "8", 1 => "x", 2 => "X", 3 => "a", 4 => "z",						),
			"gray"		=> array (0 => "9", 1 => "y", 2 => "Y", 3 => ":"								),
			
			"#CC9966"	=> array (0 => "a", 1 => "A", 2 => "!", 3 => ","						),
			"teal"		=> array (0 => "b", 1 => "B"											),
			"purple"	=> array (0 => "c", 1 => "C", 2 => "#"									),
			"#0066CC"	=> array (0 => "d", 1 => "D", 2 => "&", 3 => "$"						),
			"#6600CC"	=> array (0 => "e", 1 => "E"											),
			"#3399CC"	=> array (0 => "f", 1 => "F"											),
			"#CCFFCC"	=> array (0 => "g", 1 => "G", 2 => "'"									),
			"#006633"	=> array (0 => "h", 1 => "H", 2 => "(", 3 => "\\", 4 => "|", 5 => "<",	),
			"#990033"	=> array (0 => "i", 1 => "I", 2 => ")"									),
			"#993333"	=> array (0 => "j", 1 => "J", 2 => "?", 3 => "*", 4 => "_"				),
			"#993300"	=> array (0 => "k", 1 => "K", 2 => '"', 3 => "@"						),
			"#CC9966"	=> array (0 => "l", 1 => "L", 2 => "-"									),
			"#999966"	=> array (0 => "m", 1 => "M", 2 => "=", 3 => "]", 4 => "}"				),
			"#CCCC99"	=> array (0 => "n", 1 => "N", 2 => "¤", 3 => "%", 4 => "." 				),
			"#CCCC66"	=> array (0 => "o", 1 => "O", 2 => "/", 								),
			"#00007F"   => array (0 => ">",                                                     ),
			"#007fFF"   => array (0 => "d", 1 => "D",                                           ),
		);

		for ($i = 0; $i < count($color) / 2; $i++) {
			for ($j = 0; $j < count($color[$color[$i]]); $j++) {
				$str = str_replace("^". $color[$color[$i]][$j], "</FONT><FONT COLOR=\"" . $color[$i] . "\">", $str);
			}
		}
		return $str;
	}

(Indloon) #23

[QUOTE=Chuck;377380]Well, the .chmod files shouldn’t be chmodded per definition; rather, they should be protected by the webserver configuration if desirable of course.

Genert, about your ‘switch {}’ wouldn’t it be easier and cleaner to do something like this (this is how I do it though):


<?php
// A friend helped me make this a bit nicer. :)
// Sort the player array on team.
$teams		=	array(
	'axis'		=>	array( ),
	'allies'	=>	array( ),
	'spec'		=>	array( )
);

$team_array	=	array( '', 'axis', 'allies', 'spec' );

array_map(
	function ( $a )
	{
		// We require the two arrays.
		global $teams, $team_array;

		// Append the player to the proper team.
		$teams[ $team_array[ $a['team'] ] ][]	=	$a;
	},
	$et->player_info // full player array.
);

I am extremely interested in seeing your ‘fixOSPColors’ function, my own colorize function works like a charm - but I’m wondering how others did it, for the record:


function colorize( $nickname )
{
	// Get the quake colors.
	global $quake, $hex;

	// Replace quake colors with hex colors.
	$nickname = str_replace( $quake, $hex, $nickname );

	// Return the nickname.
	return $nickname;// . "</h4>";
}

Where ‘$quake’ is an array containing ^0-^9, ^aA-^zZ, plus some other chars like ? *, etc.
And $hex is an array containing html (hex) colours. :slight_smile:
Greetings,

Chuck.[/QUOTE]

You need to define the colorcode(^1) with the color code in HTML(red).
Some examples how to do it:
http://genert.impact.pri.ee/Chuck.phps


(Indloon) #24

Btw if you are interested.
http://genert.impact.pri.ee/test/server.php?ip=188.165.233.195:29000
This is what I got myself.
Now Im using another code by SL.Which is not up,yet.

E:Looking at my tracker and SL tracker,SL gives wrong server location.
So Scheeee update your GeoIP.dat file plz:D


(Chuck) #25

You got to clean your code up a little bit, anyway, this is my code:


<?php
// Thanks to Seniltai for some help!

// Character table.
$char_table		=	'0123456789abcdefghijklmnopqrstuvwxyz)?*|';
$char_table		.=	strtoupper( $char_table );

// Hex color table. - The colours themselves are taken from qstats!
$hex_table		=
	'000000'.	// 0
	'DA0120'.	// 1
	'00B906'.	// 2
	'E8FF19'.	// 3
	'170BDB'.	// 4
	'23C2C6'.	// 5
	'E201DB'.	// 6
	'FFFFFF'.	// 7
	'CA7C27'.	// 8
	'757575'.	// 9
	'EB9F53'.	// 0
	'106F59'.	// a
	'5A134F'.	// b
	'035AFF'.	// c
	'681EA7'.	// d
	'5097C1'.	// e
	'BEDAC4'.	// f
	'024D2C'.	// g
	'7D081B'.	// h
	'90243E'.	// i
	'743313'.	// j
	'A7905E'.	// k
	'555C26'.	// l
	'AEAC97'.	// m
	'C0BF7F'.	// n
	'000000'.	// o
	'DA0120'.	// p
	'00B906'.	// q
	'E8FF19'.	// r
	'170BDB'.	// s
	'23C2C6'.	// t
	'E201DB'.	// u
	'FFFFFF'.	// v
	'CA7C27'.	// w
	'757575'.	// x
	'CC8034'.	// y
	'DBDF70'.	// z
	'000000'.	// )
	'990000'.	// ?
	'990000'.	// *
	'000000'	// |
;

$hex_table	.=	$hex_table;

// The quake and hex arrays.
$quake	=	array();
$hex	=	array();

// Make some modifications.
for ( $i = 0, $a = 0; $i < strlen( $char_table ); $i++, $a+=6 )
{
	// Prepend '^'
	$quake[]	=	"^" . substr( $char_table, $i, 1 );

	// Add the font color.
	$hex[]		=	'<font style="color:#' . substr( $hex_table, $a, 6 ) . ';">'; // Still looking for a good fix for the html stuff ..
}

function colorize( $nickname )
{
	// Get the quake colors.
	global $quake, $hex;

	// Replace quake colors with hex colors.
	$nickname = str_replace( $quake, $hex, $nickname );

	// Return the nickname.
	return $nickname;// . "</h4>";
}

// Made by Chuck & Seniltai.

Anyone is free to use this code, just give a bit of credit to Seniltai and me (Chuck) :wink:


(Indloon) #26

I cant see.


$hex[]  = '<font style="color:#' . substr( $hex_table, $a, 6 ) . ';">'; 

Wheres the ending??
Should be with </font>


(Chuck) #27

There’s no ending yet - I’m still looking for a neat fix for that.

But as you can see, it’s a for-loop that’s not inside a function, therefor, I can’t end it with </font>. Then again, I’m still looking for a good <font> replacement as the tag is depreciated.


(Indloon) #28

[QUOTE=Chuck;377401]There’s no ending yet - I’m still looking for a neat fix for that.

But as you can see, it’s a for-loop that’s not inside a function, therefor, I can’t end it with </font>. Then again, I’m still looking for a good <font> replacement as the tag is depreciated.[/QUOTE]

<?php
// Thanks to Seniltai for some help!

// Character table.
$char_table        =    '0123456789abcdefghijklmnopqrstuvwxyz)?*|';
$char_table        .=    strtoupper( $char_table );
$font              =    '</font>';

// Hex color table. - The colours themselves are taken from qstats!
$hex_table        =
    '000000'.    // 0
    'DA0120'.    // 1
    '00B906'.    // 2
    'E8FF19'.    // 3
    '170BDB'.    // 4
    '23C2C6'.    // 5
    'E201DB'.    // 6
    'FFFFFF'.    // 7
    'CA7C27'.    // 8
    '757575'.    // 9
    'EB9F53'.    // 0
    '106F59'.    // a
    '5A134F'.    // b
    '035AFF'.    // c
    '681EA7'.    // d
    '5097C1'.    // e
    'BEDAC4'.    // f
    '024D2C'.    // g
    '7D081B'.    // h
    '90243E'.    // i
    '743313'.    // j
    'A7905E'.    // k
    '555C26'.    // l
    'AEAC97'.    // m
    'C0BF7F'.    // n
    '000000'.    // o
    'DA0120'.    // p
    '00B906'.    // q
    'E8FF19'.    // r
    '170BDB'.    // s
    '23C2C6'.    // t
    'E201DB'.    // u
    'FFFFFF'.    // v
    'CA7C27'.    // w
    '757575'.    // x
    'CC8034'.    // y
    'DBDF70'.    // z
    '000000'.    // )
    '990000'.    // ?
    '990000'.    // *
    '000000'    // |
;

$hex_table    .=    $hex_table;

// The quake and hex arrays.
$quake    =    array();
$hex    =    array();

// Make some modifications.
for ( $i = 0, $a = 0; $i < strlen( $char_table ); $i++, $a+=6 )
{
    // Prepend '^'
    $quake[]    =    "^" . substr( $char_table, $i, 1 );

    // Add the font color.
    $hex[]        =    '<font style="color:#' . substr( $hex_table, $a, 6 ) . ';">'; // Still looking for a good fix for the html stuff ..
}

function colorize( $nickname )
{
    // Get the quake colors.
    global $quake, $hex;

    // Replace quake colors with hex colors.
    $nickname = str_replace( $quake, $hex, $nickname);

    // Return the nickname.
    //return $nickname;// . "</h4>";
	return $nickname;
}

// Made by Chuck & Seniltai.

echo colorize('^2ufo^1has^0landed'),$font;

But if the main question is about,how the tracker will use it all.
++i helps!
E:Into code,add $nickname = str_replace( $quake, $hex, $nickname, $font); and return $font;
You ill see how many colortags have used:D


(Chuck) #29

++i?

The last </font> doesn’t make it valid HTML; since:

<font>ufo<font>has<font>landed</font>. How the tracker will use it isn’t that hard.

a little snippet:


<td><?php echo colorize( $name ); ?></td>

It’s on a simple for loop.

hmm,

E:Into code,add $nickname = str_replace( $quake, $hex, $nickname, $font); and return $font;

If I’m not mistaken the fourth argument of str_replace is ‘count’? - so I don’t see how thats going to work?


(Indloon) #30

Wut,it worked for me:
http://genert.impact.pri.ee/Chuck.php
E:Right,tested it on tracker,doesnt work.Seems that the repeat ****s the function.
E2:I look futher for this


(Chuck) #31

Whats the code you use in the tracker?

Anyway, you’re wrong about the str_replace ( http://php.net/manual/en/function.str-replace.php ).

  • So that means, the $font will be replaced by the number of replacements.
    (example from php.net):

<?php
// Provides: 2
$str = str_replace("ll", "", "good golly miss molly!", $count);
echo $count;
?>

On the other thing, about the tracker:

Here’s an little example I just wrote for you:


<?php foreach ( $axis as $player ): extract( $player ); ?>
						<tr>
							<td><?php echo colorize( $name ); ?></td>
							<td><?php echo $score; ?></td>
							<td><?php echo $ping; ?></td>
						</tr>
<?php endforeach; ?>

// my own code is a bit different, but not by much.


(Chuck) #32

I just wrote a neat (if I may say so myself) fix for the tag endings:

Genert, your str_replace count made me thinking … see for yourself :slight_smile:


// Make some modifications.
for ( $i = 0, $a = 0; $i < strlen( $char_table ); $i++, $a+=6 )
{
	// Prepend '^'
	$quake[]	=	"^" . substr( $char_table, $i, 1 );

	// Add the font color.
	$hex[]		=	'<b style="color:#' . substr( $hex_table, $a, 6 ) . ';">';
}

function colorize( $nickname )
{
	// Get the quake colors.
	global $quake, $hex;

	// Replace quake colors with hex colors.
	$nickname = str_replace( $quake, $hex, $nickname, $count );

	// Temporarily explode the nickname.
	$split	=	explode( '<b', $nickname );

	// Make sure colours exists in the nickname.
	if ( count( $split ) > 1 )
	{
		// Loop through the pieces.
		foreach ( $split as $key => $val )
		{
			// Make sure the piece isn't empty.
			if ( !empty( $val ) )
				// Prepend '<b' before - and append '</b>' after - the value.
				$split[ $key ] = str_replace($val, '<b'.$val.'</b>', $val);
		}
	}

	// Glue the pieces back together.
	$nickname = implode($split);

	// Return the nickname.
	return $nickname;
}


(Indloon) #33

Heh,nice to see you can handle this.
I found something in my HDD.
Note: This script was made like 7months ago and its unfinished,but it shows how you can do it.
http://genert.impact.pri.ee/color.phps


(Chuck) #34

[QUOTE=Genert;377424]Heh,nice to see you can handle this.
I found something in my HDD.
Note: This script was made like 7months ago and its unfinished,but it shows how you can do it.
http://genert.impact.pri.ee/color.phps[/QUOTE]

I’m wondering how that script could help me? It’s quite inefficient + full of errors. No offense intended.

As a replacement for your class, you could use this one (I already added my standalone color functions to my ET tracking class, so it was just copying the functions and here you go):


<?php

class color_codes 
{
	public function __construct()
	{
		/**
		 * Quake3 color stuff.
		 *
		 * - Thanks Seniltai for some help!
		 */
		// Character table.
		$char_table		=	'0123456789abcdefghijklmnopqrstuvwxyz)?*|';
		$char_table		.=	strtoupper( $char_table );

		// Hex color table.
		$hex_table		=
			'000000'.	// 0
			'DA0120'.	// 1
			'00B906'.	// 2
			'E8FF19'.	// 3
			'170BDB'.	// 4
			'23C2C6'.	// 5
			'E201DB'.	// 6
			'FFFFFF'.	// 7
			'CA7C27'.	// 8
			'757575'.	// 9
			'EB9F53'.	// 0
			'106F59'.	// a
			'5A134F'.	// b
			'035AFF'.	// c
			'681EA7'.	// d
			'5097C1'.	// e
			'BEDAC4'.	// f
			'024D2C'.	// g
			'7D081B'.	// h
			'90243E'.	// i
			'743313'.	// j
			'A7905E'.	// k
			'555C26'.	// l
			'AEAC97'.	// m
			'C0BF7F'.	// n
			'000000'.	// o
			'DA0120'.	// p
			'00B906'.	// q
			'E8FF19'.	// r
			'170BDB'.	// s
			'23C2C6'.	// t
			'E201DB'.	// u
			'FFFFFF'.	// v
			'CA7C27'.	// w
			'757575'.	// x
			'CC8034'.	// y
			'DBDF70'.	// z
			'000000'.	// )
			'990000'.	// ?
			'990000'.	// *
			'000000'	// |
		;

		$hex_table	.=	$hex_table;

		// The quake and hex arrays.
		$this->quake	=	array();
		$this->hex	=	array();

		// Make some modifications.
		for ( $i = 0, $a = 0; $i < strlen( $char_table ); $i++, $a+=6 )
		{
			// Prepend '^'
			$this->quake[]	=	"^" . substr( $char_table, $i, 1 );

			// Add the font color.
			$this->hex[]		=	'<b style="color:#' . substr( $hex_table, $a, 6 ) . ';">';
		}
	}
	
	public function colorize( $string )
	{
		// Replace Quake3 color characters with html <b> tag with the
		// appropriate hex color.
		$string	=	str_replace( $this->quake, $this->hex, $string, $count );

		// Split the string up in pieces.
		$split	=	explode( '<b', $string );

		// Loop through the string, and close the <b> tags.
		if ( count( $split ) > 1 )
		{
			// Loop through the pieces.
			foreach ( $split as $key => $value )
			{
				// Make sure the value isn't empty.
				if ( !empty( $value ) )
					// Prepend '<b>' before, append '</b>' after, the value.
					$split[ $key ]	=	str_replace( $value, '<b' . $value . '</b>', $value );
			}
		}

		// Glue the pieces back together.
		return implode( $split );
	}

	public function remove_color( $string )
	{
		return str_replace( $this->quake, '', $string );
	}
}
?>

should work…


(Indloon) #35

You coulded look at q3ColsReplace function.The other code was for making class of it.This is how it can be done.


    $colour["0"] = "000000"; 
    $colour["1"] = "DA0120"; 
    $colour["2"] = "00B906"; 
    $colour["3"] = "E8FF19"; 
    $colour["4"] = "170BDB"; 
    $colour["5"] = "23C2C6"; 
    $colour["6"] = "E201DB"; 
    $colour["7"] = "FFFFFF"; 
    $colour["8"] = "CA7C27"; 
    $colour["9"] = "757575"; 
    $colour["a"] = "EB9F53"; 
    $colour["b"] = "106F59"; 
    $colour["c"] = "5A134F"; 
    $colour["d"] = "035AFF"; 
    $colour["e"] = "681EA7"; 
    $colour["f"] = "5097C1"; 
    $colour["g"] = "BEDAC4"; 
    $colour["h"] = "024D2C"; 
    $colour["i"] = "7D081B"; 
    $colour["j"] = "90243E"; 
    $colour["k"] = "743313"; 
    $colour["l"] = "A7905E"; 
    $colour["m"] = "555C26"; 
    $colour["n"] = "AEAC97"; 
    $colour["o"] = "C0BF7F"; 
    $colour["p"] = "000000"; 
    $colour["q"] = "DA0120"; 
    $colour["r"] = "00B906"; 
    $colour["s"] = "E8FF19"; 
    $colour["t"] = "170BDB"; 
    $colour["u"] = "23C2C6"; 
    $colour["v"] = "E201DB"; 
    $colour["w"] = "FFFFFF"; 
    $colour["x"] = "CA7C27"; 
    $colour["y"] = "757575"; 
    $colour["z"] = "CC8034"; 
    $colour["/"] = "DBDF70"; 
    $colour["*"] = "BBBBBB"; 
    $colour["-"] = "747228"; 
    $colour[" "] = "993400"; 
    $colour["?"] = "670504"; 
    $colour["@"] = "623307"; 

    function  q3ColsReplace($text){ 
        global $colour; 
        $split  =       explode("^",$text); 

        foreach($split as $split){ 
                $col    =       $split[0]; 
                $split  =       substr($split,1); 
                $col    =       $colour[$col]; 
                $colline        =       "<font color=\"#$col\">$split</font>"; 

                $out[]  =       $colline; 
        } 
        $out    =       implode("",$out); 
        return $out; 
}

And that how easy this is.
I noticed that in your class,where you are using remove_color.
You can use


function q3ColRemove($text){  
        $split  =       explode("^",$text);  

        foreach($split as $split){  
                $split  =       substr($split,1);  
                $out[]  =       $split;  
        }  
        $out    =       implode("",$out);  
        return $out;  
}

(Chuck) #36

Yes, but, if I’m not mistaken:

$out[] = $split; // will trigger an e_warning because you didn’t instantiate $out as an array, or as anything really.

And why not do this?


function q3ColRemove($text)
{
    global $colour;

    $split = explode('^' , $text);
    
    $string = '';

    foreach ( $split as $something )
        $string .= substr( $something, 1 );

    return $string;  
}

and I wonder what’s wrong with my version? Look this isn’t a competition, I understand how you did it, I’m just saying ’ http://genert.impact.pri.ee/color.phps ’ is full of errors, so that’s not usefull - but I understand the basics (hence, been scripting for a long time now).

My original question was how others did it and made it work, that’s still my original question.:slight_smile:


(Indloon) #37

[QUOTE=Chuck;377443]Yes, but, if I’m not mistaken:
and I wonder what’s wrong with my version? Look this isn’t a competition, I understand how you did it, I’m just saying ’ http://genert.impact.pri.ee/color.phps ’ is full of errors, so that’s not usefull - but I understand the basics (hence, been scripting for a long time now).

My original question was how others did it and made it work, that’s still my original question.:)[/QUOTE]

The code was made in 2minutes,what do you except:D
Your version works,but its too long!!!:DDDDD
Here are examples to your question!: http://genert.impact.pri.ee/Chuck.phps


(Chuck) #38

Could you explain why my version is too long? And how it could be shortened?

My version might seem long, but really, it isn’t. the $hex_table should be seen as one line (that’s what it is, I just broke it down, so it’s more readable).

And yes, I’ve seen the examples, if I’m not mistaken example #1 is taken from QStats (I do my homework) - I found that quite interesting, but unreadable (to be more accurate, I can read it, I just find it annoying to read) because of the regexs.

The second version will trigger a ‘e_depreciated’ warning on my PHP version (5.3.0) - since, ‘ereg_replace’ is … depreciated as of PHP 5.3.0. … also, why 'array( 0 => ‘value’, 1 => ‘value’); if you do: ‘array(‘value’, ‘value’)’ php will automaticly make it: 0 => ‘value’, 1 => ‘value’, etc.

The third version isn’t complete, but I’m assuming it’s from the example you already show’d me.

It’s a choice of personal preference, of course.

I’d however think that your argument is baseless, that’s like saying ‘eat a candy bar’ to a baby because it takes to long to heat up milk (or whatever that stuff is called).

Anyway, thanks for showing the examples and reminding me of str_replace’s counter… :slight_smile:


(Indloon) #39

Personal choiches are always the best!!!:smiley:
About the first example.
It was taken from phpgstat,not from Qstat.


class et
{
    var $maxlen   = 2048;
    var $write    = "\xFF\xFF\xFF\xFFgetstatus\x00";
    var $s_info   = false;
    var $g_info   = false;
    var $p_info   = false;
    var $response = false;

    function getvalue($srv_value, $srv_data)
    {
        // search the value of selected rule and return it
        $srv_value = array_search ($srv_value, $srv_data);

        if ($srv_value === false)
        {
            return false;
        }
        else
        {
            $srv_value = $srv_data[$srv_value+1];

            return $srv_value;
        }
    }
      
    function splitdata()
    {
        // get rules from stream and write to g_info
	$c_info = explode("
", $this->s_info);
	$this->g_info = explode("\\", $c_info[1]);

        // get players from stream and write to p_info
	$index_old = 0;
	$index_new = 0;
	foreach ($c_info as $value)
	{
	    if ($index_old >= 2)
	    {
	    	$this->p_info[$index_new] = $value;
		$index_new++;
	    }
	    $index_old++;
	}
    }

    function microtime_float()
    {
        list($usec, $sec) = explode(" ", microtime());
        
        return ((float)$usec + (float)$sec);
    }

    function getstream($host, $port, $queryport)
    {   
	// get the infostream from server
        $socket = fsockopen('udp://'. $host, $port, $errno, $errstr, 30);

	if ($socket === false)
	{
            echo "Error: $errno - $errstr<br>
";
        }
	else
	{
            socket_set_timeout($socket, 3);
	    
	    $time_begin = $this->microtime_float();
	    
	    fwrite($socket, $this->write);
	    $this->s_info = fread($socket, $this->maxlen);
	    
	    $time_end = $this->microtime_float();
	}
        fclose($socket);
       
        // response time
	$this->response = $time_end - $time_begin;
	$this->response = ($this->response * 1000);
	$this->response = (int)$this->response;
        
	if ($this->s_info)
	{
	    // sort the infostring
	    $this->splitdata();
	    
	    return true;
	}
	else
	{
	    return false;
	}
    }

    function check_color($text, $switch)
    {
        $clr = array ( // colors
        "\"#000000\"", "\"#DA0120\"", "\"#00B906\"", "\"#E8FF19\"", //  1
        "\"#170BDB\"", "\"#23C2C6\"", "\"#E201DB\"", "\"#FFFFFF\"", //  2
        "\"#CA7C27\"", "\"#757575\"", "\"#EB9F53\"", "\"#106F59\"", //  3
        "\"#5A134F\"", "\"#035AFF\"", "\"#681EA7\"", "\"#5097C1\"", //  4
        "\"#BEDAC4\"", "\"#024D2C\"", "\"#7D081B\"", "\"#90243E\"", //  5
        "\"#743313\"", "\"#A7905E\"", "\"#555C26\"", "\"#AEAC97\"", //  6
        "\"#C0BF7F\"", "\"#000000\"", "\"#DA0120\"", "\"#00B906\"", //  7
        "\"#E8FF19\"", "\"#170BDB\"", "\"#23C2C6\"", "\"#E201DB\"", //  8
        "\"#FFFFFF\"", "\"#CA7C27\"", "\"#757575\"", "\"#CC8034\"", //  9
        "\"#DBDF70\"", "\"#BBBBBB\"", "\"#747228\"", "\"#993400\"", // 10
        "\"#670504\"", "\"#623307\""                                // 11
        );

        if ($switch == 1)
        { // colored string
            $search  = array (
            "/\^0/", "/\^1/", "/\^2/", "/\^3/",        //  1
            "/\^4/", "/\^5/", "/\^6/", "/\^7/",        //  2
            "/\^8/", "/\^9/", "/\^a/", "/\^b/",        //  3
            "/\^c/", "/\^d/", "/\^e/", "/\^f/",        //  4
            "/\^g/", "/\^h/", "/\^i/", "/\^j/",        //  5
            "/\^k/", "/\^l/", "/\^m/", "/\^n/",        //  6
            "/\^o/", "/\^p/", "/\^q/", "/\^r/",        //  7
            "/\^s/", "/\^t/", "/\^u/", "/\^v/",        //  8
            "/\^w/", "/\^x/", "/\^y/", "/\^z/",        //  9
            "/\^\//", "/\^\*/", "/\^\-/", "/\^\+/",    // 10
            "/\^\?/", "/\^\@/", "/\^</", "/\^>/",      // 11
            "/\^\&/", "/\^\)/", "/\^\(/", "/\^[A-Z]/", // 12
            "/\^\_/",                                  // 14
            "/&</", "/^(.*?)<\/font>/"                 // 15
            );

            $replace = array (
            "&<font color=$clr[0]>", "&<font color=$clr[1]>",   //  1
            "&<font color=$clr[2]>", "&<font color=$clr[3]>",   //  2
            "&<font color=$clr[4]>", "&<font color=$clr[5]>",   //  3
            "&<font color=$clr[6]>", "&<font color=$clr[7]>",   //  4
            "&<font color=$clr[8]>", "&<font color=$clr[9]>",   //  5
            "&<font color=$clr[10]>", "&<font color=$clr[11]>", //  6
            "&<font color=$clr[12]>", "&<font color=$clr[13]>", //  7
            "&<font color=$clr[14]>", "&<font color=$clr[15]>", //  8
            "&<font color=$clr[16]>", "&<font color=$clr[17]>", //  9
            "&<font color=$clr[18]>", "&<font color=$clr[19]>", // 10
            "&<font color=$clr[20]>", "&<font color=$clr[21]>", // 11
            "&<font color=$clr[22]>", "&<font color=$clr[23]>", // 12
            "&<font color=$clr[24]>", "&<font color=$clr[25]>", // 13
            "&<font color=$clr[26]>", "&<font color=$clr[27]>", // 14
            "&<font color=$clr[28]>", "&<font color=$clr[29]>", // 15
            "&<font color=$clr[30]>", "&<font color=$clr[31]>", // 16
            "&<font color=$clr[32]>", "&<font color=$clr[33]>", // 17
            "&<font color=$clr[34]>", "&<font color=$clr[35]>", // 18
            "&<font color=$clr[36]>", "&<font color=$clr[37]>", // 19
            "&<font color=$clr[38]>", "&<font color=$clr[39]>", // 20
            "&<font color=$clr[40]>", "&<font color=$clr[41]>", // 21
            "", "", "", "", "", "",                             // 22
            "", "</font><", "\$1"                               // 23
            );

            $ctext = preg_replace($search, $replace, $text);

            if ($ctext != $text)
            {
                $ctext = preg_replace("/$/", "</font>", $ctext);
            }

            return $ctext;
        }
        elseif ($switch == 2)
        { // colored numbers
            if ($text <= 39)
            {
                $ctext = "<font color=$clr[7]>$text</font>";
            }
            elseif ($text <= 69)
            {
                $ctext = "<font color=$clr[5]>$text</font>";
            }
            elseif ($text <= 129)
            {
                $ctext = "<font color=$clr[8]>$text</font>";
            }
            elseif ($text <= 399)
            {
                $ctext = "<font color=$clr[9]>$text</font>";
            }
            else
            {
                $ctext = "<font color=$clr[1]>$text</font>";
            }

            return $ctext;
        }
    }
    
    function getrules($phgdir)
    {
        $srv_rules['sets'] = false;
	
        // response time
	$srv_rules['response'] = $this->response . ' ms';
	
        // et setting pics
        $sets['pb']      = '<img src="' . $phgdir . 'privileges/pb.gif" alt="pb">';
	$sets['ff']      = '<img src="' . $phgdir . 'privileges/ff.gif" alt="ff">';
	$sets['antilag'] = '<img src="' . $phgdir . 'privileges/antilag.gif" alt="antilag">';
	$sets['pass']    = '<img src="' . $phgdir . 'privileges/pass.gif" alt="pw">';
	$sets['balance'] = '<img src="' . $phgdir . 'privileges/balance.gif" alt="balance">';
        
	// get the info strings from server info stream
	$srv_rules['hostname']     = $this->getvalue('sv_hostname',       $this->g_info);
	$srv_rules['gametype']     = $this->getvalue('g_gametype',        $this->g_info);
	$srv_rules['gamename']     = $this->getvalue('gamename',          $this->g_info);
	$srv_rules['version']      = $this->getvalue('version',           $this->g_info);
	$srv_rules['mapname']      = $this->getvalue('mapname',           $this->g_info);
	$srv_rules['maxclients']   = $this->getvalue('sv_maxclients',     $this->g_info);
        $srv_rules['prvclients']   = $this->getvalue('sv_privateClients', $this->g_info);
	$srv_rules['punkbuster']   = $this->getvalue('sv_punkbuster',     $this->g_info);
        $srv_rules['friendlyfire'] = $this->getvalue('g_friendlyFire',    $this->g_info);
	$srv_rules['antilag']      = $this->getvalue('g_antilag',         $this->g_info);
	$srv_rules['teambalance']  = $this->getvalue('g_balancedteams',   $this->g_info);
	$srv_rules['needpass']     = $this->getvalue('g_needpass',        $this->g_info);
        
        // scan the color tags of hostname
        $srv_rules['hostname'] = $this->check_color($srv_rules['hostname'], 1);
			
	
	// cut the long server system version info string 
	$srv_rules['version'] = substr($srv_rules['version'], 3, 4);
        
	// path to map picture and default info picture
	$srv_rules['map_path'] = 'maps/et';
	$srv_rules['map_default'] = 'default.jpg';

	// point system
	$srv_rules['points'] = 'XP';
	
	// if privatclients info string == true, write it to maxclients
	if ($srv_rules['prvclients'])
	{
	    $srv_rules['maxclients'] = $srv_rules['maxclients'] - $srv_rules['prvclients'];
	    $srv_rules['maxplayers'] = $srv_rules['maxclients'] . ' (+' . $srv_rules['prvclients'] . ')';
	}
	else
	{
	    $srv_rules['maxplayers'] = $srv_rules['maxclients'];
	}
	
	// get the connected player
	$srv_rules['nowplayers'] = (count($this->p_info))-1;
	
	// get more detail info about game and modifications
	switch ($srv_rules['gamename'])
	{
	    case 'etmain':
	        $srv_rules['gamename'] = 'Enemy Territory ' . $srv_rules['version'];
                switch ($srv_rules['gametype'])
                {
                    case 2:
                        $srv_rules['gametype'] = 'Objective';
                        break;
                    case 3:
                        $srv_rules['gametype'] = 'Stopwatch';
                        break;
                    case 4:
                        $srv_rules['gametype'] = 'Campaign';
                        break;
                    case 5:
                        $srv_rules['gametype'] = 'LMS';
                        break;
                }
                break;
	    case 'etf':
	        $srv_rules['modver']   = $this->getvalue('g_etfversion', $this->g_info);
		$srv_rules['gamename'] = 'Enemy Territory ' . $srv_rules['version'] . '<br>'
		                       . $srv_rules['modver'];
                switch ($srv_rules['gametype'])
                {
                    case 2:
                        $srv_rules['gametype'] = 'Objective';
                        break;
                    case 3:
                        $srv_rules['gametype'] = 'Stopwatch';
                        break;
                    case 4:
                        $srv_rules['gametype'] = 'Campaign';
                        break;
                    case 5:
                        $srv_rules['gametype'] = 'LMS';
                        break;
                    default:
                         $srv_rules['gametype'] = 'Unknown';
                }
		break;
	    case 'etpro':
	        $srv_rules['modver']   = $this->getvalue('mod_version', $this->g_info);
		$srv_rules['gamename'] = 'Enemy Territory ' . $srv_rules['version'] . '<br>'
	                               . 'ETPro ' . $srv_rules['modver'];
                switch ($srv_rules['gametype'])
                {
                    case 2:
                        $srv_rules['gametype'] = 'Objective';
                        break;
                    case 3:
                        $srv_rules['gametype'] = 'Stopwatch';
                        break;
                    case 4:
                        $srv_rules['gametype'] = 'Campaign';
                        break;
                    case 5:
                        $srv_rules['gametype'] = 'LMS';
                        break;
                    default:
                         $srv_rules['gametype'] = 'Unknown';
                }
	        break;
	    case 'etpub':
	        $srv_rules['modver']   = $this->getvalue('mod_version', $this->g_info);
		$srv_rules['gamename'] = 'Enemy Territory ' . $srv_rules['version'] . '<br>'
		                       . 'ETPub ' . $srv_rules['modver'];
                switch ($srv_rules['gametype'])
                {
                    case 2:
                        $srv_rules['gametype'] = 'Objective';
                        break;
                    case 3:
                        $srv_rules['gametype'] = 'Stopwatch';
                        break;
                    case 4:
                        $srv_rules['gametype'] = 'Campaign';
                        break;
                    case 5:
                        $srv_rules['gametype'] = 'LMS';
                        break;
                    default:
                         $srv_rules['gametype'] = 'Unknown';                
                }
		break;
	    case 'shrubet':
	        $srv_rules['modver']   = $this->getvalue('modversion', $this->g_info);
		$srv_rules['gamename'] = 'Enemy Territory ' . $srv_rules['version'] . '<br>'
		                       . $srv_rules['modver'];
                switch ($srv_rules['gametype'])
                {
                    case 2:
                        $srv_rules['gametype'] = 'Objective';
                        break;
                    case 3:
                        $srv_rules['gametype'] = 'Stopwatch';
                        break;
                    case 4:
                        $srv_rules['gametype'] = 'Campaign';
                        break;
                    case 5:
                        $srv_rules['gametype'] = 'LMS';
                        break;
                    default:
                         $srv_rules['gametype'] = 'Unknown';                
                }
	        break;
	    case 'tcetest':
	        $srv_rules['modver']   = $this->getvalue('tce_version', $this->g_info);
		$srv_rules['modver']   = substr($srv_rules['modver'], 0, 8);
		$srv_rules['gamename'] = 'Enemy Territory ' . $srv_rules['version'] . '<br>'
		                       . 'TC:Elite ' . $srv_rules['modver'];
                switch ($srv_rules['gametype'])
                {
                    case 5:
                        $srv_rules['gametype'] = 'Objective';
                        break;
                    case 7:
                        $srv_rules['gametype'] = 'Bodycount';
                        break;
                    default:
                         $srv_rules['gametype'] = 'Unknown';
                }
		break;
	    case 'headshot_mod':
	        $srv_rules['modver']   = $this->getvalue('modversion', $this->g_info);
		$srv_rules['gamename'] = 'Enemy Territory ' . $srv_rules['version'] . '<br>'
		                       . 'Headshot-Mod ' . $srv_rules['modver'];
                switch ($srv_rules['gametype'])
                {
                    case 2:
                        $srv_rules['gametype'] = 'Objective';
                        break;
                    case 3:
                        $srv_rules['gametype'] = 'Stopwatch';
                        break;
                    case 4:
                        $srv_rules['gametype'] = 'Campaign';
                        break;
                    case 5:
                        $srv_rules['gametype'] = 'LMS';
                        break;
                    default:
                         $srv_rules['gametype'] = 'Unknown';
                }
		break;
            case 'WegeinMod':
	         $srv_rules['gamename'] = 'Enemy Territory ' . $srv_rules['version'] . '<br>'
		                        . $srv_rules['gamename'];
                switch ($srv_rules['gametype'])
                {
                    case 2:
                        $srv_rules['gametype'] = 'Objective';
                        break;
                    case 3:
                        $srv_rules['gametype'] = 'Stopwatch';
                        break;
                    case 4:
                        $srv_rules['gametype'] = 'Campaign';
                        break;
                    case 5:
                        $srv_rules['gametype'] = 'LMS';
                        break;
                    default:
                         $srv_rules['gametype'] = 'Unknown';
                }
		break;
	    case 'domination':
	        $srv_rules['modver']   = $this->getvalue('mod_version', $this->g_info);
	        $srv_rules['gamename'] = 'Enemy Territory ' . $srv_rules['version'] . '<br>'
	                               . 'Domination ' . $srv_rules['modver'];
                switch ($srv_rules['gametype'])
		{
		    case 6:
		         $srv_rules['gametype'] = 'Powerball';
		         break;
		    case 7:
		         $srv_rules['gametype'] = 'Capture The Flag';
			 break;
	            default:
		         $srv_rules['gametype'] = 'Unknown';
		}
	        break;
        }
	
	// et pubkbuster pic
        if ($srv_rules['punkbuster'] == 1)
	{
	    $srv_rules['sets'] .= $sets['pb'];
	}
	// et friendlyfire pic
	if ($srv_rules['friendlyfire'] != 0)
	{
	    $srv_rules['sets'] .= $sets['ff'];
	}
        // et antilag pic 
	if ($srv_rules['antilag'] == 1)
	{
	    $srv_rules['sets'] .= $sets['antilag'];
	}
        // et teambalanced pic
	if ($srv_rules['teambalance'] == 1)
        {
            $srv_rules['sets'] .= $sets['balance'];
        }
        // et needpass pic
	if ($srv_rules['needpass'] == 1)
	{
	    $srv_rules['sets'] .= $sets['pass'];
	}

	if ($srv_rules['sets'] === false)
	{
	    $srv_rules['sets'] = '-';
	}

	// server general info (added by balgo 05.01.2006 only for test)
        $srv_rules['htmlinfo'] =
        '<tr><td align="left">Mapname:</td><td align="left">'
        . $srv_rules['mapname']
        . '</td></tr>' ."
"
        . '<tr><td align="left">Players:</td><td align="left">'
        . $srv_rules['nowplayers'] . ' / ' . $srv_rules['maxplayers']
        . '</td></tr>' . "
"
        . '<tr><td align="left">Response:</td><td align="left">'
        . $srv_rules['response']
        . '</td></tr>' . "
"
        . '<tr><td align="left">Privileges:</td><td align="left">'
        . $srv_rules['sets']
        . '</td></tr>' . "
";

        // server detail info (added by balgo 05.01.2006 only for test)
        $srv_rules['htmldetail'] =
        '<tr valign="top"><td align="left">Gamename:</td><td align="left">'
        . $srv_rules['gamename']
        . '</td></tr>' . "
"
        . '<tr valign="top"><td align="left">Gametype:</td><td align="left">'
        . $srv_rules['gametype']
        . '</td></tr>' . "
"
        . '<tr valign="top"><td align="left">Mapname:</td><td align="left">'
        . $srv_rules['mapname']
        . '</td></tr>' . "
"
        . '<tr valign="top"><td align="left">Players:</td><td align="left">'
        . $srv_rules['nowplayers'] . ' / ' . $srv_rules['maxplayers']
        . '</td></tr>' . "
"
        . '<tr valign="top"><td align="left">Response:</td><td align="left">'
        . $srv_rules['response']
        . '</td></tr>' . "
"
        . '<tr valign="top"><td align="left">Privileges:</td><td align="left">'
        . $srv_rules['sets']
        . '</td></tr>' . "
";

        // return all server rules
	return $srv_rules;	    
    }
      
    function getplayers()
    {
        $players = array();
	$teams = false;
	$ta = 0; 
	$tb = 0;

        // get team variable
        $teamstr = $this->getvalue('P',$this->g_info);
	
	if ($teamstr)
	{
	    // set html thead for playerlist with teams
	    $thead = '<tr><th>Rank</th>' .
	             '<th>Team</th>' .
	             '<th>Name</th>' .
	             '<th>Points</th>' .
	             '<th colspan=2>Ping</th></tr>';
	
	    $index    = 0;
	    $position = strlen($teamstr);
	    
	    while ($index != $position)
	    {
	         if ($teamstr[$index] != '-')
		 {
		     $teams .= $teamstr[$index];
		 }
		 $index++;
            }
        }
	else
	{
	    // set html thead for playerlist without teams
	    $thead = '<tr><th>Rank</th>' .
	             '<th>Name</th>' .
	             '<th>Points</th>' .
	             '<th colspan=2>Ping</th></tr>';
        }
	
        // how many players must search
        $nowplayers = count($this->p_info)-1;
	$nowplayers = $nowplayers - 1;
        $clients = 0;
       
        // get the data of each player and add the team status
        while ($nowplayers != -1)
        {
	    if ($teams)
	    {   
	        switch ($teams[$nowplayers])
		{
		    case 0:
		        $this->p_info[$nowplayers] .= 'Con';
		        break;
		    case 1:
		        $this->p_info[$nowplayers] .= 'Axis';
			break;
		    case 2:
		        $this->p_info[$nowplayers] .= 'Allies';
			break;
		    default:
		        $this->p_info[$nowplayers] .= 'Spec';
	        }
	    }
	    
	    $players[$clients] = $this->p_info[$nowplayers];
	    $nowplayers--;
	    $clients++;
        }
        
	// check the connected players and sort the ranking
	if ($players == false)
	{
	    $thead .= 
	    "<tr align=center><td>--</td>" .
	    "<td>--</td><td>--</td><td colspan=2>--</td></tr>
";
	}
	else
	{
	    sort($players, SORT_NUMERIC);
	}

	// store the html table line to the info array
	$srv_player = $thead;
        
	// manage the player data in the following code
	$index = 1;

	while ($clients)
	{
	     $clients--;
	     
	     list ($cache[$index], $player[$index], $team[$index]) = split ('\"', $players[$clients]);
	     list ($points[$index], $ping[$index]) =  split(' ', $cache[$index]);
              
             $player[$index] = htmlentities($player[$index]);
	     $player[$index] = $this->check_color($player[$index], 1);
	     $ping[$index]   = $this->check_color($ping[$index],   2);
	     
	     if ($teams)
	     {   // table with team data
	         $tdata = "<tr align=center><td>$index.</td>" .
		          "<td>$team[$index]</td>" .
		          "<td>$player[$index]</td>" .
			  "<td>$points[$index]</td>" .
			  "<td colspan=2>$ping[$index]</td></tr>
";
	         
		 // team points
		 if ($team[$index] == 'Allies')
		 {
		     $ta = $ta + $points[$index];
		 }
		 if ($team[$index] == 'Axis')
		 {
		     $tb = $tb + $points[$index];
		 }											 
	     }
	     else
	     {   // table without team data
	         $tdata = "<tr align=center><td>$index.</td>" .
	                  "<td>$player[$index]</td>" .
	                  "<td>$points[$index]</td>" .
	                  "<td colspan=2>$ping[$index]</td></tr>
";
	     }
             
	     $srv_player = $srv_player . $tdata;
	     $index++;
	}

	if ($teams)
	{
	    if ($ta > $tb)
	    {
	        $ta = "<font color=\"cyan\">$ta</font>";
            }
            elseif ($ta == $tb)
	    {
	        $ta = "<font color=\"red\">$ta</font>";
	        $tb = "<font color=\"red\">$tb</font>";
	    }
	    else
	    {
	        $tb = "<font color=\"cyan\">$tb</font>";
            }
	    
	    $srv_player = "<tr><th></th><th></th><th></th><th>Allies</th><th colspan=\"2\">Axis</th></tr>" .
	                  "<tr align=\"center\"><td></td><td></td><td></td><td>$ta</td><td colspan=2>$tb</td></tr>" .
			  $srv_player;
	}
	
        return $srv_player;
    }
}

(Chuck) #40

Yes, I see. Sorry, I must’ve confused the two. :slight_smile: