<html>
<head></head>
<link rel="stylesheet" href="admin.css">
<body rightmargin=0>
<div align=center class=trTitle>Réparation des incohérences sur les Mises</div>
<br>
<?php
include("../config.php3");
include($BIB."bib_generic.php3");
//include($BIB."bib_points.php3");

echo "Ce script répare les mises sur les paris, en conservant les proportions sur les mises.<br><br>";
if (($LIST == 1) || ($GO == 1))
{
	$players = main_db_query("SELECT * FROM Players");
	$i=0;
	while($player = db_fetch_array($players))
	{
        $pointsBet = pointsBet($player[Id],'');
		if ($player[Score] < $pointsBet)
		{
			$i++;
			//$member = getmember2($player[Id]);
	//		$m = main_db_query("SELECT * FROM Paris WHERE PlayerId='".$player[Id]."' AND MatchId='64'");
			echo "erreur sur le joueur ".$player[Id]." - ".$player[NickName]." score: ".$player[Score]." - parié:".$pointsBet;
	/*		if ($match = db_fetch_array($m))
				echo " xxx match joué ! : ".$match[Amount]."<br>";
			else*/
			//echo $member[Email].",";
			echo "<br>";
			if ($GO==1)
			{
	//			main_db_query("UPDATE Players SET Score='".pointsBet($player[Id],'')."' WHERE Id='".$player[Id]."'");
				$score = $player[Score];
				$mauvais_score = $pointsBet;
				$rapport = $score / $mauvais_score;
				$paris = main_db_query("SELECT Amount, PlayerId, MatchId FROM Paris WHERE Done='0' AND Paris.PlayerId='".$player[Id]."'");
				$somme_paris = 0;
				while ($pari = db_fetch_array($paris))
				{
					$somme_paris += floor($pari['Amount'] * $rapport);
					$query = "UPDATE Paris Set Amount='".min($player[Score]*$BET_LIMIT/100, floor($pari['Amount'] * $rapport))."' WHERE PlayerId='".$player[Id]."' AND MatchId='".$pari['MatchId']."' AND Done='0'";
					echo $query;
					if ($DEBUG != 1)
						main_db_query($query);
					else
						" - pas de maj";
					echo "<br>";
				}
				echo "Joueur ".$player[Id]." réparé (".$somme_paris."pts)<br>";
			}
			elseif ($DEL==1)
				main_db_query("DELETE FROM Paris WHERE PlayerId='".$player[Id]."' AND Done='0'");
	
			flush();
		}
	}
	
	if ($GO==1)
	{
		main_db_query("UPDATE Paris SET Amount='".$BET_LIMIT."' WHERE Amount > '".$BET_LIMIT."' AND Done='0'");
	}
		
	echo $i;
}

?>
<form>
	1. <input type=checkbox name=LIST value=1> Lister les incohérences
	<br>
	2. <input type=checkbox name=GO value=1> Réparer
	<br>
	3. Option : <input type=checkbox name=DEBUG value=1> DEBUG Mode (pas de requete)
	<br><br>
<!-- 	<input type=checkbox name=DEL value=1> Supprimer les paris des joueurs concernés -->
	<input type=submit>
</form>