Help adding to a PHP script.

Im now hosting a script for an online game. It’s a rank script to show the rank of entire guilds by polling the online game’s individual player rank. I spent some time getting it working, and trobleshooting everything.

It’s online at http://merciless.pcriot.com

What I want to do now is add a top 5 players list, that will show the top 5 players from ALL of the sub groups. As you can tell from the code, there is 4 sub groups. I would also like to switch Katar and Cariae on the site… So that Cariae comes before Katar.

Here’s the code.

[code]<?
$output = “/home/merciles/public_html/index.html”;

function FetchRanks($url) {
$filepointer = fopen($url,“r”);

if($filepointer){
while(!feof($filepointer)){
$buffer = fgets($filepointer, 4096);
$file .= $buffer;
}

fclose($filepointer);

} else {
die(“Could not create a connection to the Last Chaos website.”);
}

$pattern = “/

(.) (.) (.) (.) /iU”;

preg_match_all($pattern, $file, $players, PREG_SET_ORDER);

$guilds = array();
foreach ($players as $player) {

//Catch guidless
$player[4] == '' ? $player[4] = "(guildless)" : 0;

//Top 10.
$player[1] <= 10 ? $tmptop = $player[3] : $tmptop = 0;
$player[1] <= 10 ? $tmpin = 1 : $tmpin = 0;

//9x or 10x
$player[3] >= 90 && $player[3] <= 99 ? $tmp9x = 1 : $tmp9x = 0;
$player[3] >= 100 ? $tmp10x = 1 : $tmp10x = 0;

//9x & 10x totals
$player[3] >= 90 && $player[3] <= 99 ? $tmp9xtot = $player[3] : $tmp9xtot = 0;
$player[3] >= 100 ? $tmp10xtot = $player[3] : $tmp10xtot = 0;

//New guild, add to array
if ($player[4] != '' && !array_key_exists($player[4], $guilds)) {
	$guilds[$player[4]] = array("top50" => $player[3], "top10" => $tmptop, "in50" => 1, "in10" => $tmpin, "9x" => $tmp9x, "10x" => $tmp10x, "9xtot" => $tmp9xtot, "10xtot" => $tmp10xtot);

//Guild already in array, add player stats to guild array
} elseif ($player[4] != '') {
	$guilds[$player[4]]['top50'] = $guilds[$player[4]]['top50'] + $player[3];
	$guilds[$player[4]]['in50'] = $guilds[$player[4]]['in50'] + 1;
	
	$guilds[$player[4]]['top10'] = $guilds[$player[4]]['top10'] + $tmptop;
	$guilds[$player[4]]['in10'] = $guilds[$player[4]]['in10'] + $tmpin;
	
	$guilds[$player[4]]['9x'] = $guilds[$player[4]]['9x'] + $tmp9x;
	$guilds[$player[4]]['9xtot'] = $guilds[$player[4]]['9xtot'] + $tmp9xtot;
	
	$guilds[$player[4]]['10x'] = $guilds[$player[4]]['10x'] + $tmp10x;
	$guilds[$player[4]]['10xtot'] = $guilds[$player[4]]['10xtot'] + $tmp10xtot;
	
}

}

foreach ($guilds as $key => $row) {
$row[‘9xtot’] != 0 ? ($guilds[$key][‘avg9x’] = round($row[‘9xtot’] / $row[‘9x’])) : 0;
$row[‘10xtot’] != 0 ? ($guilds[$key][‘avg10x’] = round($row[‘10xtot’] / $row[‘10x’])) : 0;
$guilds[$key][‘pt50’] = round(($row[‘in50’] / 300) * 100, 2);
$guilds[$key][‘pt10’] = round(($row[‘in10’] / 60) * 100, 2);
}

foreach ($guilds as $key => $row) {
$top50[$key] = $row[‘top50’];
$top10[$key] = $row[‘top10’];
}

array_multisort($top50, SORT_DESC, $top10, SORT_DESC, $guilds);

//Build table
$pos = 0;
$rvalue = ‘’;
foreach ($guilds as $guild => $data) {

if ($guild != '(guildless)') {
	$pos++;

	$rvalue = $rvalue."<tr>n";
	$rvalue = $rvalue."<td>{$pos}</td>n";
    $rvalue = $rvalue."<td class="guildtd">{$guild}</td>n";
	$rvalue = $rvalue."<td>{$data['top50']}<br /><font>Players: {$data['in50']} ({$data['pt50']}%)</font></td>n";
	$data['top10'] <= 0 ? $rvalue = $rvalue."<td>-</td>n" : $rvalue = $rvalue."<td>{$data['top10']}<br /><font>Players: {$data['in10']} ({$data['pt10']}%)</font></td>n";
    $data['9x'] <= 0 ? $rvalue = $rvalue."<td>-</td>n" : $rvalue = $rvalue."<td>{$data['9x']}<br /><font>Average: {$data['avg9x']}</font></td>n";
	$data['10x'] <= 0 ? $rvalue = $rvalue."<td>-</td>n" : $rvalue = $rvalue."<td>{$data['10x']}<br /><font>Average: {$data['avg10x']}</font></td>n";
	$rvalue = $rvalue."</tr>n";			
}

}

if (array_key_exists(’(guildless)’, $guilds)) {
$rvalue = $rvalue.“

n”;
$rvalue = $rvalue." n";
$rvalue = $rvalue."(guildless)n";
$rvalue = $rvalue."{$guilds[’(guildless)’][‘top50’]}
Players: {$guilds[’(guildless)’][‘in50’]} ({$guilds[’(guildless)’][‘pt50’]}%)n";
$guilds[’(guildless)’][‘top10’] <= 0 ? $rvalue = $rvalue."-n" : $rvalue = $rvalue."{$guilds[’(guildless)’][‘top10’]}
Players: {$guilds[’(guildless)’][‘in10’]} ({$guilds[’(guildless)’][‘pt10’]}%)n";
$guilds[’(guildless)’][‘9x’] <= 0 ? $rvalue = $rvalue."-n" : $rvalue = $rvalue."{$guilds[’(guildless)’][‘9x’]}
Average: {$guilds[’(guildless)’][‘avg9x’]}n";
$guilds[’(guildless)’][‘10x’] <= 0 ? $rvalue = $rvalue."-n" : $rvalue = $rvalue."{$guilds[’(guildless)’][‘10x’]}
Average: {$guilds[’(guildless)’][‘avg10x’]}n";
$rvalue = $rvalue.“n”;
}
return $rvalue;
}

