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

// defense
if ($row[defense_lvl] =="0") {
	$defense_lvl = "0";
}else{
	$defense_lvl = $row[defense_lvl];
}
if ($row[postav_lvl] =="0") {
	$postav_lvl = "0";
}else{
	$postav_lvl = $row[postav_lvl];
}

$nombre_def = htmlentities ($_POST["nombre_def"]);
$typedef = htmlentities ($_POST["typedef"]);
$reptime = time();


////// cout ////////////
//combat
$fer_necessaire2_combat = 500;
$or_necessaire2_combat = "0";
$ada_necessaire2_combat = "0";
$ura_necessaire2_combat = "0";
$stimecombat = (round(480* $siege_temps_evolution *(bcpow(0.9, $defense_lvl,3))));
if($typedef == "defcombat"){
$fer_necessaire = $fer_necessaire2_combat;
$or_necessaire = $or_necessaire2_combat;
$ada_necessaire = $ada_necessaire2_combat;
$ura_necessaire = $ura_necessaire2_combat;
$retard = $stimecombat;
}
//lasersimple
$fer_necessaire2_lasersimple = 1200;
$or_necessaire2_lasersimple = "500";
$ada_necessaire2_lasersimple = "80";
$ura_necessaire2_lasersimple = "0";
$stimelasersimple = (round(720* $siege_temps_evolution *(bcpow(0.9, $defense_lvl,3))));
if($typedef == "deflasersimple"){
$fer_necessaire = $fer_necessaire2_lasersimple;
$or_necessaire = $or_necessaire2_lasersimple;
$ada_necessaire = $ada_necessaire2_lasersimple;
$ura_necessaire = $ura_necessaire2_lasersimple;
$retard = $stimelasersimple;
}
//laserdouble
$fer_necessaire2_laserdouble = 3000;
$or_necessaire2_laserdouble = "1500";
$ada_necessaire2_laserdouble = "240";
$ura_necessaire2_laserdouble = "0";
$stimelaserdouble = (round(4687* $siege_temps_evolution *(bcpow(0.9, $defense_lvl,3))));
if($typedef == "deflaserdouble"){
$fer_necessaire = $fer_necessaire2_laserdouble;
$or_necessaire = $or_necessaire2_laserdouble;
$ada_necessaire = $ada_necessaire2_laserdouble;
$ura_necessaire = $ura_necessaire2_laserdouble;
$retard = $stimelaserdouble;
}
//elec
$fer_necessaire2_elec = 5000;
$or_necessaire2_elec = 2500;
$ada_necessaire2_elec = 3000;
$ura_necessaire2_elec = "0";
$stimeelec = (round(10750* $siege_temps_evolution *(bcpow(0.9, $defense_lvl,3))));
if($typedef == "defelec"){
$fer_necessaire = $fer_necessaire2_elec;
$or_necessaire = $or_necessaire2_elec;
$ada_necessaire = $ada_necessaire2_elec;
$ura_necessaire = $ura_necessaire2_elec;
$retard = $stimeelec;
}
//ions
$fer_necessaire2_ions = 500000;
$or_necessaire2_ions = 2000000;
$ada_necessaire2_ions = 1000000;
$ura_necessaire2_ions = "0";
$stimeions = (round(32250* $siege_temps_evolution *(bcpow(0.9, $defense_lvl,3))));
if($typedef == "defions"){
$fer_necessaire = $fer_necessaire2_ions;
$or_necessaire = $or_necessaire2_ions;
$ada_necessaire = $ada_necessaire2_ions;
$ura_necessaire = $ura_necessaire2_ions;
$retard = $stimeions;
}
//plasma
$fer_necessaire2_plasma = 3000000;
$or_necessaire2_plasma = 2000000;
$ada_necessaire2_plasma = 4000000;
$ura_necessaire2_plasma = "500000";
$stimeplasma = (round(48375* $siege_temps_evolution *(bcpow(0.9, $defense_lvl,3))));
if($typedef == "defplasma"){
$fer_necessaire = $fer_necessaire2_plasma;
$or_necessaire = $or_necessaire2_plasma;
$ada_necessaire = $ada_necessaire2_plasma;
$ura_necessaire = $ura_necessaire2_plasma;
$retard = $stimeplasma;
}
//naqua
$fer_necessaire2_naqua = 1500000;
$or_necessaire2_naqua = 10000000;
$ada_necessaire2_naqua = 2500000;
$ura_necessaire2_naqua = 4000000;
$stimenaqua = (round(86000* $siege_temps_evolution *(bcpow(0.9, $defense_lvl,3))));
if($typedef == "defnaqua"){
$fer_necessaire = $fer_necessaire2_naqua;
$or_necessaire = $or_necessaire2_naqua;
$ada_necessaire = $ada_necessaire2_naqua;
$ura_necessaire = $ura_necessaire2_naqua;
$retard = $stimenaqua;
}
//anciens
$fer_necessaire2_anciens = 6000000;
$or_necessaire2_anciens = 6500000;
$ada_necessaire2_anciens = 4000000;
$ura_necessaire2_anciens = 4000000;
$stimeanciens = (round(258000* $siege_temps_evolution *(bcpow(0.9, $defense_lvl,3))));
if($typedef == "defanciens"){
$fer_necessaire = $fer_necessaire2_anciens;
$or_necessaire = $or_necessaire2_anciens;
$ada_necessaire = $ada_necessaire2_anciens;
$ura_necessaire = $ura_necessaire2_anciens;
$retard = $stimeanciens;
}
//drone
$fer_necessaire2_drone = 2500;
$or_necessaire2_drone = 2500;
$ada_necessaire2_drone = 5000;
$ura_necessaire2_drone = 5000;
$stimedrone = (round(196000* $siege_temps_evolution *(pow(1.1, $postav_lvl))));
if($typedef == "defdrone"){
$fer_necessaire = $fer_necessaire2_drone;
$or_necessaire = $or_necessaire2_drone;
$ada_necessaire = $ada_necessaire2_drone;
$ura_necessaire = $ura_necessaire2_drone;
$retard = $stimedrone;
}

//

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

$sql = "SELECT findeconstruction FROM matgraph_constructiondef 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;


//include ('ressource.php');
$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_def 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_def = $row[pts_def] + $fer_necessaire + $or_necessaire + $ada_necessaire + $ura_necessaire ;

$sql4 = "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($sql4) or die('Erreur SQL!<br>'.$sql4.'<br>'.mysql_error());
$req = "INSERT INTO matgraph_constructiondef (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_def=$pts_def WHERE id='$dataid[id]'";
db_mysql_Query($sql) or die('Erreur SQL!<br>'.$sql.'<br>'.mysql_error());
*/
}

$req = "SELECT id FROM matgraph_espio2 WHERE planeteespionner = $planete";
$res = db_mysql_Query($req) or die('Erreur SQL! <br>'.$req.'<br>'.mysql_error());
$num = mysql_num_rows($res);
if ( $num > 0 ) {
include('espio2_nbredef.php'); }

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

?>