Forum Moderators: coopster
Can anyone spot what the problem is?
// don't timeout!
set_time_limit(0);$fp = fsockopen($server, $port, &$errno, &$errstr) or die("$errno: $errstr");
if(!$fp)
{
echo "Unable to open MK server connection";
}
else
{
fputs($fp, "GAMELST\n");
socket_set_timeout($fp, 2);
$res='';
while (!feof($fp))
{
$res=$res.fread($fp, 1024);
}
//save game info to file
$gamefile = fopen("$date/games.txt", "w");
fwrite($gamefile,$res);
fclose($gamefile);
}
This is the complete script:
(not sure why, but these code tags did not retain my indents)
<HTML>
<HEAD>
<TITLE>Test</TITLE>
</HEAD><BODY>
<h1>MK Games Results (MK server)</h1><BR><BR>
<?php
$server = "metalknights.com";
$port = 3333;
//set data directory, all game info files written to directory named by date.
$date = date ('jmYHi');
$date = "MK_$date";
mkdir ("$date", 0777);
// don't timeout!
set_time_limit(0);
$fp = fsockopen($server, $port, &$errno, &$errstr) or die("$errno: $errstr");
if(!$fp)
{
echo "Unable to open MK server connection";
}
else
{
fputs($fp, "GAMELST\n");
# $start = time();
socket_set_timeout($fp, 2);
// $res = fread($fp, 30000);
$res='';
while (!feof($fp)) {
$res=$res.fread($fp, 1024);
}
//save game info to file
$gamefile = fopen("$date/games.txt", "w");
fwrite($gamefile,$res);
fclose($gamefile);
// read game list file
$games= file("$date/games.txt", 'r');
$num_games = count($games);
echo "total number of games : $num_games<br>";
for ($i=0; $i<$num_games; $i++)
{
$line = explode (",", $games[$i]);
//display won games
if ($line[3]=="Game WON!")
{
# echo "<table><tr><td width=200>$line[0]</td><tr></table>";
//get game details
fputs($fp, "GAMEDET $line[0]\n");
#$start = time();//maybe don't need this line
socket_set_timeout($fp, 2);//maybe don't need this line
$detres = fread($fp, 700);
//save game details to file
$gamedetsfile = fopen("$date/game_".$line[0].".txt", "w");
fwrite($gamedetsfile,$detres);
fclose($gamedetsfile);
}
}
}
//close socket connection to server
fclose($fp);
// read game list file
$games= file("$date/games.txt", 'r');//maybe not needed as already read file
$num_games = count($games);//ditto
for ($i=0; $i<$num_games; $i++)
{
$line = explode (",", $games[$i]);
//display won games
if ($line[3]=="Game WON!")
{
$game_details= file ("$date/game_".$line[0].".txt", "r");
echo "<b><font color=blue size=4><br>$game_details[1] : </b></font>";
echo "A $game_details[2] game played on the map \"$game_details[3]\". This game was a $game_details[5] and finished $game_details[4] .<br>";
For ($p=9; $p<19; $p++)
{
$deadguys= explode (",", $game_details[$p]);
for ($d=0; $d<2; $d++){
if ($deadguys[1]){
echo "$deadguys[$d] <br>";
}
}
}
}
}
?>
</BODY>
</HTML>