<?	

////// cout ////////////
//malp
$fer_necessaire_anciens = 500;
$or_necessaire_anciens = 300;
$ada_necessaire_anciens = 1200;
$ura_necessaire_anciens = 150;

//gdremorqueur
$fer_necessaire2_gdremorqueur = 10000;
$or_necessaire2_gdremorqueur = 7500;
$ada_necessaire2_gdremorqueur = 5000;
$ura_necessaire2_gdremorqueur = 2500;

//ptremorqueur
$fer_necessaire2_ptremorqueur = 2000;
$or_necessaire2_ptremorqueur = 1500;
$ada_necessaire2_ptremorqueur = 1000;
$ura_necessaire2_ptremorqueur = 500;

// sonde 
$fer_necessaire2_gdremorqueur = 2000;
$or_necessaire2_gdremorqueur = 1000;
$ada_necessaire2_gdremorqueur = 1500;
$ura_necessaire2_gdremorqueur = 1200;

// vruines 
$fer_necessaire2_vruines = 5000;
$or_necessaire2_vruines = 5000;
$ada_necessaire2_vruines = 20000;
$ura_necessaire2_vruines = 0;

// vruines 2
$fer_necessaire2_vruines2 = 10000;
$or_necessaire2_vruines2 = 10000;
$ada_necessaire2_vruines2 = 20000;
$ura_necessaire2_vruines2 = 30000;

// vruines 3
$fer_necessaire2_vruines2 = 15000;
$or_necessaire2_vruines2 = 15000;
$ada_necessaire2_vruines2 = 27000;
$ura_necessaire2_vruines2 = 36000;


$sql_spe = "SELECT * FROM matgraph_spe WHERE id = $planete";
$res_spe = db_mysql_Query($sql_spe)or die('erreur sql<br>'.$sql_spe.'<br>'.mysql_error());
$num_spe = mysql_numrows($res_spe);
$row_spe = mysql_fetch_array($res_spe);

$sql_vruines = "SELECT * FROM matgraph_vaisseauxruines WHERE id = $planete";
$res_vruines = db_mysql_Query($sql_vruines)or die('erreur sql<br>'.$sql_vruines.'<br>'.mysql_error());
$num_vruines = mysql_numrows($res_vruines);
$row_vruines = mysql_fetch_array($res_vruines);

if( $num_vruines == 0 ) {
			$sql3 = "INSERT INTO matgraph_vaisseauxruines (id,vruines,vruines2,vruines3) VALUES ($planete,0,0,0)";
			$res3 = db_mysql_Query($sql3)or die('erreur sql<br>'.$sql3.'<br>'.mysql_error());
		}


