News:

Herr Otto Partz says you're all nothing but pipsqueaks!

Main Menu

RPLInfo in PHP

Started by dreadnaut, January 22, 2013, 03:53:51 PM

Previous topic - Next topic

dreadnaut

Zak, could you share the source of your replay info script?  I'd like to port it to javascript :)

Duplode

Quote from: dreadnaut on January 22, 2013, 03:53:51 PM
Zak, could you share the source of your replay info script?  I'd like to port it to javascript :)

(Sneaking into the conversation: and when you do the port, can I incorporate it to Southern Cross?  :))

Chulk

Quote from: Duplode on January 22, 2013, 04:05:24 PM
Quote from: dreadnaut on January 22, 2013, 03:53:51 PM
Zak, could you share the source of your replay info script?  I'd like to port it to javascript :)

(Sneaking into the conversation: and when you do the port, can I incorporate it to Southern Cross?  :))
(Sneaking into an already sneaked into conversation) And can you add a way to calculate corrected penalty time to it?
Yes, it is me. No, I'm not back at racing (for now...)

dreadnaut

#3
That was in part the plan :)  I'm finishing the viewer now by the way.

@chulk: penalty time comes from the simulation, you can't get it from the replay

Actually, you can't even be sure of the correct time, or whether a replay covers the complete lap.

Duplode

Quote from: dreadnaut on January 22, 2013, 04:17:07 PM
@chulk: penalty time comes from the simulation, you can't get it from the replay

Chulk means manually adding penalty time through the HTML interface, so that it is corrected by the bonuses too (I replied exactly the same thing when he mentioned it at ZakStunts, and got this answer  ;)).

Chulk

Quote from: Duplode on January 22, 2013, 04:27:30 PM
manually adding penalty time through the HTML interface, so that it is corrected by the bonuses too
Exactly my thoughts (literally!)
Yes, it is me. No, I'm not back at racing (for now...)

alanrotoi

I can tell he used some colspan tags for the code. :D

zaqrack

#7
By popular demand here is the source. Please no comments on my poor coding skills and inefficiency :)

The main code:

<center>
<p>
WEB RPLINFO
<br>
For version BB1.1 only!
</p>
<p>
<form action="index.php?page=rplinfo&menu=<?=$menu;?>" method="post" enctype="multipart/form-data">
<input type="file" name="fileatt" /><input type="submit" value="Send" />
<br>
<input type="checkbox" name="corr"> - Show correction.</p>
<input type="checkbox" name="hexa"> - Show hex code.</p>
</form>
</body>
</html>
<?php
include ('include.php');
$hexa=$_POST['hexa'];
$corr=$_POST['corr'];
function 
gears($str){
    if (
$str=="00"){echo "Manual";}
    if (
$str=="01"){echo "Automatic";}
}
function 
opponent($str){
    if (
$str=="00"){echo "None";}
    if (
$str=="01"){echo "Bernie";}
    if (
$str=="02"){echo "Otto";}
    if (
$str=="03"){echo "Joe";}
    if (
$str=="04"){echo "Cherry";}
    if (
$str=="05"){echo "Helen";}
    if (
$str=="06"){echo "Skid";}
}

// Obtain file upload vars
$fileatt      $_FILES['fileatt']['tmp_name'];
$fileatt_type $_FILES['fileatt']['type'];
$fileatt_name $_FILES['fileatt']['name'];
if (
is_uploaded_file($fileatt)) {
// Read the file to be attached ('rb' = read binary)
$file fopen($fileatt,'rb');
$data fread($file,filesize($fileatt));
$hex=bin2hex($data);
$car=hex2ascii(substr($hex,0,8));
echo 
"<table>";

//display time
echo "<tr><td>Time:</td><td><b>";
$fupload_size=filesize($fileatt);
$timeraw=$fupload_size-1848;
$basetime=$timeraw;
echo 
floor($timeraw/1200);
$timeraw=$timeraw-(floor($timeraw/1200)*1200);
echo 
":";
if (
floor($timeraw/20)<10){
echo 
"0";
}
echo 
floor($timeraw/20);
$timeraw=$timeraw-(floor($timeraw/20)*20);
echo 
".";
if ((
$timeraw*5)<10){
echo 
"0";
}
echo 
$timeraw*5;

//multiplier
if ($corr!=NULL){
$track="C".$atrack;
    
$result mysql_query"SELECT `$car` FROM cars WHERE track='$track';")
        or die(
"Can't select table!".mysql_error());
                while (
$one_row mysql_fetch_row$result )) {
                    foreach (
$one_row as $key => $field) {
                          echo 
"</td></tr><tr><td>Correction on ",$track,"</td><td><b>",$field,"%</td></tr>";
                          
$timeraw=floor($basetime*5*((100-$field)/100));
                    }
                }
        print 
"<tr><td>With correction: </td><td><b>";
        echo 
floor($timeraw/6000);
        
$min=floor($timeraw/6000);
        
$timeraw=$timeraw-(floor($timeraw/6000)*6000);
        echo 
":";
        if (
floor($timeraw/100)<10){
        echo 
"0";
   
}
   echo 
floor($timeraw/100);
        
$sec=floor($timeraw/100);
        
$timeraw=$timeraw-(floor($timeraw/100)*100);
        echo 
".";
        if ((
$timeraw)<10){
        echo 
"0";
   
}
   echo 
$timeraw;



//display data
echo "</td></tr><tr><td>Car:</td><td><b> ",carnames($car,0);
echo 
"</td></tr><tr><td>Color:</td><td><b> ",carcolor(substr($hex,8,2),$car);
echo 
"</td></tr><tr><td>Gearbox:</td><td><b> ",gears(substr($hex,10,2));
echo 
"</td></tr><tr><td>Opponent:</td><td><b> ",opponent(substr($hex,12,2));
if ((
substr($hex,12,2))!='00'){
$oppcar=hex2ascii(substr($hex,14,8));
echo 
"</td></tr><tr><td>Opponent's car:</td><td> 
<b>"
,carnames($oppcar,0);
echo 
"</td></tr><tr><td>Opponent's color:</td><td><b> ",carcolor(substr($hex,22,2),$oppcar);
echo 
"</td></tr><tr><td>Opponent's gearbox:</td><td><b> 
"
,gears(substr($hex,24,2));
}
echo 
"</td></tr><tr><td>Track name:</td><td><b> ";
for (
$i=26;$i<44;$i++){
if (
substr($hex,$i,2)=="00"){
$i=44;
}
else {echo 
hex2ascii(substr($hex,$i,2));$i++;}
}

echo 
"</td></tr></table>";
if (
$hexa=="on"){
echo 
"<br><br><table>";
for (
$j=0;$j<200;$j++){
    echo 
"<tr><td><b><samp>",dechex($j*32),"</b>";
    for (
$i=0;$i<32;$i++){
if (($i==0) OR ($i==8) OR ($i==16) OR ($i==24)){
echo "</td><td width=2></td><td><samp>";
}
echo substr($hex,$j*32+$i*2,2);
echo " ";
    }
    echo 
"</td></tr>";
}
echo 
"</samp></table>";
}
fclose($file);
}
      
?>



and the invoked functions:

hex2ascii:

<?php
//hex2ascii
//converts hex text to ascii (rplinfo)
function hex2ascii($str){
$p '';
for (
$i=0$i strlen($str); $i=$i+2){
    
$p .= chr(hexdec(substr($str$i2)));
    }
    return 
$p;
    }
?>



carnames:

<?php
//function carnames
//returns the car name from car ID
//on $length = 0 short name
//on $length = 1 long name
//on $length = 2 car ID from database key (only in 2008)

function carnames ($car,$length){
//dbkey only for 2008!
          
$dbkey = array(
              
"1"=>"ANSX",
              
"2"=>"AUDI",
              
"3"=>"VETT",
              
"4"=>"FGTO",
              
"5"=>"RANG",
              
"6"=>"JAGU",
              
"7"=>"COUN",
              
"8"=>"LANC",
              
"9"=>"LM02",
              
"10"=>"NSKY",
              
"11"=>"P962",
              
"12"=>"PC04",
              
"13"=>"PMIN",
              
"14"=>"GATE",
              
"15"=>"SUKA",
              
"16"=>"ZF40",
              
"17"=>"ZLET",
              
"18"=>"CDOR",
              
"19"=>"ZGT3",
              
"20"=>"ZMP4"
          
);
          
$short = array(
              
"PMIN"=>"Indy",
              
"P962"=>"P962",
              
"JAGU"=>"Jaguar",
              
"FGTO"=>"Ferrari",
              
"COUN"=>"Lambo",
              
"VETT"=>"Vette",
              
"ANSX"=>"Acura",
              
"PC04"=>"Carrera",
              
"AUDI"=>"Audi",
              
"LANC"=>"Lancia",
              
"LM02"=>"LM002",
              
"RANG"=>"Ranger",
              
"NSKY"=>"Skyline",
              
"CDOR"=>"Melange",
              
"GATE"=>"Gate",
              
"ZTCG"=>"Corolla",
              
"ZF40"=>"F40",
              
"SUKA"=>"Kart",
              
"ZLET"=>"Lotus",
              
"CDOR"=>"Melange",
              
"ZGT3"=>"ZR1-GT3",
              
"ZMP4"=>"McLaren"
         
);
         
$long = array(
             
"P962"=>"Porsche 962 IMSA",
             
"JAGU"=>"Jaguar XJR9 IMSA",
             
"FGTO"=>"Ferrari GTO",
             
"COUN"=>"Lamborghini Countach",
             
"ANSX"=>"Acura NSX",
             
"PC04"=>"Porsche Carrera 4",
             
"AUDI"=>"Audi Quattro",
             
"LANC"=>"Lancia Delta Integrale",
             
"LM02"=>"Lamborghini LM002",
             
"PMIN"=>"Porsche March Indy",
             
"VETT"=>"Chevrolet Corvette ZR1",
             
"RANG"=>"Ford Ranger",
             
"NSKY"=>"Nissan Skyline GT-R",
             
"CDOR"=>"Melange XGT-88",
             
"GATE"=>"Speedgate XSD",
             
"ZTCG"=>"Toyota Corolla GTI",
             
"ZF40"=>"Ferrari F40",
             
"SUKA"=>"Superkart",
             
"ZLET"=>"Lotus Esprit Turbo",
             
"ZGT3"=>"LWT ZR1-GT3 Mark II",
             
"ZMP4"=>"McLaren Honda MP4/4",
     "ANSV"=>"Honda NSX by Mr Eriksson",
     "COCO"=>"Coconut Car Coronet Pulsar STi-R",
     "COUV"=>"Skoda Felicia Kompressor by Mr Eriksson",
     "DITS"=>"The Ditsch Car by brainSteen",
     "GT75"=>"Coconut Car GTR 7.5 GT IMSA",
     "LM00"=>"Lada Niva Comeback by Mr Eriksson",
     "PC03"=>"Porsche 911 Turbo by Mr Eriksson",
     "WIRE"=>"Wooden WireFire",
     "JAGT"=>"Jaguar XJR9 tuned for Le Mans",
     "F102"=>"Formula 1 2002 by Krys TOFF",
     "VETD"=>"Knight Rider by Mr Eriksson"
         
);

    if (
$length==1){
      return 
$short[$car];
     }
    if (
$length==2){
      return 
$dbkey[$car];
     }
    else{
      return 
$long[$car];
     }
}
?>



carcolor:

