The online racing simulator
Stats parser question
(17 posts, started )
Stats parser question
Hey guys,

I'm trying to make a piece of php code to make making the stats for this thread easier:
http://www.1stracing.nl/forum/viewtopic.php?t=100

As you can see in the thread above, I need Wins, 2nd's, 3rd's and Finished races for all our teammembers. The Excel sheet creates the rest of the stats (percentages and etc.). At the moment I'm just clicking my way through LFSW Online Racer Stats, but I think it takes way too much time

So that's where my very first php project started (see code on the bottom). Output should look like this (e.g.):
TraXXion 906 200 170 138
Scoop 685 106 123 82
Chriskart 1221 296 207 150
etc. etc.

But from the code below I get:
TraXXion 906 200 170 138
Scoop 906 200 170 138
Chriskart 906 200 170 138
etc. etc.

In other words, everyone gets my stats (the first requested) instead of their own. Would anybody know what I'm doing wrong here?


<?php
echo "<table class='moduletable' bgcolor='#EEEEEE' border='1' cellpadding='1' cellspacing='0' style='border-collapse: collapse' style='font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px'>";
$Titles = array("Meters","Fuel","Laps","Hosts joined","Wins","Second","Third","Finished","Quals","Pole","Online credits","Drags","Drag wins","Online","Hostname");
$i=0; $ii=0; $cnt=0;
$data = array();
$TeamMembers = array("traxxion","Scoop","Chriskart","kartboy","Geforcy","Thommm","auch_enne","AppiePils","Kenneth [NOR]","kaspur","Darkone55","XRRoy","Priitmek","lastpunisher");
$InGameName = array("TraXXion","Scoop","Chriskart","Kartboy","Geforcy","Thomm","SMOKE","Appie","Kenneth","Kaspur","Darkone","XRRoy","Priitmek","lastpunisher");
$TotalMembers = count($TeamMembers);
while ($cnt < $TotalMembers)
{
@$handle = fopen ("[URL]http://lfsworld.net/pubstat/get_stat2.php?action=pst&version=1.2&racer=$TeamMembers[/URL][$cnt]", "r");
while ($userinfo = @fscanf($handle, "%s"))
{
list ($data[$i]) = $userinfo;
if($i>3)
{
if($i<7)
{
$Podium[$ii]=$data[$i];
$ii++;
}
}
$i++;
}

$podiumplaces = $Podium[0] + $Podium[1] + $Podium[2];
echo "<tr><td>$InGameName[$cnt]</td><td>$data[7]</td><td>$Podium[0]</td><td>$Podium[1]</td><td>$Podium[2]</td></tr>";

@fclose($handle);
$cnt++;
sleep(10);
}
echo "</table>";
?>

Thanks in advance for any help,

René

PS They are monthly stats so I will only run the parser once every month. It's not like I'm going to get all this data (and use LFSW bandwidth) every day.
You're always using $Podium[0], $Podium[1] and $Podium[2] but you're assigning past that since $ii increments without bound. Since you're the first person whose stats are read, everyone ends up getting your stats.
Quote from traxxion :PS They are monthly stats so I will only run the parser once every month. It's not like I'm going to get all this data (and use LFSW bandwidth) every day.

Yeah, that's not a lot of information really, that file your pulling is only like 2Kb per member.


<?php 
<table class="moduletable" bgcolor="#EEEEEE" border="1" cellpadding="1" cellspacing="0" style="border-collapse: collapse" style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px">
    <
tbody>
php
    $PName 
= array("traxxion","Scoop","Chriskart","kartboy","Geforcy","Thommm","auch_enne","AppiePils","Kenneth [NOR]","kaspur","Darkone55","XRRoy","Priitmek","lastpunisher");
    
