<?php

$NB_MATCHS_CALCUL_KFS = 30;

$BAREME_NIVEAUX[0] = 0;
$BAREME_NIVEAUX[1] = 3;
$BAREME_NIVEAUX[2] = 6;
$BAREME_NIVEAUX[3] = 8;
$BAREME_NIVEAUX[4] = 10;
$BAREME_NIVEAUX[5] = 11;
$BAREME_NIVEAUX[6] = 12;
$BAREME_NIVEAUX[7] = 13;
$BAREME_NIVEAUX[8] = 14;
$BAREME_NIVEAUX[9] = 15;
$BAREME_NIVEAUX[10] = 16;
$BAREME_NIVEAUX[11] = 16.5;
$BAREME_NIVEAUX[12] = 17;
$BAREME_NIVEAUX[13] = 17.5;
$BAREME_NIVEAUX[14] = 18;
$BAREME_NIVEAUX[15] = 18.5;
$BAREME_NIVEAUX[16] = 19;
$BAREME_NIVEAUX[17] = 19.5;
$BAREME_NIVEAUX[18] = 20;
$BAREME_NIVEAUX[19] = 20.5;
$BAREME_NIVEAUX[20] = 21;
$BAREME_NIVEAUX[21] = 21.5;
$BAREME_NIVEAUX[22] = 22;
$BAREME_NIVEAUX[23] = 22.5;
$BAREME_NIVEAUX[24] = 23;
$BAREME_NIVEAUX[25] = 23.5;
$BAREME_NIVEAUX[26] = 24;
$BAREME_NIVEAUX[27] = 24.5;
$BAREME_NIVEAUX[28] = 25;
$BAREME_NIVEAUX[29] = 26;
$BAREME_NIVEAUX[30] = 27;
$BAREME_NIVEAUX[31] = 28;
$BAREME_NIVEAUX[32] = 29;
$BAREME_NIVEAUX[33] = 30;
$BAREME_NIVEAUX[34] = 31;
$BAREME_NIVEAUX[35] = 32;
$BAREME_NIVEAUX[36] = 33;
$BAREME_NIVEAUX[37] = 34;
$BAREME_NIVEAUX[38] = 35;
$BAREME_NIVEAUX[39] = 37;
$BAREME_NIVEAUX[40] = 39;
$BAREME_NIVEAUX[41] = 41;
$BAREME_NIVEAUX[42] = 43;
$BAREME_NIVEAUX[43] = 45;
$BAREME_NIVEAUX[44] = 47;
$BAREME_NIVEAUX[45] = 49;
$BAREME_NIVEAUX[46] = 51;
$BAREME_NIVEAUX[47] = 53;
$BAREME_NIVEAUX[48] = 55;
$BAREME_NIVEAUX[49] = 57;
$BAREME_NIVEAUX[50] = 59;
$BAREME_NIVEAUX[51] = 61;
$BAREME_NIVEAUX[52] = 63;
$BAREME_NIVEAUX[53] = 65;
$BAREME_NIVEAUX[54] = 67;
$BAREME_NIVEAUX[55] = 69;
$BAREME_NIVEAUX[56] = 71;
$BAREME_NIVEAUX[57] = 73;
$BAREME_NIVEAUX[58] = 75;
$BAREME_NIVEAUX[59] = 77;
$BAREME_NIVEAUX[60] = 79;
$BAREME_NIVEAUX[61] = 81;
$BAREME_NIVEAUX[62] = 83;
$BAREME_NIVEAUX[63] = 85;
$BAREME_NIVEAUX[64] = 87;
$BAREME_NIVEAUX[65] = 89;
$BAREME_NIVEAUX[66] = 91;
$BAREME_NIVEAUX[67] = 93;
$BAREME_NIVEAUX[68] = 95;
$BAREME_NIVEAUX[69] = 97;
$BAREME_NIVEAUX[70] = 99;
$BAREME_NIVEAUX[71] = 101;

/*
$BAREME_NIVEAUX[0] = 0;
$BAREME_NIVEAUX[1] = 3;
$BAREME_NIVEAUX[2] = 6;
$BAREME_NIVEAUX[3] = 8;

$cur_niveau = 10;
for ($i = 4 ; $i < 10 ; $i++)
{
	$BAREME_NIVEAUX[$i] = $cur_niveau;
	$cur_niveau++;
}
for ($i = 10 ; $i < 28 ; $i++)
{
	$BAREME_NIVEAUX[$i] = $cur_niveau;
	$cur_niveau += 0.5;
}
for ($i = 28 ; $i < 38 ; $i++)
{
	$BAREME_NIVEAUX[$i] = $cur_niveau;
	$cur_niveau++;
}
for ($i = 38 ; $i < 100 ; $i++)
{
	$BAREME_NIVEAUX[$i] = $cur_niveau;
	$cur_niveau+=2;
}
*/
$COUNT_BAREME_NIVEAUX = 72;
/*
for ($i = 0 ; $i < $COUNT_BAREME_NIVEAUX ; $i++)
{
	echo "\$BAREME_NIVEAUX[".$i."] = ".$BAREME_NIVEAUX[$i].";<br>";
}
*/
function th($x)
{
	$ex =exp($x);
	$e_x = exp(-$x);
	
	$th = - ($e_x - $ex) / ($e_x + $ex);
//	echo "th(".$x.") = ".$th."<br>";
	return $th;
	
}

