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


# pas de timeout possible quelle que soit l'action effectuée
set_time_limit($TIME_LIMIT);

####################
# Administration script defaults
####################
# pas de timeout possible qql soit l'action effectuée
function cmp_score($a,$b)
{
	if ($a==$b)
		return 0;
	
	if ($a>$b)
		return 1;
	else
		return 2;
}

function calcule_gain($Result,$Bet,$Amount,$rate1,$rate2)
{
	# si on a le meme resultat, score exact, on sort avec rate1
	if ($Result == $Bet)
	{
		return $Amount*$rate1;
	}
	
	# on etudie le result
	$tabResult = explode("-",$Result);
	$ResultType = cmp_score($tabResult[0],$tabResult[1]);

	# on etudie le Bet
	$tabBet = explode("-",$Bet);
	$BetType = cmp_score($tabBet[0],$tabBet[1]);

	# s'il a le gagnant, on sort avec rate2
	# sinon juste avec 1
	if ($BetType==$ResultType)
		return $Amount*$rate2;
	else
		return 0;
}

?>
<html>
<head></head>
<link rel="stylesheet" href="admin.css">
<body rightmargin=0>
<div align=center class=trTitle>stats</div>
<br>

<?php

if ($submit == "Valider")
{
	$matchs = main_db_query("SELECT MatchId, Result FROM Matchs WHERE Matchs.Date >= '".$DateFrom."' AND Matchs.Date <= '".$DateTo."' LIMIT 0,10");
	while($match = db_fetch_array($matchs))
		$tabMatch[$match[MatchId]] = $match[Result];

	echo db_num_rows($matchs)." matchs à traiter...<br>";

	# récup des paris et des résultats associés
	$strPlayer = " ORDER BY MatchId ASC"; 
	$rows = main_db_query("SELECT DISTINCT(MatchId), PlayerId, Bet FROM Paris WHERE Paris.Done!='0' AND Paris.Date >= '".$DateFrom."' AND Paris.Date <= '".$DateTo."' $strPlayer");	
	echo db_num_rows($rows)." paris à traiter...<br>";
	flush();
	$i=0;
	while($row = db_fetch_array($rows))
	{
		$CurMatchId = $row[MatchId];
		$CurPlayerId = $row[PlayerId];
		
		if ($tabMatch[$CurMatchId] != "")
		{
			$gain_joueur = calcule_gain($tabMatch[$CurMatchId],$row[Bet], 1, $KF_GAINS_PERFECT, $KF_GAINS_CORRECT);
			$result[$CurPlayerId][KF] += $gain_joueur;
			$result[$CurPlayerId][N] ++;
			$query = "UPDATE Paris Set Result='";
			if ($gain_joueur == $KF_GAINS_PERFECT)
				$query .= $RESULTAT_PERFECT;
			elseif ($gain_joueur == $KF_GAINS_CORRECT)
				$query .= $RESULTAT_CORRECT;
			else
				$query .= $RESULTAT_ECHEC;

			$query .= "' WHERE PlayerId='".$CurPlayerId."' AND MatchId='".$CurMatchId."'";

			main_db_query($query);	

			$i++;
			if ($i % 10000==0)
			{
				echo $i."-";
				flush();
			}
		}
		else
			echo "match : ".$row[MatchId]." non traité !<br>";
	}

	echo
	"<table border=0 cellpadding=4 cellspacing=4 class=univers>
		<tr class=trTitle>
			<td align=center colspan=16>Les meilleurs pronostiqueurs des matchs ci-dessus :</td>
		</tr>
		<tr class=trTitle>
			<td >N</td>
			
			<td >KF théorique</td>
			<td >nb matchs</td>
			
			<td >PlayerId</td>
			<td >Pseudo</td>
			<td >KF actuels</td>
			<td >NbVictoires</td>
			<td >NbPerfects</td>
			<td >NbMissed</td>-->
		</tr>";
	
	reset($result);
	arsort($result);
	
	while (list ($key, $val) = each ($result))
	{
		$r++;
		
		if ($r<=$NbWinners)
		{
			$row = db_fetch_array(main_db_query("SELECT * FROM Players LEFT JOIN Members ON Members.Id=Players.MemberId WHERE Players.Id='".$key."'"));
			$bonus = db_fetch_array(main_db_query("SELECT SUM(Price) AS total FROM CadeauxPlayers LEFT JOIN Cadeaux ON Cadeaux.Id=CadeauxPlayers.CadeauId WHERE PlayerId='".$key."'"));
			//$finalkf = db_fetch_array(main_db_query("SELECT KF FROM PlayerScores WHERE PlayerId='".$key."' AND TourId=5 ORDER BY Date DESC LIMIT 0,1"));
			$finalkf = db_fetch_array(main_db_query("SELECT KF FROM Players WHERE Id='".$key."' LIMIT 0,1"));
			$extrakf = db_fetch_array(main_db_query("SELECT SUM(KFextra) AS total FROM PlayerScores WHERE PlayerId='".$key."'"));
			$valBonus = $finalkf[KF] + $bonus[total] - $val[KF];	
			$color = ($r % 2 == 0)? "#FFFFFF":"#E8E8E8";
			echo "<tr bgcolor=".$color.">";
			echo "<td>".$r."</td>";
			echo "<td>".$val[KF]."</td>";
			echo "<td>".$val[N]."</td>";
			
			echo "<td>".$key."</td>";
			echo "<td>".$row[NickName]."</td>";

			echo "<td>".$finalkf[KF]."</td>";
			echo "<td>".$bonus[total]."</td>";
			echo "<td>".$extrakf[total]."</td>";
			
			echo "</tr>";
		}
	}
	echo "<tr class=trTitle><td colspan=16>&nbsp;<?php echo $error ?></td></tr></table>";

}
?>
<form>
<table>
	<tr>
		<td>
			Du
		</td>
		<td>
			<input type=text name=DateFrom value="<?php echo $DateFrom ?>">AAAA-MM-JJ HH:MM:SS
		</td>
	</tr>
	<tr>
		<td>
			au :
		</td>
		<td>
			<input type=text name=DateTo value="<?php echo $DateTo ?>">AAAA-MM-JJ HH:MM:SS
		</td>
	</tr>
</table>
<br><br>
<input type=submit name=submit value="Valider">
</form>