#*********************************************************************************************
#                                 Fait par JGP SOLDAT
#                               Maintenu par Kana-chan
#                             Pour l'Alliance Francophone
#                             Version 4.00, le 21 mai 2007  
#                           Première version 27 janvier 2001
#
#               (Programme d'insersion des données des meilleurs équipes)
#
#*********************************************************************************************

use Socket;
use DBI;

$test=0;
$max_teams = 100;

sub trim {
  my $chaine = shift;
  $chaine =~ s/^\s+|\s+$//g;
  return $chaine
};

# Parametres de connection a la base de donnees
$bd    = '/tmp/memory/fah.db';
$identifiant = '';      # identifiant
$motdepasse  = '';

# Connection a la base de donnees mysql
print("Connexion a la base de donnees $bd\n");
$db = DBI->connect(
        "DBI:SQLite:dbname=$bd",
        $identifiant, $motdepasse, {RaiseError => 1}
      ) or die "Connection impossible a la base de donnees $bd !\n $! \n $@\n$DBI::errstr";

open(file, "pos1.res"); $b=<file>; close (file);
$b++;
open(file, ">pos1.res"); print file ($b); close (file);

print("Recherche dans DATA/daily_team_summary.txt\n\n");
open(file, "DATA/daily_team_summary.txt");
$temp="";
$line=<file>;
$cpt=0;
while ($line ne "") {
  if ($cpt != 0 and $cpt != 1 and $cpt <= $max_teams+2) {
    $temp .= $line;
  }
  $line = <file>;
  $cpt += 1;
}
close (file);
unlink("DATA/daily_team_summary.txt");

@separe = split(/\n/,$temp);
$a=0;
$rec=$b;
$donner = "pos_general, numeroteam, score, wu, rec";
$donner1 = "numeroteam, nomteam";
@values = ();
while ($separe[$a] ne "" and $a < $max_teams) {
  @temp = split (/\t/, $separe[$a]);
  if ($temp[1] ne " ") {
    $pos = $a+1;
    $numeroteam = "T".$temp[0];
	$temp1 = $temp[1];
	$temp1 =~ s/'//g;
    $nom = trim($temp1);
    $temp1 = $temp[2];
    $temp1[0] =~ s/ //g;
    $score = int ($temp1);
    $temp1 = $temp[3];
    $temp1 =~ s/ //g;
    $wu = int ($temp1);
    $valeur = $score.",".$numeroteam.",".$wu.",".$rec.",".$nom;
    if ($test==1) { print ($valeur."\n"); }
	$values[$a] = $valeur;
  }  else  {
    print("\nPosition [".($a+1)."] = ".$separe[$a]."\n");
  }
  $a++;
}
@s = reverse(sort( {$a <=> $b} @values ));

$position = 1;
foreach $val (@s) {
  if ($val == "") {
	next;
  }
  @v = split(/,/, $val);
  $numeroteam = $v[1];
  $score = $v[0];
  $wu = $v[2];
  $rec = $v[3];
  $nom = $v[4];
  $valeur = "'".$position."','".$numeroteam."','".$score."','".$wu."','".$rec."'";
  $do = $db->prepare("INSERT INTO equipe (".$donner.") VALUES (".$valeur.")");
  $ErrNum = $do->execute() or die $DBI::errstr;
  if ($test==1) { print ("==>".$position." ".$numeroteam." ".$score." ".$wu."\n"); }
  my $foundteam = $db->selectrow_array("SELECT count(*) FROM liste_equipe WHERE numeroteam='".$numeroteam."'");
  if ($foundteam < 0) {
    $foundteam = 0;
  }

  if ($foundteam == 0) {
    if ($test==1) { print "Nouvelle equipe\n"; }
    $valeur1 = "'".$numeroteam."', '".$nom."'";
    if ($test==1) { print ($valeur1."\n"); }
    $do = $db->prepare("INSERT INTO liste_equipe (".$donner1.") VALUES (".$valeur1.")");
    $ErrNum = $do->execute() or die $DBI::errstr;
    if ($ErrNum < 0) {
      print ($ErrNum."  ".$DBI::errstr."\n\n");
    }
    #$do = $db->prepare("SELECT count(*) FROM liste_equipe WHERE numeroteam='".$numeroteam."'");
    #$do->execute() or die $ErrNum = 1;
    #if ($ErrNum != 0) {
    #  print ($ErrNum."  ".$ErrText."  ".%ErrConn."\n\n");
    #}
    #$foundteam = 0;
    #if (my $value1 = $do->fetchrow) {
    #  if ($value1 != 0) {
    #    $foundteam = 1;
    #  }
    #}
    #$do->finish();
    
    #if ($foundteam == 1 and $test==1) {
    #  print ($valeur1." ==> OK\n");
    #} elsif ($foundteam != 1) {
    #  print ($valeur1." ==> KO\n");
    #}
  }
  $position++;
}

$db->disconnect();

print("\nFin de recherche dans DATA/daily_team_summary.txt\n\n");
exit;
1;
