<?php 
include("../config.php3");
include($BIB."bib_generic.php3");

set_time_limit($TIME_LIMIT);
?>
<html>
<head></head>
<link rel="stylesheet" href="admin.css">
<body rightmargin=0>
<div align=center class=trTitle>Maintenance de la cohérence de la base</div>
<br>

<?
function countOffsprings($PId,$repair)
{
	# recup du player
	$player = getplayer2($PId);
	
	# on cherche la profondeur de l'arbre du player
	# elle maximum égale au rank max de celui de son RootPId
	$row = db_fetch_array(main_db_query("SELECT MAX(Rank) AS MRank FROM Players WHERE RootPId='".$player[RootPId]."'"));
	
	$total=0;
	for ($r=1;$r<=$row[MRank];$r++)
	{
		$n = countSons($PId,1,$r);
//		echo "n:".$n." PId:".$PId." r:".$r." Max:".$row[MRank]."<br>";
		if ($n==0)
			break;
		else
			$total += $n;
	}
	
	if ($player[Offsprings]!=$total)
	{
		echo "error Offsprings on player ".$PId." - ".$player[NickName]."/ real:".$total." shown:".$player[Offsprings]."-".$row[MRank]."<br>";
		if ($repair==1)
		{
			main_db_query("UPDATE Players SET Offsprings='".$total."' WHERE Id='".$PId."'");
			echo "repaired<br>";
		}
		flush();
	}
}


function countSons($PId,$deep,$maxDeep)
{
	$rows = main_db_query("SELECT * FROM Players WHERE FatherPId='".$PId."'");
	if ($deep<$maxDeep)
	{
		$total = 0;
		while($row=db_fetch_array($rows))
			$total += countSons($row[Id],$deep+1,$maxDeep);
	}
	else if ($deep==$maxDeep)
		$total = db_num_rows($rows);

	return $total;
}


function checkSons()
{
	# recherche les différences entre les Sons affichés et les vrais Sons.
	$alls = main_db_query("SELECT * FROM Players");
	
	while($all = db_fetch_array($alls))
	{
		$comp = db_fetch_array(main_db_query("SELECT COUNT(*) AS total FROM Players WHERE FatherPId='".$all[Id]."'"));
		if ($all[Sons]!=$comp[total])
		{
			echo "error on player ".$all[Id]." - ".$all[NickName]."/ real:".$comp[total]." shown:".$all[Sons]."<br>";
			
			# retabli la verifité
			
		}
	}
	echo "<br>Terminé";
}

if ($submit=="Valider")
{
	$row = db_fetch_array(main_db_query("SELECT Id FROM Players WHERE NickName='".$NickName."'"));
	echo countSons($row[Id],$deep,$maxDeep);
}
else if ($submit=="All")
{
	$rows = main_db_query("SELECT * FROM Players WHERE Sons>0");
	
	# pour chacun on verifie son Sons, GSons et GGSons
	while($row = db_fetch_array($rows))
	{
		$t=countSons($row[Id],1,1);
		if ($row[Sons]!=$t)
		{
			echo "error Sons on : ".$row[Id]."/".$row[NickName]." - real:".$t." shown:".$row[Sons]."<br>";
			if ($repair==1)
			{
				main_db_query("UPDATE Players SET Sons='".$t."' WHERE Id='".$row[Id]."'");
				echo "repaired<br>";
			}
		}
			
		$t=countSons($row[Id],1,2);
		if ($row[GrandSons]!=$t)
		{
			echo "error GrandSons on : ".$row[Id]."/".$row[NickName]." - real:".$t." shown:".$row[GrandSons]."<br>";
			if ($repair==1)
			{
				main_db_query("UPDATE Players SET GrandSons='".$t."' WHERE Id='".$row[Id]."'");
				echo "repaired<br>";
			}
		}

		$t=countSons($row[Id],1,3);
		if ($row[GrandGrandSons]!=$t)
		{
			echo "error GrandGrandSons on : ".$row[Id]."/".$row[NickName]." - real:".$t." shown:".$row[GrandGrandSons]."<br>";
			if ($repair==1)
			{
				main_db_query("UPDATE Players SET GrandGrandSons='".$t."' WHERE Id='".$row[Id]."'");
				echo "repaired<br>";
			}
		}
	}
	echo "<br>Terminé All";
}
else if ($submit == "Offsprings")
{
	$rows = main_db_query("SELECT * FROM Players WHERE Sons>0");
	
	# pour chacun on verifie son offsprings
	while($row = db_fetch_array($rows))
	{
		countOffsprings($row[Id],$offsprings);
	}
	echo "<br>Terminé Offsprings";
}
else if ($submit=="Members")
{
	$members = main_db_query("SELECT * FROM Members");
	while($member = db_fetch_array($members))
	{
		$isGuest=false;
		if (!db_fetch_array(main_db_query("SELECT Id FROM Players WHERE MemberId='".$member[Id]."'")))
		{
			# on vérifie si c'est un invité
			if ($guest = db_fetch_array(main_db_query("SELECT Id FROM Guests WHERE Email='".$member[Email]."'")))
				$isGuest=true;
			echo "Pas de player pour le member : ".$member[Id]." - ".$member[Email]." - Guest:".$guest[Id]."<br>\n";
			
			# suppression du member
			if ($repair_members==1)
			{
				main_db_query("DELETE FROM Members WHERE Id='".$member[Id]."'");
				echo "membre supprimé<br>";
				if ($isGuest)
				{
					main_db_query("DELETE FROM Guests WHERE Id='".$guest[Id]."'");
					echo "invitation supprimée<br>";
				}
			}
		}
	}
	
	echo "Terminé Members";
}
else if ($submit=="Players")
{
	$players = main_db_query("SELECT * FROM Players");
	while($player = db_fetch_array($players))
	{
		$isGuest=false;
		$req = main_db_query("SELECT Id FROM Members WHERE Id='".$player[MemberId]."'");
		$fet = db_fetch_array($req);
		if (!$fet)
		{
			echo "Pas de member pour le player : ".$player[Id]." - ".$player[NickName]."<br>\n";
			
			# suppression du player
			if ($repair_players==1)
			{
				main_db_query("DELETE FROM Players WHERE Id='".$player[Id]."'");
				echo "player supprimé<br>";
			}
		}
		mysql_free_result($req);
	}
	
	echo "Terminé Players";
}
else if ($submit=="Trim")
{
	$members = main_db_query("SELECT * FROM Members");
	while($member = db_fetch_array($members))
		main_db_query("UPDATE Members SET Email='".trim($member[Email])."' WHERE Id='".$member[Id]."'");

	echo "Terminé Trim";
}
?>
<form>
	NickName : <input type=text name=NickName value="<?php echo $NickName ?>">
	<br>
	deep : <input type=text name=deep value="<?php echo $deep ?>">
	<br>
	maxDeep : <input type=text name=maxDeep value="<?php echo $maxDeep ?>">
	<br>
	<input type=submit name=submit value="Valider">
	<br><br>
	Repair sons
	<input type=checkbox name=repair value=1>
	<input type=submit name=submit value="All">
	<br>
	Repair offsprings
	<input type=checkbox name=offsprings value=1>
	<input type=submit name=submit value="Offsprings">
	<br>
	Repair Members sans Players
	<input type=checkbox name=repair_members value=1>
	<input type=submit name=submit value="Members">
	<br>
	Repair Players sans Members
	<input type=checkbox name=repair_players value=1>
	<input type=submit name=submit value="Players">
	<br>
	Trim emails
	<input type=submit name=submit value="Trim">
</form>