$UName = array("TraXXion","Scoop","Chriskart","Kartboy","Geforcy","Thomm","SMOKE","Appie","Kenneth","Kaspur","Darkone","XRRoy","Priitmek","lastpunisher");
    foreach (
$UName as $i => $LFSName)
    {
        
$info file("http://lfsworld.net/pubstat/get_stat2.php?action=pst&version=1.2&racer={$LFSName}");
        echo 
"\t\t<tr>\n";
        echo 
"\t\t\t<td>{$PName[$i]}</td>\n";
        echo 
"\t\t\t<td>{$info[7]}</td>\n";
        echo 
"\t\t\t<td>{$info[4]}</td>\n";
        echo 
"\t\t\t<td>{$info[5]}</td>\n";
        echo 
"\t\t\t<td>{$info[6]}</td>\n";
        echo 
"\t\t</tr>\n";
        
sleep(5);
    }

    </
tbody>
</
table>
?>

That should do what you asked. This should do what you want.


<?php 
<html>
    <
head>
        <
title>1st Community Stats</title>
        <
style type="text/css">
            <!-- 
Place CSS Style Here -->
        </
style>
    </
head>
    <
body>
        <
table>
            <
tbody>
                <
tr>
                    <
th>UName</th>
                    <
th>Finished</th>
                    <
th>Podium</th>
                    <
th>Win</th>
                    <
th>Second</th>
                    <
th>Third</th>
                    <
th>Worse</th>
                </
tr>
php
    $UName 
= array("traxxion","Scoop","Chriskart","kartboy","Geforcy","Thommm","auch_enne","AppiePils","Kenneth [NOR]","kaspur","Darkone55","XRRoy","Priitmek","lastpunisher");
    
$PName = array("TraXXion","Scoop","Chriskart","Kartboy","Geforcy","Thomm","SMOKE","Appie","Kenneth","Kaspur","Darkone","XRRoy","Priitmek","lastpunisher");
    foreach (
$UName as $Id => $LFSName)
    {
        
$LFSName str_replace(' ''%20'$LFSName);
        
$data file("http://lfsworld.net/pubstat/get_stat2.php?action=pst&version=1.2&racer={$LFSName}");
        
$info[$Id] = array(
            
'Finished' => number_format($data[7], 0'.'''),
            
'Podium' => number_format((($data[4] + $data[5] + $data[6]) / $data[7]) * 1002'.'''),
            
'Win' => number_format((($data[4]) / $data[7]) * 1002'.'''),
            
'Second' => number_format((($data[5]) / $data[7]) * 1002'.'''),
            
'Third' => number_format((($data[6]) / $data[7]) * 1002'.'''),
            
'Worse' => number_format((($data[7] - ($data[4] + $data[5] + $data[6])) / $data[7]) * 1002'.''')
        );
        
$sort[$Id] = number_format((($data[4] + $data[5] + $data[6]) / $data[7]) * 1002'.''');
        
sleep(5);
    }
    
arsort($sort);
    foreach (
$sort as $Id => $Podium)
    {
        echo 
"\t\t\t\t<tr>\n";
        echo 
"\t\t\t\t\t<td>{$PName[$Id]}</td>\n";
        echo 
"\t\t\t\t\t<td>{$info[$Id]['Finished']}</td>\n";
        echo 
"\t\t\t\t\t<td>{$info[$Id]['Podium']}%</td>\n";
        echo 
"\t\t\t\t\t<td>{$info[$Id]['Win']}%</td>\n";
        echo 
"\t\t\t\t\t<td>{$info[$Id]['Second']}%</td>\n";
        echo 
"\t\t\t\t\t<td>{$info[$Id]['Third']}%</td>\n";
        echo 
"\t\t\t\t\t<td>{$info[$Id]['Worse']}%</td>\n";
        echo 
"\t\t\t\t</tr>\n";
    }

            </
tbody>
        </
table>
    </
body>
</
html>
?>

Rheiser, thanks for that, that made my code work indeed

Dygear, awesome... I love how the whole list is sorted by podium finishes as well! Damnit, my php is a joke

