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

($SEC, $MIN, $HOUR, $MDAY, $MON, $YEAR, $WDAY, $YDAY, $ISDST) = localtime(time);
$YEAR += 1900;

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

$COLOR1  = "lightgreen";        # 名前の色
$COLOR2  = "yellow";            # 順位の色
$RANKS   = 50;			# 何位まで出力するか

$TEXT    = "#FFFFFF";
$LINK    = "#00EEEE";
$VLINK   = "#00BBBB";
$BGCOLOR = "#000000";

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

# Header ##################################
print<<EofHeader;
Content-Type: text/html

<HTML><HEAD><TITLE>RogueLike\@lab3: Scores of NetHack 3.2</TITLE></HEAD>
<BODY text = "$TEXT" link = "$LINK" vlink = "$VLINK" bgcolor = "$BGCOLOR">
$PROGRAM ver.$VERSION (by K)<br>
<center><font color="palegreen"><h2>
- Scores of NetHack 3.2 -</h2>
Top $RANKS</font></center> As of <font color="palegreen">
EofHeader

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

print<<EofTableHead;
<table border=0 cellpadding=3 cellspacing=0>
<tr><th>No.<th>Points<th align=left>Name<th>Hp<th>Max
EofTableHead

# Get ################################################

open(RANK, "/usr/local/pkgs/Games/nethack-3.2.2/lib/record");
$ALL = "";
$i = 0;
while(<RANK>){
    $TEMP = $_;
#    &jcode'convert($TEMP,'euc');
    $TEMP =~ /^[0-9\.]+\s(\d+)\s(\d)\s([-]?\d+)\s(\d+)\s([-]?\d+)\s([-]?\d+)\s\d\s\d+\s\d+\s(\d+)\s([A-Z])([MF])\s(.*),(.*)$/;
    if($i >= $RANKS){		# 出力終了
 	last;
    }else{
	$i++;
    }

    $SCORE = $1;    $PLACE = $2;    $LEVEL = $3;    $LMAX = $4;
    $HP = $5;       $MAX = $6;      $UID = $7;      $CLASS = $8;
    $SEX = $9;      $NAME = $10;    $DIE = $11;

    # uid から login名の獲得
    if( $USER[$UID] ne "" ){
	$USERN = $USER[$UID];
    }else{
	open(U, "/usr/bin/ypmatch $UID passwd.byuid |");
	@PASSWD = <U>;
	close(U);
	$PASSWD[0] =~ /^([^:]+):.*/;
	$USERN = $1;
	$USER[$UID] = $USERN;
    }

    if($HP <= 0){
	$HP = "-";
    }
    if($USERN ne ""){
	$WAY = "<a href=\"/members/$USERN/home.html\">" . $NAME . "-" . $CLASS . "</a> ";
    }else{
	$WAY = "<font color=$COLOR1>" . $NAME . "-" . $CLASS . "</font> ";
    }
    $ADD = "";
    if($DIE =~ /ascended/){  # 昇天
	$ADD .= "ascended to demigod";
	if($SEX =~ /F/){
	    $ADD .= "dess";
	}
	$ADD .= "-hood";
    }else{                   # 死
	if($DIE =~ /killed/ || $DIE =~ /drowned/){	    $ADD .= "died ";
	}elsif($DIE =~ /poisoned/){	    $ADD .= "poisoned ";
	}elsif($DIE =~ /petrified/){	    $ADD .= "turned to stone ";
	}elsif($DIE =~ /choked/){	    $ADD .= "choked on ";
	    if($SEX =~ /F/){	$ADD .= "her ";
	    }else{		$ADD .= "his ";
	    }
	    $ADD .= "food ";
	}

	if($PLACE == 0){	    $ADD .= "in The Dungeons of Doom ";
	}elsif($PLACE == 1){	    $ADD .= "in Gehennom ";
	}elsif($PLACE == 2){	    $ADD .= "in The Gnomish Mines ";
	}elsif($PLACE == 3){	    $ADD .= "in The Quest ";
	}elsif($PLACE == 4){	    $ADD .= "in The Fort Lordius ";
	}elsif($PLACE == 5){	    $ADD .= "in The Vlad's Tower ";
	}elsif($PLACE == 6){	    $ADD .= "in The Astral Plane ";
	}
	# 階「ーで」
	if($LEVEL < 0){	    $ADD .= "の精霊界にて";
	    if($LEVEL == -5){		$ADD .= "命";
	    }elsif($LEVEL == -4){	$ADD .= "水";
	    }elsif($LEVEL == -3){	$ADD .= "火";
	    }elsif($LEVEL == -2){	$ADD .= "風";
	    }elsif($LEVEL == -1){	$ADD .= "地";
	    }
	}else{
	    $ADD .= "on level " . $LEVEL;
	    if($LEVEL != $LMAX){
		$ADD .= " [max " . $LMAX . "]";
	    }
	    $ADD .= ". ";
	}
	# 死に方
	if($DIE =~ /quit/){
	    $ADD .= "escaped.";
	}else{
	    $DIE =~ /^\s*([a-z])(.*)$/;
	    $ONE = $1 ;
	    $ONE =~ tr/a-z/A-Z/;
	    $DIE = $ONE . $2;
	    $ADD .= $DIE;
	}
    }
    $WAY .= $ADD . ".";
    $LEN = length($WAY);
    if(length($WAY) > 30 && $WAY =~ /level/){
	if($WAY =~ /\]/){
	    @WAY2 = split(/\]\./, $WAY);
	    $WAY2[0] .= "].";
	}else{
#	    @WAY2 = split(/([0-9]+\.)/, $WAY);
	    $WAY =~ /(.* on level \d+\.)(.*)/;
	    $WAY2[0] = $1; $WAY2[1] = $2;
#	    $WAY2[0] .= $1;
	}
 	print "<tr><td align=right><font color=$COLOR2>$i</font>";
	print "<td align=right>$SCORE\n";
 	print "<td>$WAY2[0]<td align=right>$HP<td align=right>[$MAX]\n";
 	print "<tr><td><td><td>$WAY2[1]<td><td>\n";
    }else{
	print "<tr><td align=right><font color=$COLOR2>$i</font>";
        print "<td align=right>$SCORE\n";
	print "<td>$WAY<td align=right>$HP<td align=right>[$MAX]\n";
    }
}
close(RANK);

# Table ##############################################
print<<EofPage;
</table><p></center>

<hr> See <a href="/misc/roguelike/member.html">the page of members-list</a>
<hr><address>
roguelike-admin<font color="palegreen">@</font>lab3.kuis.kyoto-u.ac.jp
</address>
</BODY></HTML>
EofPage

