<?

include ('logue.php') ;
include ('config.php') ;
include ('entete2.php') ;
include ('fonctions_assaut.php') ;

$debug = 1;
include ('fonctions_utiles.php') ;

Calcul_flotte($planete, date('Y-m-d H:i:s'));

// A FAIRE
// préciser les SELECT ...
// unset() les variables $_SESSION ...

//////////////////////////////
/////////////////////
/////////
////
//

/////////////////
// Fonction Select
// Capteur

$sql = "SELECT (UNIX_TIMESTAMP(matgraph_mobil.date) - UNIX_TIMESTAMP()) AS temps, a.login AS login, a.planete AS 1_planete, d.planete AS 2_planete "
.", SUM(nombre) AS total, mission, matgraph_mobil.date AS date, matgraph_mobil.iddestination AS iddestination, matgraph_mobil.iddepart AS iddepart, matgraph_mobil.voyage AS voyage, 1 AS cible "
."FROM matgraph_mobil, matgraph_users AS a, matgraph_users AS d "
."WHERE iddestination IN (SELECT id FROM matgraph_users WHERE idjoueur = ".$idlogin.") "
//."OR iddepart IN (SELECT id FROM matgraph_users WHERE idjoueur = " .$idlogin.")) "
//."WHERE iddepart = 23 OR iddestination = 23 "
."AND a.id = iddepart AND d.id = iddestination "
."GROUP BY iddepart, iddestination, matgraph_mobil.date "

."UNION "

."SELECT (UNIX_TIMESTAMP(matgraph_mobil.date) - UNIX_TIMESTAMP()) AS temps, d.login AS login, d.planete AS 1_planete, a.planete AS 2_planete "
.", SUM(nombre) AS total, mission, matgraph_mobil.date AS date, matgraph_mobil.iddestination AS iddestination, matgraph_mobil.iddepart AS iddepart, matgraph_mobil.voyage AS voyage, 0 AS cible "
."FROM matgraph_mobil, matgraph_users AS a, matgraph_users AS d "
//."WHERE (iddestination IN (SELECT id FROM matgraph_users WHERE idjoueur = ".$idlogin.") "
."WHERE iddepart IN (SELECT id FROM matgraph_users WHERE idjoueur = " .$idlogin.") "
//."WHERE iddepart = 23 OR iddestination = 23 "
."AND a.id = iddepart AND d.id = iddestination "
."GROUP BY iddepart, iddestination, matgraph_mobil.date "

."ORDER BY date ASC ";

//echo "<br>".$sql."<br>";
$res = db_mysql_Query($sql)or die('erreur sql<br>'.$sql.'<br>'.mysql_error());

$sql2 = "SELECT * FROM `matgraph_espio_sonde` WHERE idespionneur = ".$idlogin." OR idespionne = ".$idlogin." ORDER BY date ASC";
$res2 = db_mysql_Query($sql2)or die('erreur sql<br>'.$sql2.'<br>'.mysql_error());

$sql3 = "SELECT * FROM `matgraph_mobil_vruines` WHERE iddepart IN (SELECT id FROM matgraph_users WHERE idjoueur = ".$idlogin.") ORDER BY date ASC";
$res3 = db_mysql_Query($sql3)or die('erreur sql<br>'.$sql3.'<br>'.mysql_error());


if ((mysql_num_rows($res3) + mysql_num_rows($res2) + mysql_num_rows($res)) == 0 )
{
	echo '<table><tr><td width="20%" style="vertical-align:top;" align="right">-</td><td style="color: #00FF00;vertical-align:middle" align="left">Les Capteurs Interstellaires ne captent aucun vaisseau en approche.<br><br></td></tr>' ;
} else {
	
	echo "<table>";
	
}

