#*********************************************************************************************
#                                 Fait par JGP SOLDAT
#                               Maintenu par Kana-chan
#                             Pour l'Alliance Francophone 
#                             Version 4.02, le 4 juin 2007 
#*********************************************************************************************

use Socket;
use DBI;
use GD;
use GD::Graph::lines;
use Encode qw(decode encode);

$maintenu = "Kana-chan";
$destWWW = "/var/www/html";

# 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";

$test = 0;

$nb_jours = 61;

$nb_team = 30;
$nb_team_split = 10;

$pas_saut_graph_1 = 1000;
$pas_saut_graph_2 = 1000;
$pas_saut_graph_3 = 1000;
$valeur_mini_init = 100000000000;

if ($test==1) { print("\nTest Graphique 1\n"); }
print("\nTraitement des donnees\n");
$arial="C:/WINDOWS/Fonts/arial.ttf";

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

$b1=$b-$nb_jours;

@mois2 = ('janv','fév','mars','avril','mai','juin','juil','aout','sept','oct','nov','déc');
@mois1 = ('janvier','février','mars','avril','mai','juin','juillet','aout','septembre','octobre','novembre','décembre');
($sec,$min,$heure,$jour,$mois,$annee,$jour2) = (localtime(time))[0,1,2,3,4,5,6];
$annee += "1900";

$c=$nb_jours;
while ($c > 0) {
  $a[$c]=$jour." ".$mois2[$mois];
  $jour--;
  if ($jour < 1 and ($mois == 0 or $mois == 1 or $mois == 3 or $mois == 5 or $mois == 7 or $mois == 8 or $mois == 10)) { $jour=31; $mois--; if ($mois < 0) { $mois=11; $annee--; } }
  elsif ($jour < 1 and ($mois == 9 or $mois == 4 or $mois == 6 or $mois == 11)) { $jour=30; $mois--; }
  elsif ($jour < 1 and $mois == 2 and $annee == 2004) { $jour=29; $mois--; }
  elsif ($jour < 1 and $mois == 2 and $annee > 2004) { $jour=28; $mois--; }
  else {}
  $c--;
}

($sec,$min,$heure,$jour,$mois,$annee,$jour2) = (localtime(time))[0,1,2,3,4,5,6];
if ($sec < 10) { $sec = "0".$sec; }
if ($min < 10) { $min = "0".$min; }
if ($heure < 10) { $heure = "0".$heure; }
if ($jour < 10) { $jour = "0".$jour; }
$annee += "1900";

$ErrNum = 0;
if ($test==1) { print "\nSELECT * FROM equipe WHERE rec = ".$b; }
$do = $db->prepare("SELECT pos_general,numeroteam FROM equipe WHERE rec = ".$b);
$ErrNum = $do->execute() or die $DBI::errstr;
while ($ErrNum >= 0 and my @data = $do->fetchrow_array()) {
  $tri[$data[0]] = $data[1];
  $membre++;
  if ($data[1] eq "T51") {
    $pos = $data[0];
  }
}

my @data1 = ( [ $a[1] , $a[2] , $a[3] , $a[4] , $a[5] , $a[6] , $a[7] , $a[8] , $a[9] , $a[10],
                $a[11], $a[12], $a[13], $a[14], $a[15], $a[16], $a[17], $a[18], $a[19], $a[20],
                $a[21], $a[22], $a[23], $a[24], $a[25], $a[26], $a[27], $a[28], $a[29], $a[30],
                $a[31], $a[32], $a[33], $a[34], $a[35], $a[36], $a[37], $a[38], $a[39], $a[40],
                $a[41], $a[42], $a[43], $a[44], $a[45], $a[46], $a[47], $a[48], $a[49], $a[50],
                $a[51], $a[52], $a[53], $a[54], $a[55], $a[56], $a[57], $a[58], $a[59], $a[60],
                $a[61] ] );

my @data2 = ( [ $a[1] , $a[2] , $a[3] , $a[4] , $a[5] , $a[6] , $a[7] , $a[8] , $a[9] , $a[10],
                $a[11], $a[12], $a[13], $a[14], $a[15], $a[16], $a[17], $a[18], $a[19], $a[20],
                $a[21], $a[22], $a[23], $a[24], $a[25], $a[26], $a[27], $a[28], $a[29], $a[30],
                $a[31], $a[32], $a[33], $a[34], $a[35], $a[36], $a[37], $a[38], $a[39], $a[40],
                $a[41], $a[42], $a[43], $a[44], $a[45], $a[46], $a[47], $a[48], $a[49], $a[50],
                $a[51], $a[52], $a[53], $a[54], $a[55], $a[56], $a[57], $a[58], $a[59], $a[60],
                $a[61] ] );

