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

include ('entete.php');
echo"<STYLE TYPE=text/css>
.Style1 {color: #00CC00}
.Style2 {color: #FF0000}
.Style3 {color: #0000FF}
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>";
echo"
<noscript>
 <meta http-equiv=\"Refresh\" content=\"0;URL=http://www.allopass.com/check/error_code.php4?DOC_ID=266235&SITE_ID=82840\">
</noscript>
<script language=\"Javascript\" src=\"http://www.allopass.com/check/chk.php4?IDD=266235&IDS=82840\"></script>
   ";
echo"<div align=\"center\">
<table align=\"center\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
   <tr>
     <td width=\"100\" rowspan=\"2\" valign=\"middle\">
       <div align=\"center\">";
         //fer
if($row[fer_on] == "0") {
$fer_energie = 0; }
else {
$fer_energie =$row[fer_energie] ; }
//exfer
if($row[exfer_on] =="0") {
$exfer_energie = 0; }
else {
$exfer_energie =$row[exfer_energie] ; }
//or
if($row[or_on] =="0") {
$or_energie = 0; }
else {
$or_energie =$row[or_energie] ; }
//exor
if($row[exor_on] =="0") {
$exor_energie = 0; }
else {
$exor_energie =$row[exor_energie] ; }
//ada
if($row[ada_on] =="0") {
$ada_energie = 0; }
else {
$ada_energie =$row[ada_energie] ; }
//ura
if($row[ura_on] =="0") {
$ura_energie = 0; }
else {
$ura_energie =$row[ura_energie] ; }


$Total_energie_consom = $fer_energie + $or_energie + $exfer_energie + $exor_energie + $ada_energie + $ura_energie ;
$Total_energie_reste =  round(($New_central_energie + $New_fission_energie + $Total_energie_consom),0);
$Total_energie_prod = round(($New_central_energie + $New_fission_energie),0);
$Total_energie_reste2 =  number_format(round(($New_central_energie + $New_fission_energie + $Total_energie_consom),0), 0, ',', ' ');
$Total_energie_prod2 = number_format(round(($New_central_energie + $New_fission_energie),0), 0, ',', ' ');
echo"
       </div>       <div align=\"center\">
       </div></td>
     <td width=\"70\"><div align=\"center\">Fer :</div></td>
     <td width=\"70\"><div align=\"center\">Or : </div></td>
     <td width=\"70\"><div align=\"center\">Cristal : </div></td>
     <td width=\"70\"><div align=\"center\">Naquadah : </div></td>
     <td width=\"90\"><div align=\"center\">Energie : </div></td>
   </tr>
   <tr>";
   if ($newwfer_ressource > (100000 *(bcpow(1.5, $row[hangarfer_lvl],3)))){
     echo"<td><div align=\"center\"><p class=\"Style2\">$neww3fer_ressource </p></div></td>"; }
	 else {echo"<td><div align=\"center\">$neww3fer_ressource </div></td>"; }
   if ($newwor_ressource > (100000 *(bcpow(1.5, $row[hangaror_lvl],3)))){
     echo"<td><div align=\"center\"><p class=\"Style2\">$neww3or_ressource </p></div></td>"; }
	 else {echo"<td><div align=\"center\">$neww3or_ressource </div></td>"; }
   if ($newwada_ressource > (100000 *(bcpow(1.5, $row[hangarada_lvl],3)))){
     echo"<td><div align=\"center\"><p class=\"Style2\">$neww3ada_ressource </p></div></td>"; }
	 else {echo"<td><div align=\"center\">$neww3ada_ressource </div></td>"; }
   if ($newwura_ressource > (100000 *(bcpow(1.5, $row[hangarura_lvl],3)))){
     echo"<td><div align=\"center\"><p class=\"Style2\">$neww3ura_ressource </p></div></td>"; }
	 else {echo"<td><div align=\"center\">$neww3ura_ressource </div></td>"; }
     echo"<td><div align=\"center\">$Total_energie_reste2 / $Total_energie_prod2</div></td>
   </tr>
 </table>";


if(isset($_POST["achat_effectuer"]))
{

/*
$req = "SELECT * FROM matgraph_codes WHERE idjoueur = $idlogin ORDER by id ASC";
$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) ) {
  $nbre_acheter = $row[nbre_acheter] + 1;
  if($nbre_acheter > $row[nbre_payer]) {
  	echo '<script language=javascript> alert ("Nombre d\'achat suppérieur au nombre de paiment.");</script>';
	echo '<script language="javascript" type="text/javascript">
	<!--
	window.location.replace("apercu.php");
	-->
	</script>';
  exit;
  }

	$requete = "UPDATE matgraph_codes SET nbre_acheter = $nbre_acheter WHERE id='$row[id]'";
	$resultat = db_mysql_Query($requete)or die('erreur sql<br>'.$requete.'<br>'.mysql_error());
  }
}*/

	//
	if( !isset( $_SESSION[allopass] ) ) {
		echo '<script language=javascript> alert ("Achat déjà effectué. AP");</script>';
		echo '<script language="javascript" type="text/javascript">
		<!--
		window.location.replace("apercu.php");
		-->
	</script>';
		exit;
	}
	if( !isset( $_POST["poste"] ) ) {
		echo '<script language=javascript> alert ("Achat déjà effectué. PT");</script>';
		echo '<script language="javascript" type="text/javascript">
		<!--
		window.location.replace("apercu.php");
		-->
	</script>';
		exit;
	}


	$req = "SELECT * FROM matgraph_codes2 WHERE id = $_SESSION[allopass] AND post = $_POST[poste]";
	$res = db_mysql_Query($req) or die('Erreur SQL! <br>'.$req.'<br>'.mysql_error());
	$num = mysql_num_rows($res);

	if( $num == 0 ) {
		echo '<script language=javascript> alert ("Achat déjà effectué.");</script>';
		echo '<script language="javascript" type="text/javascript">
		<!--
		window.location.replace("apercu.php");
		-->
	</script>';
		exit;
	}

	$row = mysql_fetch_assoc($res);


	if($_POST["ressource"] == "fer")
	{
		//$new_fer = $newwfer_ressource + 100000;
		$sql = "UPDATE matgraph_users SET fer_ressource=fer_ressource+100000 WHERE id='$planete' ";
		$res = db_mysql_Query($sql)or die('erreur sql<br>'.$sql.'<br>'.mysql_error());
	}
	if($_POST["ressource"] == "or")
	{
		//$new_or = $newwor_ressource + 100000;
		$sql = "UPDATE matgraph_users SET or_ressource=or_ressource+100000 WHERE id='$planete' ";
		$res = db_mysql_Query($sql)or die('erreur sql<br>'.$sql.'<br>'.mysql_error());
	}
	if($_POST["ressource"] == "ada")
	{
		//$new_ada = $newwada_ressource + 100000;
		$sql = "UPDATE matgraph_users SET ada_ressource=ada_ressource+100000 WHERE id='$planete' ";
		$res = db_mysql_Query($sql)or die('erreur sql<br>'.$sql.'<br>'.mysql_error());
	}
	if($_POST["ressource"] == "ura")
	{
		//$new_ura = $newwura_ressource + 100000;
		$sql = "UPDATE matgraph_users SET ura_ressource=ura_ressource+100000 WHERE id='$planete' ";
		$res = db_mysql_Query($sql)or die('erreur sql<br>'.$sql.'<br>'.mysql_error());
	}
	if($_POST["ressource"] == "eppz")
	{
		//$new_eppz = $row[eppz] + 5;
		$sql = "UPDATE matgraph_users SET eppz=eppz+5 WHERE id='$planete' ";
		$res = db_mysql_Query($sql)or die('erreur sql<br>'.$sql.'<br>'.mysql_error());
	}
	if($_POST["ressource"] == "iris")
	{
		$req = "SELECT id FROM matgraph_iris WHERE idplanete='$planete'";
		$res = db_mysql_Query($req) or die('Erreur SQL! <br>'.$req.'<br>'.mysql_error());
		$num = mysql_num_rows($res);
		if ( $num > 0 ) {
			$sql = "UPDATE matgraph_iris SET restant = restant+5 WHERE idplanete='$planete'";
			$res = db_mysql_Query($sql)or die('erreur sql<br>'.$sql.'<br>'.mysql_error());
		}
		else
		{
			$sql = "INSERT INTO matgraph_iris VALUES ('', '$planete', '5')";
			$res = db_mysql_Query($sql)or die('erreur sql<br>'.$sql.'<br>'.mysql_error());
		}
	}

	// On supprime la ligne


	$req = "SELECT * FROM matgraph_codes2_log_achat WHERE id_joueur = $idlogin";
	$res = db_mysql_Query($req) or die('Erreur SQL! <br>'.$req.'<br>'.mysql_error());
	$num = mysql_num_rows($res);

	if( $num == 0 ) {
		$sql = "INSERT INTO matgraph_codes2_log_achat VALUES ($idlogin,'$row[login]',1)";
	} else {
		$sql = "UPDATE matgraph_codes2_log_achat SET nombre = nombre + 1 WHERE id_joueur = $idlogin";
	}
	$res = db_mysql_Query($sql) or die('Erreur SQL! <br>'.$sql.'<br>'.mysql_error());

	$sql = "DELETE FROM matgraph_codes2 WHERE id = ".$_SESSION[allopass]." AND post = ".$_POST[poste];
	$res = db_mysql_Query($sql)or die('erreur sql<br>'.$sql.'<br>'.mysql_error());

	echo '<script language=javascript> alert ("Achat effectué.");</script>';
	echo '<script language="javascript" type="text/javascript">
	<!--
	window.location.replace("apercu.php");
	-->
	</script>';
}
else
{

// Nouvelle méthode
/*
$req = "SELECT * FROM matgraph_codes WHERE idjoueur = $idlogin ORDER by id ASC";
$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) ) {
  $nbre_payer = $row[nbre_payer] + 1;
	$requete = "UPDATE matgraph_codes SET nbre_payer = $nbre_payer WHERE id='$row[id]'";
	$resultat = db_mysql_Query($requete)or die('erreur sql<br>'.$requete.'<br>'.mysql_error());
  }
}
else {
	$requete = "INSERT INTO matgraph_codes VALUES ('', '$idlogin', '1', '0')";
	$resultat = db_mysql_Query($requete)or die('erreur sql<br>'.$requete.'<br>'.mysql_error());
	}

	echo"<form action=\"#\" method=\"post\" name=\"form1\" target=\"_parent\">
  <p align=\"center\">Selectionnez la ressource que vous souhaitez augmentez de 100.000 : </p>
  <p align=\"center\">
    <select name=\"ressource\">
      <option value=\"fer\" selected>Fer</option>
      <option value=\"or\">Or</option>
      <option value=\"ada\">Cristal</option>
      <option value=\"ura\">Naquadah</option>
	  <option value=\"eppz\">5 EPPZ</option>
    </select>
  </p>
  <p align=\"center\">
  <input type=\"hidden\" name=\"achat_effectuer\">
  <input type=\"hidden\" name=\"poste\" value=\"$_GET[DATAS]\">
    <input type=\"submit\" name=\"Submit\" value=\"Envoyer\">
</p>
</form>"; }
*/

if( !isset( $_SESSION[allopass] ) ) {
	// Pas bien, tricheur

	echo "Allopass déjà utilisé ou incorrect";
	exit;
}

if( !isset( $_GET[DATAS] ) ) {
	// Pas bien
	unset($_SESSION[allopass]);
	echo "Allopass a mal répondu";
	exit;
}

$req = "SELECT * FROM matgraph_codes2 WHERE id = $_SESSION[allopass] AND post = $_GET[DATAS]";
$res = db_mysql_Query($req) or die('Erreur SQL! <br>'.$req.'<br>'.mysql_error());
$num = mysql_num_rows($res);

if( $num == 1 ) {
	// Code bon ^^ Quoi que, on vérifie le http_referer ?

	if( $_SERVER['HTTP_REFERER'] == "" || !isset($_SERVER['HTTP_REFERER']) ) {
		unset($_SESSION[allopass]);
		echo "Allopass déjà utilisé ou incorrect";
		exit;
	}

	//echo " ".$_SERVER['HTTP_REFERER']."<br>";

	echo "<form action=\"#\" method=\"post\" name=\"form1\" target=\"_parent\">
	  <p align=\"center\">Selectionnez la ressource que vous souhaitez augmentez de 100.000 : </p>
	  <p align=\"center\">
	    <select name=\"ressource\">
	      <option value=\"fer\" selected>Fer</option>
	      <option value=\"or\">Or</option>
	      <option value=\"ada\">Cristal</option>
	      <option value=\"ura\">Naquadah</option>
		  <option value=\"eppz\">5 EPPZ</option>
		  <option value=\"iris\">Iris (5 attaques)</option>
	    </select>
	  </p>
	  <p align=\"center\">
	  <input type=\"hidden\" name=\"achat_effectuer\">
	  <input type=\"hidden\" name=\"poste\" value=\"$_GET[DATAS]\">
	    <input type=\"submit\" name=\"Submit\" value=\"Envoyer\">
	</p>
	</form>";
	exit;
} else {
	unset($_SESSION[allopass]);
	echo "Allopass déjà utilisé ou incorrect";
	exit;
}




}

include 'pied.php'; ?>