PDA

View Full Version : Urgent PHP help needed; Probably simple for someone with experience


joshdifabio
17th February 2006, 21:55
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

Dygear
17th February 2006, 22:49
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;
}

$rsv_list = list_file_by_ext($_SERVER['DOCUMENT_ROOT'] . '/resources/rsv', 'rsv');
print_r($rsv_list);

joshdifabio
17th February 2006, 22:51
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? :)

Dygear
17th February 2006, 22:58
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.

Dygear
17th February 2006, 23:03
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

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'));

?>

joshdifabio
17th February 2006, 23:07
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.

Dygear
17th February 2006, 23:26
I know, it's ment to give you that :).
Here's how this helps you.


<?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>";
}
?>

joshdifabio
17th February 2006, 23:31
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.

Dygear
17th February 2006, 23:51
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.

joshdifabio
17th February 2006, 23:53
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

Dygear
18th February 2006, 00:03
<?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.

joshdifabio
18th February 2006, 00:04
That was quick! Thank you very much indeed, you have been most helpful :).

Josh Di Fabio

joshdifabio
18th February 2006, 00:36
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 (http://spdoracing.frih.net/vixen/team_qual.php). Basically I am wondering whether the script can be made to ignore 'Qualification started' and 'Race started'.

Thanks, Josh Di Fabio

Dygear
18th February 2006, 03:06
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.

joshdifabio
18th February 2006, 03:17
:) I should probably go to bed.

Frankmd
19th February 2006, 10:37
I should add that the original tool was made by Smith (who also made the stats tool), and not by me :)

joshdifabio
19th February 2006, 12:48
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...

joshdifabio
19th February 2006, 18:47
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

Dygear
19th February 2006, 23:49
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?

joshdifabio
19th February 2006, 23:50
Very nice :).

Pinto_PT
20th February 2006, 13:11
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 :D would be very happy.

If not :thumb:

Dygear
20th February 2006, 23:50
I will share the code after the LFS : F1 seasion is over.

Dygear
5th April 2006, 20:20
Is it possible for you to share the code?
I think a lot of people, including me :D would be very happy.

If not :thumb:

http://www.lfsforum.net/showthread.php?t=6233

Pinto_PT
6th April 2006, 19:51
I saw it yesterday :)

Nice work
:thumb: