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

// chantier
if ($row[chantier_lvl] =="0") {
$chantier_lvl = "0";
}
else {
$chantier_lvl = $row[chantier_lvl];
}
// labo
if ($row[labo_lvl] =="0") {
$labo_lvl = "0";
}
else {
$labo_lvl = $row[labo_lvl];
}

$sql = "SELECT ptremorqueur,gdremorqueur,labo_lvl,malp,chantier_lvl,hangarfer_lvl,hangarfer_evolution,hangaror_lvl,hangaror_evolution,hangarada_lvl,hangarada_evolution,hangaror_lvl,hangarura_lvl,hangarura_evolution,deflasersimple,deflaserdouble,defelec,defions,defplasma,defnaqua,defanciens,defense_lvl,defense_evolution,defcombat,robo_lvl,labo_lvl,labo_evolution,fer_energie,or_energie,ada_energie,ura_energie,exfer_energie,exor_energie,central_energie  FROM matgraph_users WHERE id='$planete' ";
$res = db_mysql_Query($sql)or die('erreur sql<br>'.$sql.'<br>'.mysql_error());
$row = mysql_fetch_array($res); 

$sql2 = "SELECT pts,techenergie_lvl,techenergie_evolution,techlaser_lvl,techlaser_evolution,techions_lvl,techions_evolution,techplasma_lvl,techplasma_evolution,techordi_lvl,techordi_evolution,techcombustion_lvl,techcombustion_evolution,techAsgaards_lvl,techAsgaards_evolution,techhyperespace_lvl,techhyperespace_evolution,techespio1_lvl,techespio1_evolution,techespio2_lvl,techespio2_evolution,techArmement_lvl,techArmement_evolution,techBouclier_lvl,techBouclier_evolution,techcoque_lvl,techcoque_evolution,robo_lvl,labo_lvl,labo_evolution,fer_energie,or_energie,ada_energie,ura_energie,exfer_energie,exor_energie,central_energie,techproAsgaard_lvl,techproAsgaard_evolution,techproAncien_lvl,techproAncien_evolution,techinfra_lvl,techinfra_evolution,technaqua_lvl,technaqua_evolution  FROM matgraph_users WHERE id='$dataid[id]' ";
$res2 = db_mysql_Query($sql2)or die('erreur sql<br>'.$sql2.'<br>'.mysql_error());
$row2 = mysql_fetch_array($res2); 

// chantier
if ($row[chantier_lvl] =="0") {
$chantier_lvl = "0";
}
else {
$chantier_lvl = $row[chantier_lvl];
}
// labo
if ($row[labo_lvl] =="0") {
$labo_lvl = "0";
}
else {
$labo_lvl = $row[labo_lvl];
}
// techenergie
if ($row2[techenergie_lvl] =="0") {
$techenergie_lvl = "0";
}
else {
$techenergie_lvl = $row2[techenergie_lvl];
}
// techlaser
if ($row2[techlaser_lvl] =="0") {
$techlaser_lvl = "0";
}
else {
$techlaser_lvl = $row2[techlaser_lvl];
}
// techions
if ($row2[techions_lvl] =="0") {
$techions_lvl = "0";
}
else {
$techions_lvl = $row2[techions_lvl];
}
// techplasma
if ($row2[techplasma_lvl] =="0") {
$techplasma_lvl = "0";
}
else {
$techplasma_lvl = $row2[techplasma_lvl];
}
// techordi
if ($row2[techordi_lvl] =="0") {
$techordi_lvl = "0";
}
else {
$techordi_lvl = $row2[techordi_lvl];
}
// techcombustion
if ($row2[techcombustion_lvl] =="0") {
$techcombustion_lvl = "0";
}
else {
$techcombustion_lvl = $row2[techcombustion_lvl];
}
// techAsgaards
if ($row2[techAsgaards_lvl] =="0") {
$techAsgaards_lvl = "0";
}
else {
$techAsgaards_lvl = $row2[techAsgaards_lvl];
}
// techhyperespace
if ($row2[techhyperespace_lvl] =="0") {
$techhyperespace_lvl = "0";
}
else {
$techhyperespace_lvl = $row2[techhyperespace_lvl];
}
// techespio1
if ($row2[techespio1_lvl] =="0") {
$techespio1_lvl = "0";
}
else {
$techespio1_lvl = $row2[techespio1_lvl];
}
// techespio2
if ($row2[techespio2_lvl] =="0") {
$techespio2_lvl = "0";
}
else {
$techespio2_lvl = $row2[techespio2_lvl];
}
// techArmement
if ($row2[techArmement_lvl] =="0") {
$techArmement_lvl = "0";
}
else {
$techArmement_lvl = $row2[techArmement_lvl];
}
// techBouclier
if ($row2[techBouclier_lvl] =="0") {
$techBouclier_lvl = "0";
}
else {
$techBouclier_lvl = $row2[techBouclier_lvl];
}
// techcoque
if ($row2[techcoque_lvl] =="0") {
$techcoque_lvl = "0";
}
else {
$techcoque_lvl = $row2[techcoque_lvl];
}
// techproAsgaard
if ($row2[techproAsgaard_lvl] =="0") {
$techproAsgaard_lvl = "0";
}
else {
$techproAsgaard_lvl = $row2[techproAsgaard_lvl];
}
// techproAncien
if ($row2[techproAncien_lvl] =="0") {
$techproAncien_lvl = "0";
}
else {
$techproAncien_lvl = $row2[techproAncien_lvl];
}
// techinfra
if ($row2[techinfra_lvl] =="0") {
$techinfra_lvl = "0";
}
else {
$techinfra_lvl = $row2[techinfra_lvl];
}
// technaqua
if ($row2[technaqua_lvl] =="0") {
$technaqua_lvl = "0";
}
else {
$technaqua_lvl = $row2[technaqua_lvl];
}
$reptime = time();
$nombre_def = htmlentities ($_POST["nombre_def"]);
$typedef = htmlentities ($_POST["typedef"]);

$ok ="pasok";
if ($chantier_lvl > "2" && $techespio1_lvl > "2" && $labo_lvl > "4" && $typedef == "malp") { $ok = "ok"; }
if ($chantier_lvl > "2" && $techcombustion_lvl > "1" && $labo_lvl > "4" && $typedef == "ptremorqueur") { $ok = "ok"; }
if ($chantier_lvl > "4" && $techcombustion_lvl > "3" && $labo_lvl > "4" && $typedef == "gdremorqueur") { $ok = "ok"; }
if ($chantier_lvl > "6" && $techcombustion_lvl > "3" && $techhyperespace_lvl > "5" && $techhyperespace_lvl > "5" && $typedef == "sonde") { $ok = "ok"; }
if ($chantier_lvl > "3" && $techAsgaards_lvl > "3" && $techcoque_lvl > "4" && $typedef == "vruines") { $ok = "ok"; }
if ($chantier_lvl > "6" && $techhyperespace_lvl > "3" && $techBouclier_lvl > "6" && $techproAncien_lvl > "5" && $typedef == "vruines2") { $ok = "ok"; }

if($ok !="ok") {
	echo '<script language="javascript" type="text/javascript">
	<!--
	window.location.replace("centrespe.php");
	-->
	</script>';
	exit;
}


////// cout ////////////
//malp
$fer_necessaire_anciens = 500;
$or_necessaire_anciens = 300;
$ada_necessaire_anciens = 1200;
$ura_necessaire_anciens = 150;
$stimeanciens = (round(300* $siege_temps_evolution *(bcpow(0.9, $chantier_lvl,3))));
if($typedef == "malp"){
$fer_necessaire = $fer_necessaire_anciens;
$or_necessaire = $or_necessaire_anciens;
$ada_necessaire = $ada_necessaire_anciens;
$ura_necessaire = $ura_necessaire_anciens;
$retard = $stimeanciens;
}
//gdremorqueur
$fer_necessaire2_gdremorqueur = 10000;
$or_necessaire2_gdremorqueur = 7500;
$ada_necessaire2_gdremorqueur = 5000;
$ura_necessaire2_gdremorqueur = 2500;
$stimegdremorqueur = (round(3300* $siege_temps_evolution *(bcpow(0.9, $chantier_lvl,3))));
if($typedef == "gdremorqueur"){
$fer_necessaire = $fer_necessaire2_gdremorqueur;
$or_necessaire = $or_necessaire2_gdremorqueur;
$ada_necessaire = $ada_necessaire2_gdremorqueur;
$ura_necessaire = $ura_necessaire2_gdremorqueur;
$retard = $stimegdremorqueur;
}
//ptremorqueur
$fer_necessaire2_ptremorqueur = 2000;
$or_necessaire2_ptremorqueur = 1500;
$ada_necessaire2_ptremorqueur = 1000;
$ura_necessaire2_ptremorqueur = 500;
$stimeptremorqueur = (round(1200* $siege_temps_evolution *(bcpow(0.9, $chantier_lvl,3))));
if($typedef == "ptremorqueur"){
$fer_necessaire = $fer_necessaire2_ptremorqueur;
$or_necessaire = $or_necessaire2_ptremorqueur;
$ada_necessaire = $ada_necessaire2_ptremorqueur;
$ura_necessaire = $ura_necessaire2_ptremorqueur;
$retard = $stimeptremorqueur;
}

