<?php
##########################################
#
# remet les joueurs à 100pts 
# + leurs points de parrainage
#
##########################################

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

?>
<html>
<head></head>
<link rel="stylesheet" href="admin.css">
<body rightmargin=0>
<div align=center class=trTitle>Remise à zéro des scores</div>
<br>
<?php

function checkTriche($playerId)
{
	$row = db_fetch_array(main_db_query("SELECT SUM(IsSuspect) AS total,COUNT(*) AS nb FROM Players LEFT JOIN Members ON Members.Id=Players.MemberId WHERE FatherPId='".$playerId."'"));
	if ($row[nb]!=0)
		return $row[total]/$row[nb];
	else
		return 0;
}


if ($ok == "Valider")
{
	if ($parrainage==1)
	{
		# recup des players
		$players = main_db_query("SELECT * FROM Players");
		while($player=db_fetch_array($players))
		{
			# chaque joueur repart avec $score
			$tabPlayer[$player[Id]] = $score;
		}
		db_data_seek($players,0);
		echo "nb joueurs : ".db_num_rows($players)."<br>";
		
		# rajout des gains en parrainage de chaque joueur
		while($player=db_fetch_array($players))
		{
			# il n'y a pas de bonus pour les joueurs ayant été parrainé
			$tmp = $player[Sons]*$NB_PTS_PAR_FILLEUL + $player[GrandSons]*$NB_PTS_PAR_P_FILLEUL + $player[GrandGrandSons]*$NB_PTS_PAR_AP_FILLEUL;
			$tabPlayer[$player[Id]] += $tmp;
			echo $player[Id]." / ".$player[NickName]." / triche : ".checkTriche($player[Id])." / old : ".$player[Score]." / new : ".$tabPlayer[$player[Id]]." / parrainage ".$tmp."<br>\n";
			main_db_query("UPDATE Players SET Score='".$tabPlayer[$player[Id]]."' WHERE Id='".$player[Id]."'");
		}
		echo "nb players after : ".count($tabPlayer);
	}
	else
	{
		$str = "Score='".$score."'";
		
		if ($zerokf == 1)
			$str .= ", KF='100'";
			
		if ($zeroPerfect == 1)
			$str .= ", Niveau='0'";
			
		//main_db_query("UPDATE Players SET Score='".$score."', KF='".$kf."'");
		/*
		if ($zerokf == 1)
		{
			#echo "UPDATE Players SET Score='".$score."', KF=0";
			main_db_query("UPDATE Players SET Score='".$score."', KF=100");
		}
		else
		{
			#echo "UPDATE Players SET Score='".$score."'";
			main_db_query("UPDATE Players SET Score='".$score."'");
		}
		*/
		
		main_db_query("UPDATE Players SET ".$str);
	}
	echo "<br>Terminé";
}

if ($score=="")
	$score = $kf = $NB_PTS_INSCRIPTION;
?>
<form>
Remettre les scores à <input size=4 type=text name=score value="<?php echo $score ?>"> points
<br><br>
Remettre les KF à <?php echo $NB_PTS_INSCRIPTION ?> : <input type=checkbox name=zerokf value=1>
<br><br>
Remettre les Perfects à 0 : <input type=checkbox name=zeroPerfect value=1>
<br><br>
<!--<input type=checkbox name=parrainage value=1> ajouter les points de parrainage-->
<br><br>
<input type=submit value="Valider" name=ok>
</form>