<?php
// GAR STAT FILE NAME
$statfname = "playerStats.txt";
ob_start(); 
?>
<HTML>
<HEAD>
<TITLE>Galactic Arms Race (GAR) - Offical Server Ranking</TITLE>
</HEAD>
<BODY>
<h1><center>Galactic Arms Race (GAR)</h1></center><h3><center>GAR Official 32-player Server Stats</center></h3></center>

<?php

$filename = $statfname;

if (file_exists($filename)) 
{
    echo "<center>last updated on: " . date ("F d Y H:i:s.", filemtime($filename));
	echo "</center>";
}
?><?php
function microtime_float()
{
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}

$row = 1;
$time_start = microtime_float();
error_reporting(E_ALL); ini_set('display_errors', 1);
$h = fopen("$statfname", 'r');
$players = array();


//-------------------------------------
// FormatFieldName()
//-------------------------------------
function formatFieldName($field) 
{
    $parts = array();
    $p = &$parts[];
    $flen = strlen($field);
    for ($i = 0; $i < $flen; ++$i) {
        if ($field[$i] == strtoupper($field[$i])) $p = &$parts[];
        $p .= $field[$i];
    }
    $formatted = ucfirst(implode(' ', $parts));
    return $formatted;
}

while (!feof($h)) 
{
  $l = fgets($h);
  $bits = explode(' ', $l);
  $id = $bits[0];
  $bitsLen = count($bits);
  for ($i=1; $i<$bitsLen; ++$i) 
  {
    $attr = substr($bits[$i], 0, -1);
    $players[$id][$attr] = (int) $bits[++$i];
  }
}
fclose($h);


//-------------------------------------
// Compare()
//-------------------------------------
function compare($x, $y) 
{
  if ($x == $y) return 0;
  elseif ($x > $y) return 1;
  else return -1;
}


//-------------------------------------
// SortPlayerBy()
//-------------------------------------
function sortPlayersBy($field) 
{
  return create_function('$x,$y', 'return compare($y["'.$field.'"], $x["'.$field.'"]);');
}

// at this point, $players should be set but not yet sorted

$fields = array();

if (!empty($players)) {
    $fields = array_keys($players[key($players)]);
    if (!isset($_GET['sort']) || !in_array($_GET['sort'], $fields)) {
        $sortField = 'deaths'; // this is the default field to sort by
    } else {
        $sortField = $_GET['sort'];
    }
    uasort($players, sortPlayersBy($sortField));
}

// we can now use both $fields as $sortField in our display as well
$rank = 1;
echo '<pre>';
// var_dump($players);
?>
<table cellpadding="5" cellspacing="0" border="1">
    <thead>
	<th>Rank</th>
        <th>Username</th>
    <?php foreach ($fields as $fieldName): ?>
        <th><a href="http://gar.eecs.ucf.edu/stats/garStats.php?sort=<?php echo $fieldName; ?>">Sort by: <?php $fieldName = formatFieldName("$fieldName"); echo $fieldName; ?></a></th>
    <?php endforeach; ?>
    </thead>
    <tbody>
    <?php foreach ($players as $pName => $pAttrs): ?>
        <tr <?php if($row == 1){ $row = 2; echo "BGCOLOR=\"#FFFFFF\""; }ELSE{$row = 1; echo "BGCOLOR=\"#e6e6e6\"";} ?> >
            <td><?php echo $rank; $rank++;?></td><td><?php echo $pName; ?></td>
        <?php foreach ($pAttrs as $pAttrV): ?>
            <td><?php echo $pAttrV; ?></td>
        <?php endforeach; ?>
	        </tr>
	<?php endforeach; ?>
    </tbody>
</table>
<?php 
$time_end = microtime_float();
$time = $time_end - $time_start;
echo "$time seconds to sort";
?>

Sorting Code / Table / Half the HTML by: erisco@brisco.kent.net | freenode/##php 
Everything Else by: [Force5] JRWR jrwr@nerdshack.com 
</BODY>
</HEAD>
<?php ob_end_flush(); ?>