The online racing simulator
Urgent PHP help needed; Probably simple for someone with experience
The first race of the Vixen Challenge is on monday 17th february and a tool which I am going to be using in qualifying is still not completely functional.

There will be two servers running qualifying sessions simultaneously and so we are going to be using a tool (don't know the name all i have is a file called quali.exe and the readme) to grab times from the servers using insim.

Frankmd sent me the tool along with a php file he made which gets drivers' best times out of the files created by the tool. The php script converts times to milliseconds and goes through a file finding drivers' best times and then lists drivers with their best times in milliseconds.

Basically what i need someone to do is to make the php script list drivers in order of their best times and also to convert their times back into minutes:seconds.milliseconds.

This is less important but would be very useful if not too hard to impliment. There is a line of code that reads
$lines = file('quali001.rsv');

This tells the script what file to retrieve the laptimes from. Since quali.exe creates a new file after every 100 lines, the script will only retrieve the first 100 laps and then if i want it to get the rest i have to manually open quali002.rsv, quali003.rsv etc. and copy and paste the content into the first file. It would be a lot better if it would look for additional files itself.

Any help would be greatly appreciated.

Thanks, Josh Di Fabio
Attached files
qualitool.zip - 42.5 KB - 186 views

<?php 
    
function list_file_by_ext($dir$ext)
    {
        if (
$handle opendir($dir))
        {
            while (
false !== ($file readdir($handle)))
            {
                
$file_ext explode('.'$file);
                if (
$file != "." && $file != ".." && $file_ext[(count($file_ext) - 1)] == $ext)
                    
$names[] = $file;
            }
            
closedir($handle);
        }
        return 
$names;
    }
?>


<?php 
$rsv_list 
list_file_by_ext($_SERVER['DOCUMENT_ROOT'] . '/resources/rsv''rsv');
print_r($rsv_list);
?>

Thanks. I will have a play with that then but i am a PHP noob so i'm not holding out for much lol.

Edit: Arg sorry, i really don't know what to do with this. Could you give me a hint?
Yea, now let me check that that function works , I riped it out of the LFS_SDK that I am working on, but I made some modifications for it, and did not have a chance to test it.
Yep, I'd say that worked.

Array
(
[0] => 02.10.05_232347_results.csv
[1] => 04.09.05_233502_results.csv
[2] => 04.12.05_152956_results.csv
[3] => 09.01.06_050325_results.csv
[4] => 09.10.05_231829_results.csv
[5] => 09.12.05_211223_results.csv
[6] => 11.09.05_235801_results.csv
[7] => 12.02.06_212631_results.csv
[8] => 15.08.05_131626_results.csv
[9] => 18.12.05_161141_results.csv
[10] => 19.11.05_185313_results.csv
[11] => 21.08.05_231434_results.csv
)

<?php 
php

function list_file_by_ext($dir$ext)
{
    if (
$handle opendir($dir))
    {
        while (
false !== ($file readdir($handle)))
        {
            
$file_ext explode('.'$file);
            if (
$file != "." && $file != ".." && $file_ext[(count($file_ext) - 1)] == $ext)
                
$names[] = $file;
        }
        
closedir($handle);
    }
    return 
$names;
}

print_r(list_file_by_ext($_SERVER['DOCUMENT_ROOT'] . '/resources/csv''csv'));

?>

Ok that gives me the following [url removed]. But as i said i am a php noob and i don't have a clue how this helps me?

Thanks for your help, Josh.

Edit: When i say PHP noob i mean i know nothing... all i can do is use some common sense to rearrange things etc.
I know, it's ment to give you that .
Here's how this helps you.


<?php 
php
function list_file_by_ext($dir$ext)
{
    if (
$handle opendir($dir))
    {
        while (
false !== ($file readdir($handle)))
        {
            
$file_ext explode('.'$file);
            if (
$file != "." && $file != ".." && $file_ext[(count($file_ext) - 1)] == $ext)
                
$names[] = $file;
        }
        
closedir($handle);
    }
    return 
$names;
}

function 
convert_time_to_ms($time)
{
    
$bit explode (":"$time);
    
$min $bit[0];
    
$bat explode ("."$bit[1]);
    
$sec $bat[0];
    
$hs $bat[1];
    
    
$total = ($min 60 100) + ($sec 100) + $hs;
    return 
$total;
}

    
DEFINE ("QUAL_MAX_LAPS"100);
    
    
$DRIVERARRAY = array();
    
    
// EDIT THE PATH IN THIS LINE
    
$files list_file_by_ext($_SERVER['DOCUMENT_ROOT'] . '/resources/csv''csv');
    for (
$i 0$i count($files); $i++)
        
$file_line[] = file($files[$i]);
    
    
$w 0;
    while( 
$w count($files) )
    {
        for (
$i 0$i count($file_line[$w]); $i++)
            
$lines[] = $file_line[$w][$i];
        
$w++;
    }
        
    foreach (
$lines as $line)
    {
        
$bits explode("\t"$line);    
        
        
$bits[2] = trim($bits[2]);
        
        if (!isset(
$DRIVERARRAY[$bits[0]]))
        {
            
$DRIVERARRAY[$bits[0]]['laps'] = 0;
            
$DRIVERARRAY[$bits[0]]['time'] = 600000;
        }
                    
        
        if (
$DRIVERARRAY[$bits[0]]['laps'] < QUAL_MAX_LAPS)
        {
            
$DRIVERARRAY[$bits[0]]['laps'] += 1;
            
            if (
$DRIVERARRAY[$bits[0]]['time'] > convert_time_to_ms($bits[1]))
            {
                
$DRIVERARRAY[$bits[0]]['time'] = $bits[1];
            }
        }        
    }
    
    foreach (
$DRIVERARRAY as $key => $driver)
    {
        print 
$key " " $driver['time'] . "<br>";
    }

?>

Thanks you even made it change the times back from ms nice one . Small bug though i think cause it is only taking the times from the first .rsv file still. Have a look at this [url removed]. All of those laptimes are from the first .rsv file.

Any ideas?

Josh

Edit: Looking at it again, it is going through all 3 files. However it is just returning the first time each driver has done and not their best time.
That's due to the orignal file's code.
If you would give me some time with this, about an hour, I could make it work the way you want. But no promises and keep in mind, my code is not allways the most optimized.
I would really appreciate that but only if it is not too much trouble. 'Optimized' is not a word that's in my dictionary; you should see the html used in the websites i've made lol.

Thanks, Josh

<?php 
php
function list_file_by_ext($dir$ext)
{
    if (
$handle opendir($dir))
    {
        while (
false !== ($file readdir($handle)))
        {
            
$file_ext explode('.'$file);
            if (
$file != "." && $file != ".." && $file_ext[(count($file_ext) - 1)] == $ext)
                
$names[] = $file;
        }
        
closedir($handle);
    }
    return 
$names;
}

function 
convert_time_to_ms($time)
{
    
$bit explode (":"$time);
    
$min $bit[0];
    
$bat explode ("."$bit[1]);
    
$sec $bat[0];
    
$hs $bat[1];
    
    
$total = ($min 60 100) + ($sec 100) + $hs;
    return 
$total;
}
    
DEFINE ("QUAL_MAX_LAPS"100);
    
$DRIVERARRAY = array();
    
    
$files list_file_by_ext($_SERVER['DOCUMENT_ROOT'] . '/PHP/LFS/qual''rsv');
    for (
$i 0$i count($files); $i++)
        
$file_line[] = file($files[$i]);
    
    
$w 0;
    while( 
$w count($files) )
    {
        for (
$i 0$i count($file_line[$w]); $i++)
            
$lines[] = $file_line[$w][$i];
        
$w++;
    }
    
    foreach (
$lines as $line)
    {
        
$bits explode("\t"$line);
        
$bits[2] = trim($bits[2]);
        if (!isset(
$DRIVERARRAY[$bits[0]]))
        {
            
$DRIVERARRAY[$bits[0]]['laps'] = 0;
            
$DRIVERARRAY[$bits[0]]['time'] = 600000;
        }
        if (
$DRIVERARRAY[$bits[0]]['laps'] < QUAL_MAX_LAPS)
        {
            
$DRIVERARRAY[$bits[0]]['laps'] += 1;
            if (
convert_time_to_ms($DRIVERARRAY[$bits[0]]['time']) > convert_time_to_ms($bits[1]))
            {
                
$DRIVERARRAY[$bits[0]]['time'] = $bits[1];
            }
        }
    }

<
TABLE>

php
    
foreach ($DRIVERARRAY as $key => $driver)
    {
        echo 
"\t<TR>\n";
        echo 
"\t\t<TD>$key</TD>\n";
        echo 
"\t\t<TD>${driver['time']}</TD>\n";
        echo 
"\t</TR>\n";
    }


</
TABLE>
?>

Well, it has a nice output, but, the times don't sort .
It does however now show their best times.
That was quick! Thank you very much indeed, you have been most helpful .

Josh Di Fabio
The sorting problem is kinda solved as i have used some java thing i found on google. Got the idea from something tristan cliffe made for the Vixen Challenge last season.

However, there is one other thing, which i think someone adept with php could quite easily solve. Whenever a new session starts on one of the servers being 'watched' through insim, 'Qualification started' or 'Race started' is written to the quali###.rsv file and so this appears on the page as a driver name. You can see this here. Basically I am wondering whether the script can be made to ignore 'Qualification started' and 'Race started'.

Thanks, Josh Di Fabio
Yea, I can do that, just got to read the statment as it is stored in Key.
I am sure I can filter that out.
I should probably go to bed.
I should add that the original tool was made by Smith (who also made the stats tool), and not by me
Quote from Frankmd :I should add that the original tool was made by Smith (who also made the stats tool), and not by me

Thanks, i was trying to remember when i wrote my first post but couldn't...
Tuusita added something to filter out "qualification started" and "race started" which was nice of him, so no need for any of you to worry about that .

Thanks, Josh
Look what I made ...



See what you can do with some time and some progamming know how.
It gets all of that from parsing csv files and rsv files. Pretty sweet huh?
Very nice .
Quote from Dygear :Look what I made ...

http://thenz.org/images/F1/LFS-F1-THEME.png

See what you can do with some time and some progamming know how.
It gets all of that from parsing csv files and rsv files. Pretty sweet huh?

Is it possible for you to share the code?
I think a lot of people, including me would be very happy.

If not
I will share the code after the LFS : F1 seasion is over.
I saw it yesterday

Nice work

FGED GREDG RDFGDR GSFDG