$req = "SELECT * FROM matgraph_constructionspe WHERE findeconstruction < ".time()." AND proprietaire='$planete' "; 
$res = db_mysql_Query($req) or die('erreur sql<br>'.$req.'<br>'.mysql_error());
$num = mysql_num_rows($res);
if ($num >0) {
  While ( $row = mysql_fetch_array($res) ) {
	if( $row[typedef] == "malp" || $row[typedef] == "gdremorqueur" || $row[typedef] == "ptremorqueur" ) {

		$req2 = "SELECT $row[typedef] FROM matgraph_users WHERE id = '$planete'";
		$res2 = db_mysql_Query($req2)or die('erreur sql<br>'.$req2.'<br>'.mysql_error());
		$row2 = mysql_fetch_array($res2); 
	} elseif($row[typedef] == "sonde") {

		$req2 = "SELECT $row[typedef] FROM matgraph_spe WHERE id = '$planete'";
		$res2 = db_mysql_Query($req2)or die('erreur sql<br>'.$req2.'<br>'.mysql_error());
		$row2 = mysql_fetch_array($res2); 
	} else {

		$req2 = "SELECT $row[typedef] FROM matgraph_vaisseauxruines WHERE id = '$planete'";
		$res2 = db_mysql_Query($req2)or die('erreur sql<br>'.$req2.'<br>'.mysql_error());
		$row2 = mysql_fetch_array($res2); 
	}
	
	if($row[typedef] == "malp"){
	$fer_necessaire = $fer_necessaire_anciens;
	$or_necessaire = $or_necessaire_anciens;
	$ada_necessaire = $ada_necessaire_anciens;
	$ura_necessaire = $ura_necessaire_anciens;
	}else if($row[typedef] == "gdremorqueur"){
	$fer_necessaire = $fer_necessaire2_gdremorqueur;
	$or_necessaire = $or_necessaire2_gdremorqueur;
	$ada_necessaire = $ada_necessaire2_gdremorqueur;
	$ura_necessaire = $ura_necessaire2_gdremorqueur;
	}else if($row[typedef] == "ptremorqueur"){
	$fer_necessaire = $fer_necessaire2_ptremorqueur;
	$or_necessaire = $or_necessaire2_ptremorqueur;
	$ada_necessaire = $ada_necessaire2_ptremorqueur;
	$ura_necessaire = $ura_necessaire2_ptremorqueur;
	}else if($row[typedef] == "sonde"){
	$fer_necessaire = $fer_necessaire2_sonde;
	$or_necessaire = $or_necessaire2_sonde;
	$ada_necessaire = $ada_necessaire2_sonde;
	$ura_necessaire = $ura_necessaire2_sonde;
	}else if($row[typedef] == "vruines"){
	$fer_necessaire = $fer_necessaire2_vruines;
	$or_necessaire = $or_necessaire2_vruines;
	$ada_necessaire = $ada_necessaire2_vruines;
	$ura_necessaire = $ura_necessaire2_vruines;
	}else if($row[typedef] == "vruines2"){
	$fer_necessaire = $fer_necessaire2_vruines2;
	$or_necessaire = $or_necessaire2_vruines2;
	$ada_necessaire = $ada_necessaire2_vruines2;
	$ura_necessaire = $ura_necessaire2_vruines2;
	}else if($row[typedef] == "vruines3"){
	$fer_necessaire = $fer_necessaire2_vruines3;
	$or_necessaire = $or_necessaire2_vruines3;
	$ada_necessaire = $ada_necessaire2_vruines3;
	$ura_necessaire = $ura_necessaire2_vruines3;
	}
	
	$Score_spe += $fer_necessaire + $or_necessaire + $ada_necessaire + $ura_necessaire;
	
	if( $row[typedef] == "malp" || $row[typedef] == "gdremorqueur" || $row[typedef] == "ptremorqueur" ) {
		$new_nombre = $row2[$row[typedef]] + 1 ;
	
		$sql3 = "UPDATE matgraph_users SET $row[typedef]='$new_nombre' WHERE id = '$planete'";
		$res3 = db_mysql_Query($sql3)or die('erreur sql<br>'.$sql3.'<br>'.mysql_error());
		$sql4 = "DELETE FROM matgraph_constructionspe WHERE id = $row[id]";
		$res4 = db_mysql_Query($sql4)or die('erreur sql<br>'.$sql4.'<br>'.mysql_error());
	} elseif($row[typedef] == "sonde") {
		if( $num_spe == 0 ) {
			$sql3 = "INSERT INTO matgraph_spe (id) VALUES ($planete)";
			$res3 = db_mysql_Query($sql3)or die('erreur sql<br>'.$sql3.'<br>'.mysql_error());
		}
		$sql3 = "UPDATE matgraph_spe SET $row[typedef]=$row[typedef] + 1 WHERE id = '$planete'";
		$res3 = db_mysql_Query($sql3)or die('erreur sql<br>'.$sql3.'<br>'.mysql_error());
		$sql4 = "DELETE FROM matgraph_constructionspe WHERE id = $row[id]";
		$res4 = db_mysql_Query($sql4)or die('erreur sql<br>'.$sql4.'<br>'.mysql_error());
	} elseif($row[typedef] == "vruines") {
		$sql3 = "UPDATE matgraph_vaisseauxruines SET $row[typedef]=$row[typedef] + 1 WHERE id = '$planete'";
		$res3 = db_mysql_Query($sql3)or die('erreur sql<br>'.$sql3.'<br>'.mysql_error());
		$sql4 = "DELETE FROM matgraph_constructionspe WHERE id = $row[id]";
		$res4 = db_mysql_Query($sql4)or die('erreur sql<br>'.$sql4.'<br>'.mysql_error());
	} elseif($row[typedef] == "vruines2") {
		$sql3 = "UPDATE matgraph_vaisseauxruines SET $row[typedef]=$row[typedef] + 1 WHERE id = '$planete'";
		$res3 = db_mysql_Query($sql3)or die('erreur sql<br>'.$sql3.'<br>'.mysql_error());
		$sql4 = "DELETE FROM matgraph_constructionspe WHERE id = $row[id]";
		$res4 = db_mysql_Query($sql4)or die('erreur sql<br>'.$sql4.'<br>'.mysql_error());
	} elseif($row[typedef] == "vruines3") {
		$sql3 = "UPDATE matgraph_vaisseauxruines SET $row[typedef]=$row[typedef] + 1 WHERE id = '$planete'";
		$res3 = db_mysql_Query($sql3)or die('erreur sql<br>'.$sql3.'<br>'.mysql_error());
		$sql4 = "DELETE FROM matgraph_constructionspe WHERE id = $row[id]";
		$res4 = db_mysql_Query($sql4)or die('erreur sql<br>'.$sql4.'<br>'.mysql_error());
	}
  }
}

//Maj pts

	$req2 = "SELECT pts_flotte FROM matgraph_users WHERE id = '$idlogin'";
	$res2 = db_mysql_Query($req2)or die('erreur sql<br>'.$req2.'<br>'.mysql_error());
	$row2 = mysql_fetch_array($res2); 
	
	$Score_spe += $row2[pts_flotte];
	
	$req_UPDATE = "UPDATE matgraph_users SET pts_flotte = '$Score_spe' WHERE id = '$idlogin'" ;
	$maj=db_mysql_Query($req_UPDATE) or die('Erreur SQL! <br>' . $req_UPDATE . '<br>' . mysql_error()) ;
?>