if(mysql_num_rows($res) > 0 ) {
	echo "<table>
	<tr>
		<td><b><small>Temps</small></b></td>
		<td><b><small>Flotte</small><b></td>
	</tr>
";

	while( $row = mysql_fetch_array($res) ) {
		
		UNSET ($etimefer);
		UNSET ($edayfer);
		UNSET ($ehourfer);
		UNSET ($eminfer);
		UNSET ($esecfer);

		$etimefer = $row['temps'];
		$eminfer = $etimefer / 60;
		
		if ( floor($eminfer) != $eminfer )
		  $esecfer = floor( ($eminfer - floor( $eminfer )) * 60);
		
		  if ( $eminfer > 60 ) {
		
		    $ehourfer = floor( $eminfer / 60 );
		    $eminfer = $eminfer - ( 60 * $ehourfer );
		
		    if ( $ehourfer > 24 ) {
		      $edayfer = floor( $ehourfer / 24);
		      $ehourfer = $ehourfer - ( 24 * $edayfer );
		    }
		
		  }
		
		$etimefer ="";
		
		if (!empty($edayfer) )
		  $etimefer.=$edayfer."j";
		
		if (!empty($ehourfer) )
		  $etimefer.=$ehourfer."h";
		
		if ($eminfer != 0)
		  $etimefer.= floor($eminfer)."min";
		
		if (!empty($esecfer) && edayfer < 0)
		  $etimefer.=$esecfer."s";

	/* Attaque */

		// Planete du joueur attaque
		if( $row['cible'] == 1 && $row['mission'] == 'aller' ) {

			echo "	<tr style=\"color: #FF0000;\">
		<td><small>Dans ".$etimefer." </small></td>
		<td><small>Vous etes attaque par une flotte de ".$row['total']
		." vaisseaux<br> provenant ".$row['1_planete']." du joueur ".$row['login']
		." sur votre planete ".$row['2_planete']
		.".</small></td>
		
	</tr>";

		}
		// Retour de flotte
		if( $row['cible'] == 0 && $row['mission'] == 'retour' ) {

			echo "	<tr style=\"color: #A6A8C1;\">
		<td><small>Dans ".$etimefer." </small></td>
		<td><small>Une flotte de ".$row['total']
		." vaisseaux<br>revient depuis la planete ".$row['1_planete']." du joueur ".$row['login']
		." sur votre planete ".$row['2_planete']
		.".</small></td>
	</tr>";

		}
		// Attaque du joueur
		if( $row['cible'] == 0 && $row['mission'] == 'aller' ) {

			echo "	<tr style=\"color: #D60079;\">
		<td><small>Dans ".$etimefer." </small></td>
		<td><small>Vous attaquez avec une flotte de ".$row['total']
		." vaisseaux <br />la planete ".$row['1_planete']." du joueur ".$row['login']
		." (départ de ".$row['2_planete']
		.").</td><td><small><form name=\"retour\" method=\"post\" action=\"flotte_retour.php\" target=\"_parent\">
           <input type=\"hidden\" name=\"verif1\" value=\"$row[voyage]\">
           <input type=\"hidden\" name=\"verif2\" value=\"$row[date]\">
           <input type=\"hidden\" name=\"verif3\" value=\"$row[iddepart]\">
           <input type=\"hidden\" name=\"verif4\" value=\"$row[iddestination]\">
           <input type=\"hidden\" name=\"verif5\" value=\"$row[mission]\">
          <br /><input type=\"submit\" name=\"Submit\" value=\"Retour\" Style=\"border:1px solid #7E7E7E;\">
                  </form></small></td>
	</tr>";

		}

	/* Attaque */

	/* Baser */

		if( $row['cible'] == 0 && $row['mission'] == 'baser' ) {

			echo "	<tr style=\"color: #FFFF00;\">
		<td><small>Dans ".$etimefer." </small></td>
		<td><small>Une flotte de ".$row['total']
		." vaisseaux<br>de la planete ".$row['2_planete']." du joueur ".$row['login']." arrive sur votre planete ".$row['1_planete']
		.".</td><td><small><form name=\"retour\" method=\"post\" action=\"flotte_retour.php\" target=\"_parent\">
           <input type=\"hidden\" name=\"verif1\" value=\"$row[voyage]\">
           <input type=\"hidden\" name=\"verif2\" value=\"$row[date]\">
           <input type=\"hidden\" name=\"verif3\" value=\"$row[iddepart]\">
           <input type=\"hidden\" name=\"verif4\" value=\"$row[iddestination]\">
           <input type=\"hidden\" name=\"verif5\" value=\"$row[mission]\">
          <br /><input type=\"submit\" name=\"Submit\" value=\"Retour\" Style=\"border:1px solid #7E7E7E;\">
                  </form></small></td>
	</tr>";

		}

	/* Baser */

	/* Transport */

		if( $row['cible'] == 0 && $row['mission'] == 'transport' ) {

			echo "	<tr style=\"color: #00CC00;\">
		<td><small>Dans ".$etimefer." </small></td>
		<td><small>Une flotte de ".$row['total']
		." vaisseaux<br>de la planete ".$row['1_planete']." du joueur ".$row['login']." transporte des ressources<br>"
		." sur la planete ".$row['2_planete']
		.".</td><td><small><form name=\"retour\" method=\"post\" action=\"flotte_retour.php\" target=\"_parent\">
           <input type=\"hidden\" name=\"verif1\" value=\"$row[voyage]\">
           <input type=\"hidden\" name=\"verif2\" value=\"$row[date]\">
           <input type=\"hidden\" name=\"verif3\" value=\"$row[iddepart]\">
           <input type=\"hidden\" name=\"verif4\" value=\"$row[iddestination]\">
           <input type=\"hidden\" name=\"verif5\" value=\"$row[mission]\">
          <br /><input type=\"submit\" name=\"Submit\" value=\"Retour\" Style=\"border:1px solid #7E7E7E;\">
                  </form></small></td>
	</tr>";

		}

		if( $row['cible'] == 1 && $row['mission'] == 'transport' ) {

			echo "	<tr style=\"color: #2732CD;\">
		<td><small>Dans ".$etimefer." </small></td>
		<td><small>Une flotte de ".$row['total']
		." vaisseaux<br>de la planete ".$row['1_planete']." du joueur ".$row['login']
		."<br>transporte des ressources sur votre planete ".$row['2_planete']
		.".</small></td>
	</tr>";

		}

	/* Transport */

	}
}	
if(mysql_num_rows($res2) > 0 ) {
	echo"<tr>
		<td><b><small>Temps</small></b></td>
		<td><b><small>Sondes</small><b></td>
	</tr>";
	while( $row2 = mysql_fetch_array($res2) ) {
		$datetime = $row2[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})", $datetime, $thetime);
		$time = mktime($thetime[4],$thetime[5],$thetime[6],$thetime[2],$thetime[3],$thetime[1]);
		
		$diftemps = time() - $time;
		
		UNSET ($etimefer);
		UNSET ($edayfer);
		UNSET ($ehourfer);
		UNSET ($eminfer);
		UNSET ($esecfer);

		$etimefer = -$diftemps;
		$eminfer = $etimefer / 60;
		
		if ( floor($eminfer) != $eminfer )
		  $esecfer = floor( ($eminfer - floor( $eminfer )) * 60);
		
		  if ( $eminfer > 60 ) {
		
		    $ehourfer = floor( $eminfer / 60 );
		    $eminfer = $eminfer - ( 60 * $ehourfer );
		
		    if ( $ehourfer > 24 ) {
		      $edayfer = floor( $ehourfer / 24);
		      $ehourfer = $ehourfer - ( 24 * $edayfer );
		    }
		
		  }
		
		$etimefer ="";
		
		if (!empty($edayfer) )
		  $etimefer.=$edayfer."j";
		
		if (!empty($ehourfer) )
		  $etimefer.=$ehourfer."h";
		
		if ($eminfer != 0)
		  $etimefer.= floor($eminfer)."min";
		
		if (!empty($esecfer) && edayfer < 0)
		  $etimefer.=$esecfer."s";
		
		
		if($row2[idespionneur] == $idlogin) {
			$sql5 = "SELECT planete FROM matgraph_users WHERE id='$row2[iddepart]' ";
			$res5 = db_mysql_Query($sql5)or die('erreur sql<br>'.$sql5.'<br>'.mysql_error());
			$row5 = mysql_fetch_array($res5);
			 
			$sql4 = "SELECT planete,login FROM matgraph_users WHERE id='$row2[iddestination]' ";
			$res4 = db_mysql_Query($sql4)or die('erreur sql<br>'.$sql4.'<br>'.mysql_error());
			$row4 = mysql_fetch_array($res4);
			
			echo "	<tr style=\"color: #F58212;\">
			<td><small>Dans ".$etimefer." </small></td>
			<td><small>Une sonde provenant de votre planete ".$row5['planete']
			."<br>va espionner sur la planete ".$row4['planete']." du joueur ".$row4['login']
			.".</small></td>
			</tr>";
		}
		
		if($row2[idespionne] == $idlogin) {
			$sql5 = "SELECT planete,login FROM matgraph_users WHERE id='$row2[iddepart]' ";
			$res5 = db_mysql_Query($sql5)or die('erreur sql<br>'.$sql5.'<br>'.mysql_error());
			$row5 = mysql_fetch_array($res5);
			 
			$sql4 = "SELECT planete FROM matgraph_users WHERE id='$row2[iddestination]' ";
			$res4 = db_mysql_Query($sql4)or die('erreur sql<br>'.$sql4.'<br>'.mysql_error());
			$row4 = mysql_fetch_array($res4);
			
			echo "	<tr style=\"color: #FF0000;\">
			<td><small>Dans ".$etimefer." </small></td>
			<td><small>Une sonde provenant de la planete ".$row5['planete']." du joueur ".$row5['login']
			."<br>arrivera sur votre planete ".$row4['planete']
			.".</small></td>
			</tr>";
		}
	}
}

