#!/usr/bin/perl
use Time::Local;
require "../cgi-lib.pl";
ReadParse(*params);
print "Content-Type: text/html\n\n";
$file = "/home/dave/public_html/projectx/fbtb_trivia_data2.txt";
open(FILE,$file) or die "Ack! Couldnt open data file!\n";

print "<div align=center><b><font size=+1>FBTB Lego Trivia Questions and Aggregate Scores</font></b></div><br>\n";
print "See the whole <a href=\"http://www.fbtbforums.net/viewtopic.php?t=13485\">Lego Trivia Thread</a>";
print " on <a href=\"http://www.fbtb.net/\">FBTB</a>,";
print "and the <a href=\"http://www.fbtbforums.net/viewtopic.php?t=15013\">official score sheet</a>. ";
print "Some questions and answers have been modified from their original wording for clarity or context, and comments have been provided by David Eaton (aka DaveE, aka davee123)\n";
print "<br><br>\n";

if($params{user}) {
    print "<b>Questions asked by $params{user}<b> (<a href=\"$ENV{SCRIPT_NAME}\">View All</a>)\n";
    print "<table border=1>\n";
    print "<tr bgcolor=#CCCCEE>\n";
    print "<th>#</th>\n";
    print "<th>Answered By</th>\n";
    print "<th>Question</th>\n";
    print "<th>Answer</th>\n";
    print "</tr>\n";
    while (<FILE>) {
	next if(/^\-/);
	next unless(/[A-Za-z]/);
	chomp;
	my($text) = '';
	#my($qid,$qnum,$asktime,$askdate,$asker,$ansdate,$anstime,$winner,$question,$answer,$struck,$comment) = split /\t/,$_;
	my($qid,$qnum,$asktime,$askdate,$askpostid,$asker,$anstime,$ansdate,$anspostid,$winner,$question,$answer,$struck,$comment) = split /\t/,$_;
	my($asktimestamp,$anstimestamp);
	if(($askdate.$asktime) =~ /^(\d\d\d\d)(\d\d)(\d\d)(\d+):(\d+)$/) {
	    my($y,$m,$d,$hr,$min) = ($1,$2,$3,$4,$5);
	    $asktimestamp = timelocal(30,$min,$hr,$d,$m-1,$y-1900);
	}
	if(($ansdate.$anstime) =~ /^(\d\d\d\d)(\d\d)(\d\d)(\d+):(\d+)$/) {
	    my($y,$m,$d,$hr,$min) = ($1,$2,$3,$4,$5);
	    $anstimestamp = timelocal(30,$min,$hr,$d,$m-1,$y-1900);
	}
	my($diff) = $anstimestamp - $asktimestamp;
	my($diffdays) = int($diff / (24*60*60));
	$diff -= ($diffdays * 24*60*60);
	my($diffhrs) = int($diff / (60*60));
	$diff -= ($diffhrs * 60*60);
	my($diffmin) = $diff / 60;
	$diff -= ($diffmin * 60);
	my($diffsec) = $diff;
	#$qnum++;
	$invalid = 0;
	if($struck =~ /struck/i) {
	    $invalid = 1;
	    $winner = "Invalid/Stricken";
	} elsif($winner =~ /^\s*\-\s*$/) {
	    $score{$asker}{stump}++;
	    $winner = "Nobody (stumper!)";
	    $score{$asker}{points}++;
	} else {
	    $score{$winner}{answered}++;
	    $score{$winner}{points}++;
	}
	$score{$asker}{asked}++ unless($invalid);
	if($asker eq $params{user}) { $bgcolor = ($aski++ % 2) ? "#EEEEFF" : "#DDDDEE"; }
	elsif($winner eq $params{user}) { $bgcolor = ($ansi++ % 2) ? "#EEEEFF" : "#DDDDEE"; }
	if($invalid) { $bgcolor = "#FFCCCC"; }
	$text = "<tr bgcolor=$bgcolor><td>$qnum.</td><td><a href=\"$ENV{SCRIPT_NAME}?user=$asker\">$asker</a></td><td><a href=\"$ENV{SCRIPT_NAME}?user=$winner\">$winner</a></td><td>$question</td><td>$answer</td></tr>";
	if($comment && $comment =~ /[A-Za-z]/) { $text .= "<tr bgcolor=$bgcolor><td colspan=5><b>$comment</b></td></tr>\n"; }
	#$qnum-- if($invalid);
	$text =~ s/<td><a href=\"[^\"]*\">\Q$params{user}\E<\/a><\/td>//g;
	if($asker eq $params{user}) {
	    print $text;
	} elsif($winner eq $params{user}) {
	    $winrows .= $text;
	}
    }
    print "</table>\n";
    print "<br>\n";
    print "<b>Questions answered by $params{user}<b> (<a href=\"$ENV{SCRIPT_NAME}\">View All</a>)\n";
    print "<table border=1>\n";
    print "<tr bgcolor=#CCCCEE>\n";
    print "<th>#</th>\n";
    print "<th>Asked By</th>\n";
    print "<th>Question</th>\n";
    print "<th>Answer</th>\n";
    print "</tr>\n";
    print $winrows;
    print "</table>\n";    
    exit(0);
}

