<?php

	include('correlation.php');
	include('echec_fonctions_communes.php');

function check_date($data) 
{
    if (date('d-m-Y', strtotime($data)) == $data) 
	{
        return "OK";
    } 
	else 
	{
        return "Erreur";
    }
}
			
			$J1 = $_POST['J1'];
			$J2 = $_POST['J2'];
			$selection_score = $_POST['selection_score'];
			$id_partie = $_POST['id_partie'];
			
			$flag_validation = false;
		
			switch ($selection_score)
			{
				case "Blanc gagne":
					$score_blanc = "1";
					$score_noir = "0";
					$flag_validation = true;
					break;
				
				case "Blanc perd":
					$score_blanc = "0";
					$score_noir = "1";
					$flag_validation = true;
					break;
				
				case "Noir gagne":
					$score_blanc = "0";
					$score_noir = "1";
					$flag_validation = true;
					break;
				
				case "Noir perd":
					$score_blanc = "1";
					$score_noir = "0";
					$flag_validation = true;
					break;
				
				case "Match nul":
					$score_blanc = "0.5";
					$score_noir = "0.5";
					$flag_validation = true;
					break;

			}
			
			if($flag_validation == true)
			{
				 $id_partie = $id_partie - 1;
				
				// sauvegarde du classement dans un fichier
	
				$sql = "SELECT * FROM parties ORDER by date asc"; 
				$results = mysql_query($sql);
				$nb_parties = mysql_num_rows($results);
							
				while($row = mysql_fetch_assoc($results))
				{
					$blanc = $row['w_name'];
					$noir = $row['b_name'];
					$date = $row['date'];
					$resultat = $row['result'];
					$date_partie = date('d/m/Y', $date);
					
					if($resultat == 1)
					{
						$_score_blanc = 1;
						$_score_noir = 0;
					}
					if($resultat == 2)
					{
						$_score_blanc = 0;
						$_score_noir = 1;
					}
					if($resultat == 3)
					{
						$_score_blanc = 0.5;
						$_score_noir = 0.5;
					}
				
					$table_csv[0] = $date_partie;
					$table_csv[1] = $blanc;
					$table_csv[2] = $noir;
					$table_csv[3] = $_score_blanc;
					$table_csv[4] = $_score_noir;

					$table_fichier[] = $table_csv;
				}
	
				$nom_fichier = "sauvegarde_edition.txt";
							
				$fp = fopen($nom_fichier, 'w'); 
				for ($i=0; $i < $nb_parties; $i++)
				{
					$str_comma_separated = implode(";", $table_fichier[$i]);
					fputs ($fp, $str_comma_separated);
					if($i != $nb_parties - 1)
					fputs($fp, "\n");
				}
						
				fclose($fp);
		
				// creation du fichier modifie
				
				$nom_fichier = "new_edition.txt";
							
				$fp = fopen($nom_fichier, 'w'); 
				for ($i=0; $i < $nb_parties; $i++)
				{
					
					if($i == $id_partie )
					{
						$table_fichier[$i][1] = $J1;
						$table_fichier[$i][2] = $J2;
						$table_fichier[$i][3] = $score_blanc;
						$table_fichier[$i][4] = $score_noir;
					}
				
					$str_comma_separated = implode(";", $table_fichier[$i]);
					fputs ($fp, $str_comma_separated);
					if($i != $nb_parties - 1)
					fputs($fp, "\n");
				}
						
				fclose($fp);

			   // joueur
				$joueur = array();
				$sql = "SELECT * FROM classement ORDER by joueur asc"; 
				$results = mysql_query($sql);
								
				while($row = mysql_fetch_assoc($results))
				{
					$joueur[] = $row['joueur'];
				}
				
				// regenerer le classement
				mysql_query('TRUNCATE TABLE classement;');
				mysql_query('TRUNCATE TABLE evolution;');
				mysql_query('TRUNCATE TABLE parties;');
	
				stockage_joueurs_DB($joueur);
	
				$file = fopen ($nom_fichier,"r");
		
				$indice =  1;	
				while(! feof($file))
				{
						$tmp = fgets($file);
						$tmp = strtolower($tmp);
						//echo $tmp ."<BR>";
						//$tmp = str_to_noaccent($tmp);
						$pieces = explode(";", $tmp);
						$donnees[0] = $pieces[0];
						list($jour, $mois, $annee) = explode('/', $donnees[0]);
						$timestamp = mktime (0, 0, 0, $mois, $jour, $annee);
						$donnees[0] = $timestamp; 
						$donnees[1] = $pieces[1];
						$donnees[2] = $pieces[2];
						$donnees[3] = $pieces[3];
						$donnees[4] = $pieces[4];
						algo_elo($donnees, "nostatistique", "sql", $indice );
						$indice = $indice + 1;
				}
					
				echo 1;
					

			}


?>