<?
function Pushing_Systeme($id_envoyeur,$pts_envoyeur,$id_destinataire,$pts_destinataire,$fer,$or,$cristal,$naquadah,$militaires,$eppzs,$malps,$prs,$grs) {

	// config
	//en % (80% minimum donc 20%)
	$seuille_de_tolerance = 80;
	// 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;
	
	//vérification des ressources et autres
	if ( $fer < 0 OR $fer == "" ) { $fer = 0; } else { $fer = round($fer); }
	if ( $or < 0 OR $or == "" ) { $or = 0; } else { $or = round($or); }
	if ( $cristal < 0 OR $cristal == "" ) { $cristal = 0; } else { $cristal = round($cristal); }
	if ( $naquadah < 0 OR $naquadah == "" ) { $naquadah = 0; } else { $naquadah = round($naquadah); }
	if ( $militaires < 0 OR $militaires == "" ) { $militaires = 0; } else { $militaires = round($militaires); }
	if ( $eppzs < 0 OR $eppzs == "" ) { $eppzs = 0; } else { $eppzs = round($eppzs); }
	if ( $malps < 0 OR $malps == "" ) { $malps = 0; } else { $malps = round($malps); }
	if ( $prs < 0 OR $prs == "" ) { $prs = 0; } else { $prs = round($prs); }
	if ( $grs < 0 OR $grs == "" ) { $grs = 0; } else { $grs = round($grs); }

	// calcul des valeurs en fer de "produit"
	$grs = $grs * $coef_grs;
	$eppzs = $eppzs * $coef_eppzs;
	$prs = $prs * $coef_prs;
	$malps = $malps * $coef_malps;
	$naquadah = $naquadah * $coef_naquadah;
	$cristal = $cristal * $coef_cristal;
	$or = round( $or * $coef_or );
	
	// si envois vers joueur fort
	if ( $pts_envoyeur < $pts_destinataire ) {
		$datetime =  strftime("%Y-%m-%d %H:%M:%S", time());
		$sql = "INSERT INTO `Pushing_Systeme` (`id_envoyeur`, `pts_envoyeur`, `id_destinataire`, `pts_destinataire`, `fer`, `orr`, `cristal`, `naquadah`, `militaires`, `eppzs`, `malps`, `prs`, `grs`, `date`) VALUES ($id_envoyeur, $pts_envoyeur, $id_destinataire, $pts_destinataire, $fer, $or, $cristal, $naquadah, $militaires, $eppzs, $malps, $prs, $grs, '$datetime')";
		$resbis = db_mysql_Query($sql) or die('Erreur SQL! <br>'.$sql.'<br>'.mysql_error());
	}elseif ( $pts_envoyeur > $pts_destinataire ) {
		// on recherche les infos concernants les deux joueurs dans la table
		$sql = "SELECT * FROM `Pushing_Systeme` WHERE `id_envoyeur` = $id_destinataire AND `id_destinataire` = $id_envoyeur ORDER BY date ASC";
		$res = db_mysql_Query($sql) or die('Erreur SQL! <br>'.$sql.'<br>'.mysql_error());

		$num = mysql_num_rows($res);

		// s'ils ont déjà faient des échanges
		if ( $num > "0" ) {
			$datetime_now = strftime("%Y-%m-%d %H:%M:%S", time());
			$datetime_48 = strftime("%Y-%m-%d %H:%M:%S", time()- 48*60*3600);
			$date=$datetime_now;
			while($row = mysql_fetch_array($res)){
				// calcul du total des ressources envoyé par le joueur faible
				$total_envoye_faible = $total_envoye_faible+ $row[fer] + $row[orr] + $row[cristal] + $row[naquadah] + $row[militaires] + $row[eppzs] + $row[malps] + $row[prs] + $row[grs];
				if($row['date']<$date){
					$date=$row['date'];
				}
				
			}
			// calcul du total des ressources envoyé par le joueur fort
			$total_envoye_fort = $fer + $or + $cristal + $naquadah + $militaires + $eppzs + $malps + $prs + $grs ;
			$id=array();
			// si le delais de 48h n'a pas été dépassé
			if ( $date > $datetime_48 ) {
				echo "lol";
				// si il rembourse largement ce qu'il faut...
				if ( $total_envoye_fort > $total_envoye_faible ) {
					echo "Remboursement ok"; 
					$sql = "DELETE FROM `Pushing_Systeme` WHERE `id_envoyeur` = $id_destinataire AND `id_destinataire` = $id_envoyeur";
					$resbis = db_mysql_Query($sql) or die('Erreur SQL! <br>'.$sql.'<br>'.mysql_error());
				}

				// calcul du pourcentage
				$pourcentage = ($total_envoye_fort*100)/$total_envoye_faible;
				// si le seuille est respecté
				if ( $seuille_de_tolerance < $pourcentage ) {
					$sql = "DELETE FROM `Pushing_Systeme` WHERE `id_envoyeur` = $id_destinataire AND `id_destinataire` = $id_envoyeur";
					$resbis = db_mysql_Query($sql) or die('Erreur SQL! <br>'.$sql.'<br>'.mysql_error());
				}else{
					echo "lol";
					$sql = "SELECT id FROM `Pushing_Systeme` WHERE `id_envoyeur` = $id_destinataire AND `id_destinataire` = $id_envoyeur ORDER BY date ASC";
					$res = db_mysql_Query($sql) or die('Erreur SQL! <br>'.$sql.'<br>'.mysql_error());
					while($row = mysql_fetch_array($res)){
						array_push($id,$row['id']);
					}
					foreach($id as $valeur){
					$sql = "SELECT * FROM `Pushing_Systeme` WHERE `id_envoyeur` = $id_destinataire AND `id_destinataire` = $id_envoyeur AND id='".$valeur."'";
					$res = db_mysql_Query($sql) or die('Erreur SQL! <br>'.$sql.'<br>'.mysql_error());
					$row = mysql_fetch_array($res);
					
					$datetime_now = strftime("%Y-%m-%d %H:%M:%S", time());
					
					$release[grs] = $row[grs];
					$release[eppzs] = $row[eppzs];
					$release[prs] = $row[prs];
					$release[malps] = $row[malps];
					$release[naquadah] = $row[naquadah];
					$release[cristal] = $row[cristal];
					$release[orr] = $row[orr];
					$release[militaires] = $row[militaires];
					$release[fer] = $row[fer];
				
					// grs
					if ( $row[grs] != "0" && $total_envoye_fort != "0" ) {
						if ( $row[grs] > $total_envoye_fort ) {
							$release[grs] = $row[grs] - $total_envoye_fort;
							$total_envoye_fort = "0";
						}else{
							$total_envoye_fort = $total_envoye_fort - $row[grs];
							$release[grs] = 0;
						}
					}
					// eppzs
					if ( $row[eppzs] != "0" && $total_envoye_fort != "0" ) {
						if ( $row[eppzs] > $total_envoye_fort ) {
							$release[eppzs] = $row[eppzs] - $total_envoye_fort;
							$total_envoye_fort = "0";
						}
						else {
							$total_envoye_fort = $total_envoye_fort - $row[eppzs];
							$release[eppzs] = 0;
						}
					}
					// prs
					if ( $row[prs] != "0" && $total_envoye_fort != "0" ) {
						if ( $row[prs] > $total_envoye_fort ) {
							$release[prs] = $row[prs] - $total_envoye_fort;
							$total_envoye_fort = "0";
						}
						else {
							$total_envoye_fort = $total_envoye_fort - $row[prs];
							$release[prs] = 0;
						}
					}						
					// malps
					if ( $row[malps] != "0" && $total_envoye_fort != "0" ) {
						if ( $row[malps] > $total_envoye_fort ) {
							$release[malps] = $row[malps] - $total_envoye_fort;
							$total_envoye_fort = "0";
						}
						else {
							$total_envoye_fort = $total_envoye_fort - $row[malps];
							$release[malps] = 0;
						}
					}
					// naquadah
					if ( $row[naquadah] != "0" && $total_envoye_fort != "0" ) {
						if ( $row[naquadah] > $total_envoye_fort ) {
							$release[naquadah] = $row[naquadah] - $total_envoye_fort;
							$total_envoye_fort = "0";
						}
						else {
							$total_envoye_fort = $total_envoye_fort - $row[naquadah];
							$release[naquadah] = 0;
						}
					}					
					// cristal
					if ( $row[cristal] != "0" && $total_envoye_fort != "0" ) {
						if ( $row[cristal] > $total_envoye_fort ) {
							$release[cristal] = $row[cristal] - $total_envoye_fort;
							$total_envoye_fort = "0";
						}
						else {
							$total_envoye_fort = $total_envoye_fort - $row[cristal];
							$release[cristal] = 0;
						}
					}
					// or
					if ( $row[orr] != "0" && $total_envoye_fort != "0" ) {
						if ( $row[orr] > $total_envoye_fort ) {
							$release[orr] = $row[orr] - $total_envoye_fort;
							$total_envoye_fort = "0";
						}
						else {
							$total_envoye_fort = $total_envoye_fort - $row[orr];
							$release[orr] = 0;
						}
					}
					// militaires
					if ( $row[militaires] != "0" && $total_envoye_fort != "0" ) {
						if ( $row[militaires] > $total_envoye_fort ) {
							$release[militaires] = $row[militaires] - $total_envoye_fort;
							$total_envoye_fort = "0";
						}
						else {
							$total_envoye_fort = $total_envoye_fort - $row[militaires];
							$release[militaires] = 0;
						}
					}
					// fer
					if ( $row[fer] != "0" && $total_envoye_fort != "0" ) {
						if ( $row[fer] > $total_envoye_fort ) {
							$release[fer] = $row[fer] - $total_envoye_fort;
							$total_envoye_fort = "0";
						}else{
							$total_envoye_fort = $total_envoye_fort - $row[fer];
							$release[fer] = 0;
						}
					}
					$somme_result=array_sum($release);
					if($somme_result==0){
						$sql = "DELETE FROM pushing_systeme WHERE id='".$valeur."'";
						$resbis = db_mysql_Query($sql) or die('Erreur SQL! <br>'.$sql.'<br>'.mysql_error());
					}else{
						$sql = "UPDATE `Pushing_Systeme` SET `fer` = '$release[fer]', `orr` = '$release[orr]', `cristal` = '$release[cristal]', `naquadah` = '$release[naquadah]', `militaires` = '$release[militaires]', `eppzs` = '$release[eppzs]', `malps` = '$release[malps]', `prs` = '$release[prs]', `grs` = '$release[grs]', date='$datetime_now' WHERE id='".$valeur."'";
						$resbis = db_mysql_Query($sql) or die('Erreur SQL! <br>'.$sql.'<br>'.mysql_error());
					}
					}
				}
			}
		}
	}
}
?>