if(mysql_num_rows($res3) > 0 ) {
	echo"<tr>
		<td><b><small>Temps</small></b></td>
		<td><b><small>Vaisseaux ruines</small><b></td>
	</tr>";
	while( $row3 = mysql_fetch_array($res3) ) {
		$datetime = $row3[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})", $datetime, $thetime);
		$time = mktime($thetime[4],$thetime[5],$thetime[6],$thetime[2],$thetime[3],$thetime[1]);
		
		$diftemps = time() - $time;
		
		UNSET ($etimefer);
		UNSET ($edayfer);
		UNSET ($ehourfer);
		UNSET ($eminfer);
		UNSET ($esecfer);

		$etimefer = -$diftemps;
		$eminfer = $etimefer / 60;
		
		if ( floor($eminfer) != $eminfer )
		  $esecfer = floor( ($eminfer - floor( $eminfer )) * 60);
		
		  if ( $eminfer > 60 ) {
		
		    $ehourfer = floor( $eminfer / 60 );
		    $eminfer = $eminfer - ( 60 * $ehourfer );
		
		    if ( $ehourfer > 24 ) {
		      $edayfer = floor( $ehourfer / 24);
		      $ehourfer = $ehourfer - ( 24 * $edayfer );
		    }
		
		  }
		
		$etimefer ="";
		
		if (!empty($edayfer) )
		  $etimefer.=$edayfer."j";
		
		if (!empty($ehourfer) )
		  $etimefer.=$ehourfer."h";
		
		if ($eminfer != 0)
		  $etimefer.= floor($eminfer)."min";
		
		if (!empty($esecfer) && edayfer < 0)
		  $etimefer.=$esecfer."s";
		  
			$sql5 = "SELECT planete,galaxi,system,position FROM matgraph_users WHERE id='$row3[iddepart]' ";
			$res5 = db_mysql_Query($sql5)or die('erreur sql<br>'.$sql5.'<br>'.mysql_error());
			$row5 = mysql_fetch_array($res5);
			
		$tot = $row3[vruines] + $row3[vruines2];
		
		if($row3[mission] == 'aller') {
		echo "	<tr style=\"color: #EAA25B;\">
		<td><small>Dans ".$etimefer." </small></td>
		<td><small>Votre flotte de ". $tot." vaisseaux ruines de votre planéte $row5[planete] [$row5[galaxi] : $row5[system] : $row5[position]]
		<br> atteindra le champs de ruines [$row3[galaxi] : $row3[system] : $row3[position]]
		.</small></td><td><small><form name=\"retour\" method=\"post\" action=\"flotte_ruines_retour.php\" target=\"_parent\">
           <input type=\"hidden\" name=\"verif1\" value=\"$row3[galaxi]\">
           <input type=\"hidden\" name=\"verif2\" value=\"$row3[system]\">
           <input type=\"hidden\" name=\"verif3\" value=\"$row3[position]\">
           <input type=\"hidden\" name=\"verif4\" value=\"$row3[iddepart]\">
           <input type=\"hidden\" name=\"verif5\" value=\"$row3[mission]\">
           <input type=\"hidden\" name=\"verif6\" value=\"$row3[date]\">
           <input type=\"hidden\" name=\"verif7\" value=\"$row3[vruines]\">
          <br /><input type=\"submit\" name=\"Submit\" value=\"Retour\" Style=\"border:1px solid #7E7E7E;\">
                  </form></small></td>
	</tr>";
		}
		if($row3[mission] == 'retour') {
		echo "	<tr style=\"color: #7AB255;\">
		<td><small>Dans ".$etimefer." </small></td>
		<td><small>Votre flotte de ". $tot." vaisseaux ruines de votre planéte $row5[planete] [$row5[galaxi] : $row5[system] : $row5[position]]
		<br> revient du champs de ruines [$row3[galaxi] : $row3[system] : $row3[position]]
		.</small></td>
		</tr>";
		}
	}
}

echo "</table>";


echo '<br><br>' ;

include ('config.php') ;
include ('entete2.php') ;
?>