May I ask you how to get something similar to the "updating content" message on one of your websites?

Thanks a lot anyway!!
That's AJAX. I've used the Dojo toolkit on my website has it saves some bandwidth, and it just looks cool. If you want it, I'm sure I could zip you up the package I use for TheNZ.org or something. As for your code, man it is very good code, it's just that I've been around PHP for quite along time now and I've had a change to look at more function. The idea is sound, and the execution worked, it's just that there will allways be someone better then your self. Like filur for me, man he's a bloody good prorammer. I simply don't have the skill or the time to learn how to program the code he has made with PHP for InSim. Speaking of filur, I now use serialized and unserialize on SimFIA for the lfs world stats cache files (for each user on the website, so that's alot of data). That speed up the stat's reading process by a wooping 700%!

A good idea when starting a project in PHP, is to look around at the functions you'll be using. Use the PHP manual it is hands down the best programming manuals and function lists out there for any programming language. You might just find one that will do all of the things you need in one step, and that will make it run faster. For me that was the file function, but I could of used file_get_contents but seeing as I wanted it in a array the file function did the job, and did it faster as I did not have to call the explode function in php that would of slowed down the program as a whole. When you can call a function that does it all in one, use it. This way the program will ran faster, and your server will be happier has it can take a harder hit. Programming for speed in PHP is kinda of an oxymoron tho.
Updated : Now Caches for 1 day. (Thanks filur).

<?php 
php
    
function dispName($str)
    {    
// Take you filur for the function.
        
$array_colors = array(
            
'0' => 'black',
            
'1' => 'red',
            
'2' => 'lime',
            
'3' => 'yellow',
            
'4' => 'blue',
            
'5' => 'purple',
            
'6' => 'cyan',
            
'7' => 'white',
            
'8' => 'gray',
            
'9' => 'lime'
        
);
        if (
strlen($str) == 1)
            return 
$array_colors[$str];
        return 
preg_replace("/(\^([0-9]))([^\^]+)/e""'<font color=\"'.dispName(\\2).'\">\\3</font>'"$str);
    }
    
//cache life in seconds
    
define("CACHE_TTL"86400);
    
//cache output
    
define("CACHE_FILE""itek_cache.html");
    
//use cache or not?
    
if (file_exists(CACHE_FILE) && filemtime(CACHE_FILE) + CACHE_TTL time())
        exit(
file_get_contents(CACHE_FILE));
    
ob_start();

<
html>
    <
head>
        <
title>ITEK Community Stats</title>
        <
style type="text/css">
            <!-- 
Place CSS Style Here -->
        </
style>
    </
head>
    <
body>
        <
table>
            <
tbody>
                <
tr>
                    <
th>UName</th>
                    <
th>Finished</th>
                    <
th>Podium</th>
                    <
th>Win</th>
                    <
th>Second</th>
                    <
th>Third</th>
                    <
th>Worse</th>
                </
tr>
php
    $UName 
= array(
            
=> 'B11TME',
            
=> 'hardcoreobscure',
            
=> 'bigdave2967',
            
=> 'highwave',
            
=> 'lrb_Aly',
            
=> 'psymonhilly',
            
=> 'Shaguar',
            
=> 'STROBE',
            
=> 'Zyber',
            );
    
