<?php
include ('logue.php'); 
include ('entete2.php');
include ('siege.php');
$reptime = time();

$nombre_vaisseaux = htmlentities ($_POST["nombre_vaisseaux"]);

$id = htmlentities ($_POST["id"]);

$req = "SELECT * FROM matgraph_vaisseaux WHERE iduser = $idlogin AND id=$id";
$res = db_mysql_Query($req) or die('Erreur SQL! <br>'.$req.'<br>'.mysql_error());
$rowres = mysql_fetch_array($res);

$fer_necessaire = $rowres['fer'];
$sql9 = "SELECT chantier_lvl FROM matgraph_users WHERE id = '$planete'";
$res9 = db_mysql_Query($sql9) or die('Erreur SQL!<br>'.$sql9.'<br>'.mysql_error());
$row9 = mysql_fetch_array($res9); 
$retard = (round(($fer_necessaire / 4)* $siege_temps_evolution *(bcpow(0.9, $row9[chantier_lvl],3))));
$ada_necessaire = $rowres['ada'];
$or_necessaire = $rowres['orr'];
$ura_necessaire = $rowres['ura'];



$req = "SELECT * FROM matgraph_constructionvaisseaux WHERE findeconstruction > ".time()." AND proprietaire='$planete' ";
$res = db_mysql_Query($req);
$num = mysql_num_rows($res);
if ($num >0) {

$sql = "SELECT findeconstruction FROM matgraph_constructionvaisseaux WHERE proprietaire = '$planete' ORDER BY findeconstruction desc";
$res = db_mysql_Query($sql) or die('Erreur SQL!<br>'.$sql.'<br>'.mysql_error());

$reptime = mysql_result($res, 0);

}

for ($i = 0; $i < $nombre_vaisseaux; $i++) {

  $temps_de_fin = $reptime + ($i+1) * $retard;

$sql = "SELECT fer_ressource,or_ressource,ada_ressource,ura_ressource FROM  matgraph_users WHERE id = '$planete'";
$res = db_mysql_Query($sql);
$row = mysql_fetch_array($res);

$fer_possede = $row[fer_ressource];
if ($fer_possede < $fer_necessaire ) {
	$pas_assez = 1;
}
$or_possede = $row[or_ressource];
if ($or_possede < $or_necessaire ) {
	$pas_assez = 1;
}
$ada_possede = $row[ada_ressource];
if ($ada_possede < $ada_necessaire ) {
	$pas_assez = 1;
}
$ura_possede = $row[ura_ressource];
if ($ura_possede < $ura_necessaire ) {
	$pas_assez = 1;
}

if ($pas_assez == 1) {
	break;
}

$new_fer = $fer_possede - $fer_necessaire ;
$new_or = $or_possede - $or_necessaire ;
$new_ada = $ada_possede - $ada_necessaire ;
$new_ura = $ura_possede - $ura_necessaire ;

$sql = "SELECT pts_flotte FROM matgraph_users WHERE id='$dataid[id]'";
$res = db_mysql_Query($sql)or die('erreur sql<br>'.$sql.'<br>'.mysql_error());
$row = mysql_fetch_array($res); 

$pts_flotte = $row[pts_flotte] + $fer_necessaire + $or_necessaire + $ada_necessaire + $ura_necessaire ;

$sql = "UPDATE matgraph_users SET fer_ressource = $new_fer, or_ressource = $new_or, ada_ressource = $new_ada, ura_ressource = $new_ura WHERE id = '$planete'";
db_mysql_Query($sql) or die('Erreur SQL!<br>'.$sql.'<br>'.mysql_error());
$req = "INSERT INTO matgraph_constructionvaisseaux (id, nom, proprietaire, findeconstruction) VALUES ('','$id','$planete','$temps_de_fin')";
db_mysql_Query($req) or die('Erreur SQL!<br>'.$req.'<br>'.mysql_error());
/*
$sql = "UPDATE matgraph_users set pts_flotte=$pts_flotte WHERE id='$dataid[id]'";
db_mysql_Query($sql) or die('Erreur SQL!<br>'.$sql.'<br>'.mysql_error());
*/
}


$req = "SELECT id,vaisseau FROM matgraph_nbrevaisseaux WHERE planete = '".$planete."'";
$res = db_mysql_Query($req) or die('Erreur SQL! <br>'.$req.'<br>'.mysql_error());
$num = mysql_num_rows($res);
While ( $row = mysql_fetch_array($res) ) {
	$req2 = "SELECT id FROM matgraph_vaisseaux WHERE id = '".$row['vaisseau']."'";
	$res2 = db_mysql_Query($req2) or die('Erreur SQL! <br>'.$req2.'<br>'.mysql_error());
	$num2 = mysql_num_rows($res2);
	if ( $num2 == 0 ) {
		$req2 = "DELETE FROM matgraph_nbrevaisseaux WHERE id = '".$row['id']."'";
		$res2 = db_mysql_Query($req2) or die('Erreur SQL! <br>'.$req2.'<br>'.mysql_error());
	}
}

echo '<script language="javascript" type="text/javascript">
<!--
window.location.replace("chantier.php");
-->
</script>';

?>