<?php
echo "<script type=\"text/javascript\">
function aff_div(id){
	var div_aff=document.getElementById(id);
	var a_href_aff = document.getElementById('a'+id);
	if(div_aff.style.display=='none'){
		div_aff.style.display='block';
		document.getElementById('a'+id).value='-';
	}else{
		div_aff.style.display='none';
		document.getElementById('a'+id).value='+';
	};
};
</script>";
function calc_val($tableau){
	$retour=0;
	foreach($tableau as $valeur){
		$retour=$retour+$valeur;
	}
	return $retour;
}
function affichage_push($tab){
	global $tableau_result;
	$tableau_result=array();
	$message='';
	foreach($tab as $cle=>$valeur){
		if(!ereg('id',$cle)){
			if(ereg("fort",$cle)){
				$cle2=str_replace('_fort','',$cle);
				$tableau_result['fort'][$cle2]=explode(';',$valeur);
			}
			if(ereg("faible",$cle)){
				$cle2=str_replace('_faible','',$cle);
				$tableau_result['faible'][$cle2]=explode(';',$valeur);
			}
			if(ereg("dates",$cle)){
				$tableau_result['dates']=explode(';',$valeur);
			}
		}
	}
	for($i=0;$i<=(count($tableau_result)-1);$i++){
		while (list($key, $value) = each($tableau_result['fort'])) {
    		if($value[$i]>$tableau_result['faible'][$key][$i]){
				$message .="Envoi de la part du joueur <span style=\"color: rgb(255, 0, 0);\">Fort</span> : &nbsp;&nbsp;&nbsp;".number_format($value[$i],0,',',' ')." ".$key.",&nbsp;&nbsp;&nbsp;le ".strftime("%Y-%m-%d %H:%M:%S", $tableau_result['dates'][$i] )."<br/>";
			}
			if($value[$i]<$tableau_result['faible'][$key][$i]){
				$message .="Envoi de la part du joueur <span style=\"color: rgb(51, 204, 0);\">Faible</span> : &nbsp;&nbsp;&nbsp;".number_format($tableau_result['faible'][$key][$i],0,',',' ')." ".$key.",&nbsp;&nbsp;&nbsp;le ".strftime("%Y-%m-%d %H:%M:%S", $tableau_result['dates'][$i] )."<br/>";
			}
		}
		reset($tableau_result['fort']);
	}
	return $message;
}
// en heures
$delais_avant_push = 48;
$coef_or = 1.33;
$coef_cristal = 2;
$coef_naquadah = 4;
$coef_malps = 3900;
$coef_prs = 8000;
$coef_grs = 40000;
$coef_eppzs = 10000;

if ( $_GET['f'] == "supprimer" && $_POST['id_push'] != NULL) {
	$id_push = $_POST['id_push'];
	$sql = "DELETE FROM `Pushing_Systeme` WHERE id = '".$idpush."'";
	$res = db_mysql_Query($sql) or die('Erreur SQL: <br>$sql<br>'.mysql_error());
	echo "<script language=\"javascript\" type=\"text/javascript\"> window.location.replace(\"sgu_systeme_push_print.php\"); </script>";
}

