<?php

// Pinewood Derby system
// Race Schedule creation/management module

// mode:
// 0 - default - Select rank
// 1 - display current race schedule
// 2 - create new race schedule

$mode=0;
$rankid=0;

$mode=$_POST["mode"];

$rankid=$_POST["rankid"];

$servername = "localhost";
$username = "root";
$password = "de2la614";
$database = "pinewood";


// Create connection
mysql_connect($servername, $username, $password);
@mysql_select_db($database) or die( "Unable to select database");

$query="SELECT rankid, rank_name from rank_table";

$result=mysql_query($query);


echo("Select group: ");
echo "<form action=\"random.php\" method=\"post\">";
echo("<select name=\"rankid\">");

// read rank names
while($row=mysql_fetch_array($result))
{
echo "<option value='".$row['rankid']."'>".$row['rank_name']." </option>";
}
echo "</select><br>";
echo "<button name=\"mode\" value=\"2\" type=\"submit\">Create new schedule</button>";
echo "<button name=\"mode\" value=\"1\" type=\"submit\">List race schedule</button>";
echo "</form>";


// Create new race schedule 

if ($mode==2)
{
$query="SELECT racer_id FROM racers WHERE rankid=$rankid";
$result=mysql_query($query);

$max=mysql_numrows($result);

// populate racer list from the DB

for($x=0; $x<$max; $x++)
{
	$racerlist[$x] = mysql_result($result, $x, "racer_id");
}

// Randomize racer list
shuffle($racerlist);

// copy racer list to each lane and shuffle

// Create 1st lane

$lane[0] = $racerlist;

// Do a 1, 2, 6 rotation
for ($x=0; $x<$max; $x++)
{
	$rotation1 = $x+1;
	$rotation2 = $x+2;
	$rotation3 = $x+6;

	if ($rotation1 > $max-1) { $rotation1 = $rotation1-$max; }
	if ($rotation2 > $max-1) { $rotation2 = $rotation2-$max;}
	if ($rotation3 > $max-1) { $rotation3 = $rotation3-$max;}

	// lane 2 = lane 1 + rotation1
	$lane[1][$x] = $lane[0][$rotation1];
	$lane[2][$x] = $lane[0][$rotation2];
	$lane[3][$x] = $lane[0][$rotation3];
}

// Output to DB

// Purge old race data for $rankid

$query = "DELETE FROM racedata WHERE rank_id=$rankid";
mysql_query($query);

for ($x=0; $x<$max; $x++)
{
	for ($y=0; $y<4; $y++)
	{
	$racerid = $lane[$y][$x];
	$query = "INSERT INTO racedata VALUES ($rankid, $x,$y,$racerid, 0)";
	mysql_query($query);
	}
}
$query="UPDATE status SET num_heats=$max WHERE rank_id=$rankid";
mysql_query($query);
$mode=1;
}


if ($mode==1)
{

$query="SELECT max(heat_num) AS max_heat from racedata where rank_id=$rankid;";

$result=mysql_query($query);
$row=mysql_fetch_row($result);
$max_heat=$row[0];

// Read data from the racedata table
$query="SELECT * from racedata where rank_id=$rankid";
$result=mysql_query($query);

$records_read = mysql_num_rows($result);

echo $records_read." records returned";

echo "<center><table style=\"width:75%\"><tr align=\"center\"><th>Heat</th><th>Lane 1</th><th>Lane 2</th><th>Lane 3</th><th>Lane 4</th></tr>";

while($row=mysql_fetch_array($result))
{
	$heat=$row['heat_num'];
	$lane=$row['lane_num'];
	$racer=$row['racer_id'];
	$racedata[$heat][$lane]=$racer;
}

for($x=0; $x<$max_heat+1; $x++)
{
	$realracenum=$x+1;
	echo "<tr align=\"center\"><td>$realracenum</td>";
	for($y=0; $y<4; $y++)
	{
		echo "<td align=\"center\">".$racedata[$x][$y]."</td>";
	}
	echo("</tr>");
}


echo "</table></center>";

}
?>