$PName = array(
            
=> '^0[^1ITEK^0] ^8B11TME',
            
=> '^0[^1ITEK^0] ^8Jamie',
            
=> '^0[^1ITEK^0] ^8The End',
            
=> '^0[^1ITEK^0] ^8Highwave',
            
=> '^0[^1ITEK^0] ^8LRB_Aly',
            
=> '^0[^1ITEK^0] ^8Psymonhilly',
            
=> '^0[^1ITEK^0] ^8Shaguar',
            
=> '^0[^1ITEK^0] ^8STROBE',
            
=> '^0[^1ITEK^0] ^8Zyber',
            );
    foreach (
$UName as $Id => $LFSName)
    {
        
$LFSName str_replace(' ''%20'$LFSName);
        
$data file("http://lfsworld.net/pubstat/get_stat2.php?action=pst&version=1.2&racer={$LFSName}");
        
$info[$Id] = array(
            
'Finished' => number_format($data[7], 0'.'''),
            
'Podium' => number_format((($data[4] + $data[5] + $data[6]) / $data[7]) * 1002'.'''),
            
'Win' => number_format((($data[4]) / $data[7]) * 1002'.'''),
            
'Second' => number_format((($data[5]) / $data[7]) * 1002'.'''),
            
'Third' => number_format((($data[6]) / $data[7]) * 1002'.'''),
            
'Worse' => number_format((($data[7] - ($data[4] + $data[5] + $data[6])) / $data[7]) * 1002'.''')
        );
        
$sort[$Id] = number_format((($data[4] + $data[5] + $data[6]) / $data[7]) * 1002'.''');
        
sleep(5);
    }
    
arsort($sort);
    foreach (
$sort as $Id => $Podium)
    {
        echo 
"\t\t\t\t<tr>\n";
        echo 
"\t\t\t\t\t<td>" dispName($PName[$Id]) . "</td>\n";
        echo 
"\t\t\t\t\t<td>{$info[$Id]['Finished']}</td>\n";
        echo 
"\t\t\t\t\t<td>{$info[$Id]['Podium']}%</td>\n";
        echo 
"\t\t\t\t\t<td>{$info[$Id]['Win']}%</td>\n";
        echo 
"\t\t\t\t\t<td>{$info[$Id]['Second']}%</td>\n";
        echo 
"\t\t\t\t\t<td>{$info[$Id]['Third']}%</td>\n";
        echo 
"\t\t\t\t\t<td>{$info[$Id]['Worse']}%</td>\n";
        echo 
"\t\t\t\t</tr>\n";
    }

            </
tbody>
        </
table>
    </
body>
</
html>
php
    file_put_contents
(CACHE_FILEob_get_contents());
    
ob_end_flush();
?>


<?php 
php
    
//cache time to live seconds.
    
define("CACHE_TTL"86400);
    
//cache file path.
    
define("CACHE_FILE""cache.html");
    
//config file path.
    
define("CONFIGFILE""config.txt");
    
//test cache ttl.
    
if (file_exists(CACHE_FILE) && filemtime(CACHE_FILE) + CACHE_TTL time())
        exit(
file_get_contents(CACHE_FILE));
    
ob_start();
    function 
dispName($str)
    {    
// Take you filur for the function.
        
$array_colors = array(
            
'0' => 'black',
            
'1' => 'red',
            
'2' => 'lime',
            
'3' => 'yellow',
            
'4' => 'blue',
            
'5' => 'purple',
            
'6' => 'cyan',
            
'7' => 'white',
            
'8' => 'gray',
            
'9' => 'lime'
        
);
        if (
strlen($str) == 1)
            return 
$array_colors[$str];
        return 
preg_replace("/(\^([0-9]))([^\^]+)/e""'<font color=\"'.dispName(\\2).'\">\\3</font>'"$str);
    }
    
    
$file file(CONFIGFILE);
    
$title $file[0]; unset($file[0]);
    foreach (
$file as $line => $data)
        list(
$UName[], $PName[]) = explode(','$data);
    foreach (
$PName as $i => $Name)
        
