<?php
function trouvNom($id_user){
	global $table_user;
	$base = new bdd();
	$base->connexion();
	$resultat = $base->unique_query("SELECT pseudo FROM $table_user WHERE id = $id_user;");
	$base->deconnexion();
	return $resultat['pseudo'];
}

function trouvId($nom){
	global $table_user;
	$base = new bdd();
	$base->connexion();
	$resultat = $base->unique_query("SELECT id FROM $table_user WHERE pseudo = '$nom';");
	$base->deconnexion();
	return $resultat['id'];
}

function trouvInfo($id_user, $info){
	global $table_user;
	$base = new bdd();
	$base->connexion();
	$resultat = $base->unique_query("SELECT $info FROM $table_user WHERE id = $id_user;");
	$base->deconnexion();
	return $resultat[$info];
}

function affTemp($secondes){
	$heures = intval($secondes/3600);
	$minutes = intval($secondes/60-($heures*60));
	if ($minutes < 10) $minutes = '0'.$minutes;
	$seconde = $secondes-($heures*3600)-($minutes*60);
	if ($seconde < 10) $seconde = '0'.$seconde;

	return $heures.':'.$minutes.':'.$seconde;
}

function separerNombres($nombre) {
	return number_format($nombre, 0, ',', ' ');
}

function sec($time) {
	$output = '';
	$tab = array ('jour' => '86400', 'heure' => '3600', 'minute' => '60', 'seconde' => '1');
	foreach ($tab as $key => $value) {
		$compteur = 0;
		while ($time > ($value-1)) {
			$time = $time - $value;
			$compteur++;
		}
		if ($compteur != 0) {
			$output .= $compteur.' '.$key;
			if ($compteur > 1) $output .= 's';
			if ($value != 1) $output .= ' ';
		}
	}
	return $output;
}

function readDeblok($tableau, $queryPlanete){
	global $batimentVAR, $technoloVAR, $casernenVAR, $nomterrnVAR, $nomvaisnVAR;
	if ($tableau == '0') return 'Débloqué';
	else {
		$nbDeblok = count($tableau);
		$return = '';
		for ($i=0 ; $i<$nbDeblok ; $i++) {
			if ($x = array_search($tableau[$i][1], $batimentVAR)) $var = 'batiments';
			elseif ($x = array_search($tableau[$i][1], $technoloVAR) || $tableau[$i][1] == $technoloVAR[0]) $var = 'technologies';
			elseif ($x = array_search($tableau[$i][1], $casernenVAR)) $var = 'casernes';
			elseif ($x = array_search($tableau[$i][1], $nomterrnVAR)) $var = 'terrestres';
			elseif ($x = array_search($tableau[$i][1], $nomvaisnVAR)) $var = 'vaisseaux';
			else return '#Error';

			//print $tableau[$i][2];
			if (isset($var)) $return .= techno($tableau[$i][0], $queryPlanete->{$var}[$x], $tableau[$i][2]).'<br />';
		}
		if (empty($return)) return 'Débloqué';
		else return $return;
	}
}

function requestDeblok($tableau, $queryPlanete){
	global $batimentVAR, $technoloVAR, $casernenVAR, $nomterrnVAR, $nomvaisnVAR;
	if ($tableau == '0') return true;
	else {
		$nbDeblok = count($tableau);
		$return = true;
		for ($i=0 ; $i<$nbDeblok ; $i++) {
			if ($x = array_search($tableau[$i][1], $batimentVAR)) $var = 'batiments';
			elseif ($x = array_search($tableau[$i][1], $technoloVAR) || array_search($tableau[$i][1], $technoloVAR) === 0) $var = 'technologies';
			elseif ($x = array_search($tableau[$i][1], $casernenVAR)) $var = 'casernes';
			elseif ($x = array_search($tableau[$i][1], $nomterrnVAR)) $var = 'terrestres';
			elseif ($x = array_search($tableau[$i][1], $nomvaisnVAR)) $var = 'vaisseaux';
			else return false;

			if (isset($var) && $queryPlanete->{$var}[$x] < $tableau[$i][2]) $return = false;
		}
		return $return;
	}
}

function techno($nom, $niveau, $niveau_ress) {
	if (empty($niveau)) $niveau = 0;
	if ($niveau < $niveau_ress) return '<span class="lack">'.$nom.' '.$niveau_ress.' ('.$niveau.')</span>';
	else return ''.$nom.' '.$niveau_ress.' ('.$niveau.')';
}