// sonde 
$fer_necessaire2_sonde = 2000;
$or_necessaire2_sonde = 1000;
$ada_necessaire2_sonde = 1500;
$ura_necessaire2_sonde = 1200;
$stimesonde = (round(900* $siege_temps_evolution *(bcpow(0.9, $chantier_lvl,3))));
if($typedef == "sonde"){
$fer_necessaire = $fer_necessaire2_sonde;
$or_necessaire = $or_necessaire2_sonde;
$ada_necessaire = $ada_necessaire2_sonde;
$ura_necessaire = $ura_necessaire2_sonde;
$retard = $stimesonde;
}

// vruines 
$fer_necessaire2_vruines = 5000;
$or_necessaire2_vruines = 5000;
$ada_necessaire2_vruines = 20000;
$ura_necessaire2_vruines = 0;
$stimevruines = (round(1500* $siege_temps_evolution *(bcpow(0.9, $chantier_lvl,3))));
if($typedef == "vruines"){
$fer_necessaire = $fer_necessaire2_vruines;
$or_necessaire = $or_necessaire2_vruines;
$ada_necessaire = $ada_necessaire2_vruines;
$ura_necessaire = $ura_necessaire2_vruines;
$retard = $stimevruines;
}

// vruines2
$fer_necessaire2_vruines2 = 10000;
$or_necessaire2_vruines2 = 10000;
$ada_necessaire2_vruines2 = 20000;
$ura_necessaire2_vruines2 = 30000;
$stimevruines2 = (round(3000* $siege_temps_evolution *(bcpow(0.9, $chantier_lvl,3))));
if($typedef == "vruines2"){
$fer_necessaire = $fer_necessaire2_vruines2;
$or_necessaire = $or_necessaire2_vruines2;
$ada_necessaire = $ada_necessaire2_vruines2;
$ura_necessaire = $ura_necessaire2_vruines2;
$retard = $stimevruines2;
}

$req = "SELECT * FROM matgraph_constructionspe WHERE findeconstruction > ".time()." AND proprietaire='$planete' ";
$res = db_mysql_Query($req);
$num = mysql_num_rows($res);
if ($num >0) {

$sql = "SELECT findeconstruction FROM matgraph_constructionspe WHERE proprietaire = '$planete' ORDER BY findeconstruction desc";
$res = db_mysql_Query($sql) or die('Erreur SQL!<br>'.$sql.'<br>'.mysql_error());

$reptime = mysql_result($res, 0);

}

for ($i = 0; $i < $nombre_def; $i++) {

  $temps_de_fin = $reptime + ($i+1) * $retard;

$sql ="SELECT fer_ressource,or_ressource,ada_ressource,ura_ressource FROM matgraph_users WHERE id='$planete'";
$res = db_mysql_Query($sql);
$row = mysql_fetch_array($res);

$fer_possede = $row[fer_ressource];
if ($fer_possede < $fer_necessaire ) {
$pas_assez = 1;
}
$or_possede = $row[or_ressource];
if ($or_possede < $or_necessaire ) {
$pas_assez = 1;
}
$ada_possede = $row[ada_ressource];
if ($ada_possede < $ada_necessaire ) {
$pas_assez = 1;
}
$ura_possede = $row[ura_ressource];
if ($ura_possede < $ura_necessaire ) {
$pas_assez = 1;
}

if ($pas_assez == 1) {
break;
}

$new_fer = $fer_possede - $fer_necessaire ;
$new_or = $or_possede - $or_necessaire ;
$new_ada = $ada_possede - $ada_necessaire ;
$new_ura = $ura_possede - $ura_necessaire ;

$sql = "SELECT pts_flotte FROM matgraph_users WHERE id='$dataid[id]'";
$res = db_mysql_Query($sql)or die('erreur sql<br>'.$sql.'<br>'.mysql_error());
$row = mysql_fetch_array($res); 

$pts_flotte = $row[pts_flotte] + $fer_necessaire + $or_necessaire + $ada_necessaire + $ura_necessaire ;

$sql = "UPDATE matgraph_users set fer_ressource = $new_fer, or_ressource = $new_or, ada_ressource = $new_ada, ura_ressource = $new_ura WHERE id='$planete'";
db_mysql_Query($sql) or die('Erreur SQL!<br>'.$sql.'<br>'.mysql_error());
$req = "INSERT INTO matgraph_constructionspe (id, typedef, proprietaire, findeconstruction) VALUES ('','$typedef','$planete','$temps_de_fin')";
db_mysql_Query($req) or die('Erreur SQL!<br>'.$req.'<br>'.mysql_error());
/*
$sql = "UPDATE matgraph_users set pts_flotte=$pts_flotte WHERE id='$dataid[id]'";
db_mysql_Query($sql) or die('Erreur SQL!<br>'.$sql.'<br>'.mysql_error());
*/
}


echo '<script language="javascript" type="text/javascript">
<!--
window.location.replace("centrespe.php");
-->
</script>';

?>