<?php

$sql_start_1 = "SELECT id FROM matgraph_espio_sonde WHERE
(idespionneur = $flotte_espio_idlogin OR idespionne = $flotte_espio_idlogin) AND date <= now()";
$res_start_1 = mysql_query($sql_start_1)or die('erreur sql<br>'.$sql_start_1.'<br>'.mysql_error());
/*
$sql_start_2 = "SELECT id FROM matgraph_espio_sonde WHERE
idespionne = $flotte_espio_idlogin AND date <= now()";
$res_start_2 = mysql_query($sql_start_2)or die('erreur sql<br>'.$sql_start_2.'<br>'.mysql_error());
*/
if( mysql_numrows($res_start_1) > 0 ) {

// Il espionne

	while( $row_sonde = mysql_fetch_array(mysql_query("
SELECT * FROM matgraph_espio_sonde WHERE
(idespionneur = $flotte_espio_idlogin OR idespionne = $flotte_espio_idlogin) AND date <= now() LIMIT 1")) ) {

	// On delete de suite
	mysql_query("DELETE FROM matgraph_espio_sonde WHERE id=$row_sonde[id] LIMIT 1") or die('erreur sql<br>DELETE FROM matgraph_espio_sonde WHERE id=$row_sonde[id] LIMIT 1<br>'.mysql_error());

$idJoueurEspionné = $row_sonde[idespionne] ; 
$idPlaneteEspionné = $row_sonde[iddestination] ; 
$idJoueurEspionneur = $row_sonde[idespionneur] ; 
$idPlaneteEspionneur = $row_sonde[iddepart] ;

$planete = $idPlaneteEspionné ;
$idlogin = $idJoueurEspionné ;
 
/*
$id_espionner = $row_sonde[iddestination] ;
$id_espionner2 = $row_sonde[idespionne] ;
$planete = $row_sonde[iddestination] ;
$dataid['id'] = $row_sonde[idespionne] ;
$idlogin = $dataid['id'] ;
*/

//include ('ressource2.php');
//include ('calcullabo.php');
//include ('chantiercalcul.php');
//include ('centredefcalcul.php');
//include ('centrespecalcul.php');

include ('ressourceapercu.php');

$sqlinfo = "SELECT * FROM matgraph_users WHERE id=$idPlaneteEspionné";
$resinfo = mysql_query($sqlinfo)or die('erreur sql<br>'.$sqlinfo.'<br>'.mysql_error());
$rowinfo = mysql_fetch_array($resinfo); 

$sqlinfo2 = "SELECT * FROM matgraph_users WHERE id=$idJoueurEspionné";
$resinfo2 = mysql_query($sqlinfo2)or die('erreur sql<br>'.$sqlinfo2.'<br>'.mysql_error());
$rowinfo2 = mysql_fetch_array($resinfo2); 

$fer = number_format(floor($rowinfo[fer_ressource]), 0, ',', ' ');
$or = number_format(floor($rowinfo[or_ressource]), 0, ',', ' ');
$ada = number_format(floor($rowinfo[ada_ressource]), 0, ',', ' ');
$ura = number_format(floor($rowinfo[ura_ressource]), 0, ',', ' ');
$ressource = "<table width=\"618\"  border=\"1\" cellspacing=\"0\" cellpadding=\"0\">
  <tr>
    <td>Ressource en Fer </td>
    <td>Ressource en Or </td>
    <td>Ressource en Cristal </td>
    <td>Ressource en Naquadah </td>
  </tr>
  <tr>
    <td>$fer</td>
    <td>$or</td>
    <td>$ada</td>
    <td>$ura</td>
  </tr>";
  
if($rowinfo[fer_lvl] > 0) {
$fer = "<br>Mine de fer ($rowinfo[fer_lvl])"; }
if($rowinfo[exfer_lvl] > 0) {
$exfer = "<br>Centre d'extraction de fer ($rowinfo[exfer_lvl])"; }
if($rowinfo[or_lvl] > 0) {
$or = "<br>Mine d'or ($rowinfo[or_lvl])"; }
if($rowinfo[exor_lvl] > 0) {
$exor = "<br>Centre d'extraction d'or ($rowinfo[exor_lvl])"; }
if($rowinfo[ada_lvl] > 0) {
$ada = "<br>Mine de Cristal ($rowinfo[ada_lvl])"; }
if($rowinfo[ura_lvl] > 0) {
$ura = "<br>Mine de Naquadah ($rowinfo[ura_lvl])"; }
if($rowinfo[central_lvl] > 0) {
$central = "<br>Centrale d'énergie thermique ($rowinfo[central_lvl])"; }
if($rowinfo[fission_lvl] > 0) {
$fission = "<br>Centrale énergétique au Naquadah ($rowinfo[fission_lvl])"; }
if($rowinfo[robo_lvl] > 0) {
$robo = "<br>Usine Robotique ($rowinfo[robo_lvl])"; }
if($rowinfo[mili_lvl] > 0) {
$mili = "<br>Caserne Militaire ($rowinfo[mili_lvl])"; }
if($rowinfo[chantier_lvl] > 0) {
$chantier = "<br>Chantier spatial ($rowinfo[chantier_lvl])"; }
if($rowinfo[defense_lvl] > 0) {
$defense = "<br>Centre Militaire de Defense Interplanétaire ($rowinfo[defense_lvl])"; }
if($rowinfo[labo_lvl] > 0) {
$labo = "<br>Centre de recherches scientifiques ($rowinfo[labo_lvl])"; }
if($rowinfo[terri_lvl] > 0) {
$terri = "<br>Aménagement du territoire ($rowinfo[terri_lvl])"; }
if($rowinfo[silo_lvl] > 0) {
$silo = "<br>Tour de Babel ($rowinfo[silo_lvl])"; }

$batimentsdebut = "<tr>
    <td colspan=\"4\">&nbsp;</td>
  </tr>
  <tr>
    <td colspan=\"4\"><b><u>Bâtiments :<br></b></u>";

$batimentsfin = "</td>
  </tr>";
$batiments ="";
if (!empty($batimentsdebut) )
  $batiments.="$batimentsdebut";
if (!empty($fer) )
  $batiments.="$fer";
if (!empty($exfer) )
  $batiments.="$exfer";
if (!empty($or) )
  $batiments.="$or";
if (!empty($exor) )
  $batiments.="$exor";
if (!empty($ada) )
  $batiments.="$ada";
if (!empty($ura) )
  $batiments.="$ura";
if (!empty($central) )
  $batiments.="$central";
if (!empty($fission) )
  $batiments.="$fission";
if (!empty($robo) )
  $batiments.="$robo";
if (!empty($mili) )
  $batiments.="$mili";
if (!empty($chantier) )
  $batiments.="$chantier";
if (!empty($defense) )
  $batiments.="$defense";
if (!empty($labo) )
  $batiments.="$labo";
if (!empty($terri) )
  $batiments.="$terri";
if (!empty($silo) )
  $batiments.="$silo";
if (!empty($batimentsfin) )
  $batiments.="$batimentsfin";
$flotte ="";  
$flottedebut = "<tr>
    <td colspan=\"4\">&nbsp;</td>
  </tr>
  <tr>
    <td colspan=\"4\"><b><u>Flotte :</b></u>";
$flotte.= "$flottedebut";
$req = "SELECT * FROM matgraph_vaisseaux WHERE iduser = '$idJoueurEspionné' ORDER by id ASC";
$res = mysql_query($req) or die('Erreur SQL! <br>'.$req.'<br>'.mysql_error());
$num = mysql_num_rows($res);
if ( $num > 0 ) {
  $i=1;
  While ( $row = mysql_fetch_array($res) ) {
$sql2 = "SELECT nbre FROM matgraph_nbrevaisseaux WHERE vaisseau='$row[id]' AND planete='$idPlaneteEspionné'";
$res2 = mysql_query($sql2)or die('erreur sql<br>'.$sql2.'<br>'.mysql_error());
$row2 = mysql_fetch_array($res2); 
	if($row[chantier_lvl] == 1){$infra="Infrastructure du X-301";}
      if($row[chantier_lvl] == 2){$infra="Infrastructure du Cargo Teltak";}
      if($row[chantier_lvl] == 4){$infra="Infrastructure de la Navette Bedrosienne";}
      if($row[chantier_lvl] == 5){$infra="Infrastructure du Prométhé";}
      if($row[chantier_lvl] == 7){$infra="Infrastructure du Vaisseau Amiral Goa'ulds";}
      if($row[chantier_lvl] == 10){$infra="Infrastructure du Vaisseau mére Asgaard";}
if($row2[nbre] > 0){
$i = "<br><br><b>$row[nom] ( $row2[nbre] )</b>
<br>Type d'infrastructure : $infra
<br>Puissance de feux : $row[arme]
<br>Boucliers : $row[bouclier]
<br>Coque : $row[coque]
<br>Vitesse : $row[vitesse]
<br>Fret : $row[place]"; 
$flotte.= "$i";}
$i++;}
}
$req = "SELECT * FROM matgraph_nbrevaisseaux WHERE planete = '$idPlaneteEspionné' AND nbre != '0' ORDER by id ASC";
$res = mysql_query($req) or die('Erreur SQL! <br>'.$req.'<br>'.mysql_error());
$num = mysql_num_rows($res);
if ( $num == 0 ) {
$flottevide = " il n'y a pas de flotte à quai sur la planète espionnée.";
$flotte.= "$flottevide"; }

$flottefin = "</td>
  </tr>";
$flotte.= "$flottefin";


$defense ="";  
$defensedebut = "<tr>
    <td colspan=\"4\">&nbsp;</td>
  </tr>
  <tr>
    <td colspan=\"4\"><b><u>Défense :<br></b></u>";
$defense.= "$defensedebut";

if($rowinfo[defcombat] > 0) {
$combat = "<br>Tourelles de Combat ($rowinfo[defcombat])"; }
if($rowinfo[deflasersimple] > 0) {
$lasersimple = "<br>Tourelles laser simple ($rowinfo[deflasersimple])"; }
if($rowinfo[deflaserdouble] > 0) {
$laserdouble = "<br>Tourelle laser à double rayon ($rowinfo[deflaserdouble])"; }
if($rowinfo[defelec] > 0) {
$elec = "<br>Tour électromagnétique ($rowinfo[defelec])"; }
if($rowinfo[defions] > 0) {
$ions = "<br>Mini Satellite à ions ($rowinfo[defions])"; }
if($rowinfo[defplasma] > 0) {
$plasma = "<br>Tour à plasma ($rowinfo[defplasma])"; }
if($rowinfo[defnaqua] > 0) {
$naqua = "<br>Tourelle de Naquadah ($rowinfo[defnaqua])"; }
if($rowinfo[defanciens] > 0) {
$anciens = "<br>Satellite des Anciens ($rowinfo[defanciens])"; }

if (!empty($combat) )
  $defense.="$combat";
if (!empty($lasersimple) )
  $defense.="$lasersimple";
if (!empty($laserdouble) )
  $defense.="$laserdouble";
if (!empty($elec) )
  $defense.="$elec";
if (!empty($ions) )
  $defense.="$ions";
if (!empty($plasma) )
  $defense.="$plasma";
if (!empty($naqua) )
  $defense.="$naqua";
if (!empty($anciens) )
  $defense.="$anciens";

if((empty($combat) ) AND (empty($lasersimple) ) AND (empty($laserdouble) ) AND (empty($elec) ) AND (empty($ions) ) AND (empty($plasma) ) AND (empty($naqua) ) AND (empty($anciens) )) { 
$defensevide = " Aucune defense présente sur cette planète.";
$defense.= "$defensevide"; }

$defensefin = "</td>
  </tr>";
$defense.= "$defensefin";

$technologies ="";  
$technologiesdebut = "<tr>
    <td colspan=\"4\">&nbsp;</td>
  </tr>
  <tr>
    <td colspan=\"4\"><b><u>Technologies :<br></b></u>";
$technologies.= "$technologiesdebut";

if($rowinfo2[techinfra_lvl] > 0) {
$techinfra_lvl = "<br>Technologie d'infrastructure des Vaisseaux ($rowinfo2[techinfra_lvl])"; }
if (!empty($techinfra_lvl) )
  $technologies.="$techinfra_lvl";
  
if($rowinfo2[techenergie_lvl] > 0) {
$techenergie_lvl = "<br>Technologie de maîtrise de l'Energie ($rowinfo2[techenergie_lvl])"; }
if (!empty($techenergie_lvl) )
  $technologies.="$techenergie_lvl";

if($rowinfo2[techlaser_lvl] > 0) {
$techlaser_lvl = "<br>Technologie de Laser ($rowinfo2[techlaser_lvl])"; }
if (!empty($techlaser_lvl) )
  $technologies.="$techlaser_lvl";
  
if($rowinfo2[techions_lvl] > 0) {
$techions_lvl = "<br>Technologie d'ions ($rowinfo2[techions_lvl])"; }
if (!empty($techions_lvl) )
  $technologies.="$techions_lvl";

if($rowinfo2[techplasma_lvl] > 0) {
$techplasma_lvl = "<br>Technologie de plasma ($rowinfo2[techplasma_lvl])"; }
if (!empty($techplasma_lvl) )
  $technologies.="$techplasma_lvl";
  
if($rowinfo2[techordi_lvl] > 0) {
$techordi_lvl = "<br>Technologie Salle de contrôle ($rowinfo2[techordi_lvl])"; }
if (!empty($techordi_lvl) )
  $technologies.="$techordi_lvl";
  
if($rowinfo2[techcombustion_lvl] > 0) {
$techcombustion_lvl = "<br>Réacteur à combustion ($rowinfo2[techcombustion_lvl])"; }
if (!empty($techcombustion_lvl) )
  $technologies.="$techcombustion_lvl";
  
if($rowinfo2[techAsgaards_lvl] > 0) {
$techAsgaards_lvl = "<br>Réacteurs Asgards ($rowinfo2[techAsgaards_lvl])"; }
if (!empty($techAsgaards_lvl) )
  $technologies.="$techAsgaards_lvl";
  
if($rowinfo2[techhyperespace_lvl] > 0) {
$techhyperespace_lvl = "<br>Technologie des Réacteur hyperespace ($rowinfo2[techhyperespace_lvl])"; }
if (!empty($techhyperespace_lvl) )
  $technologies.="$techhyperespace_lvl";
  
if($rowinfo2[techespio1_lvl] > 0) {
$techespio1_lvl = "<br>Technologie Espionnage de Type 1 ($rowinfo2[techespio1_lvl])"; }
if (!empty($techespio1_lvl) )
  $technologies.="$techespio1_lvl";
  
if($rowinfo2[techespio2_lvl] > 0) {
$techespio2_lvl = "<br>Technologie Espionnage de Type 2 ($rowinfo2[techespio2_lvl])"; }
if (!empty($techespio2_lvl) )
  $technologies.="$techespio2_lvl";
  
if($rowinfo2[techArmement_lvl] > 0) {
$techArmement_lvl = "<br>Technologie de l'Armement ($rowinfo2[techArmement_lvl])"; }
if (!empty($techArmement_lvl) )
  $technologies.="$techArmement_lvl";
  
if($rowinfo2[techBouclier_lvl] > 0) {
$techBouclier_lvl = "<br>Technologie Bouclier de protection ($rowinfo2[techBouclier_lvl])"; }
if (!empty($techBouclier_lvl) )
  $technologies.="$techBouclier_lvl";
  
if($rowinfo2[techcoque_lvl] > 0) {
$techcoque_lvl = "<br>Technologie de renforcement de la coque ($rowinfo2[techcoque_lvl])"; }
if (!empty($techcoque_lvl) )
  $technologies.="$techcoque_lvl";
  
if($rowinfo2[techproAsgaard_lvl] > 0) {
$techproAsgaard_lvl = "<br>Technologie Asgard ($rowinfo2[techproAsgaard_lvl])"; }
if (!empty($techproAsgaard_lvl) )
  $technologies.="$techproAsgaard_lvl";
  
if($rowinfo2[techproAncien_lvl] > 0) {
$techproAncien_lvl = "<br>Technologie \"Les Anciens\" ($rowinfo2[techproAncien_lvl])"; }
if (!empty($techproAncien_lvl) )
  $technologies.="$techproAncien_lvl";
  
if($rowinfo2[technaqua_lvl] > 0) {
$technaqua_lvl = "<br>Technologie de Naquadah ($rowinfo2[technaqua_lvl])"; }
if (!empty($technaqua_lvl) )
  $technologies.="$technaqua_lvl";
 
if($rowinfo2[techanti_lvl] > 0) {
$techanti_lvl = "<br>Technologie Champs Anti-gravité ($rowinfo2[techanti_lvl])"; }
if (!empty($techanti_lvl) )
  $technologies.="$techanti_lvl";
  

if((empty($techinfra_lvl) ) AND (empty($techenergie_lvl) ) AND (empty($techlaser_lvl) ) AND (empty($techions_lvl) ) AND (empty($techplasma_lvl) ) AND (empty($techordi_lvl) ) AND (empty($techcombustion_lvl) ) AND (empty($techAsgaards_lvl) ) AND (empty($techhyperespace_lvl) ) AND (empty($techespio1_lvl) ) AND (empty($techespio2_lvl) ) AND (empty($techArmement_lvl) ) AND (empty($techBouclier_lvl) ) AND (empty($techcoque_lvl) ) AND (empty($techproAsgaard_lvl) ) AND (empty($techproAncien_lvl) ) AND (empty($technaqua_lvl) ) AND (empty($techanti_lvl) )) { 
$technologiesvide = " Aucune technologie présente sur cette planète.";
$technologies.= "$technologiesvide"; }

$technologiesfin = "</td>
  </tr>";
$technologies.= "$technologiesfin";

$militaires = "<tr>
    <td colspan=\"4\">&nbsp;</td>
  </tr>
  <tr>
    <td colspan=\"4\"><b><u>Militaires :<br></b></u>
	". number_format(floor($rowinfo[mili_nbre]), 0, ',', ' ') ."
	</td>
  </tr>";

$sql3 = "SELECT techespio1_lvl FROM matgraph_users WHERE id=$idJoueurEspionneur";
$res3 = mysql_query($sql3)or die('erreur sql<br>'.$sql3.'<br>'.mysql_error());
$row3 = mysql_fetch_array($res3);   

$sql99 = "SELECT techespio1_lvl FROM matgraph_users WHERE id=$idJoueurEspionné";
$res99 = mysql_query($sql99)or die('erreur sql<br>'.$sql99.'<br>'.mysql_error());
$row99 = mysql_fetch_array($res99);  


$y = rand(1,100);	
if($y < 11) {
$x = 1; }
else {
$x= 0; }

$Dif_espio2 = $row3[techespio1_lvl] - $row99[techespio1_lvl] + $x;

if($id_espionner2 == "1") { echo"$Dif_espio2 = $row3[techespio1_lvl] - $row99[techespio1_lvl] + $x "; }

$sujet = "$rowinfo[planete] ($rowinfo[login]) [$rowinfo[galaxi]:$rowinfo[system]:$rowinfo[position]]" ;
if ($Dif_espio2 > 3) {
$msg = "";
if (!empty($ressource) )
$msg.="$ressource";
$msg.="$flotte";
$msg.="$defense";
$msg.="$batiments";
$msg.="$militaires";
$msg.="$technologies";
$msg.="</table>";
$msg2 = addslashes($msg);
$requete = "INSERT INTO  matgraph_espio1 VALUES ('', 'non', now(), '$row_sonde[idespionneur]', '$row_sonde[iddepart]', '".addslashes($sujet)."', '".addslashes($msg2)."')";
$resultat = mysql_query($requete)or die('erreur sql<br>'.$requete.'<br>'.mysql_error()); }

if ($Dif_espio2 == 3) {
$msg = "";
if (!empty($ressource) )
$msg.="$ressource";
$msg.="$flotte";
$msg.="$defense";
$msg.="$batiments";
$msg.="$militaires";
$msg.="</table>";
$msg2 = addslashes($msg);
$requete = "INSERT INTO  matgraph_espio1 VALUES ('', 'non', now(), '$row_sonde[idespionneur]', '$row_sonde[iddepart]', '".addslashes($sujet)."', '".addslashes($msg2)."')";
$resultat = mysql_query($requete)or die('erreur sql<br>'.$requete.'<br>'.mysql_error()); }

if ($Dif_espio2 == 2) {
$msg = "";
if (!empty($ressource) )
$msg.="$ressource";
$msg.="$flotte";
$msg.="$defense";
$msg.="</table>";
$msg2 = addslashes($msg);
$requete = "INSERT INTO  matgraph_espio1 VALUES ('', 'non', now(), '$row_sonde[idespionneur]', '$row_sonde[iddepart]', '".addslashes($sujet)."', '".addslashes($msg2)."')";
$resultat = mysql_query($requete)or die('erreur sql<br>'.$requete.'<br>'.mysql_error()); }

if ($Dif_espio2 == 1) {
$msg = "";
if (!empty($ressource) )
$msg.="$ressource";
$msg.="$flotte";
$msg.="</table>";
$msg2 = addslashes($msg);
$requete = "INSERT INTO  matgraph_espio1 VALUES ('', 'non', now(), '$row_sonde[idespionneur]', '$row_sonde[iddepart]', '".addslashes($sujet)."', '".addslashes($msg2)."')";
$resultat = mysql_query($requete)or die('erreur sql<br>'.$requete.'<br>'.mysql_error()); }

if ($Dif_espio2 == 0) {
$msg = "";
if (!empty($ressource) )
$msg.="$ressource";
$msg.="</table>";
$msg2 = addslashes($msg);
$requete = "INSERT INTO  matgraph_espio1 VALUES ('', 'non', now(), '$row_sonde[idespionneur]', '$row_sonde[iddepart]', '".addslashes($sujet)."', '".addslashes($msg2)."')";
$resultat = mysql_query($requete)or die('erreur sql<br>'.$requete.'<br>'.mysql_error()); }

if ($Dif_espio2 < 0 AND $Dif_espio2 >= "-2") {
$msg = "";
if (!empty($ressource) )
$msg.="$ressource";
$msg.="</table>";
$msg2 = addslashes($msg);
$requete = "INSERT INTO  matgraph_espio1 VALUES ('', 'non', now(), '$row_sonde[idespionneur]', '$row_sonde[iddepart]', '".addslashes($sujet)."', '".addslashes($msg2)."')";
$resultat = mysql_query($requete)or die('erreur sql<br>'.$requete.'<br>'.mysql_error()); }

if ($Dif_espio2 < "-2") {
$msg = "";
if (!empty($ressource) )
$msg.="Votre niveau d'espionnage est trop faible.";
$msg.="</table>";
$msg2 = addslashes($msg);
$requete = "INSERT INTO  matgraph_espio1 VALUES ('', 'non', now(), '$row_sonde[idespionneur]', '$row_sonde[iddepart]', '".addslashes($sujet)."', '".addslashes($msg2)."')";
$resultat = mysql_query($requete)or die('erreur sql<br>'.$requete.'<br>'.mysql_error()); }


$sql4 = "SELECT galaxi,system,position FROM matgraph_users WHERE id=$row_sonde[iddepart]";
$res4 = mysql_query($sql4)or die('erreur sql<br>'.$sql4.'<br>'.mysql_error());
$row4 = mysql_fetch_array($res4);  

$title = "<font color=\"FF0000\">VOUS AVEZ ETE ESPIONNE</font><br>";
$topic = "Salle de contrôle :<br><br>
Une sonde spatiale est arrivé en orbite autour de la planète et a été détruit. Cependant il est possible qu’elle est transmis des informations à sa planète d’origine en [$row4[galaxi]:$row4[system]:$row4[position]] a propos de votre planete $rowinfo[planete] [$rowinfo[galaxi]:$rowinfo[system]:$rowinfo[position]]";

$requete = "INSERT INTO  matgraph_contreespio VALUES ('',now(), 'non', '$id_espionner2', '$id_espionner', '".addslashes($title)."', '".addslashes($topic)."')";
$resultat = mysql_query($requete)or die('erreur sql<br>'.$requete.'<br>'.mysql_error());

}

}

$idlogin = $flotte_espio_idlogin;

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