<?PHP

/* ====================
Land Down Under - Website engine
Copyright Neocrome
http://www.neocrome.net

[BEGIN_LDU]
File=plugins/extended/bfvstats/bfvstats.php
Version=800
Updated=10-01-2006
Type=Plugin
Author=Joeke
Description=BattleField Vietnam Server stats
[END_LDU]

[BEGIN_LDU_EXTPLUGIN]
Code=bfvstats
Part=bfvstats
File=bfvstats
Hooks=standalone
Tags=
Minlevel=0
Order=10
[END_LDU_EXTPLUGIN]

==================== */

function parseInfo($info) { 
$info=str_replace (chr(0x00).chr(0x00),chr(0x00)." ".chr(0x00),$info);
    $retval = array(); 
    if ($info != '') { 
        $key = strtok($info, chr(0x00)); 
        while ($key) { 
            $val = strtok(chr(0x00)); 
            if ($val !== false){ 
            if(empty($val)){ 
                        $retval[$key] = "0"; 
                        }else{ 
                        $retval[$key] = ucfirst($val); 
                        } 
            } 
            else 
                        { 
                        break; 
                        } 
            $key = strtok(chr (0x00)); 
        } 
    } 
    return $retval; 
}

function parsePlayers($players){
$retval = array(); 
$players=substr ($players, strpos ($players, "kills_".chr(0x00).chr(0x00))+8);
$players=substr ($players,0,strpos ($players, chr(0x00).chr(0x00)));
$val = strtok($players, chr(0x00));
$i=0;
while ($val) {
	$retval['player_'.$i]=$val;
	$val = strtok(chr(0x00));
	$retval['score_'.$i]=$val;
	$val = strtok(chr(0x00)); 
	$retval['deaths_'.$i]=$val;
	$val = strtok(chr(0x00)); 
	$retval['ping_'.$i]=$val;
	$val = strtok(chr(0x00));
	$retval['team_'.$i]=$val;
	$val = strtok(chr(0x00));
	$retval['kills_'.$i]=$val;
	$val = strtok(chr(0x00));
	$i++;
}
return $retval;
}

function parseTeams($teams){
$retval = array(); 
$teams=strrev($teams);
$teams=strrev(substr ($teams,0,strpos ($teams, chr(0x00).chr(0x00))));
if ($teams != '') { 
	$key = strtok($teams, chr(0x00)); 
	while ($key) { 
        	$val = strtok(chr(0x00)); 
        	if ($val !== false){ 
        		if(empty($val)){ 
                     		$retval[$key] = "0"; 
                        	}
			else{ 
                        	$retval[$key] = $val; 
              		} 
       		} 
            	else{ 
                	break; 
              	} 
	$key = strtok(chr (0x00)); 
        } 
    	} 
return $retval; 
}

function get_team($team){ 
   $teams = array("","red","blue",""); 
   if ($team < 4) 
    { 
    $out = $teams[$team]; 
    } 
return $out; 
} 

$queryport = $cfg['plugin']['bfvstats']['queryport'];
$queryip   = $cfg['plugin']['bfvstats']['queryip'];
$sock = fsockopen("udp://".$queryip, $queryport);
if (!$sock) { 
  exit("<strong>No socket error</strong><br />Message:".$errstr."<br />Number:".$errno."<br />IP:".$queryip."<br />Port:".$queryport."<br />Go to <a href=http://www.neocrome.net/forums.php?m=topics&s=4>here</a> and get some help.");
}
fputs($sock, chr(0xFE).chr(0xFD).chr(0x00).chr(0x04).chr(0x05).chr(0x06).chr(0x07).chr(0xFF).chr(0xFF).chr(0xFF));
fread($sock, 4);
$status = socket_get_status($sock);
if ($status["unread_bytes"] == 0)
	{
	exit($cfg['plugin']['bfvstats']['offline']);
	}
$data = "";
$starttime = Time(); 
do
	{
	$data .= fread($sock, 1);
	$status = socket_get_status($sock);
	if ((Time() - $starttime) > 5) { 
	exit($cfg['plugin']['tostats']['offline']);
  	} 
	}
	while ($status["unread_bytes"] != 0);
fclose($sock);

$stats = parseInfo(substr ($data, strpos ($data, "hostname"), strpos ($data, chr(0x00).chr(0x00).chr(0x00))));
$players = parsePlayers($data);
$teams = parseTeams($data);
 
if (file_exists("plugins/extended/bfvstats/images/".str_replace(" ","_",$stats['mapname']).".jpg"))
	{
		$mappic = str_replace(" ","_",$stats['mapname']).".jpg";
	}
else if (file_exists("plugins/extended/bfvstats/images/".strtolower(str_replace(" ","_",$stats['mapname'])).".jpg"))
	{
		$mappic = strtolower(str_replace(" ","_",$stats['mapname'])).".jpg";
	}
else 
	{
		$mappic = "custom.gif";
	}