$PName[$i] = trim($Name);
    foreach (
$UName as $Id => $LFSName)
    {
        
$LFSName str_replace(' ''%20'$LFSName);
        
$data file("http://lfsworld.net/pubstat/get_stat2.php?action=pst&version=1.2&racer={$LFSName}");
        
$info[$Id] = array(
            
'Finished' => number_format($data[7], 0'.'''),
            
'Podium' => number_format((($data[4] + $data[5] + $data[6]) / $data[7]) * 1002'.'''),
            
'Win' => number_format((($data[4]) / $data[7]) * 1002'.'''),
            
'Second' => number_format((($data[5]) / $data[7]) * 1002'.'''),
            
'Third' => number_format((($data[6]) / $data[7]) * 1002'.'''),
            
'Worse' => number_format((($data[7] - ($data[4] + $data[5] + $data[6])) / $data[7]) * 1002'.''')
        );
        
$sort[$Id] = number_format((($data[4] + $data[5] + $data[6]) / $data[7]) * 1002'.''');
        
sleep(5);
    }
    
arsort($sort);
    
$Place 1;

<
html>
    <
head>
        <
title>echo $title;  Stats</title>
        <
style type="text/css">
            
body{
                
background-color#F5F5F5;
                
font-familysans-serif;
                
font-size15px;
                
padding0;
                
margin0.5em;
                
color#000000;
            
}
            
table#LFSWCS, tr#LFSWCS.head{
                
padding0.1em 0.5em 0.1em 0.5em;
            }
            
tr#LFSWCS.head{
                
background-color#D0DCE0;
                
font-weightbold;
                
color#000000;
                
margin0.1em;
            }
            
tr#LFSWCS.odd{
                
background-color#E5E5E5;
                
text-alignleft;
            }
            
tr#LFSWCS.even{
                
background-color#D5D5D5;
                
text-alignleft;
            }
            
td#LFSWCS:hover, tr#LFSWCS.odd:active, tr#LFSWCS.even:active{
                
background-color#CCFFCC;
            
}
            
td#LFSWCS:active, tr#LFSWCS.odd:hover, tr#LFSWCS.even:hover{
                
background-color#FFCC99;
            
}
        </
style>
    </
head>
    <
body>
        <
table id="LFSWCS" align="center">
            <
tbody>
                <
tr id="LFSWCS" class="head">
                    <
th id="LFSWCS">Place</th>
                    <
th id="LFSWCS">UName</th>
                    <
th id="LFSWCS">Finished</th>
                    <
th id="LFSWCS">Podium</th>
                    <
th id="LFSWCS">Win</th>
                    <
th id="LFSWCS">Second</th>
                    <
th id="LFSWCS">Third</th>
                    <
th id="LFSWCS">Worse</th>
                </
tr>foreach ($sort as $Id => $Podium): 
                <
tr id="LFSWCS" echo ($Place == 0) ? "class=\"even\"" "class=\"odd\""; >
                    <
td id="LFSWCS">echo $Place; .</td>
                    <
th id="LFSWCS">echo dispName($PName[$Id]); </td>
                    <
td id="LFSWCS">echo "{$info[$Id]['Finished']}"; </th>
                    <
td id="LFSWCS">echo "{$info[$Id]['Podium']}%"; </td>
                    <
td id="LFSWCS">echo "{$info[$Id]['Win']}%"; </td>
                    <
td id="LFSWCS">echo "{$info[$Id]['Second']}%"; </td>
                    <
td id="LFSWCS">echo "{$info[$Id]['Third']}%"; </td>
                    <
td id="LFSWCS">echo "{$info[$Id]['Worse']}%"; </td>
                </
tr>$Place++;echo"\n";endforeach; 
            </
tbody>
        </
table>
    </
body>
</
html>
php
    
//write cache
    
file_put_contents(CACHE_FILEob_get_contents());
    
ob_end_flush();
?>

Have fun. This is the last public release I'm going to be making for this.

config.txt
Eagle Racing LFS
Thunder Child,^1(E^7AGL^4E)^7Thunderchild
SmokyTyrz,^1(E^7AGL^4E)^7SmokyTyrz
Dygear,^1(E^7AGL^4E)^7Dygear
canadaman11,^1(E^7AGL^4E)^7Can-Man
rAcEr2418,^1(E^7AGL^4E)^7RaCeR
dougmorrissey,^1(E^7AGL^4E)^7Dougie
mike11973,^1(E^7AGL^4E)^7IndyMike
dave4002000,^1(E^7AGL^4E)^7Sic-SRT4
VT-1,^1(E^7AGL^4E)^7VT1