function calcule_niveau($kfs_moyens)
{
	echo "Calcul du nic de $kfs_moyens<br>";
	global $BAREME_NIVEAUX, $COUNT_BAREME_NIVEAUX;

	for ($i = 0 ; $i < $COUNT_BAREME_NIVEAUX-1 ; $i++)
	{
		if ($kfs_moyens < $BAREME_NIVEAUX[$i+1])
			return $i;
	}
}



function calcule_niveau_math($kfs_moyens)
{
/*
	$CONSTANTE_A = 1/15;
	$CONSTANTE_B = 1 - 75*$CONSTANTE_A;
	$CONSTANTE_C = 1250*$CONSTANTE_A;
*/
	$LIMITE_INVERSION = 15;

	$tmp = $kfs_moyens - 25;
	$tmp = $tmp / 13;
	$tmp = th($tmp);
	return floor(15*$tmp + 15);
	/*
	if ($kfs_moyens >= $LIMITE_INVERSION)
	{
		$param = $kfs_moyens;
		return floor($CONSTANTE_A*$param*$param + $CONSTANTE_B*$param + $CONSTANTE_C);
	}
	else
	{
		$param = 2*$LIMITE_INVERSION - $kfs_moyens;
		$tmp = $CONSTANTE_A*$param*$param + $CONSTANTE_B*$param + $CONSTANTE_C;
		return floor(2*$LIMITE_INVERSION - $tmp);
	}
	*/
	
	}
function get_kfs($playerId, $nb_matchs, $date_calcul)
{
	global $KF_GAINS_PERFECT, $KF_GAINS_CORRECT, $KF_GAINS_ECHEC;
	global $RESULTAT_PERFECT, $RESULTAT_CORRECT, $RESULTAT_ECHEC;

	$query = "SELECT Date, Result FROM Paris WHERE PlayerId='".$playerId."' AND Date<'".$date_calcul."' AND Done!='0' AND Result!='0' ORDER By Date DESC LIMIT 0,".$nb_matchs;
//	echo $query."<br>";

	$matchs_concernes = main_db_query($query);
	$nb_matchs_concernes = db_num_rows($matchs_concernes);
	$i = 1;
//	echo "Nb de matchs : ".$nb_matchs_concernes."<br>";
	if ($nb_matchs_concernes <= 0)
		return 0;
	db_data_seek($matchs_concernes, $nb_matchs_concernes-1);
	$match_le_plus_ancien = db_fetch_array($matchs_concernes);
	$date_depart = $match_le_plus_ancien['Date'];
//	echo "+ ancien : ".$date_depart."<br>";
	// On compte les perfects & les corrects:

	$query = "SELECT Count(*) AS Total FROM Paris WHERE PlayerId='".$playerId."' AND Result='".$RESULTAT_PERFECT."' AND Date<'".$date_calcul."' AND Done!='0' AND Date>='".$date_depart."'";
//	echo $query."<br>";
	$perfects = db_fetch_array(main_db_query($query));
	$corrects = db_fetch_array(main_db_query("SELECT Count(PlayerId) AS Total FROM Paris WHERE PlayerId='".$playerId."' AND Result='".$RESULTAT_CORRECT."' AND Date<'".$date_calcul."' AND Done!='0' AND Date>='".$date_depart."'"));
	$echecs =   db_fetch_array(main_db_query("SELECT Count(PlayerId) AS Total FROM Paris WHERE PlayerId='".$playerId."' AND Result='".$RESULTAT_ECHEC."' AND Date<'".$date_calcul."' AND Done!='0' AND Date>='".$date_depart."'"));
	$nb_kfs = $perfects['Total'] * $KF_GAINS_PERFECT + $corrects['Total'] * $KF_GAINS_CORRECT + $echecs['Total'] * $KF_GAINS_ECHEC;
//	echo "Nb kfs :".$nb_kfs."<br>";
	return $nb_kfs;
}

function get_niveau($playerId)
{
	global $date, $NB_MATCHS_CALCUL_KFS;
	
	return calcule_niveau(get_kfs($playerId, $NB_MATCHS_CALCUL_KFS, $date)/$NB_MATCHS_CALCUL_KFS);
}