if ($stats['numplayers'] !=0)
	{
	for($i = 0;!empty($players['player_'.$i]);$i++){ 
		$t-> assign(array(
			"BFVSTATS_PLAYER_TEAM" => get_team($players['team_'.$i]),
			"BFVSTATS_PLAYER_NAME" => $players['player_'.$i],
			"BFVSTATS_PLAYER_SCORE" => round($players['score_'.$i]),
			"BFVSTATS_PLAYER_PING" => $players['ping_'.$i],
			"BFVSTATS_PLAYER_DEATHS" => round($players['deaths_'.$i]),
			"BFVSTATS_PLAYER_KILLS" => round($players['kills_'.$i]),
			));
		$t->parse("MAIN.BFVSTATS_PLAYERS");
		} 
	}
	else
	{
		$t->parse("MAIN.BFVSTATS_PLAYERS_EMPTY");
	}

$t-> assign(array(
"BFVSTATS_ACTIVE_MODS" => $stats['active_mods'],
"BFVSTATS_ALLOW_NOSE_CAM" => $stats['allow_nose_cam'],
"BFVSTATS_AUTO_BALANCE_TEAMS" => $stats['auto_balance_teams'],
"BFVSTATS_BANDWIDTH_CHOKE_LIMIT" => $stats['bandwidth_choke_limit'],
"BFVSTATS_BOT_SKILL" => $stats['bot_skill'],
"BFVSTATS_CPU" => $stats['cpu'],
"BFVSTATS_DEDICATED" => $stats['dedicated'],
"BFVSTATS_EXTERNAL_VIEW" => $stats['external_view'],
"BFVSTATS_FREE_CAMERA" => $stats['free_camera'],
"BFVSTATS_GAME_ID" => $stats['game_id'],
"BFVSTATS_GAME_ID_NAME" => $stats['game_id_name'],
"BFVSTATS_GAME_START_DELAY" => $stats['game_start_delay'],
"BFVSTATS_GAMEMODE" => $stats['gamemode'],
"BFVSTATS_GAMESTYLE" => $stats['gamestyle'],
"BFVSTATS_GAMETYPE" => $stats['gametype'],
"BFVSTATS_GAMEVER" => $stats['gamever'],
"BFVSTATS_HOSTIP" => $queryip,
"BFVSTATS_HOSTNAME" => $stats['hostname'],
"BFVSTATS_HOSTPORT" => $stats['hostport'],
"BFVSTATS_KICKBACK" => $stats['kickback'],
"BFVSTATS_KICKBACK_ON_SPLASH" => $stats['kickback_on_splash'],
"BFVSTATS_MAP_ID" => $stats['map_id'],
"BFVSTATS_MAPNAME" => ucwords(strtolower($stats['mapname'])),
"BFVSTATS_MAPPIC" => $mappic,
"BFVSTATS_MAXPLAYERS" => $stats['maxplayers'],
"BFVSTATS_NAME_TAG_DISTANCE" => $stats['name_tag_distance'],
"BFVSTATS_NAME_TAG_DISTANCE_SCOPE" => $stats['name_tag_distance_scope'],
"BFVSTATS_NUMBER_OF_ROUNDS" => $stats['number_of_rounds'],
"BFVSTATS_NUMPLAYERS" => $stats['numplayers'],
"BFVSTATS_NVA_TEAM_RATIO" => $stats['nva_team_ratio'],
"BFVSTATS_PASSWORD" => $stats['password'],
"BFVSTATS_PUNKBUSTER" => $stats['sv_punkbuster'],
"BFVSTATS_RESERVEDSLOTS" => $stats['reserverdslots'],
"BFVSTATS_SOLDIER_FRIENDLY_FIRE" => $stats['soldier_friendly_fire'],
"BFVSTATS_SPAWN_DELAY" => $stats['spawn_delay'],
"BFVSTATS_SPAWN_WAVE_TIME" => $stats['spawn_wave_time'],
"BFVSTATS_SPECTATORSALLOWED" => $stats['spectatorsallowed'],
"BFVSTATS_SPECTATORSWITCHTIME" => $stats['spectatorswitchtime'],
"BFVSTATS_SPECTATORVOTING" => $stats['spectatorvoting'],
"BFVSTATS_STATUS" => $stats['status'],
"BFVSTATS_TEAM_RED_SCORE" => $teams['red'],
"BFVSTATS_TEAM_BLUE_SCORE" => $teams['blue'],
"BFVSTATS_TICKET_RATIO" => $stats['ticket_ratio'],
"BFVSTATS_TIMELIMIT" => $stats['timelimit'],
"BFVSTATS_TITLE" => $cfg['plugin']['bfvstats']['pagetitle'],
"BFVSTATS_US_TEAM_RATIO" => $stats['us_team_ratio'],
"BFVSTATS_VEHICLE_FRIENDLY_FIRE" => $stats['vehicle_friendly_fire'],
"BFVSTATS_VEHICLE_FRIENDLY_FIRE_ON_SPLASH" => $stats['vehicle_friendly_fire_on_splash'],
))
?>