function txtmission($mission){
	if ($mission == 0) return 'Stationner';
	elseif ($mission == 1) return 'Attaquer';
	elseif ($mission == 2) return 'Transporter';
	elseif ($mission == 3) return 'Coloniser';
	elseif ($mission == 4) return 'Recycler';
	elseif ($mission == 5) return 'Espionner';
	elseif ($mission == 6) return 'Retour';
	else return 'Erreur';
}

function txtTactique($tactique){
	switch($tactique){
		case 1: return '1'; break;
		case 2: return '2'; break;
		case 3: return '3'; break;
		case 4: return '4'; break;
		case 5: return '5'; break;
		case 6: return '6'; break;
		case 7: return '7'; break;
		case 8: return '8'; break;
		case 9: return '9'; break;
		case 10: return '10'; break;
		case 11: return '11'; break;
		case 12: return '12'; break;
		case 13: return '13'; break;
		case 14: return '14'; break;
		case 15: return '15'; break;
		case 16: return '16'; break;
		case 17: return '17'; break;
		case 18: return '18'; break;
		case 19: return '19'; break;
		case 20: return '20'; break;
		case 21: return '21'; break;
		default: return 'autre'; break;
	}
}

function tactique($tactique){
	switch($tactique){
		case 1: return array(1,0,12,false); break;
		case 2: return array(2,88,12,false); break;
		case 3: return array(3,75,25,false); break;
		case 4: return array(4,63,37,false); break;
		case 5: return array(5,50,50,false); break;
		case 6: return array(6,37,63,false); break;
		case 7: return array(7,25,75,false); break;
		case 8: return array(8,12,88,false); break;
		case 9: return array(9,12,88,true); break;
		case 10: return array(10,25,75,true); break;
		case 11: return array(11,37,63,true); break;
		case 12: return array(12,50,50,true); break;
		case 13: return array(13,63,37,true); break;
		case 14: return array(14,75,25,true); break;
		case 15: return array(15,75,26,true); break;
		case 16: return array(16,75,27,true); break;
		case 17: return array(17,75,28,true); break;
		case 18: return array(18,75,29,true); break;
		case 19: return array(19,75,30,true); break;
		case 20: return array(20,75,31,true); break;
		case 21: return array(21,75,32,true); break;
		default: return array(1,0,10,false); break;
	}
}

function pillage($metal, $cristal, $hydrogene, $vfm){
	$Qm = $metal / $vfm;
	$Qc = $cristal / $vfm;
	$Qh = $hydrogene / $vfm;

	$somme = $Qm + $Qc + $Qh;

	if ($somme < 2) {
		$Fm = $metal / 2;
		$Fc = $cristal / 2;
		$Fh = $hydrogene / 2;
	}
	else {
		$Fm = $Qm / $somme * $vfm;
		$Fc = $Qc / $somme * $vfm;
		$Fh = $Qh / $somme * $vfm;
	}

	return array(floor($Fm), floor($Fc), floor($Fh));
}

function vais_tempsDeplacement($start_galaxie,$start_ss,$start_position,$end_galaxie,$end_ss,$end_position,$vitesse,$coefvitesse = 1,$preparation = 0,$chauffe = 0) {
	if ($end_galaxie-$start_galaxie == 0 && $end_ss-$start_ss == 0 && $end_position-$start_position == 0) $temps = 0;
	elseif ($end_galaxie-$start_galaxie == 0 && $end_ss-$start_ss == 0 && $chauffe == 0) $temps = $preparation+abs($end_position-$start_position)*$vitesse/12;
	elseif ($end_galaxie-$start_galaxie == 0 && $chauffe == 0) $temps = $preparation+abs($end_ss-$start_ss)*$vitesse;
	elseif ($chauffe == 0) $temps = $preparation+abs($end_galaxie-$start_galaxie)*$vitesse*300;
	elseif ($end_galaxie-$start_galaxie == 0 && $end_ss-$start_ss == 0) $temps = $preparation+abs($end_position-$start_position)*($vitesse*2)/12+$preparation;
	/*elseif ($end_galaxie-$start_galaxie == 0 && $end_ss-$start_ss <= $chauffe) $temps = abs($end_ss-$start_ss)*(($vitesse/$chauffe)*($chauffe-abs($end_ss-$start_ss))+$vitesse)+$preparation;
	elseif ($end_galaxie-$start_galaxie == 0) $temps = $vitesse*abs($end_ss-$start_ss)+$vitesse*10+$preparation;*/
	elseif ($end_galaxie-$start_galaxie == 0) $temps = (abs($end_ss-$start_ss)*(($vitesse/$chauffe)*((abs($chauffe-abs($end_ss-$start_ss))+($chauffe-abs($end_ss-$start_ss)))/2)+$vitesse))+(20-(abs($chauffe-abs($end_ss-$start_ss))+($chauffe-abs($end_ss-$start_ss)))/2)*54+$preparation;
	else $temps = (($vitesse/$chauffe)*($chauffe-abs($end_galaxie-$start_galaxie))+$vitesse)*300+108+$preparation;

	return $temps/$coefvitesse;
}

