#!/usr/local/bin/perl
# -*- mode: Perl; -*-

print "Content-Type: text/html\n\n";

# Config #############################################

($SEC, $MIN, $HOUR, $MDAY, $MON, $YEAR, $WDAY, $YDAY, $ISDST) = localtime(time);
$YEAR += 1900;
$COLOR1 = "#98fb98";          # ランキングの色
$COLOR2 = "#ffd700";               # タイトルの色
$RANKS = 50;                    # 何位まで出力するか

$VERSION = "1.5";
($PROGRAM = $0) =~ s@.*/@@;
$DEBUG = 0;

# Read File ##########################################

$BASEDIR = "/home/home2/share/WWW/html/misc/roguelike/quiz";
open(ENAME, "$BASEDIR/ename");
$EMAILLIST = ":";
while(<ENAME>){
    #                                               全角スペース↓
    $_ =~ /^([0-9\/]+)\-([0-9:]+): quiz .{0,1} (\d+) (\d+) (\S+)　(\S*)\s+\((\S+)\)/;
    $DATE = $1;    $TIME = $2;    $QMAX = $3;    $QX = $4;
    $JNAME = $5;    $EMAIL = $6;    $MACHINE = $7;

    print "DATE:$DATE, TIME:$TIME, QMAX:$QMAX, QX:$QX\n" if ($DEBUG);
    print "JNAME:$JNAME, EMAIL:$EMAIL, MACHINE:$MACHINE\n" if ($DEBUG);

    $PARC = $QX * 100 / $QMAX;
    $KEY = $DATE . "-" . $TIME;

    print "PARC:$PARC, KEY:$KEY\n" if ($DEBUG);


    # 一人ひとつだけ表示 (E-mail address で区別)

    if($EMAIL =~ /^\s*@/ || $EMAIL =~ /@\s*$/ || $EMAIL =~ /^\s*$/){
 	next;
    }
    # E-mail がないもの 且つ 内部は登録しない
    if((!($EMAIL =~ /@.+/)) && (!($MACHINE =~ /\./))){
	next;
    }
    $EMAIL =~ /^\S+@(\S+)$/; $DOMAINNAME = $1;

    
    
    print "EMAILLIST[$EMAILLIST],EMAIL[$EMAIL],JNAME[$JNAME]\n" if ($DEBUG);
    if(!($EMAILLIST =~ /:$EMAIL:/)){
	$SCORE{$KEY} = $PARC;
	$SDATE{$KEY} = $DATE;
#	$SNAME{$KEY} = $JNAME . " (" . $EMAIL . ")";
	$SNAME{$KEY} = $JNAME . " <font color=\"gray\">@" . $DOMAINNAME . "</font>";
	$EMAILLIST .= $EMAIL . ":";
	$EMAIL2KEY{$EMAIL} = $KEY;
    }elsif($EMAILLIST =~ /:$EMAIL:/){
	print "MATCH!\n" if ($DEBUG); 
	$OLDKEY = $EMAIL2KEY{$EMAIL};
	print "OLDKEY[$OLDKEY]\n" if ($DEBUG);
	print "PARC[$PARC], SCORE{OLDKEY}[$SCORE{$OLDKEY}]\n" if ($DEBUG);
	if($PARC <= $SCORE{$OLDKEY}){ # こっちの方がいい
	    print "delete SCORE{OLDKEY}[$SCORE{$OLDKEY}]\n" if ($DEBUG);
	    delete $SCORE{$OLDKEY};
	    print "delete SDATE{OLDKEY}[$SDATE{$OLDKEY}]\n" if ($DEBUG);
	    delete $SDATE{$OLDKEY};
	    print "delete SNAME{OLDKEY}[$SNAME{$OLDKEY}]\n" if ($DEBUG);
	    delete $SNAME{$OLDKEY};
	    $EMAIL2KEY{$EMAIL} = $KEY;
	    $SCORE{$KEY} = $PARC;
	    $SDATE{$KEY} = $DATE;
#	    $SNAME{$KEY} = $JNAME . " (" . $EMAIL . ")";
	    $SNAME{$KEY} = $JNAME . " <font color=\"gray\">@" . $DOMAINNAME . "</font>";
	}
    }	    
}
close(ENAME);

# Header #############################################

print<<EofHeader;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML><HEAD><TITLE>RogueLike\@lab3: Quiz Score Ranking</TITLE>
<LINK REV=MADE HREF="mailto:roguelike-admin\@lab3.kuis.kyoto-u.ac.jp">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=EUC-JP">
</HEAD>
<BODY text = "#FFFFFF" link = "#00EEEE" vlink = "#00BBBB" bgcolor = "#000000">
$PROGRAM ver.$VERSION (by K)<br>
<center><font color="$COLOR1">
<h2>- NH Quiz: Score Ranking -</h2>
<em>Thank you for playing!</em></font></center>
EofHeader

printf "As of <font color=\"$COLOR1\">$YEAR / %d / $MDAY ( %02d:%02d )</font>", $MON+1, $HOUR, $MIN;
print " localtime<br>\n";
print "<hr><p><center>\n";

# Print Ranking #######################################

print<<EofTtag;
<table cellpadding=3 cellspacing=2>
<tr><th>Rank.<th>Date<th>Score<th>Name (Not E-MAIL address!)
EofTtag

@SORTEDKEYS = sort by_score keys(%SCORE);
sub by_score {
    $SCORE{$a} <=> $SCORE{$b};
}

$I = 1;
$PREP = 101;

foreach(@SORTEDKEYS){
    if($I > $RANKS){
	last;
    }
    $RS = 100.0 - $SCORE{$_};
    if($PREP > $RS){
	$NUM = $I;
    }else{
	$NUM = $PREN;
    }
	
    if($RS == 100){	$BGC = "yellow";
    }elsif($RS >= 80){	$BGC = "cyan";
    }elsif($RS >= 70){	$BGC = "green";
    }elsif($RS >= 50){	$BGC = "pink";
    }elsif($RS >= 30){	$BGC = "red";
    }else{  $BGC = "gray";}

#    printf "<tr bgcolor=\"$BGC\"><td>$SDATE{$_}<td><font color=\"$COLOR2\">%2.1f%%</font><td>$SNAME{$_}<br>\n", $RS;
    printf "<tr><td align=right>$NUM<td>$SDATE{$_}<td align=right><font color=\"$COLOR2\">%2.1f%%</font><td>$SNAME{$_}<br>\n", $RS;
    $PREP = $RS;
    $PREN = $NUM;
    $I++;
}

# Footer ###############################################

print<<EofFooter;
</table>

</center></p><hr>
<address>roguelike-admin<!--
--><font color="$COLOR1">@</font>lab3.kuis.kyoto-u.ac.jp</address>
</BODY></HTML>
EofFooter

# end ###################################################

exit(0);
