#!/usr/bin/perl
$|=1;
use DBI;

sub ShowStatistics(){
	&Get("games");
	&Get("archives");

	$players=keys(%remaining);
	$matches=keys(%m);
	print "<font size=2 color=blue>$players players in $matches matches.</font><br>";

	$row=0;
	print "<table border=1 cellspacing=0 cellpadding=0>\n";
	foreach $match (sort(keys(%m))){
		$points=0;
		$cols=0;
		$leader="";
		$leaderpoints=0;
		foreach $user ((keys(%{$m{$match}}))){
			if($m{$match}->{$user} eq ""){$m{$match}->{$user}=0;}
			if($m{$match}->{$user} > $leaderpoints){
				$leader=$user;
				$leaderpoints=$m{$match}->{$user};
			}
			$points+=$m{$match}->{$user};
		}

		$determined=1;
		$tiedetermined=1;
		foreach $user ((keys(%{$m{$match}}))){
			if(($m{$match}->{$user} + $remaining{$user} > $leaderpoints) && ($user ne $leader)){
				$determined=0;
			}
			if(($m{$match}->{$user} + $remaining{$user} >= $leaderpoints) && ($user ne $leader)){
				$tiedetermined=0;
			}
		}

		print "<tr";
		if($points==$gamecount{$match}){
			print " class=done";
		} elsif($tiedetermined==1){
			print " class=determined";
		} elsif(($row%2)==1){
			print " class=darkbg";
		}
		$row++;
		print "><td><a href='../matchstats.cgi?id=$match'><font class=matchid>$match</font></a></td>";

		@userlist=sort {$m{$match}->{$b} <=> $m{$match}->{$a}} keys(%{$m{$match}});
		
		#foreach $user (sort(keys(%{$m{$match}}))){
		foreach $user (@userlist){
			print "<td><font ";
			$class="";
			if($m{$match}->{$user} eq $leaderpoints){
				if($determined==1){
					$class="winner";
				} else {
					$class="leader";
				}
			} elsif($m{$match}->{$user} + $remaining{$user} < $leaderpoints){
				$class="looser";
			} else {
				$class="possible";
			}
			print " class=$class";

			if(HasWithdrawn($user)==1){
				$class="withdrawn"
			}
			
			print "><a href='../profile.php?username=$user'><font class='$class'>$user</font></a> ".$m{$match}->{$user}.":$completed{$user}:$remaining{$user}</font></td>";
			$cols++;
		}

		while($cols<7){print "<td>&nbsp;</td>"; $cols++;}

		print "<td><font class=finished>$points of $gamecount{$match} finished</font></td>";
		print "</tr>\n";
	}
	print "</table>";
}

sub HasWithdrawn{
	my $user=shift;
	my @withdrawn=('battousai');

	foreach $u (@withdrawn){
		if($user eq $u){
			return 1;
		}
	}
	return 0;
}

sub Get{
   my $table=shift;

	foreach $match (@matches){
      $sql="select id,white,black,winner,gameover from $table where matchid='$match';";
		$q=$dbh->prepare($sql);
		if(!$q->execute()){print "Error executing query: ".$DBI::errstr; exit;}
		while($row=$q->fetchrow_hashref()){
			$gamecount{$match}++;

			if($m{$match}->{$row->{black}} eq ""){$m{$match}->{$row->{black}}=0;}
			if($m{$match}->{$row->{white}} eq ""){$m{$match}->{$row->{white}}=0;}
			if($completed{$row->{white}} eq ""){$completed{$row->{white}}=0;}
			if($completed{$row->{black}} eq ""){$completed{$row->{black}}=0;}
			if($remaining{$row->{white}} eq ""){$remaining{$row->{white}}=0;}
			if($remaining{$row->{black}} eq ""){$remaining{$row->{black}}=0;}

			if($row->{gameover} ne "" && $row->{gameover} ne "aborted"){
			   $completed{$row->{white}}++;
			   $completed{$row->{black}}++;
				if($row->{winner} eq "white"){
#print $row->{white}." $row->{id}<br>\n";
					$m{$match}->{$row->{white}}++;
					$m{$match}->{$row->{black}}=$m{$match}->{$row->{black}};
				} elsif ($row->{winner} eq "black"){
#print $row->{black}." $row->{id}<br>\n";
					$m{$match}->{$row->{black}}++;
					$m{$match}->{$row->{white}}=$m{$match}->{$row->{white}};
				} else {
					$m{$match}->{$row->{black}}+=.5;
					$m{$match}->{$row->{white}}+=.5;
				}
			} else {
			   $remaining{$row->{white}}++;
			   $remaining{$row->{black}}++;
			}
		}
	}
}

                    
