<?php

$liste_id = "(0";

$reqtmpid = "SELECT id FROM matgraph_users WHERE idjoueur = $idlogin";
$restmpid = mysql_query($reqtmpid) or die('Erreur SQL! <br>'.$reqtmpid.'<br>'.mysql_error());
while( $rowtmpid = mysql_fetch_array($restmpid) ) {
	$liste_id = $liste_id." OR iddepart = $rowtmpid[id]";
}
$liste_id = $liste_id.")";


$req = "SELECT * FROM matgraph_mobil_vruines WHERE ".$liste_id." AND date < now() AND mission='aller' ORDER by date ASC";
$res = 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) ) {
		
		// temps retour
		if($row[vruines] == 0) {
			$vitesse = 4;
		} else {
			$vitesse = 2;
		}
			//cc = coordonnées
			$sqlcc = "SELECT galaxi,system,position FROM matgraph_users WHERE id='$row[iddepart]' ";
			$rescc = mysql_query($sqlcc)or die('erreur sql<br>'.$sqlcc.'<br>'.mysql_error());
			$rowcc = mysql_fetch_array($rescc);
		
			$Dif_position = $row[position] - $rowcc[position] ;
			if ($Dif_position < 0) { $Dif_position = -$Dif_position ; }
		
			$Dif_system = $row[system] - $rowcc[system] ;
			if ($Dif_system < 0) { $Dif_system = -$Dif_system ; }
		
			$Dif_galaxi = $row[galaxi] - $rowcc[galaxi] ;
			if ($Dif_galaxi < 0) { $Dif_galaxi = -$Dif_galaxi ; }
		
			if(($Dif_system + $Dif_galaxi + $Dif_position) == 0)
			{
				$temps = (7*60*4) / $vitesse;
			}elseif(($Dif_system + $Dif_galaxi) == 0)
			{
				$temps_position = $Dif_position*0.5 ;
				$temps = (600 * ( 5 + $temps_position) / $vitesse)/10 ;
			}elseif($Dif_galaxi == 0)
			{
				$temps_position = $Dif_position*0.5 ;
				$temps_system = $Dif_system*1 ;
				$temps = (600 * ( 20 + $temps_position + $temps_system) / $vitesse)/13.5 ;
			}else
			{
				$temps_position = $Dif_position*0.03 ;
				$temps_system = $Dif_system*0.3 ;
				$temps_galaxi = $Dif_galaxi*30 ;
				$temps = (600 * ( 40 + $temps_position + $temps_system + $temps_galaxi) / $vitesse)/10 ;
			}
			
		ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})", $row['date'], $thetime);
		$time = mktime($thetime[4],$thetime[5],$thetime[6],$thetime[2],$thetime[3],$thetime[1]);
		$diftemps = $time - time() ;
		
		$temps_retour =  strftime("%Y-%m-%d %H:%M:%S", time()+ (round($temps - $diftemps)));

		//
		$sql2 = "SELECT * FROM matgraph_ruines WHERE galaxi='$row[galaxi]' AND system='$row[system]' AND position='$row[position]' AND (fer + orr + ada) > 0 ORDER BY ID ASC limit 1";
		$res2 = mysql_query($sql2)or die('erreur sql<br>'.$sql2.'<br>'.mysql_error());
		$num2 = mysql_num_rows($res2);
		if($num2 > 0) { //champs actif
			$row2 = mysql_fetch_array($res2); 
			
			// enlève se que ceux d'avant prendront
			$req4 = "SELECT * FROM matgraph_mobil_vruines WHERE date < '$row[date]' AND galaxi='$row[galaxi]' AND system='$row[system]' AND position='$row[position]' AND mission='aller' ORDER by date ASC";
			$res4 = mysql_query($req4) or die('Erreur SQL! <br>'.$req4.'<br>'.mysql_error());
			$num4 = mysql_num_rows($res4);
			if ( $num4 > 0 ) {
				While ( $row4 = mysql_fetch_array($res4) ) {
		
										// temps retour
										if($row4[vruines] == 0) {
											$vitesse = 4;
										} else {
											$vitesse = 2;
										}
											//cc = coordonnées
											$sqlcc = "SELECT galaxi,system,position FROM matgraph_users WHERE id='$row[iddepart]' ";
											$rescc = mysql_query($sqlcc)or die('erreur sql<br>'.$sqlcc.'<br>'.mysql_error());
											$rowcc = mysql_fetch_array($rescc);
										
											$Dif_position = $row4[position] - $rowcc[position] ;
											if ($Dif_position < 0) { $Dif_position = -$Dif_position ; }
										
											$Dif_system = $row4[system] - $rowcc[system] ;
											if ($Dif_system < 0) { $Dif_system = -$Dif_system ; }
										
											$Dif_galaxi = $row4[galaxi] - $rowcc[galaxi] ;
											if ($Dif_galaxi < 0) { $Dif_galaxi = -$Dif_galaxi ; }
										
											if(($Dif_system + $Dif_galaxi + $Dif_position) == 0)
											{
												$temps = (7*60*4) / $vitesse;
											}elseif(($Dif_system + $Dif_galaxi) == 0)
											{
												$temps_position = $Dif_position*0.5 ;
												$temps = (600 * ( 5 + $temps_position) / $vitesse)/10 ;
											}elseif($Dif_galaxi == 0)
											{
												$temps_position = $Dif_position*0.5 ;
												$temps_system = $Dif_system*1 ;
												$temps = (600 * ( 20 + $temps_position + $temps_system) / $vitesse)/13.5 ;
											}else
											{
												$temps_position = $Dif_position*0.03 ;
												$temps_system = $Dif_system*0.3 ;
												$temps_galaxi = $Dif_galaxi*30 ;
												$temps = (600 * ( 40 + $temps_position + $temps_system + $temps_galaxi) / $vitesse)/10 ;
											}
											
										ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})", $row4['date'], $thetime);
										$timeavant = mktime($thetime[4],$thetime[5],$thetime[6],$thetime[2],$thetime[3],$thetime[1]);
										$diftemps = $time - time() ;
										
										$temps =  strftime("%Y-%m-%d %H:%M:%S", time()+ (round($temps - $diftemps)));
								
										//
										$sql5 = "SELECT * FROM matgraph_ruines WHERE galaxi='$row[galaxi]' AND system='$row[system]' AND position='$row[position]' ORDER BY ID ASC limit 1";
										$res5 = mysql_query($sql5)or die('erreur sql<br>'.$sql5.'<br>'.mysql_error());
										$num5 = mysql_num_rows($res5);
										if($num5 > 0) { //champs actif
											$row5 = mysql_fetch_array($res5); 
																				
											$fret = $row4[vruines]*15000 + $row4[vruines2]*50000 + $row4[vruines3]*100000;
											
											if(($row5[fer] + $row5[orr] + $row5[ada]) < $fret) { //on prend tout
												
												$fer_a_prendre = $row5[fer];
												$or_a_prendre = $row5[orr];
												$ada_a_prendre = $row5[ada];
												
											} else { // on peu pas tout prendre	
											
											$fer_a_prendre = $row5[fer];
											$or_a_prendre = $row5[orr];
											$ada_a_prendre = $row5[ada];
											
											$a_prendre = $row5[fer] + $row5[orr] + $row5[ada] ;
											
											$ratio = $fret / $a_prendre;
								
											$ratio = ($ratio > 1) ? 1 : $ratio; //sécu ^^
								
											$fer_a_prendre *= $ratio;
											$or_a_prendre *= $ratio;
											$ada_a_prendre *= $ratio;
								
											$fer_a_prendre = ceil($fer_a_prendre);
											$or_a_prendre = ceil($or_a_prendre);
											$ada_a_prendre = ceil($ada_a_prendre);
											
											}
										
										//pas de champs
										} else {
											
											$fer_a_prendre = 0;
											$or_a_prendre = 0;
											$ada_a_prendre = 0;
										}
										
										$req2 = "UPDATE matgraph_mobil_vruines SET mission='retour', date ='".$temps."', fer='".$fer_a_prendre."', orr='".$or_a_prendre."', ada='".$ada_a_prendre."' WHERE id=$row4[id]";
										$res2 = mysql_query($req2) or die('Erreur SQL! <br>'.$req2.'<br>'.mysql_error());
										
										$req2 = "UPDATE matgraph_ruines SET fer = fer - ".$fer_a_prendre.", orr = orr - ".$or_a_prendre.", ada = ada - ".$ada_a_prendre." WHERE galaxi='$row4[galaxi]' AND system='$row4[system]' AND position='$row4[position]'";
										$res2 = mysql_query($req2) or die('Erreur SQL! <br>'.$req2.'<br>'.mysql_error());
										
										$title = "<font color=\"#EAA25B\">Vaisseaux ruines</font>";
								
										$msg = "Vos vaisseaux ruines sont bien arrivés aux coordonnées :  $row4[galaxi] : $row4[system] : $row4[position] : <br> Vous avez récupéré: <br> ".number_format($fer_a_prendre, 0, ',', ' ')." Fer <br> ".number_format($or_a_prendre, 0, ',', ' ')." Or <br> ".number_format($ada_a_prendre, 0, ',', ' ')." Cristal";
										$requete = "INSERT INTO  matgraph_msg VALUES ('',now(), 'non', '$row4[iddepart]', '$title', '".addslashes($msg)."')";
										$resultat = mysql_query($requete)or die('erreur sql<br>'.$requete.'<br>'.mysql_error());
				}
				$sql2 = "SELECT * FROM matgraph_ruines WHERE galaxi='$row[galaxi]' AND system='$row[system]' AND position='$row[position]' ORDER BY ID ASC limit 1";
				$res2 = mysql_query($sql2)or die('erreur sql<br>'.$sql2.'<br>'.mysql_error());
				$row2 = mysql_fetch_array($res2); 
			}
			
			$fret = $row[vruines]*15000 + $row[vruines2]*50000 + $row[vruines3]*100000;
			
			if(($row2[fer] + $row2[orr] + $row2[ada]) < $fret) { //on prend tout
				
				$fer_a_prendre = $row2[fer];
				$or_a_prendre = $row2[orr];
				$ada_a_prendre = $row2[ada];
				
			} else { // on peu pas tout prendre	
			
			$fer_a_prendre = $row2[fer];
			$or_a_prendre = $row2[orr];
			$ada_a_prendre = $row2[ada];
			
			$a_prendre = $row2[fer] + $row2[orr] + $row2[ada] ;
			
			$ratio = $fret / $a_prendre;

			$ratio = ($ratio > 1) ? 1 : $ratio; //sécu ^^

			$fer_a_prendre *= $ratio;
			$or_a_prendre *= $ratio;
			$ada_a_prendre *= $ratio;

			$fer_a_prendre = ceil($fer_a_prendre);
			$or_a_prendre = ceil($or_a_prendre);
			$ada_a_prendre = ceil($ada_a_prendre);
			
			}
		
		//pas de champs
		} else {
			
			$fer_a_prendre = 0;
			$or_a_prendre = 0;
			$ada_a_prendre = 0;
		}
		
		$req2 = "UPDATE matgraph_mobil_vruines SET mission='retour', date ='".$temps_retour."', fer='".$fer_a_prendre."', orr='".$or_a_prendre."', ada='".$ada_a_prendre."' WHERE id=$row[id]";
		$res2 = mysql_query($req2) or die('Erreur SQL! <br>'.$req2.'<br>'.mysql_error());
		
		$req2 = "UPDATE matgraph_ruines SET fer = fer - ".$fer_a_prendre.", orr = orr - ".$or_a_prendre.", ada = ada - ".$ada_a_prendre." WHERE galaxi='$row[galaxi]' AND system='$row[system]' AND position='$row[position]'";
		$res2 = mysql_query($req2) or die('Erreur SQL! <br>'.$req2.'<br>'.mysql_error());
		
		$title = "<font color=\"#EAA25B\">Vaisseaux ruines</font>";

		$msg = "Vos vaisseaux ruines sont bien arrivés aux coordonnées :  $row[galaxi] : $row[system] : $row[position] : <br> Vous avez récupéré: <br> ".number_format($fer_a_prendre, 0, ',', ' ')." Fer <br> ".number_format($or_a_prendre, 0, ',', ' ')." Or <br> ".number_format($ada_a_prendre, 0, ',', ' ')." Cristal";
		$requete = "INSERT INTO  matgraph_msg VALUES ('',now(), 'non', '$row[iddepart]', '$title', '".addslashes($msg)."')";
		$resultat = mysql_query($requete)or die('erreur sql<br>'.$requete.'<br>'.mysql_error());
	}
}