my @data3 = ( [ $a[1] , $a[2] , $a[3] , $a[4] , $a[5] , $a[6] , $a[7] , $a[8] , $a[9] , $a[10],
                $a[11], $a[12], $a[13], $a[14], $a[15], $a[16], $a[17], $a[18], $a[19], $a[20],
                $a[21], $a[22], $a[23], $a[24], $a[25], $a[26], $a[27], $a[28], $a[29], $a[30],
                $a[31], $a[32], $a[33], $a[34], $a[35], $a[36], $a[37], $a[38], $a[39], $a[40],
                $a[41], $a[42], $a[43], $a[44], $a[45], $a[46], $a[47], $a[48], $a[49], $a[50],
                $a[51], $a[52], $a[53], $a[54], $a[55], $a[56], $a[57], $a[58], $a[59], $a[60],
                $a[61] ] );

$ga_ok=(0,0,0);
$num = 1;
$min1=$valeur_mini_init; $max1=0;
$min2=$valeur_mini_init; $max2=0;
$min3=$valeur_mini_init; $max3=0;

while ($num < $nb_team+1) {
  if ($test==1) { print "\n\nSELECT rec,numeroteam,pos_general,score FROM equipe WHERE numeroteam = '".$tri[$num]."' AND rec BETWEEN ".$b1." AND ".$b." order by pos_general\n\n"; }
  $do = $db->prepare("SELECT rec,numeroteam,pos_general,score FROM equipe WHERE numeroteam = '".$tri[$num]."' AND rec BETWEEN ".$b1." AND ".$b." order by pos_general");
  $ErrNum = $do->execute() or die $DBI::errstr;
  $compte = 0;
  $tableau2 = 0;
  $tableau3 = 0;
  $maximum=0; $minimum=$valeur_mini_init;
  while ($ErrNum >= 0 and my @data = $do->fetchrow_array()) {
    $rec = $data[0];
    $nom[$rec] = $data[1];
    $nom[$rec] =~ s/;/ /g;
    $posg[$rec] = $data[2];
    $u[$rec] = int($data[3]);
    if ($test == 1) { print("Score[".$rec."] = [".$u[$rec]."]\n"); }
    $compte++; 
  }
  if ($test == 1) { print("compte = [".$compte."]\n"); }

  if ($test == 1) { print("SELECT nomteam FROM liste_equipe WHERE numeroteam = '".$nom[$rec]."'\n"); }
  $do = $db->prepare("SELECT nomteam FROM liste_equipe WHERE numeroteam = '".$nom[$rec]."'");
  $ErrNum = $do->execute() or die $DBI::errstr;
  while ($ErrNum >= 0 and my @data = $do->fetchrow_array()) {
    $nom = $data[0];
    if ($nom =~ /Default /) {
      $nom = "Default team";
    }
    if ($test==1) { print ("Num = ".$num."  nom = ".$nom," \n\n"); }
  }

  $tmp = 0;
  while ($tmp < $compte-1) {
    $n[$tmp] = $u[$b-$tmp]-$u[$b-$tmp-1];
    if ($n[$tmp] < 0) {
      $n[$tmp] = 0;
    }
    
    if ($num >= 1) {
      if ($n[$tmp] > $maximum) {$maximum = $n[$tmp];}
      if ($n[$tmp] < $minimum) {$minimum = $n[$tmp];}
    }
    if ($test == 1) { print($n[$tmp]." "); if ($n[$tmp] < 100000) { print(" "); } }
    $tmp++;
    if ($tmp%10 == 0 and $test == 1 ) { print("\n"); }
  }
  
  if ($nb_team-$nb_team_split > 0 and $num > $nb_team_split) { $tableau2 = 1; }
  if ($nb_team-$nb_team_split > 0 and $num > (2*$nb_team_split)) { $tableau3 = 1; $tableau2 = 0; }
  if ($tableau3 == 1) {
    $ga_ok[2] = 1;
    if ($maximum > $max3) { $max3 = $maximum; }
    if ($minimum < $min3) { $min3 = $minimum; }
    push (@tn3, encode("cp1252", decode("utf8", $nom)));
    push (@data3,[ $n[60] , $n[59] , $n[58] , $n[57] , $n[56] , $n[55] , $n[54] , $n[53] , $n[52] , $n[51], 
                   $n[50], $n[49], $n[48], $n[47], $n[46], $n[45], $n[44], $n[43], $n[42], $n[41],
                   $n[40], $n[39], $n[38], $n[37], $n[36], $n[35], $n[34], $n[33], $n[32], $n[31],
                   $n[30], $n[29], $n[28], $n[27], $n[26], $n[25], $n[24], $n[23], $n[22], $n[21],
                   $n[20], $n[19], $n[18], $n[17], $n[16], $n[15], $n[14], $n[13], $n[12], $n[11],
                   $n[10], $n[9], $n[8], $n[7], $n[6], $n[5], $n[4], $n[3], $n[2], $n[1], $n[0] ] );
  } elsif ($tableau2 == 1) {
    $ga_ok[1] = 1;
    if ($maximum > $max2) { $max2 = $maximum; }
    if ($minimum < $min2) { $min2 = $minimum; }
    push (@tn2, encode("cp1252", decode("utf8", $nom)));
    push (@data2,[ $n[60] , $n[59] , $n[58] , $n[57] , $n[56] , $n[55] , $n[54] , $n[53] , $n[52] , $n[51], 
                   $n[50], $n[49], $n[48], $n[47], $n[46], $n[45], $n[44], $n[43], $n[42], $n[41],
                   $n[40], $n[39], $n[38], $n[37], $n[36], $n[35], $n[34], $n[33], $n[32], $n[31],
                   $n[30], $n[29], $n[28], $n[27], $n[26], $n[25], $n[24], $n[23], $n[22], $n[21],
                   $n[20], $n[19], $n[18], $n[17], $n[16], $n[15], $n[14], $n[13], $n[12], $n[11],
                   $n[10], $n[9], $n[8], $n[7], $n[6], $n[5], $n[4], $n[3], $n[2], $n[1], $n[0] ] );
  } else {
    $ga_ok[0] = 1;
    if ($maximum > $max1) { $max1 = $maximum; }
    if ($minimum < $min1) { $min1 = $minimum; }
    push (@tn1, encode("cp1252", decode("utf8", $nom)));
    push (@data1,[ $n[60] , $n[59] , $n[58] , $n[57] , $n[56] , $n[55] , $n[54] , $n[53] , $n[52] , $n[51], 
                   $n[50], $n[49], $n[48], $n[47], $n[46], $n[45], $n[44], $n[43], $n[42], $n[41],
                   $n[40], $n[39], $n[38], $n[37], $n[36], $n[35], $n[34], $n[33], $n[32], $n[31],
                   $n[30], $n[29], $n[28], $n[27], $n[26], $n[25], $n[24], $n[23], $n[22], $n[21],
                   $n[20], $n[19], $n[18], $n[17], $n[16], $n[15], $n[14], $n[13], $n[12], $n[11],
                   $n[10], $n[9], $n[8], $n[7], $n[6], $n[5], $n[4], $n[3], $n[2], $n[1], $n[0] ] );
  }
  $num++;
}

