<?php
include ('logue.php'); 
include ('entete2.php');
include ('entete.php');
include ('ressource.php');


$sql2 = "SELECT robo_lvl,chantier_lvl,hangarfer_lvl,hangarfer_evolution,hangaror_lvl,hangaror_evolution,hangarada_lvl,hangarada_evolution,hangaror_lvl,hangarura_lvl,hangarura_evolution,labo_lvl,labo_evolution,fer_energie,or_energie,ada_energie,ura_energie,exfer_energie,exor_energie,central_energie FROM matgraph_users WHERE id='$planete' ";
$res2 = db_mysql_Query($sql2)or die('erreur sql<br>'.$sql2.'<br>'.mysql_error());
$row2 = mysql_fetch_array($res2); 

$galaxi = htmlentities ($_POST["galaxi"]);
$system = htmlentities ($_POST["system"]);
$position = htmlentities ($_POST["position"]);
$vruines = htmlentities ($_POST["vruines"]);
$vruines2 = htmlentities ($_POST["vruines2"]);

if(!is_numeric($vruines) || floor($vruines)!=$vruines  || empty($vruines) || $vruines < 0) {
	$vruines = 0;
}
if(!is_numeric($vruines2) || floor($vruines2)!=$vruines2  || empty($vruines2) || $vruines2 < 0) {
	$vruines2 = 0;
}

