<?php
include ('logue.php');
include ('entete2.php');
include ('entete.php');
include ('ressource.php');

$sql2 = "SELECT robo_lvl,chantier_lvl,hangarfer_lvl,hangarfer_evolution,hangaror_lvl,hangaror_evolution,hangarada_lvl,hangarada_evolution,hangaror_lvl,hangarura_lvl,hangarura_evolution,labo_lvl,labo_evolution,fer_energie,or_energie,ada_energie,ura_energie,exfer_energie,exor_energie,central_energie FROM matgraph_users WHERE id='$planete' ";
$res2 = mysql_query($sql2)or die('erreur sql<br>'.$sql2.'<br>'.mysql_error());
$row2 = mysql_fetch_array($res2);

$galaxi = htmlentities ($_POST["galaxi"]);
$system = htmlentities ($_POST["system"]);
$position = htmlentities ($_POST["position"]);


$sql = "SELECT galaxi,system,position,eppz,place,placepris,planete FROM matgraph_users WHERE id='$planete' ";
$res = mysql_query($sql)or die('erreur sql<br>'.$sql.'<br>'.mysql_error());
$row = mysql_fetch_array($res);
$placelibre = $row[place]-$row[placepris];

$eppz = floor($row[eppz]) + 1 ;
$seppz = number_format(($row[eppz] * 60000), 0, ',', ' ');
$eppzff = ($row[eppz] * 60000);

echo"<script language=\"javascript\">
function couteppz() {

  var Besoin_energie = 0;

  if ( Math.round( document.getElementById('galaxi').value ) != $row[galaxi] ) {

    Dif_galaxi = Math.abs ( Math.round(document.getElementById('galaxi').value) - $row[galaxi] ) ;

    Besoin_energie = Dif_galaxi*30000;

  }
  else {

    if ( Math.round(document.getElementById('system').value) != $row[system] ) {

      Dif_system = Math.abs ( Math.round(document.getElementById('system').value) - $row[system] );

      if (Dif_system > 50)
        Besoin_energie = 30000;

      else if (Dif_system > 20)
        Besoin_energie = 20000;

      else if (Dif_system > 10)
        Besoin_energie = 10000;

      else if (Dif_system > 5)
        Besoin_energie = 5000;

      else
        Besoin_energie = 2000;
    }
    else {

      if ( Math.round(document.getElementById('position').value) != $row[position] )
        Besoin_energie = 1000;

    }

  }

  startnb = Besoin_energie.toString();

  imax = Math.ceil( startnb.length / 3);
  endnb = '';
  nombre = new Array();

  for( i = 1; i <= imax; i++) {

    j = startnb.length - ( i * 3 );
    if ( j < 0 ) {
       h = 3 +  j;
       j = 0;
    }
    else {
       h =3;
    }

    nombre[i] = startnb.substr( j , h );

  }

  for( i-=1;  i > 0;  i--) {

    if ( i > 0 )
       endnb+=' ';

    endnb+= nombre[i];

  }

  document.getElementById('couteppz_ligne').innerHTML = endnb;
}

