<?php
include ('config.php');
include ('fichier_definition.php');
require_once ('include/general.php');
/*
if($idlogin !== '1') {
echo '<script language=javascript> alert ("L'univers est fermé pour le moment.");</script>';
echo '<script language="javascript" type="text/javascript">
<!--
window.location.replace("index.php");
-->
</script>';
exit; }
*/
session_start();
$login = $_SESSION["login"];
if(!defined("SECU_IP_SESSION"))
{
	// On évite la multi-inclusion
	define("SECU_IP_SESSION", 1);
	
	function get_ip() {
	    return $_SERVER['REMOTE_ADDR'];
	}
	$ip = get_ip();
	if($_SESSION['ip'] != $ip) {
		
		session_destroy();
		echo '<script language="javascript" type="text/javascript">
		<!--
		window.location.replace("http://www.sg-univers.com/");
		-->
		</script>'; 
		exit;
	}
}

$sql2 = "SELECT * FROM matgraph_users WHERE login='$login' AND id = idjoueur ORDER BY ID ASC limit 1";
$res2 = db_mysql_Query($sql2)or die('erreur sql<br>'.$sql2.'<br>'.mysql_error());
$row2 = mysql_fetch_array($res2); 
$dataid['id'] = $row2['id'] ;
$idlogin = $dataid['id'] ;
$limite_mp = $row2['limite_mp'];
$energie_tech_lvl = $row2['techenergie_lvl'];

// sécu multi connection
$queryyy="SELECT secu
				FROM matgraph_connexion
	    	WHERE idjoueur='".$idlogin."'";
$resyy = mysql_query($queryyy) or TraiterErreur($queryyy, mysql_error(),__FILE__,__LINE__);
$rowyy = mysql_fetch_array($resyy);
if ($rowyy['secu'] != $_SESSION['secu_multi_pc']) {
	session_destroy();
	echo '<script language=javascript> alert ("Double connection interdite");</script>';
	echo '<script language="javascript" type="text/javascript">
	<!--
	window.location.replace("http://www.sg-univers.fr/");
	-->
	</script>'; 
	exit;
}

$req3 = "SELECT planete FROM matgraph_idjoueur WHERE idjoueur='$idlogin'";
$res3 = db_mysql_Query($req3)or die('erreur sql<br>'.$req3.'<br>'.mysql_error());
$row3 = mysql_fetch_array($res3); 
$planete = $row3['planete'];

if($planete == $idlogin)
{
	$row = $row2;
}
else
{
	$sql = "SELECT * FROM matgraph_users WHERE id='$planete' ";
	$res = db_mysql_Query($sql)or die('erreur sql<br>'.$sql.'<br>'.mysql_error());
	$row = mysql_fetch_array($res); 
}

$pnom = $row['planete'];
$pgalaxi = $row['galaxi'];
$psystem  = $row['system'];
$pposition = $row['position'];

if (($row['placepris'] + 1 ) <= $row['place']) {
$place = 1 ;}
else {
$place = -1 ; }


if($row2[vacance] !== "0000-00-00 00:00:00") {

ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})", $row2[vacance], $thetime);
$time = mktime($thetime[4],$thetime[5],$thetime[6],$thetime[2],$thetime[3],$thetime[1]);

$diftemps = time() - $time;
$difheure = round($diftemps / 3600,1);

if($difheure >= 48) {
	$requete = "UPDATE matgraph_users SET date = now() WHERE idjoueur='$idlogin'";
	$resultat = db_mysql_Query($requete)or die('erreur sql<br>'.$requete.'<br>'.mysql_error());
	
	$requete = "UPDATE matgraph_users SET vacance = '0000-00-00 00:00:00' WHERE id='$idlogin'";
	$resultat = db_mysql_Query($requete)or die('erreur sql<br>'.$requete.'<br>'.mysql_error());
	
	$requete = "INSERT INTO matgraph_vacance VALUES ('', '$idlogin',now() )";
	$resultat = db_mysql_Query($requete)or die('erreur sql<br>'.$requete.'<br>'.mysql_error());
}
else
{

	echo '<script language=javascript> alert ("Vous etes en mode \"pause\" ");</script>';
	
	echo '<script language="javascript" type="text/javascript">
	<!--
	window.location.replace("http://www.sg-univers.com/");
	-->
	</script>';
	
	exit;
	}

}


if($row2[pts_def] < 0 OR $row2[pts_flotte] < 0) {
if($row2[pts_def] < 0) {
$New_pts = $row2[pts_flotte] + $row2[pts_bats] + $row2[pts_tech]; }
else {
$New_pts = $row2[pts_bats] + $row2[pts_def] + $row2[pts_tech]; } }
else {
$New_pts = $row2[pts_flotte] + $row2[pts_bats] + $row2[pts_def] + $row2[pts_tech]; }

$requete = "UPDATE matgraph_users SET pts=$New_pts WHERE id='$idlogin'";
$resultat = db_mysql_Query($requete)or die('erreur sql<br>'.$requete.'<br>'.mysql_error());


//pour pour rester sur la meme page lords du changement de colo
$PHP_SELF = $_SERVER['PHP_SELF'] ;


// maj connectés
$sql_connectes="SELECT count(*)
	  FROM matgraph_connectes
	  WHERE idjoueur = '".$idlogin."'";
$res_connectes = db_mysql_Query($sql_connectes)or die('erreur sql<br>'.$sql_connectes.'<br>'.mysql_error());
$row_connectes = mysql_fetch_array($res_connectes);

if($row_connectes['count(*)'] > 0) { // update
	$sql_connectes="UPDATE matgraph_connectes
			SET time = '".time()."'
		  WHERE idjoueur = '".$idlogin."'";
	$res_connectes = db_mysql_Query($sql_connectes)or die('erreur sql<br>'.$sql_connectes.'<br>'.mysql_error());
} else { // insert
	$sql_connectes="INSERT INTO `matgraph_connectes` ( `idjoueur` , `time` )
	VALUES (
	'".$idlogin."', '".time()."'
	)";
	$res_connectes = db_mysql_Query($sql_connectes)or die('erreur sql<br>'.$sql_connectes.'<br>'.mysql_error());
}

// vide ancien
$sql_connectes="DELETE FROM matgraph_connectes
	  WHERE time < '".(time() - 5*60)."'";
$res_connectes = db_mysql_Query($sql_connectes)or die('erreur sql<br>'.$sql_connectes.'<br>'.mysql_error());


?>