elseif ( $_GET['f'] == "bannir" && $_POST['id_fort'] != NULL && $_POST['id_faible'] != NULL && $_POST['nbr_jours'] != NULL && $_POST['motif'] != NULL) {
	$id_fort = $_POST['id_fort'];
	$id_faible = $_POST['id_faible'];
	$motif = $_POST['motif'];
	$nbr_jours = $_POST['nbr_jours'];
	$finduban = strftime("%Y-%m-%d %H:%M:%S", time()+ (round(60*60*6*$nbr_jours)));
	echo $finduban;

	// login du joueur fort
	$req_dest = "SELECT login,pts FROM matgraph_users WHERE id = '".$id_fort."'";
	$res_dest = db_mysql_Query($req_dest) or die('Erreur SQL: <br>$req_dest<br>'.mysql_error());
	$row_dest = mysql_fetch_array($res_dest);
	// login du joueur faible
	$req_env = "SELECT login,pts FROM matgraph_users WHERE id = '".$id_faible."'";
	$res_env = db_mysql_Query($req_env) or die('Erreur SQL: <br>$req_env<br>'.mysql_error());
	$row_env = mysql_fetch_array($res_env);
	
	// verification de si le joueur fort est pas déjà bannis
	$sql_vb_dest = "SELECT * FROM `matgraph_banni` WHERE `login` = '".$row_dest['login']."'";
	$res_vb_dest = db_mysql_Query($sql_vb_dest) or die('Erreur SQL: <br>$sql_vb_dest<br>'.mysql_error());
	$num_vb_dest = mysql_num_rows($res_vb_dest);
	
	if ( $num_vb_dest == NULL ) {
		
		// déactivation du mode vacance
		$sql_vac = "UPDATE matgraph_users SET vacance = '0000-00-00 00:00:00' WHERE login = '".$row_dest['login']."'";
		$res_vac = db_mysql_Query($sql_vac)or die('Erreur SQL:<br>$sql_vac<br>'.mysql_error());

		// kick
		$sql_kick = "UPDATE matgraph_connexion SET secu = 'ban' WHERE idjoueur = '".$id_fort."'";
		$res_kick = db_mysql_Query($sql_kick)or die('Erreur SQL: <br>$sql_kick<br>'.mysql_error());

		// ban
		$sql_ban = "INSERT INTO `matgraph_banni` (`id`, `login`, `jour`, `heure`, `minute`, `date`, `motif`, `Intervenant`) VALUES (NULL, '".$row_dest['login']."', '".$nbr_jours."', '0', '0', '".$finduban."', '".$motif."', '');";
		$res_ban = db_mysql_Query($sql_ban) or die('Erreur SQL: <br>$sql_ban<br>'.mysql_error());

	}

	// verification de si le joueur faible est pas déjà bannis
	$sql_vb_env = "SELECT * FROM `matgraph_banni` WHERE `login` = '".$row_env['login']."'";
	$res_vb_env = db_mysql_Query($sql_vb_env) or die('Erreur SQL: <br>$sql_vb_env<br>'.mysql_error());
	$num_vb_env = mysql_num_rows($res_vb_env);
	
	if ( $num_vb_env == NULL ) {
	
		// déactivation de mode vacance
		$sql_vac = "UPDATE matgraph_users SET vacance = '0000-00-00 00:00:00' WHERE login = '".$row_env['login']."'";
		$res_vac = db_mysql_Query($sql_vac)or die('Erreur SQL:<br>$sql_vac<br>'.mysql_error());
		
		// kick
		$sql_kick = "UPDATE matgraph_connexion SET secu = 'ban' WHERE idjoueur = '".$id_envoyeur."'";
		$res_kick = db_mysql_Query($sql_kick)or die('Erreur SQL: <br>$sql_kick<br>'.mysql_error());
		
		// ban
		$sql_ban = "INSERT INTO `matgraph_banni` (`id`, `login`, `jour`, `heure`, `minute`, `date`, `motif`, `Intervenant`) VALUES (NULL, '".$row_env['login']."', '".$nbr_jours."', '0', '0', '".$finduban."', '".$motif."', '');";
		$res_ban = db_mysql_Query($sql_ban) or die('Erreur SQL: <br>$sql_ban<br>'.mysql_error());
		
	}

	// suppression de l'entrée
	$sql = "DELETE FROM `Pushing_Systeme` WHERE id_fort = '".$id_fort."' AND id_faible = '".$id_faible."'";
	$res = db_mysql_Query($sql) or die('Erreur SQL: <br>$sql<br>'.mysql_error());

	// echo "<script language=\"javascript\" type=\"text/javascript\"> window.location.replace(\"sgu_systeme_push_print.php\");<//script>";
}else {
	$datetime_48 = time()-86400*2;
	$sql2 = "SELECT * FROM `Pushing_Systeme`";
	$res2 = db_mysql_Query($sql2) or die('Erreur SQL: <br>$sql2<br>'.mysql_error());
	$num = mysql_num_rows($res2);
	$x=0;
	if ( $num > 0 ) {
		while ( $row = mysql_fetch_array($res2) ) {
			$tab_dates=explode(";",$row['dates_echanges']);
			if($tab_dates[0]<$datetime_48){
			$x++;
			// calcul des valeurs en fer de "produit"
			foreach($row as $cle=>$valeur){
				if($cle=="fer_fort"){
					$fer_fort_tab=explode(";",$valeur);
					$fer_fort=calc_val($fer_fort_tab);
				}elseif($cle=="orr_fort"){
					$or_fort_tab=explode(";",$valeur);
					$or_fort=round(calc_val($or_fort_tab)*$coef_or);
				}elseif($cle=="cristal_fort"){
					$cristal_fort_tab=explode(";",$valeur);
					$cristal_fort=calc_val($cristal_fort_tab)*$coef_cristal;
				}elseif($cle=="naquadah_fort"){
					$naquadah_fort_tab=explode(";",$valeur);
					$naquadah_fort=calc_val($naquadah_fort_tab)*$coef_naquadah;
				}elseif($cle=="militaires_fort"){
					$militaires_fort_tab=explode(";",$valeur);
					$militaires_fort=calc_val($militaires_fort_tab);
				}elseif($cle=="eppzs_fort"){
					$eppzs_fort_tab=explode(";",$valeur);
					$eppzs_fort=calc_val($eppzs_fort_tab)*$coef_eppzs;
				}elseif($cle=="malps_fort"){
					$malps_fort_tab=explode(";",$valeur);
					$malps_fort=calc_val($malps_fort_tab)*$coef_malps;
				}elseif($cle=="prs_fort"){
					$prs_fort_tab=explode(";",$valeur);
					$prs_fort=calc_val($prs_fort_tab)*$coef_prs;
				}elseif($cle=="grs_fort"){
					$grs_fort_tab=explode(";",$valeur);
					$grs_fort=calc_val($grs_fort_tab)*$coef_grs;
				}elseif($cle=="fer_faible"){
					$fer_faible_tab=explode(";",$valeur);
					$fer_faible=calc_val($fer_faible_tab);
				}elseif($cle=="orr_faible"){
					$or_faible_tab=explode(";",$valeur);
					$or_faible=round(calc_val($or_faible_tab)*$coef_or);
				}elseif($cle=="cristal_faible"){
					$cristal_faible_tab=explode(";",$valeur);
					$cristal_faible=calc_val($cristal_faible_tab)*$coef_cristal;
				}elseif($cle=="naquadah_faible"){
					$naquadah_faible_tab=explode(";",$valeur);
					$naquadah_faible=calc_val($naquadah_faible_tab)*$coef_naquadah;
				}elseif($cle=="militaires_faible"){
					$militaires_faible_tab=explode(";",$valeur);
					$militaires_faible=calc_val($militaires_faible_tab);
				}elseif($cle=="eppzs_faible"){
					$eppzs_faible_tab=explode(";",$valeur);
					$eppzs_faible=calc_val($eppzs_faible_tab)*$coef_eppzs;
				}elseif($cle=="malps_faible"){
					$malps_faible_tab=explode(";",$valeur);
					$malps_faible=calc_val($malps_faible_tab)*$coef_malps;
				}elseif($cle=="prs_faible"){
					$prs_faible_tab=explode(";",$valeur);
					$prs_faible=calc_val($prs_faible_tab)*$coef_prs;
				}elseif($cle=="grs_faible"){
					$grs_faible_tab=explode(";",$valeur);
					$grs_faible=calc_val($grs_faible_tab)*$coef_grs;
				}
			}
			//calcul total push
			$fer=$fer_faible-$fer_fort;
			$or=$or_faible-$or_fort;
			$cristal=$cristal_faible-$crystal_fort;
			$naquadah=$naquadah_faible-$naquadah_fort;
			$militaires=$militaires_faible-$militaires_fort;
			$eppzs=$eppzs_faible-$eppzs_fort;
			$malps=$malps_faible-$malps_fort;
			$prs=$prs_faible-$prs_fort;
			$grs=$grs_faible-$grs_fort;
			
			$total=$fer+$or+$cristal+$naquadah+$militaires+$eppzs+$malps+$prs+$grs;
			if($total<0){
				$query="DELETE FROM `Pushing_Systeme` WHERE id='".$row['id']."'";
				$sql=db_mysql_Query($query);
				$query="OPTIMIZE TABLE `Pushing_Systeme`";
				$sql=db_mysql_Query($query);
				echo "Aucun push d&eacute,tect&eacute;";
				exit;
			}
			
			// login joueur fort
			$req_dest = "SELECT login,pts FROM matgraph_users WHERE id = '".$row['id_fort']."'";
			$res_dest = db_mysql_Query($req_dest) or die('Erreur SQL: <br>$req_dest<br>'.mysql_error());
			$row_dest = mysql_fetch_array($res_dest);
			// login joueur faible
			$req_env = "SELECT login,pts FROM matgraph_users WHERE id= '".$row['id_faible']."'";
			$res_env = db_mysql_Query($req_env) or die('Erreur SQL: <br>$req_env<br>'.mysql_error());
			$row_env = mysql_fetch_array($res_env);
			//calcul des points
			$pts_destinataire = number_format(floor(($row_dest['pts'] / 1000)), 0, ',', ' ');
			$pts_envoyeur = number_format(floor(($row_env['pts'] / 1000)), 0, ',', ' ');
			//calcul de l'heure du push
			$datetime =  strftime("%Y-%m-%d %H:%M:%S", $tab_dates[0] );
			echo "
			<div style=\"text-align: center;\">
				<table style=\"background-color: rgb(17, 17, 17); width: 720px; height: 20px;\">
					<tr>
						<td>
							Premier Transfert : $datetime :: <span style=\"color: rgb(255, 0, 0);\">$row_dest[login] ($pts_destinataire pts)</span> :: <span style=\"color: rgb(51, 204, 0);\">$row_env[login] ($pts_envoyeur pts)</span>
						</td>
						<td>
							<div style=\"text-align: right;\">
								<form name=\"supprimer\" method=\"post\" action=\"sgu_systeme_push_print.php?f=supprimer\" target=\"_parent\">
									<input type=\"hidden\" name=\"id_push\" value=\"".$row['id']."\">
									<input type=\"image\" alt=\"del\" src=\"../imgs/del.gif\" border=\"0\" height=\"15\" width=\"20\">
								</form>
							</div>
						</td>
					</tr>
				</table>
				<table style=\"text-align: center; width: 720px; height: 30px;\">
					<tr>
						<td><input style=\"color: #5596bf; ; background-color: black ; border: 1px solid silver;\" type=\"button\" id=\"apush".$row['id']."\" onclick=\"aff_div('push".$row['id']."');\" value=\"+\" /></td><td style=\"text-align:left;\"><u>Push avec pour &eacute;quivalent Fer :</u> ".number_format($total,0,',',' ')." unit&eacute;s de ressource</td>
					</tr>
					<tr>
						<td colspan=\"2\">
							<div id=\"push".$row['id']."\" style=\"display:none;text-align:left;padding-left:1cm;\"><font size=\"2\">
							".affichage_push($row)."
							</font></div>
						</td>
				</table>
				<table style=\"text-align: right; background-color: rgb(17, 17, 17); width: 720px; height: 25px;\">
					<tr>
						<td>
							<form name=\"bannir\" method=\"post\" action=\"sgu_systeme_push_print.php?f=bannir\" target=\"_parent\">
								<input type=\"hidden\" name=\"id_destinataire\" value=\"$row[id_destinataire]\">
								<input type=\"hidden\" name=\"id_envoyeur\" value=\"$row[id_envoyeur]\">
								Nbr de jours: <input type=\"text\" size=\"5\" name=\"nbr_jours\" value=\"0\" maxlength=\"4\">
								Motif: <input type=\"text\" size=\"25\" name=\"motif\" value=\"Push\">
								<input type=\"submit\" name=\"Submit\" value=\"Bannir\" Style=\"border:1px solid #7E7E7E;\">
							</form>
						</td>
					</tr>
				</table>
			<br>
			</div>
			";
		}
		if($x==0){
			echo "<div style=\"text-align: center;\">Aucun push d&eacute;tect&eacute;.</div><br>";
		}
		}
	}
	else {
		echo "<div style=\"text-align: center;\">Aucun push d&eacute;tect&eacute;.</div><br>";
	}
}
?>