if ($test==1) { 
  print("Groupe 1 = ", join (", ", @tn1), "\n");
  print("Groupe 2 = ", join (", ", @tn2), "\n");
  print("Groupe 3 = ", join (", ", @tn3), "\n");
}

$temp1=1; $pass1=$pas_saut_graph_1;
while ($min1+($temp1*$pass1) < $max1) { $temp1++; if ($temp1 > 15) { $temp1=1; $pass1+=$pas_saut_graph_1; } }
$temp2=($temp1*$pass1)+$min1;
if ($test==1) { print ("Min1    = ".$min1."  Max1    = ".$max1."\n\n".$min1." + (".$temp1." sauts de ".$pass1.") = ".$temp2."\n"); }

my $graph = GD::Graph::lines->new(970, 600);
$graph->set_title_font($arial, 14);
$graph->set_x_label_font($arial, 6);
$graph->set_y_label_font($arial, 14);
$graph->set_x_axis_font($arial, 10);
$graph->set_y_axis_font($arial, 9);
$graph->set_values_font($arial, 10);
$graph->set_legend_font(['verdana', 'arial', gdMediumBoldFont], 12);
#lyellow, yellow, dyellow, lgreen, green, dgreen, white, lgray, black, gold, lblue, blue, dblue, gray, dgray, 
#lorange, orange, lpurple, purple, dpurple, pink, dpink, lbrown, cyan, lred, red, dred, dbrown, marine,
$graph->set(
    title             => "Production en image des ".$nb_team_split." premières équipes \(".$jour." ".$mois1[$mois]." ".$annee."\).",
    x_label           => 'Fait par JGP SOLDAT. Maintenu par '.$maintenu.'.     ',
    y_label           => 'Points',
    x_label_position  => 1,
    y_label_position  => 0.5,
    x_labels_vertical => 1, 
    y_label_vertical  => 0,
    x_label_skip      => 2,
    y_label_skip      => 1,
    x_all_ticks       => 1, 
    y_all_ticks       => 0, 
    y_min_value       => $min1,
    y_max_value       => $temp2,
    y_tick_number     => $temp1,
    y_long_ticks      => 1, 
    dclrs             => ['blue', 'green', 'cyan', 'lblue', 'gold', 'red', 'orange', 'dpink', 'dblue', 'dyellow', 'lyellow', 'purple'],
    line_types        => [1, 1, 1, 1, 1, 1, 1, 1, 3, 2, 1, 1, 2, 3, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3],
    line_width        => 2,
    line_distance     => 20,
    long_ticks        => 0,
    bgclr             => 'white',
    accentclr         => 'cyan', 
    fgclr             => 'black',
    boxclr            => 'lgray',
    shadowclr         => 'lorange',
    logo              => 'images/alliance.png',
    logo_resize       => 0.5,
    lg_cols           => 4,
    logo_position     => 'LR',
    transparent       => 0,
    r_margin          => 24,
    t_margin          => 4,
    b_margin          => 4,
    l_margin          => 0,
    two_axes          => 0,
    zero_axis         => 0,
    box_axis          => 1, 
);