$header = <<<HEAD

Unofficial Last Chaos USA Guild Rankings
Guild Rankings Guild rank is determined by added level of top guild members. lvl 90 + lvl 90 = 180pts
Server:  Katar  |  Cariae  |  Sarissa  |  Hatzring
HEAD;

$servhead = <<<SERVHEAD

*NAME*

Updated: *DATE*

SERVHEAD;

$footer = “n”;

$katar = str_replace(array(“NAME”, “DATE”), array(“Katar”, date(“Y-m-d H:i:s T”)), $servhead).FetchRanks(“http://lastchaos.aeriagames.com/community/rankings")."

  Guild Top 50 Top 10 Lvl. 9x Lvl. 10x

n”;

$cariae = str_replace(array(“NAME”, “DATE”), array(“Cariae”, date(“Y-m-d H:i:s T”)), $servhead).FetchRanks(“http://lastchaos.aeriagames.com/community/rankings/active/Cariae")."
n”;

$sarissa = str_replace(array(“NAME”, “DATE”), array(“Sarissa”, date(“Y-m-d H:i:s T”)), $servhead).FetchRanks(“http://lastchaos.aeriagames.com/community/rankings/active/Sarissa")."
n”;

$hatzring = str_replace(array(“NAME”, “DATE”), array(“Hatzring”, date(“Y-m-d H:i:s T”)), $servhead).FetchRanks(“http://lastchaos.aeriagames.com/community/rankings/active/Hatzring")."
n”;

$handle = fopen($output, ‘w’);

fwrite($handle, $header.$katar.$cariae.$sarissa.$hatzring.$footer);
fclose($handle);

?>[/code]

To fill out a top-5, you can use an array to keep the highest levels tracked:

$highestplayers = array();
$levelcap = 0;
foreach ($allplayers as $value) {
  if (count($highestplayers) < 5) {
    $highestplayers[] = $value;
    if ($levelcap > $value['level']) { $levelcap = $value['level']; }
  } else {
    if ($value['level'] > $levelcap) {
      $highestplayers[4] = $value;
      sort($highestplayers);
    }
  }
}

This should give you a list with the highest players in the list of players. Look up the sort() function as I’m not 100% sure if it does what should be done there.

Sponsor our Newsletter | Privacy Policy | Terms of Service