$req = "SELECT * FROM matgraph_mobil_vruines WHERE ".$liste_id." AND date < now() AND mission='retour' ORDER by date ASC";
$res = 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) ) {
		
		// del voyage
		$req2 = "DELETE FROM matgraph_mobil_vruines WHERE id=$row[id]";
		$res2 = mysql_query($req2) or die('Erreur SQL! <br>'.$req2.'<br>'.mysql_error());
		
		// maj ressource
		$req2 = "UPDATE matgraph_users SET fer_ressource = fer_ressource + '".$row[fer]."', or_ressource = or_ressource + '".$row[orr]."', ada_ressource = ada_ressource + '".$row[ada]."' WHERE id=$row[iddepart]";
		$res2 = mysql_query($req2) or die('Erreur SQL! <br>'.$req2.'<br>'.mysql_error());	
		
		// maj vruines
		$req2 = "UPDATE matgraph_vaisseauxruines SET vruines = vruines + $row[vruines] , vruines2 = vruines2 + $row[vruines2], vruines3 = vruines3 + $row[vruines3] WHERE id=$row[iddepart]";
		$res2 = mysql_query($req2) or die('Erreur SQL! <br>'.$req.'<br>'.mysql_error());
		
	
		// msg retour
		
			// select planete
		$sql2 = "SELECT planete,galaxi,system,position FROM matgraph_users WHERE id=$row[iddepart]";
		$res2 = mysql_query($sql2) or die('Erreur SQL! <br>'.$sql2.'<br>'.mysql_error());
		$row2 = mysql_fetch_array($res2);
		
		$title = "<font color=\"#7AB255\">Retour Vaisseaux ruines</font>";

		$msg = "Vos vaisseaux ruines sont rentrés sur votre planète $row2[planete] [$row2[galaxi] : $row2[system] : $row2[position]] : <br> Ils rapportent : <br> ".number_format($row[fer], 0, ',', ' ')." Fer <br> ".number_format($row[orr], 0, ',', ' ')." Or <br> ".number_format($row[ada], 0, ',', ' ')." Cristal";
		$requete = "INSERT INTO  matgraph_msg VALUES ('', '$row[date]', 'non', '$row[iddepart]', '$title', '".addslashes($msg)."')";
		$resultat = mysql_query($requete)or die('erreur sql<br>'.$requete.'<br>'.mysql_error());

	}
}		
include ('entete2.php');
?>