<?php
##################################
# test l'unicité des nicknames
##################################
include("../config.php3");
include($BIB."bib_generic.php3");


if ($submit=="Valider")
{
	$rows=main_db_query("SELECT * FROM Players");

	while($row = db_fetch_array($rows))
	{
		$new_nick = trim($row[NickName]);
	
		if ($trimit==1)
		{
			$req = "UPDATE Players SET NickName='".$new_nick."' WHERE Id='".$row[Id]."'";
			$res = main_db_query($req);
			if (!$res)
				echo "<br>Pb : ".$req."<br>";
		}
		else
		{
			$req = db_fetch_array(main_db_query("SELECT * FROM Players WHERE NickName='".$new_nick."' AND Id!='".$row[Id]."'"));
			if ($req)
			{
				# s'il y a un doublon
				# recherche de la date du dernier pari car il va falloir en virer un des deux
				$bet1 = db_fetch_array(main_db_query("SELECT MAX(Date) AS maxDate FROM Paris WHERE PlayerId='".$row[Id]."'"));
				$bet2 = db_fetch_array(main_db_query("SELECT MAX(Date) AS maxDate FROM Paris WHERE PlayerId='".$req[Id]."'"));
				
				$nb1 = db_fetch_array(main_db_query("SELECT COUNT(*) AS nb FROM Paris WHERE PlayerId='".$row[Id]."'"));
				$nb2 = db_fetch_array(main_db_query("SELECT COUNT(*) AS nb FROM Paris WHERE PlayerId='".$req[Id]."'"));

				echo "doublon - PId : ".$row[Id]." MId:".$row[MemberId]." HH".$row[NickName]."HH (".$bet1[maxDate].") (".$nb1[nb].")<br>";
				echo "avec -----PId : ".$req[Id]." MId:".$req[MemberId]." HH".$req[NickName]."HH (".$bet2[maxDate].") (".$nb2[nb].")<br><br>";
			}
		}
	}
	echo "Terminé";
}
?>
<form>
<input type=checkbox name=trimit value=1>OK pour un trim sur les nicknames
<input type=submit name=submit value="Valider">
</form>