<?
// retour des vaisseaux de l'Attaquant ------------- CODE AUTONOME ET SIMPLIFIE -------------

$sql_1 = "SELECT date, iddepart, iddestination FROM matgraph_mobil WHERE mission = 'retour' ORDER BY date, iddepart, iddestination" ;
include ('config.php') ;
$res_1 = mysql_query($sql_1) ;
mysql_close() ;
$num_1 = mysql_num_rows($res_1) ;
if ( $num_1 > 0 )
{
	while ( $row_1 = mysql_fetch_array($res_1) ) // rassemble les vaisseaux en flotte
	{
 		if ( $row_1[date] != $date_arrive[count($date_arrive)-1] )
 		{
			$Vol_fer = 0 ; 
			$Vol_or = 0 ; 
			$Vol_ada = 0 ; 
			$Vol_ura = 0 ; 			
 			$date_arrive[] = $row_1[date] ;
 			ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})", $row_1[date], $thetime) ; 
 			$time = mktime($thetime[4],$thetime[5],$thetime[6],$thetime[2],$thetime[3],$thetime[1]) ; 
 			$diftemps = time() - $time ;
 			if ($diftemps >= 0)
 			{
				$Rapport_retour ='<B>Retour de la flotte :</B><br>' ;
 				$sql_2 = "SELECT idjoueur, planete, fer_ressource, or_ressource, ura_ressource, ada_ressource FROM matgraph_users WHERE id = '$row_1[iddepart]'" ;
 				include ('config.php') ;
 				$res_2 = mysql_query($sql_2) or die('Erreur SQL! <br>' . $sql_2 . '<br>'.mysql_error()) ;
 				mysql_close() ;
 				$row_2 = mysql_fetch_array($res_2) ;
 				$sql_3 = "SELECT planete FROM matgraph_users WHERE id = '$row_1[iddestination]'" ;
 				include ('config.php') ;
 				$res_3 = mysql_query($sql_3) or die('Erreur SQL! <br>' . $sql_3 . '<br>'.mysql_error()) ;
 				mysql_close() ;
 				$row_3 = mysql_fetch_array($res_3) ;
 				$sujet = 'Le ' . $row_1[date] . ',<br>flotte de retour sur la planète " ' . $row_2[planete] . ' ",<br>après un assaut victorieux sur la planète " ' . $row_3[planete] . ' "<br>' ;

				$sql_4 = "SELECT * FROM matgraph_mobil WHERE date = '$row_1[date]' AND iddepart = '$row_1[iddepart]' AND mission = 'retour' ORDER BY date, iddepart, iddestination " ;
				include ('config.php') ;
				$res_4 = mysql_query($sql_4) ;
				mysql_close() ;
				while ( $row_4 = mysql_fetch_array($res_4) ) // rassemble les vaisseaux en flotte
				{
					// Remet à disponibilité les vaisseaux
 					$sql_5 = "SELECT nbre FROM matgraph_nbrevaisseaux WHERE planete = '$row_1[iddepart]' AND vaisseau = '$row_4[idvaisseau]'" ;
 					include ('config.php') ;
 					$res_5 = mysql_query($sql_5) or die('Erreur SQL! <br>' . $sql_5 . '<br>'.mysql_error()) ;
 					mysql_close() ;
 					$row_5 = mysql_fetch_array($res_5) ;
					$sql_6 = "SELECT nom FROM matgraph_vaisseaux WHERE iduser = '$row_2[idjoueur]' AND id = '$row_4[idvaisseau]'" ;
					include ('config.php') ;
					$res_6 = mysql_query($sql_6) ;
					mysql_close() ;
					$row_6 = mysql_fetch_array($res_6) ;
					$Rapport_retour .= '- ' .  $row_6['nom'] . ' ( ' . $row_4['nombre'] . ' )<br>' ;				
					$Add_vaisseau = $row_4['nombre'] + $row_5['nbre'] ;
					$req_UPDATE = "UPDATE matgraph_nbrevaisseaux SET nbre='$Add_vaisseau' WHERE planete = '$row_1[iddepart]' AND vaisseau = '$row_4[idvaisseau]'" ;
					include ('config.php') ;
					$maj=mysql_query($req_UPDATE) or die('Erreur SQL! <br>' . $req_UPDATE . '<br>' . mysql_error()) ;
					mysql_close() ;
					// Addition du fret de tous les vaisseaux de retour
					$Vol_fer += $row_4['fret_fer'] ; 
					$Vol_or += $row_4['fret_or'] ; 
					$Vol_ada += $row_4['fret_ada'] ; 
					$Vol_ura += $row_4['fret_ura'] ; 
				}

				// Intègre les ressources du fret				
				$Rapport_retour .= '<br><B>La flotte rapporte un total de ressources de :</B><br>fer : ' . $Vol_fer . '<br>or : ' . $Vol_or . '<br>Cristal : ' . $Vol_ada . '<br>Naquadah : ' . $Vol_ura . '<br>' ;
				$nouveau_fer = $row_2['fer_ressource'] + $Vol_fer ;
				$nouveau_or = $row_2['or_ressource'] + $Vol_or ;
				$nouveau_ada = $row_2['ada_ressource'] + $Vol_ada ;
				$nouveau_ura = $row_2['ura_ressource'] + $Vol_ura ;
				$req_UPDATE = "UPDATE matgraph_users SET fer_ressource = '$nouveau_fer', or_ressource = '$nouveau_or', ura_ressource = '$nouveau_ada', ada_ressource = '$nouveau_ura' WHERE id = '$row_1[iddepart]'" ;
				include ('config.php') ;
				$maj=mysql_query($req_UPDATE) or die('Erreur SQL! <br>' . $req_UPDATE . '<br>' . mysql_error()) ;
				mysql_close() ;

				// Efface les lignes de mobil
				$req_DELETE = "DELETE FROM matgraph_mobil WHERE iddepart = '$row_1[iddepart]' AND date = '$row_1[date]'" ;
				include ('config.php') ;
				$maj=mysql_query($req_DELETE) or die('Erreur SQL! <br>' . $req_DELETE . '<br>' . mysql_error()) ;
				mysql_close() ;

				// ajout du message à l'arrivée
				$req_INSERT = "INSERT INTO matgraph_msg VALUES ('', '$row_1[date]', 'non', '$row_1[iddepart]', '$sujet', '$Rapport_retour')" ;
				include ('config.php') ;
				$maj=mysql_query($req_INSERT) or die('Erreur SQL! <br>' . $req_INSERT . '<br>' . mysql_error()) ;
				mysql_close() ;
 			}
 		}
 	}
}
include ('config.php') ;
?>