print "<table border=1>\n";
print "<tr bgcolor=#CCCCEE>\n";
print "<th>#</th>\n";
print "<th>Asker</th>\n";
print "<th>Answered By</th>\n";
print "<th>Question</th>\n";
print "<th>Answer</th>\n";
print "</tr>\n";
while (<FILE>) {
    next if(/^\-/);
    next unless(/[A-Za-z]/);
    chomp;
    #my($qid,$qnum,$asktime,$askdate,$asker,$ansdate,$anstime,$winner,$question,$answer,$struck,$comment) = split /\t/,$_;
    #my($asker,$winner,$question,$answer,$comment) = split /\t/,$_;
    my($qid,$qnum,$asktime,$askdate,$askpostid,$asker,$anstime,$ansdate,$anspostid,$winner,$question,$answer,$struck,$comment) = split /\t/,$_;
    my($asktimestamp,$anstimestamp);
    if(($askdate.$asktime) =~ /^(\d\d\d\d)(\d\d)(\d\d)(\d+)\:(\d+)$/) {
	my($y,$m,$d,$hr,$min) = ($1,$2,$3,$4,$5);
	$asktimestamp = timelocal(30,$min,$hr,$d,$m-1,$y-1900);
    }
    if(($ansdate.$anstime) =~ /^(\d\d\d\d)(\d\d)(\d\d)(\d+):(\d+)$/) {
	my($y,$m,$d,$hr,$min) = ($1,$2,$3,$4,$5);
	$anstimestamp = timelocal(30,$min,$hr,$d,$m-1,$y-1900);
    }
    my($diff) = $anstimestamp - $asktimestamp;
    my($diffdays) = int($diff / (24*60*60));
    $diff -= ($diffdays * 24*60*60);
    my($diffhrs) = int($diff / (60*60));
    $diff -= ($diffhrs * 60*60);
    my($diffmin) = $diff / 60;
    $diff -= ($diffmin * 60);
    my($diffsec) = $diff;
    #$qnum++;
    $invalid = 0;
    if($struck =~ /struck/i) {
	$invalid = 1;
	$winner = "Invalid/Stricken";
    } elsif($winner =~ /^\s*\-\s*$/) {
	$score{$asker}{stump}++;
	$winner = "Nobody (stumper!)";
	$score{$asker}{points}++;
    } else {
	$score{$winner}{answered}++;
	$score{$winner}{points}++;
    }
    $score{$asker}{asked}++ unless($invalid);
    if($comment) { $score{$asker}{comments}++; }
    $bgcolor = ($i++ % 2) ? "#EEEEFF" : "#DDDDEE";
    if($invalid) { $bgcolor = "#FFCCCC"; }
    print "<tr bgcolor=$bgcolor><td>$qnum.</td><td><a href=\"$ENV{SCRIPT_NAME}?user=$asker\">$asker</a></td><td><a href=\"$ENV{SCRIPT_NAME}?user=$winner\">$winner</a>";
#    if(!$invalid && $winner =~ /\w/ && !($winner =~ /nobody/i)) {
#	print "(";
#	print "$diffdays days " if($diffdays);
#	print sprintf("%02d:%02d",$diffhrs,$diffmin);
#	print ")";
#    }
    print "</td><td><a href=\"http://www.fbtbforums.net/viewtopic.php?t=13485&start=$askpostid\">$question</a></td>";
    if($anspostid =~ /\d/) {
	print "<td><a href=\"http://www.fbtbforums.net/viewtopic.php?t=13485&start=$anspostid\">$answer</a></td></tr>";
    } else {
	print "<td>$answer</a></td></tr>\n";
    }
    if($comment && $comment =~ /[A-Za-z]/) { print "<tr bgcolor=$bgcolor><td colspan=5><b>$comment</b></td></tr>\n"; }
    $qnum-- if($invalid);
}
print "</table>\n";
print "<table border=1>\n";
print "<tr bgcolor=#CCCCFF>\n";
print "<th>Player</th>\n";
print "<th># Answered</th>\n";
print "<th># Stumpers</th>\n";
print "<th># Asked</th>\n";
print "</tr>\n";
foreach my $player (sort {$score{$b}{points} <=> $score{$a}{points} || $score{$b}{answered} <=> $score{$a}{answered} || $score{$b}{asked} <=> $score{$a}{asked}} (keys %score)) {
    $bgcolor = ($i++ % 2) ? "#EEEEFF" : "#DDDDEE";
    print "<tr bgcolor=$bgcolor>\n";
    print "<td><a href=\"$ENV{SCRIPT_NAME}?user=$player\">$player</td>\n";
    print "<td>".($score{$player}{answered}||0)."</td>\n";
    print "<td>".($score{$player}{stump}||0)."</td>\n";
    print "<td>".($score{$player}{asked}||0)."</td>\n";
    print "<td>".($score{$player}{comments}||0)."</td>\n" if($params{grade} && $score{$player}{asked});
    print "<td>".($score{$player}{comments}/$score{$player}{asked})."</td>\n" if($params{grade} && $score{$player}{asked});
    print "</tr>\n";
}
