<?php
///////////////////////////
//configuration
$max_system=100;
//
///////////////////////////
print "<center>Reconstruction de l'Univers en cours ...</center>";
$sql = 'SELECT * FROM matgraph_users ORDER BY galaxi, system, position';
$res = db_mysql_Query($sql);
$univers=array();
while ($row = mysql_fetch_array($res)){
	$univers[$row['galaxi']][$row['system']][$row['position']]=array($row['id'],$row['idjoueur']);
}
foreach($univers as $galaxi=>$systems){
	print("Galaxi n° ".$galaxi.":<br/>");
	foreach($systems as $system=>$positions){
		$req="SELECT * FROM matgraph_users WHERE system='". ($system-1) ."' AND galaxi='1'";
		$sql=mysql_query($req) or die('Erreur MySql : '.mysql_error().'<br/>'.$req);
		$num=mysql_num_rows($sql);
		while($num == '0' && ($system-1)>0){
			$update="UPDATE matgraph_users SET system='". ($system-1) ."' WHERE system ='".$system."' AND galaxi='1'";
			$upd=mysql_query($update) or die('Erreur MySQL : '.mysql_error());
			$system=($system-1);
			$req="SELECT * FROM matgraph_users WHERE system='". ($system-1) ."' AND galaxi='1'";
			$sql=mysql_query($req) or die('Erreur MySql : '.mysql_error().'<br/>'.$req);
			$num=mysql_num_rows($sql);
		}
		if($system != $max_system){
			$query="SELECT system FROM matgraph_users WHERE galaxi='". ($galaxi-1) ."'ORDER BY system DESC";
			$sql=mysql_query($query) or die('Erreur MySQL : '.mysql_error());
			$num=mysql_num_rows($sql);
			if($num < $max_system && ($galaxi-1)!=0){
				$query="SELECT system FROM matgraph_users WHERE galaxi='". ($galaxi-1) ."'ORDER BY system DESC LIMIT 0,1";
				$sql=mysql_query($query) or die('Erreur MySQL : '.mysql_error());
				$row=mysql_fetch_array($sql);
				$update="UPDATE matgraph_users SET galaxi='". ($galaxi-1) ."',system='". ($row[0]+1) ."' WHERE galaxi='".$galaxi."' AND system='".$system."'";
				$upd=mysql_query($update) or die('Erreur MySQL : '.mysql_error());
			}
		}
		print("->Système n° ".$system.": <font style='color:green;'>Ok</font><br/>");
	}
	set_time_limit(180);
	print('<br/>');
}
?>