<?php
//function carcolor
//car color value for rplinfo
function carcolor ($color,$car){
if ((
$car==LAMB) OR ($car=P962)){
    if (
$color==00){$out="Blue";}
    if (
$color==01){$out="Red";}
    if (
$color==02){$out="Yellow";}
    if (
$color==03){$out="Cyan";}
    if (
$color==04){$out="Green";}
    if (
$color==05){$out="Purple";}
    if (
$color==06){$out="White";}
}
if (
$car==JAGU){
    if (
$color==00){$out="Green";}
    if (
$color==01){$out="Gold";}
    if (
$color==02){$out="Cyan";}
    if (
$color==03){$out="Red";}
    if (
$color==04){$out="Grey";}
    if (
$color==05){$out="White";}
    if (
$color==06){$out="Purple";}
}
if (
$car==FGTO){
    if (
$color==00){$out="Red";}
    if (
$color==01){$out="Blue";}
    if (
$color==02){$out="White";}
    if (
$color==03){$out="Yellow";}
    if (
$color==04){$out="Cyan";}
    if (
$color==05){$out="Green";}
    if (
$color==06){$out="Purple";}
}
if (
$car==LANC){
    if (
$color==00){$out="Yellow";}
    if (
$color==01){$out="White";}
    if (
$color==02){$out="Cyan";}
    if (
$color==03){$out="Gold";}
    if (
$color==04){$out="Red";}
    if (
$color==05){$out="Purple";}
    if (
$color==06){$out="Green";}
}
if (
$car==LM02){
    if (
$color==00){$out="Red";}
    if (
$color==01){$out="Grey";}
    if (
$color==02){$out="Green";}
    if (
$color==03){$out="Blue";}
    if (
$color==04){$out="Gold";}
    if (
$color==05){$out="White";}
    if (
$color==06){$out="Cyan";}
}
if (
$car==PCAR){
    if (
$color==00){$out="Purple";}
    if (
$color==01){$out="Blue";}
    if (
$color==02){$out="Gold";}
    if (
$color==03){$out="Cyan";}
    if (
$color==04){$out="Purple";}
    if (
$color==05){$out="Grey";}
    if (
$color==06){$out="Red";}
}
if (
$car==PMIN){
    if (
$color==00){$out="Blue";}
    if (
$color==01){$out="White";}
    if (
$color==02){$out="Red";}
    if (
$color==03){$out="Yellow";}
    if (
$color==04){$out="Cyan";}
    if (
$color==05){$out="Green";}
    if (
$color==06){$out="Purple";}
}
if (
$car==VETT){
    if (
$color==00){$out="Red";}
    if (
$color==01){$out="Cyan";}
    if (
$color==02){$out="Yellow";}
    if (
$color==03){$out="Grey";}
    if (
$color==04){$out="Green";}
    if (
$color==05){$out="White";}
    if (
$color==06){$out="Gold";}
}
if (
$car==ANSX){
    if (
$color==00){$out="Blue";}
    if (
$color==01){$out="Red";}
    if (
$color==02){$out="Yellow";}
    if (
$color==03){$out="White";}
    if (
$color==04){$out="Cyan";}
    if (
$color==05){$out="Grey";}
    if (
$color==06){$out="Gold";}
}
if (
$car==AUDI){
    if (
$color==00){$out="Cyan";}
    if (
$color==01){$out="Red";}
    if (
$color==02){$out="Gold";}
    if (
$color==03){$out="Purple";}
    if (
$color==04){$out="Green";}
    if (
$color==05){$out="White";}
    if (
$color==06){$out="Blue";}
}

if (
$car==RANG){
    if (
$color==00){$out="Blue";}
    if (
$color==01){$out="Red";}
    if (
$color==02){$out="Yellow";}
    if (
$color==03){$out="White";}
    if (
$color==04){$out="Green";}
    if (
$color==05){$out="Dark Red";}
    if (
$color==06){$out="Pink";}
}
if (
$car==NSKY){
    if (
$color==00){$out="Blue";}
    if (
$color==01){$out="Red";}
    if (
$color==02){$out="Yellow";}
    if (
$color==03){$out="Grey";}
    if (
$color==04){$out="Purple";}
    if (
$color==05){$out="Dark Grey";}
    if (
$color==06){$out="Bronze";}
}
if (
$car==GATE){
    if (
$color==00){$out="Black";}
    if (
$color==01){$out="Red";}
    if (
$color==02){$out="Yellow";}
    if (
$color==03){$out="Blue" ;}
    if (
$color==04){$out="Green";}
    if (
$color==05){$out="Purple";}
    if (
$color==06){$out="White";}
}
if (
$car==SUKA){
    if (
$color==00){$out="Green";}
    if (
$color==01){$out="Gray";}
    if (
$color==02){$out="Red";}
    if (
$color==03){$out="Yellow" ;}
    if (
$color==04){$out="Cyan";}
    if (
$color==05){$out="Blue";}
    if (
$color==06){$out="Black";}
    if (
$color==07){$out="Brown";}
}
if (
$car==ZF40){
    if (
$color==00){$out="Red";}
    if (
$color==01){$out="Gray";}
    if (
$color==02){$out="Yellow";}
    if (
$color==03){$out="White" ;}
    if (
$color==04){$out="Dark Grey";}
    if (
$color==05){$out="Dark Red";}
    if (
$color==06){$out="Indigo";}
}
if (
$car==ZLET){
    if (
$color==00){$out="Green";}
    if (
$color==01){$out="Gray";}
    if (
$color==02){$out="Red";}
    if (
$color==03){$out="Blue" ;}
    if (
$color==04){$out="White";}
    if (
$color==05){$out="Gold";}
    if (
$color==06){$out="Dark Grey";}
}
if (
$car==CDOR){
    if (
$color==00){$out="Blue";}
    if (
$color==01){$out="Red";}
    if (
$color==02){$out="Yellow";}
    if (
$color==03){$out="Light Grey" ;}
    if (
$color==04){$out="Black";}
    if (
$color==05){$out="Green";}
    if (
$color==06){$out="Purple";}
    if (
$color==07){$out="Striped";}}
if (
$car==ZGT3){
    if (
$color==00){$out="Red";}
    if (
$color==01){$out="Gray";}
    if (
$color==02){$out="Blue";}
    if (
$color==03){$out="Green" ;}
    if (
$color==04){$out="Light Red";}
    if (
$color==05){$out="Purple";}
    if (
$color==06){$out="Yellow";}
}
if (
$car==ZMP4){
    if (
$color==00){$out="Senna";}
    if (
$color==01){$out="Prost";}}
else {
 
$out=$color;
}

return 
$out;
}
?>




dreadnaut


dreadnaut

#9
Uhm, I think reading files could be simpler using unpack():

$file fopen($filename'rb');
$data fread($file26);
$info unpack("A4player_car/C1player_color/C1player_shift/C1opponent_type/A4opponent_car/C1opponent_color/C1opponent_shift/a8track_name/x/v1replay_granularity/v1replay_length"$data
);
var_dump($info);


output:


array
  'player_car' => string 'PMIN' (length=4)
  'player_color' => int 0
  'player_shift' => int 1
  'opponent_type' => int 0
  'opponent_car' => string 'ÿ' (length=1)
  'opponent_color' => int 0
  'opponent_shift' => int 0
  'track_name' => string 'ALICANTE (length=8)
  'replay_granularity' => int 595
  'replay_length' => int 0

zaqrack

seems very reasonable. I code with my limited toolbox: axe and hammer. It shows :D
E.g. Until 2008 all SQL queries were simple dump of full data columns, with PHP code to filter results. Slow and inefficient but that's what I was able to do back then. Corrected since :)


dreadnaut

Old code is old code. I usually shed a tear when I look at mine :-X

Anyway, here's some new code! Squeeze all information out of your replays with this nice class!