#9 - D34N0
Quote from Dygear :No, this will be the last update ... added support for php4.

http://www.TheNZ.org/PHP/LFSWTeamStats.zip

I know you said this is the last update but I have an issue with the line sleep(5).
This will only work if you are using Internet Explorer or when using LFSW Premium Stats and by adding the LFSW IDKey and premium stats options to the line getting the stats and commenting the line Sleep(5).
When using Mozilla/FireFox it has a timeout of 30 seconds therfore only allowing for the retrieval of 6 members and would never complete and create the cache file. T7R has 20 but luckily we use premium stats.
I'm not sure if there is a workaround for this as I would like to incorporate this into my Setup Station. My PHP is limited so would appreciate some help.

Regards
Dean
Don't set up for direct access to a page that takes ages to update, refresh the cache behind the scenes and offer that through a webserver.
As filur says ideally you should build the cache in the background - ideally on a cron - rather than letting the client actually goto the page. What I'd personally do is either create the cache file (as a flat file, which may cause problems due to file locking) or by inserting and recalling from a database.
Thank you for your replies. I think I will go down the entering and recalling from a table in the database, and have the table updated if data more than 24hrs old.

Thanks
Dean
How about something similar to this:


Edit: nvm, old news :P

Oh and I didnt thank you before Dygear... thanks very much for your effort to make this! It's well funny that your public release works with the format I had used for my Excel sheet, hehe. Your code is really interesting in many ways. There's so much stuff in there that I can learn from, so thanks again, well appreciated!

BTW, I put it to use here:
http://www.1stracing.nl/compon ... n,com_wrapper/Itemid,205/

TraXX
The php file is only ment to bulid the cache file. Ya there can be some issues with the tar pit that LFSWord has, but like you said premium stats will fix that pretty fast, or you could use a local php.ini file that would change the max execution time for a script to whatever you need. Cron should be used, but you could sit on the webpage your self, once evey 24 hours, if you don't know how to use cron. I would love to add some AJAX to this, as that would get rid of the tarpit problem real fast, and it'd just look cool. I might do another update with that ... who knows ... well see.

Quote from traxxion :It's well funny that your public release works with the format I had used for my Excel sheet

Yeah, what can I say. You inspire me.
[quote=Dygear;205906]The php file is only ment to bulid the cache file. Ya there can be some issues with the tar pit that LFSWord has, but like you said premium stats will fix that pretty fast, or you could use a local php.ini file that would change the max execution time for a script to whatever you need. Cron should be used, but you could sit on the webpage your self, once evey 24 hours, if you don't know how to use cron. I would love to add some AJAX to this, as that would get rid of the tarpit problem real fast, and it'd just look cool. I might do another update with that ... who knows ... well see.
/quote]

How would I implement the local php.ini file and would this just bypass the ISP php.ini file for duration of the script creating the cahe file?
Using Cron i would not be able to test it on my windows version correct?

Sorry if this seems to be basic but it is new to me.
Regards
Dean
Quote from D34N0 :How would I implement the local php.ini file and would this just bypass the ISP php.ini file for duration of the script creating the cahe file?
Using Cron i would not be able to test it on my windows version correct?

Sorry if this seems to be basic but it is new to me.
Regards
Dean

If PHPsuexec is installed on the web host then local php.ini files should be upheld, that is not the ideal situation. If you where to run add set_time_limit(0); to the script the script will die when it's done ... or it would never die if there is a bug ... set_time_limit(300); That's 5 minutes. If you don't get it all done by then there is problay something wrong with the program.

Stats parser question
(17 posts, started )
FGED GREDG RDFGDR GSFDG