</script>
<style>
input {
border:1px solid #7E7E7E;
background-color:#000000;
font-family:'trebuchet ms',sans-serif;
color:white;
}
textarea {
border:1px solid #7E7E7E;
background-color:#000000;
font-family:'trebuchet ms',sans-serif;
color:white;
}
select, option {
background-color:#000000;
color:white;
}
</style>
<STYLE TYPE=text/css>
.Style1 {color: #0000FF}
.Style2 {color: #FF0000}
</STYLE>";
echo"<div align=\"center\"><form name=\"colo\" method=\"post\" action=\"colonie.php\" target=\"_parent\">
<table align=\"center\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
   <tr>
     <td width=\"100\" rowspan=\"2\" valign=\"middle\">
       <div align=\"center\">
         <input name=\"page\" value=\"" . $PHP_SELF . "\" type=\"hidden\" id=\"page\">
         <select name=\"idcolo\" onchange=submit()>";
include ('colo2.php');

$form = "<div align=\"center\"><br><br>Vous possédez $eppz E.P.P.Z . Un E.P.P.Z contient 60 000 d'energies.<br><br>
<form name=\"form1\" method=\"post\" action=\"coloniser_envoi.php\" target=\"_parent\">
  <table width=\"618\" border=\"0\" cellspacing=\"0\" cellpadding=\"2\" bordercolor=\"#000000\" style=\"font-family:trebuchet ms,sans-serif; font-size: 9pt ;\">
    <tr style =\"background-color:#222222;\">
      <td colspan=\"3\"><div align=\"center\">Entrez les coordonn&eacute;es de la planète à coloniser</div></td>
    </tr>
    <tr style =\"background-color:#111111;\">
      <td width=\"203\"><div align=\"center\">Galaxie</div></td>
      <td width=\"203\"><div align=\"center\">Systeme Solaire </div></td>
      <td width=\"204\"><div align=\"center\">Position </div></td>
    </tr>
    <tr>
      <td><div align=\"center\">
        <input name=\"galaxi\" id=\"galaxi\" type=\"text\" value=\"$galaxi\" onChange=\"couteppz();\">
      </div></td>
      <td><div align=\"center\">
        <input name=\"system\" id=\"system\" type=\"text\" value=\"$system\" onChange=\"couteppz();\">
      </div></td>
      <td><div align=\"center\">
        <input name=\"position\" id=\"position\" type=\"text\" value=\"$position\" onChange=\"couteppz();\">
      </div></td>
    </tr>
    <tr style =\"background-color:#111111;\">
      <td colspan=\"3\" height=\"40\"><div align=\"center\">
        <input type=\"submit\" name=\"Submit\" value=\"Activer la porte des étoiles\">
      </div></td>
    </tr>
  </table>
</form></div>
<table width=\"618\"  border=\"1\" cellspacing=\"0\" cellpadding=\"0\">
    <tr>
      <td align=\"center\">La somme de vos E.P.P.Z fait $seppz .
	  <br>Votre consomation d'energie est de : <b><font color=\"FF0000\"><div name=\"couteppz_ligne\" id=\"couteppz_ligne\">0</div></font></b></td>
    </tr>
  </table>";

$Test_errorgalaxi = preg_match('!^[0-9]*$!', $galaxi) ;
$Test_errorsystem = preg_match('!^[0-9]*$!', $system) ;
$Test_errorposition = preg_match('!^[0-9]*$!', $position) ;
if ( $system > 100 OR $system < 1 OR  $position > 10 OR $position < 1 OR $Test_errorgalaxi == 0 OR $Test_errorgalaxi == FALSE OR $Test_errorsystem == 0 OR $Test_errorsystem == FALSE OR $Test_errorposition == 0 OR $Test_errorposition == FALSE) {
echo"<br><br>Message de la salle de contrôle :
<br><br>La porte des étoiles ne s'est pas activée, les coordonnées saisies ne correspondent à aucune planète.
";
echo"$form";
exit;}

$sql = "
SELECT galaxi,system,position
FROM matgraph_users
WHERE system = (
													SELECT system
													FROM matgraph_users
													WHERE galaxi = (
																						SELECT galaxi
																						FROM matgraph_users
																						ORDER BY galaxi DESC
																						LIMIT 1
																					)
													ORDER BY system DESC
													LIMIT 1
													)
AND galaxi = (
							SELECT galaxi
							FROM matgraph_users
							ORDER BY galaxi DESC
							LIMIT 1
							)
ORDER BY position DESC
LIMIT 1";
$res = mysql_query($sql);
$row = mysql_fetch_array($res);


$galaximax = $row['galaxi'];
$galaximax1 = $galaximax + 1 ;
$systemmax = ($row['system'] + 2);
$positionmax = $row['position'];

// Pas de colonie a plus de 2 galaxie du max
if ( $galaxi > $galaximax1 ) {

echo"<br><br>Il n'y a pas assez de joueurs inscrit sur le jeu pour coloniser ici.";
echo"$form";
exit;}

// Pas de colonie dans la galaxie suivant tant que le systeme 97 de la derniere galaxie a ete colonisé
if ( $galaxi == $galaximax1 AND $systemmax < 99) {

echo"<br><br>Il n'y a pas assez de joueurs inscrit sur le jeu pour coloniser ici.";
echo"$form";
exit;}

// On ne colonise pas plus loin que le dernier systeme colonisé + 1
if ( $galaxi == $galaximax AND $system >= $systemmax) {

echo"<br><br>Il n'y a pas assez de joueurs inscrit sur le jeu pour coloniser ici.";
echo"$form";
exit;}


$sql = "SELECT techrare_colo,mili_nbre FROM matgraph_users WHERE id='$planete'";
$res = mysql_query($sql)or die('erreur sql<br>'.$sql.'<br>'.mysql_error());
$row = mysql_fetch_array($res);
if ($row[mili_nbre] < 1000) {

echo"<br><br>Vous n'avez pas assez de soldats disponible sur cette planète.";
exit;}

if($row[techrare_colo] == "1") {
$nb_colo = 11; }
else {
$nb_colo = 9; }
$req = "SELECT id FROM matgraph_users WHERE idjoueur=$idlogin";
$res = mysql_query($req);
$num = mysql_num_rows($res);
if ($num > $nb_colo) {

echo"<br><br>Vous avez ateint le nombre maximum de colonie.";
exit;}

$sql = "SELECT labo_lvl FROM matgraph_users WHERE id='$planete' ";
$res = mysql_query($sql)or die('erreur sql<br>'.$sql.'<br>'.mysql_error());
$row = mysql_fetch_array($res);
if ($row[labo_lvl] >= 5 ) {


if ($galaxi > 0 AND $system > 0 AND $system < 101 AND $position > 0 AND $position < 11) {
$req = "SELECT login FROM matgraph_users WHERE galaxi='$galaxi' AND system='$system' AND position='$position'";
$res = 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) ) {
		echo"<div align=\"center\">Rapport des membres de l'expédition sur la planète [$galaxi:$system:$position] :
			<br><br>Nous avons passé la porte des étoiles sans encombres.
			<br>Cependant la planète ciblé est déjà habité, nous rebroussons chemin.";
		echo"$form";
		exit;
	}
}
//////////////////////////////////////////////////////////
///////////////////////////////////////////////
///////////////////////////////////
///////////////////////
/////////////////
$sql2 = "SELECT galaxi,system,position,robo_lvl,chantier_lvl,hangarfer_lvl,hangarfer_evolution,hangaror_lvl,hangaror_evolution,hangarada_lvl,hangarada_evolution,hangaror_lvl,hangarura_lvl,hangarura_evolution,labo_lvl,labo_evolution,fer_energie,or_energie,ada_energie,ura_energie,exfer_energie,exor_energie,central_energie FROM matgraph_users WHERE id='$planete' ";
$res2 = mysql_query($sql2)or die('erreur sql<br>'.$sql2.'<br>'.mysql_error());
$row2 = mysql_fetch_array($res2);
$systempla = $row2[system];
if ($system == $systempla AND $galaxi == $row2[galaxi]) {
$Besoin_energie = 100; }
if ($system != $systempla AND $galaxi == $row2[galaxi]) {
$Dif_system = $system - $systempla;
if($Dif_system<0) { $Dif_system = -1*$Dif_system; }

	if ($Dif_system < 101) {
	$Besoin_energie = 3000; }

	if ($Dif_system < 51) {
	$Besoin_energie = 2000; }

	if ($Dif_system < 21) {
	$Besoin_energie = 1000; }

	if ($Dif_system < 11) {
	$Besoin_energie = 500; }

	if ($Dif_system < 6) {
	$Besoin_energie = 200; }

}
if ($galaxi !== $row2[galaxi])
{
	$Dif_galaxi = ($galaxi - $row2[galaxi]);

	if($Dif_galaxi < 0)
	{
		$Dif_galaxi = -1*$Dif_galaxi;
	}

	$Besoin_energie = 3000*$Dif_galaxi;
}

$New_Besoin_energie = $Besoin_energie*10;

$sqlenergie = "SELECT eppz,fer_energie,or_energie,ada_energie,ura_energie FROM matgraph_users WHERE id='$planete' ";
$resenergie = mysql_query($sqlenergie)or die('erreur sql<br>'.$sqlenergie.'<br>'.mysql_error());
$rowenergie = mysql_fetch_array($resenergie);
$eppz = 60000*$rowenergie[eppz];

$New_Besoin_energie2 = number_format($New_Besoin_energie, 0, ',', ' ');
if($eppz < $New_Besoin_energie){

echo"<div align=\"center\">$New_Besoin_energie2 d'energie sont necessaire pour aller à ces coordonnées. Vous devez vous procurer des eppz.";
echo"$form";
exit; }
/////////////////////
/////////////
////////
///
//
if ($position <= 2 ){
$taille = rand (10, 100);
}
if ($position >= 3 AND $position <= 6 ){
$taille = rand (150, 200);
}
if ($position >= 7 AND $position <= 8 ){
$taille = rand (100, 150);
}
if ($position >= 9){
$taille = rand (50, 100);
}
$req = "SELECT planete_img FROM matgraph_users WHERE idjoueur ='$idlogin' ORDER by id DESC limit 1";
$res = mysql_query($req) or die('Erreur SQL! <br>'.$req.'<br>'.mysql_error());
$row = mysql_fetch_array($res);
$img = $row[planete_img] + 1;
if($img == 21) {
$img = 1; }

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

$loginnew = addslashes($row99[login]);

$requete = "INSERT INTO matgraph_users VALUES ('', '$idlogin', '$loginnew', 'no', '$row2[mail]', 'no', 'Colonie', '1', 'no', '0','no', '0000-00-00 00:00:00', '$img', '0', '0', '0', '$galaxi', '$system', '$position', '$taille', '0', '0', '0', '0', '0', '0', now(), '0', '0', '0000-00-00 00:00:00', '600', '0', '0', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '1', '400', '0', '0', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '1', '0', '0', '0', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '1', '0', '0', '0', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '1', '0', '0', '0000-00-00 00:00:00', '0', '0', '0000-00-00 00:00:00', '0', '0', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '0', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '0', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '0', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '0', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '0', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '0', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '0', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '0', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '0', '0', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '1', '0', '0', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '1', '0', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '0', '0', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '0', '0000-00-00 00:00:00', '0', '0000-00-00 00:00:00', '0', '0000-00-00 00:00:00', '0', '0000-00-00 00:00:00', '0', '0000-00-00 00:00:00', '0', '0000-00-00 00:00:00', '0', '0000-00-00 00:00:00', '0', '0000-00-00 00:00:00', '0', '0000-00-00 00:00:00', '0', '0000-00-00 00:00:00', '0', '0000-00-00 00:00:00', '0', '0000-00-00 00:00:00', '0', '0000-00-00 00:00:00', '0', '0000-00-00 00:00:00', '0', '0000-00-00 00:00:00', '0', '0000-00-00 00:00:00', '0', '0000-00-00 00:00:00', '0', '0000-00-00 00:00:00', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0')";
$resultat = mysql_query($requete)or die('erreur sql<br>'.$requete.'<br>'.mysql_error());

$sql99 = "SELECT id,fer_ressource,or_ressource FROM matgraph_users WHERE galaxi='$galaxi' AND system='$system' AND position='$position' AND idjoueur=$idlogin";
$res99 = mysql_query($sql99)or die('erreur sql<br>'.$sql99.'<br>'.mysql_error());
$row99 = mysql_fetch_array($res99);

$req = "SELECT vaisseau FROM matgraph_nbrevaisseaux WHERE planete=$idlogin";
$res = mysql_query($req);
$num = mysql_num_rows($res);
if ($num >0) {
  While ( $row = mysql_fetch_array($res) ) {
$requete = "INSERT INTO matgraph_nbrevaisseaux VALUES ('', '$idlogin', '$row99[id]', '$row[vaisseau]', '0')";
$resultat = mysql_query($requete)or die('erreur sql<br>'.$requete.'<br>'.mysql_error());
}}
$sql = "SELECT mili_nbre FROM matgraph_users WHERE id='$planete'";
$res = mysql_query($sql)or die('erreur sql<br>'.$sql.'<br>'.mysql_error());
$row = mysql_fetch_array($res);
$mili_nbre = $row[mili_nbre] - 1000;

$requete = "UPDATE matgraph_users SET mili_nbre=$mili_nbre WHERE id='$planete'";
$resultat = mysql_query($requete)or die('erreur sql<br>'.$requete.'<br>'.mysql_error());

$New_eppz = (($eppzff - $New_Besoin_energie) / 60000);
$requete = "UPDATE matgraph_users SET eppz=$New_eppz WHERE id='$planete'";
$resultat = mysql_query($requete)or die('erreur sql<br>'.$requete.'<br>'.mysql_error());

echo"Rapport des membres de l'expédition sur la planète [$galaxi:$system:$position]
<br><br>Nous avons passé la porte des étoiles sans encombres.
<br>Nous avons exploré les alentours de la planète et elle semble inhabitée.
<br>Nous commençons nos installations.
<br><br><form name=\"form1\" method=\"post\" action=\"colonies2.php\" target=\"_parent\">
<input type=\"hidden\" name=\"idcolo\" value=\"$row99[id]\">
<input type=\"submit\" name=\"Submit\" value=\"OK\">
</form>";
}
else {

echo"<br><br>Message de la salle de contrôle :
<br><br>La porte des étoiles ne s'est pas activée, les coordonnées saisies ne correspondent à aucune planète.
";
echo"$form";
	}

}
else {
echo"<br><br>Vous n'avez pas de porte des étoiles sur cette planete."; }
?>