#!/n/gnu/bin/perl
#
# computes grades
# has to be given as parameter the list of stick.html files
# creates html file in current directory

use CGI;
$PUBLIC=".";

# for subroutine notas
@counter = (BOAS,VITO,EMPA,INV,TEMP,ERR,PTOS);

$valid_games = 0;
# le^ dos arquivos
foreach $file (@ARGV) {
    $valid_games++;
    # destrincha e bota valores no hash
    @items = notas($file);
    foreach $item (@items) {
	$name = $item->{NAME};
	$vito = $item->{VITO};
	$empa = $item->{EMPA};
	$derr = $item->{DERR};
	$desc = $item->{DESC};
	#print "$name $vito $empa $derr $desc\n";
	$stick{$file}{$name}{V} = $vito;
	$stick{$file}{$name}{E} = $empa;
	$stick{$file}{$name}{D} = $derr;
	$stick{$file}{$name}{X} = $desc;
	$stick{$file}{$name}{S} = $vito + $empa + $derr + $desc;
	$pontos{$name} = 0;
	$partidas{$name} = 0;
	$desc{$name} = 0;
    }
}

# computa pontos brutos
# vitoria vale 2, empate vale 1
# computa tb subsidios para ver funcionamento minimo

foreach $file (@ARGV) {
    foreach $name (keys %{$stick{$file}}) {
	$pontos{$name} +=
	    2*($stick{$file}{$name}{V}) +
		$stick{$file}{$name}{E};
        $partidas{$name} += $stick{$file}{$name}{S};
        $desc{$name} += $stick{$file}{$name}{X};
    }
}

# calcula max e min entre os que tem func. min.

$max = 0;
$min = 20*scalar(keys %pontos);

foreach $name (keys %pontos) {
    if (10*$desc{$name} <= $partidas{$name}) {
        $max = $pontos{$name} if $max < $pontos{$name};
        $min = $pontos{$name} if $min > $pontos{$name};
    }
}

# calcula notas

foreach $name (keys %pontos) {
    if (10*$desc{$name} <= $partidas{$name}) {
        $nota{$name} = 5 + 5.0*($pontos{$name}-$min)/($max - $min);
    } else {
        $nota{$name} = 5.0*($partidas{$name}-$desc{$name})/$partidas{$name};
    }
}

# parte de gerar html

$query = new CGI;
$title = "Notas";
print $query->start_html($title);
print $query->h1($title);

print $query->p("Número de camps válidos: $valid_games");

print $query->p("Max.pontos = $max, Min.pontos = $min");

print "\n<table>\n";
print "<tr><th>Jogador" .
    "<th>" . join("<th>", @ARGV) .
    "<th>Total" .
    "<th>Nota\n";
foreach $name (sort { $nota{$b} <=> $nota{$a} } keys %nota) {
    print "<tr><td>$name";
    foreach $file (@ARGV) {
	print "<td>";
	print 2*($stick{$file}{$name}{V}) +
	    $stick{$file}{$name}{E};
    }
    print "<td>$pontos{$name}";
    print "<td>";
    printf "%4.1f", $nota{$name};
    print "\n";
}

print "</table>\n";
print $query->end_html;

#------------------------------------------------------------
# subrotinas

sub notas {
    my $FILE = shift;
    my $grade;
    my $name;
    my @list = ();
    open FILE,$FILE or return ();
    my %hash = ();
    my $c = 0;
    while(<FILE>) {

	/width/ && /(\d+\.\d)/ && do {
	    # ignore grade
	    next;
	};

	/width/ && /\>(\d\d\d\d\d\d)\</ && do {
	    $hash{NAME} = $1;
	    my %new_hash = ( NAME => $hash{NAME},
			     VITO => $hash{VITO},
			     EMPA => $hash{EMPA},
			     DERR => $hash{BOAS} - $hash{VITO} - $hash{EMPA},
			     DESC => $hash{INV} + $hash{TEMP} + $hash{ERR}
			     );
	    push @list, \%new_hash;
	    %hash = ();
	    $c = 0;
	    next;
	};

	/width/ && /\>(\d+)\</ && do {
	    $hash{$counter[$c++]} = $1;
	    next;
	};
    }
    return @list;
}