if ($tn1[3] eq "Ars Technica Team Egg Roll") {$tn1[4]="Ars Technica Team Egg";}
if ($tn1[7] eq "Ars Technica Team Egg Roll") {$tn1[7]="Ars Technica Team Egg";}
if ($tn1[11] eq "Ars Technica Team Egg Roll") {$tn1[11]="Ars Technica Team Egg";}

$graph->set_legend_font(GD::gdFontTiny);
$graph->set_legend(@tn1);
my $gd = $graph->plot( \@data1 );

open(OUT, ">$destWWW/graphique1.png") or die "Ne peux pas ouvrir le fichier: ".$!;
binmode(OUT);
print OUT $gd->png();
close(OUT);
print("Fermeture du fichier graphique1.png\n");

$temp1=1; $pass1=$pas_saut_graph_2;
while ($min2+($temp1*$pass1) < $max2) { $temp1++; if ($temp1 > 15) { $temp1=1; $pass1+=$pas_saut_graph_2; } }
$temp2=($temp1*$pass1)+$min2;
if ($test==1) { print ("Min2    = ".$min2."  Max2    = ".$max2."\n\n".$min2." + (".$temp1." sauts de ".$pass1.") = ".$temp2."\n"); }

$graph = GD::Graph::lines->new(970, 600);
$graph->set_title_font($arial, 14);
$graph->set_x_label_font($arial, 6);
$graph->set_y_label_font($arial, 14);
$graph->set_x_axis_font($arial, 10);
$graph->set_y_axis_font($arial, 9);
$graph->set_values_font($arial, 10);
$graph->set_legend_font(['verdana', 'arial', gdMediumBoldFont], 12);
$graph->set(
    title             => "Production en image des ".$nb_team_split." équipes suivantes \(".$jour." ".$mois1[$mois]." ".$annee."\).",
    x_label           => 'Fait par JGP SOLDAT. Maintenu par '.$maintenu.'.      ',
    y_label           => 'Points',
    x_label_position  => 1,
    y_label_position  => 0.5,
    x_labels_vertical => 1, 
    y_label_vertical  => 0,
    x_label_skip      => 2,
    y_label_skip      => 1,
    x_all_ticks       => 1, 
    y_all_ticks       => 0, 
    y_tick_number     => 6,
    y_min_value       => $min2,
    y_max_value       => $temp2,
    y_long_ticks      => $temp1,
    dclrs             => ['blue', 'green', 'cyan', 'gold', 'lblue', 'red', 'orange', 'pink', 'dblue', 'dyellow', 'lyellow', 'lgreen', 'purple', 'dbrown', 'dred', 'yellow', 'marine'],
    line_types        => [1, 1, 1, 1, 1, 1, 1, 1, 3, 2, 1, 1, 2, 3, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3],
    line_width        => 2,
    line_distance     => 20,
    long_ticks        => 0,
    bgclr             => 'white',
    accentclr         => 'cyan', 
    fgclr             => 'black',
    boxclr            => 'lgray',
    shadowclr         => 'lorange',
    lg_cols           => 4,
    logo              => 'images/alliance.png',
    logo_resize       => 0.5,
    logo_position     => 'LR',
    transparent       => 0,
    r_margin          => 24,
    t_margin          => 4,
    b_margin          => 4,
    l_margin          => 0,
    two_axes          => 0,
    zero_axis         => 0,
    box_axis          => 1, 
);
$graph->set_legend_font(GD::gdFontTiny);
$graph->set_legend(@tn2);
if ($ga_ok[1] == 0) {
  push (@data2,[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] );
}
$gd = $graph->plot( \@data2 );
open(OUT, ">$destWWW/graphique1a.png") or die "Ne peux pas ouvrir le fichier: ".$!;
binmode(OUT);
print OUT ($gd->png());
close(OUT);
print("Fermeture du fichier graphique1a.png\n");

