#!/usr/local/bin/perl

print <<EOF
Content-type: text/html

<html>
<head>
<title>AI Olympics 2001</title>

</head>

<body bgcolor=#ffffff>

<font face="Arial,Helvetica,Geneva">

<center><img src="images/title.GIF"> <br>

<img src="images/satin_flags.GIF" usemap="#satin_flags" border="0">

<pre>
</pre>

EOF
    ;

# By the way, if you are thinking of using this file as a guide for
# any future files, don't.  use last year's instead.  

# find the number of events that have been played and the score
# for each team.  Yeow this is harder than it looks!
@events= <events/*>;

%scores = ("china", 0, "france", 0, "italy", 0, "japan", 0);
$totalscore = 0;

foreach $event (@events) {
    ($dir= $event)=~ s!^.*/([^/]*)!$1!;
    if (open(IN, "$event/info")) {
	while(<IN>) {
	    if (/^([^:]*):(.*)/) {
		($type, $value) = ($1, $2);
		if ($type eq "SCORE") {
		    $value=~ s/^\s+//;  # strip the whitespace from the beginning
		    $value=~ s/\s+$//;  # and the end of the string
		    @places= split(/\s*\/\s*/, $value);
		    @points = (5, 3, 2, 1);
		    $i = 0;
		    foreach $team (@places) {
			if ($team=~ /\s+/) {
			    @tied= split(/\s+/, $team);
			    if ( $#tied == 2 ) {
				$tiedpoints = ($points[$i] + $points[$i+1]) / 2;
				$i = $i + 2;
			    }
			    else {
				$tiedpoints = ($points[$i] + $points[$i+1] + $points[i+2]) /3;
				$i = $i + 3;
			    }
			    foreach $tiedteam (@tied) {
				$scores{$tiedteam} = $scores{$tiedteam} + $tiedpoints;
			    }
			    $i = $i + 2;
			}
			else {
			    $scores{$team} = $scores{$team} + $points[$i];
			    $i = $i + 1;
			}
		    }
		    $totalscore = $totalscore + 11;

		}
	    }
	}
    }
}

if ($totalscore != 0) {
    foreach $team (keys %scores) {
	$scores{$team} = ($scores{$team} * 100) / $totalscore;
	$scores{$team}=~ s/^([^\.]*)\..*/$1/;
    }
}

$totalbattles = $totalscore / 11;


print <<EOF
<table width=600 border>
<tr> 
  <td>Total Battles </td>
  <td colspan=4>Percent Won</td>
</tr>
<tr>
EOF
    ;
print( "<td> $totalbattles </td>");
print("<td bgcolor=\"Yellow\"><img src=\"images/japan.gif\" width=50> $scores{japan}%</td>");
print( "<td bgcolor=#AAAAAA><img src=\"images/china_white.gif\" width=50> $scores{china}%</td>");
print( "<td bgcolor=#ff0000><img src=\"images/france.gif\" width=50> $scores{france}%</td>");
print( "<td bgcolor=\"blue\"><img src=\"images/italy.gif\" width=50> $scores{italy}%</td>");

print <<EOF
</tr>

</table>

<table width=600>
<tr>
<td> <a href="score.html"><font face="Arial,Geneva,Helvetica">A note on scoring</font></a> </td>
</tr>
</table>

 </center>

<map name="satin_flags">
<area shape="rect" coords="210,194,342,233" href="assign.cgi" title="">
<area shape="rect" coords="153,247,401,284" href="schedule.cgi" title="">
<area shape="rect" coords="219,299,331,336" href="about.html" title="">
<area shape="poly" coords="19,158,123,264,275,114,170,7" href="team.cgi?name=japan" title="">
<area shape="poly" coords="278,113,429,263,529,163,378,13" href="team.cgi?name=china" title="">
<area shape="poly" coords="24,367,174,517,273,417,121,264" href="team.cgi?name=france" title="">
<area shape="poly" coords="279,420,430,267,529,365,377,518,278,418" href="team.cgi?name=italy" title="">
<area shape="default" nohref>
</map>
<p><hr>
<font size=-1>Olympic Chairperson/MC: <a href="mailto:calvarad@ai.mit.edu">Christine Alvarado</a></font>

</body>
</html>
EOF
;