echo"
<script type='text/javascript'>
function Maxvruines() {
	var total = Math.round('$row[vruines]');
  	document.getElementsByName('vruines')[0].value = total;
}
function Maxvruines2() {
	var total = Math.round('$row[vruines2]');
  	document.getElementsByName('vruines2')[0].value = total;
}
function Maxvruines3() {
	var total = Math.round('$row[vruines3]');
  	document.getElementsByName('vruines3')[0].value = total;
}
</script>
<style>
input {
border:1px solid #7E7E7E;
background-color:#000000;
font-family:'trebuchet ms',sans-serif;
color:white;
}
textarea {
border:1px solid #7E7E7E;
background-color:#000000;
font-family:'trebuchet ms',sans-serif;
color:white;
}
select, option {
background-color:#000000;
color:white;
}
</style>
<STYLE TYPE=text/css>
.Style1 {color: #0000FF}
.Style2 {color: #FF0000}
</STYLE>";
echo"<div align=\"center\"><form name=\"colo\" method=\"post\" action=\"colonie.php\" target=\"_parent\">
<table align=\"center\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
   <tr>
     <td width=\"100\" rowspan=\"2\" valign=\"middle\">
       <div align=\"center\">
         <input name=\"page\" value=\"" . $PHP_SELF . "\" type=\"hidden\" id=\"page\">
         <select name=\"idcolo\" onchange=submit()>";
include ('colo2.php');  

$form ="<div align=\"center\">
<table width=\"618\"  border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
    <tr>
      <td colspan=\"3\" align=\"center\"><b>Mission Recyclage :</b>
<br><br>

La mission recyclage consiste à ramener des ressources disponible enorbite d'une planète.
</td>
    </tr>
  </table><br><br>
<form name=\"form1\" method=\"post\" action=\"flotte_ruines_envoi.php\" target=\"_parent\">
  <table width=\"618\" border=\"0\" cellspacing=\"0\" cellpadding=\"2\" bordercolor=\"#000000\" style=\"font-family:trebuchet ms,sans-serif; font-size: 9pt ;\">
    <tr style =\"background-color:#222222;\">
      <td colspan=\"4\"><div align=\"center\">Entrez les coordonn&eacute;es du champs de ruines visé</div></td>
    </tr>
    <tr style =\"background-color:#111111;\">
      <td width=\"203\"><div align=\"center\">Galaxie</div></td>
      <td width=\"203\"><div align=\"center\">Systeme Solaire </div></td>
      <td width=\"204\" colspan='2'><div align=\"center\">Position </div></td>
    </tr>
    <tr>
      <td><div align=\"center\">
        <input name=\"galaxi\" id=\"galaxi\" type=\"text\" value=\"$galaxi\">
      </div></td>
      <td><div align=\"center\">
        <input name=\"system\" id=\"system\" type=\"text\" value=\"$system\">
      </div></td>
      <td colspan='2'><div align=\"center\">
        <input name=\"position\" id=\"position\" type=\"text\" value=\"$position\">
      </div></td>
    </tr>
    <tr>
      <td rowspan=\"2\">Vaisseaux Ruines : </td>
      <td height=\"39\"><div align=\"left\">Prototype Vaisseau Ruines : 
        <input name=\"vruines\" type=\"text\" id=\"vruines\" value=\"0\">  <a href=\"javascript:Maxvruines();\"> Max </a>
      </div></td>
      <td>X-303B Vaisseau Ruines : 
      <input name=\"vruines2\" type=\"text\" id=\"vruines2\" value=\"0\"> <a href=\"javascript:Maxvruines2();\"> Max </a></td>
	  <td>BC-304B Vaisseau Ruines : 
      <input name=\"vruines3\" type=\"text\" id=\"vruines3\" value=\"0\"> <a href=\"javascript:Maxvruines3();\"> Max </a></td>
    </tr>
    <tr style =\"background-color:#111111;\">
      <td colspan=\"4\" height=\"40\"><div align=\"center\">
        <input type=\"submit\" name=\"Submit\" value=\"Envoyer la sonde\">
      </div></td>
    </tr>
  </table>
</form></div>";

$Test_errorgalaxi = preg_match('!^[0-9]*$!', $galaxi) ;
$Test_errorsystem = preg_match('!^[0-9]*$!', $system) ;
$Test_errorposition = preg_match('!^[0-9]*$!', $position) ;

if ( $system > 100 OR $system < 1 OR  $position > 10 OR $position < 1 OR $Test_errorgalaxi == 0 OR $Test_errorgalaxi == FALSE OR $Test_errorsystem == 0 OR $Test_errorsystem == FALSE OR $Test_errorposition == 0 OR $Test_errorposition == FALSE) {
	echo"<br><br>Message de la salle de contrôle :
	<br><br>Les coordonnées saisies ne correspondent à aucune planète. <br><br>"; 
	echo"$form";
	exit;
}

$sql = "SELECT u.galaxi,u.system,u.position,v.vruines,v.vruines2,v.vruines3 FROM matgraph_vaisseauxruines v,matgraph_users u WHERE v.id='$planete' and u.id='$planete' AND (v.vruines + v.vruines2 + v.vruines3) > 0";
$res = db_mysql_Query($sql)or die('erreur sql<br>'.$sql.'<br>'.mysql_error());
$num = mysql_num_rows($res);
if ($num > 0 ) {
$row = mysql_fetch_array($res); }

if($num == 0 || $vruines > $row[vruines] || $vruines2 > $row[vruines2] || $vruines3 > $row[vruines3]) {
echo"<br><br>Vous n'avez pas assez de vaisseaux ruines pour remplir cette mission.<br><br>";
echo $form; 
exit;
}

if(($vruines + $vruines2 + $vruines3) == 0) {
echo"<br><br>Vous devez selectionner au moins un vaisseau ruines pour remplir cette mission.<br><br>";
echo $form; 
exit;
}

/////////////////////
/////////////
////////
///
//
if($vruines == 0) {
	$vitesse = 4;
} else {
	$vitesse = 2;
}
	/*//cc = coordonnées
	$sqlcc = "SELECT galaxi,system,position FROM matgraph_users WHERE id='$planete' ";
	$rescc = db_mysql_Query($sqlcc)or die('erreur sql<br>'.$sqlcc.'<br>'.mysql_error());
	$rowcc = mysql_fetch_array($rescc);
*/
	$Dif_position = $_POST['position'] - $row[position] ;
	if ($Dif_position < 0) { $Dif_position = -$Dif_position ; }

	$Dif_system = $_POST['system'] - $row[system] ;
	if ($Dif_system < 0) { $Dif_system = -$Dif_system ; }

	$Dif_galaxi = $_POST['galaxi'] - $row[galaxi] ;
	if ($Dif_galaxi < 0) { $Dif_galaxi = -$Dif_galaxi ; }

	if(($Dif_system + $Dif_galaxi + $Dif_position) == 0)
	{
		$temps = (60*4) / $vitesse;
	}elseif(($Dif_system + $Dif_galaxi) == 0)
	{
		$temps_position = $Dif_position*0.5 ;
		$temps = (600 * ( 5 + $temps_position) / $vitesse)/10 ;
	}elseif($Dif_galaxi == 0)
	{
		$temps_position = $Dif_position*0.5 ;
		$temps_system = $Dif_system*1 ;
		$temps = (600 * ( 20 + $temps_position + $temps_system) / $vitesse)/13.5 ;
	}else
	{
		$temps_position = $Dif_position*0.03 ;
		$temps_system = $Dif_system*0.3 ;
		$temps_galaxi = $Dif_galaxi*30 ;
		$temps = (600 * ( 40 + $temps_position + $temps_system + $temps_galaxi) / $vitesse)/10 ;
	}
$temps =  strftime("%Y-%m-%d %H:%M:%S", time()+ (round($temps)));

$requete = "INSERT INTO `matgraph_mobil_vruines` ( `id` , `iddepart` , `galaxi` , `system` , `position` , `orr` , `fer` , `ada` , `vruines` , `vruines2` , `vruines3`, `date` , `mission`)
VALUES (
'', '".$planete."', '".$galaxi."', '".$system."', '".$position."', '0', '0', '0', '".$vruines."', '".$vruines2."', '".$vruines3."', '".$temps."', 'aller'
)";
$resultat = db_mysql_Query($requete)or die('erreur sql<br>'.$requete.'<br>'.mysql_error());

$requete = "UPDATE matgraph_vaisseauxruines SET vruines = vruines - $vruines, vruines2 = vruines2 - $vruines2, vruines3 = vruines3 - $vruines3 WHERE id = '$planete'";
$resultat = db_mysql_Query($requete)or die('erreur sql<br>'.$requete.'<br>'.mysql_error());

echo"<br><br> Le(s) vaisseau(x) ruines ont bien été envoyés.<br><br>";
echo"$form";





exit;

?>