$temp1=1; $pass1=$pas_saut_graph_3;
while ($min3+($temp1*$pass1) < $max3) { $temp1++; if ($temp1 > 15) { $temp1=1; $pass1+=$pas_saut_graph_3; } }
$temp2=($temp1*$pass1)+$min3;
if ($test==1) { print ("Min3    = ".$min3."  Max3    = ".$max3."\n\n".$min3." + (".$temp1." sauts de ".$pass1.") = ".$temp2."\n"); }

$graph = GD::Graph::lines->new(970, 600);
$graph->set_title_font($arial, 14);
$graph->set_x_label_font($arial, 6);
$graph->set_y_label_font($arial, 14);
$graph->set_x_axis_font($arial, 10);
$graph->set_y_axis_font($arial, 9);
$graph->set_values_font($arial, 10);
$graph->set_legend_font(['verdana', 'arial', gdMediumBoldFont], 12);
$graph->set(
    title             => "Production en image des ".($nb_team-(2*$nb_team_split))." équipes suivantes \(".$jour." ".$mois1[$mois]." ".$annee."\).",
    x_label           => 'Fait par JGP SOLDAT. Maintenu par '.$maintenu.'.      ',
    y_label           => 'Points',
    x_label_position  => 1,
    y_label_position  => 0.5,
    x_labels_vertical => 1, 
    y_label_vertical  => 0,
    x_label_skip      => 2,
    y_label_skip      => 1,
    x_all_ticks       => 1, 
    y_all_ticks       => 0, 
    y_tick_number     => 6,
    y_min_value       => $min3,
    y_max_value       => $temp2,
    y_long_ticks      => $temp1,
    dclrs             => ['blue', 'green', 'cyan', 'gold', 'lblue', 'red', 'orange', 'pink', 'dblue', 'dyellow', 'lyellow', 'lgreen', 'purple', 'dbrown', 'dred', 'yellow', 'marine'],
    line_types        => [1, 1, 1, 1, 1, 1, 1, 1, 3, 2, 1, 1, 2, 3, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3],
    line_width        => 2,
    line_distance     => 20,
    long_ticks        => 0,
    bgclr             => 'white',
    accentclr         => 'cyan', 
    fgclr             => 'black',
    boxclr            => 'lgray',
    shadowclr         => 'lorange',
    lg_cols           => 4,
    logo              => 'images/alliance.png',
    logo_resize       => 0.5,
    logo_position     => 'LR',
    transparent       => 0,
    r_margin          => 24,
    t_margin          => 4,
    b_margin          => 4,
    l_margin          => 0,
    two_axes          => 0,
    zero_axis         => 0,
    box_axis          => 1, 
);
$graph->set_legend_font(GD::gdFontTiny);
$graph->set_legend(@tn3);
if ($ga_ok[2] == 0) {
  push (@data3,[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] );
}
$gd = $graph->plot( \@data3 );
open(OUT, ">$destWWW/graphique1b.png") or die "Ne peux pas ouvrir le fichier: ".$!;
binmode(OUT);
print OUT ($gd->png());
close(OUT);
print("Fermeture du fichier graphique1b.png\n");

$db->disconnect();

print("Fin du traitement\n");
exit;
1;