function vaisTempsdeplacement($start_amas, $start_ss, $start_pos, $end_amas,$end_ss, $end_pos, $vitessevaisseaux, $vitesseflotte=100) {
	if($end_amas - $start_amas != 0) return (10+(35000/$vitesseFlotte*sqrt(abs($end_amas-$start_amas)*20000000/$vitesseVaisseaux)));
	elseif($end_ss-$start_ss != 0) return (10+(35000/$vitesseFlotte*sqrt(2700000+abs($end_ss-$start_ss)*95000/$vitessevais)));
	elseif($end_pos-$start_pos != 0) return (10+(35000/$vitesseFlotte*sqrt((abs($end_pos-$start_pos)*5000/$vitessevais))));
	else return (10+(35000/$vitesseFlotte-sqrt(5000/$vitesseVais)));
}


function vais_conso($tempsDeplacement,$nbvais = 1){
	return intval($tempsDeplacement*$nbvais/20);
}

function send_mail($admail, $sujet, $corps){
	require_once(_FCORE."../game/Class/class.phpmailer.php");

	$mail = new PHPmailer();
	$mail->SetLanguage('fr', _FCORE."../game/Class/");
	$mail->IsSMTP();
	$mail->IsHTML(true);
	$mail->Host='s-fr.com';
	$mail->From='no-reply@halo-battle.s-fr.com';
	$mail->FromName='Halo-Battle';
	$mail->SMTPAuth=true;
	$mail->Username='no-reply@halo-battle.s-fr.com';
	$mail->Password='hD3e2nXu';

	$mail->AddAddress($admail);
	$mail->AddReplyTo('no-reply@halo-battle.s-fr.com');
	$mail->Subject = $sujet;
	$mail->Body = $corps;

	$return = $mail->Send();
	$mail->SmtpClose();

	return $return;
}

function send_mailHTML($admail, $sujet, $corps){
	require_once(_FCORE."../game/Class/class.phpmailer.php");

	$mail = new PHPmailer();
	$mail->SetLanguage('fr', _FCORE."../game/Class/");
	$mail->IsSMTP();
	$mail->IsHTML(true);
	$mail->Host='s-fr.com';
	$mail->From='no-reply@halo-battle.s-fr.com';
	$mail->FromName='Halo-Battle';
	$mail->SMTPAuth=true;
	$mail->Username='no-reply@halo-battle.s-fr.com';
	$mail->Password='hD3e2nXu';

	$mail->AddAddress($admail);
	$mail->AddReplyTo('no-reply@halo-battle.s-fr.com');
	$mail->Subject = 'Halo-Battle :: '.$sujet;
	$mail->Body = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html><head><title>Halo-Battle :: '.$sujet.'</title></head><body><p>'.$corps.'<br /><br />A bient&ocirc;t dans Halo-Battle,<br />Le staff</p></body></html>';

	$return = $mail->Send();
	$mail->SmtpClose();

	return $return;
}

function erreur($message, $color = "red", $lien = "", $temps = 2500){
	global $template;
	$template->assign('message', $message);
	if (!empty($lien)) $template->assign('script','<script type="text/javascript">setTimeout(\'document.location.href="'.$lien.'";\', '.$temps.');</script>');
	$template->assign('couleur',$color);
	$template->display('game/erreur.tpl');
	exit;
}

function send_mp($joueur, $titre, $message){
	global $table_mail;
	$temps = time();
	if (!is_numeric($joueur)) $joueur = trouvId($joueur);
	if (!empty($joueur)) {
		$base = new bdd();
		$base->connexion();
		$base->escape($joueur);
		$base->escape($titre);
		$base->escape($message);
		$base->query("INSERT INTO $table_mail (destinataire, sujet, contenu, temps) VALUES($joueur, '$titre', '$message', '$temps');");
		$base->deconnexion();
	}
}

function nameLink($id, $type){
	global $race;
	require(_FCORE."../game/noms.php");

	if ($type == "technologie") return '<a href="?p=description&amp;t=1">'.$technolo[$id].'</a>';
}

function mdp($nom, $mdp, $alea = false){
	if (empty($alea)) {
		$alea = random(1024);
		return array(hash_var(cxor(strtoupper($nom).':'.$mdp.'♂♪',$alea)), $alea);
	}
	else return hash_var(cxor(strtoupper($nom).':'.$mdp.'♂♪',$alea));
}
?>