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

require 'jcode.pl';

($SEC, $MIN, $HOUR, $MDAY, $MON, $YEAR, $WDAY, $YDAY, $ISDST) = localtime(time);
$YEAR += 1900;
$COLOR1 = "lightgreen";		# 名前の色
$COLOR2 = "yellow";             # 順位の色
$RANKS = 50;			# 何位まで出力するか

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

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML><HEAD><TITLE>RogueLike\@lab3: Scores of jNetHack 1.0.5</TITLE>
<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="palegreen"><h2>
- Scores of jNetHack 1.0.5 -</h2>
Top $RANKS</font></center>\n 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/jnethack-1.0.5/lib/record");
$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;

    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/\">" . $NAME . "-" . $CLASS . "</a> ";
    }else{
	$WAY = "<font color=$COLOR1>" . $NAME . "-" . $CLASS . "</font> ";
    }
    $ADD = "は";
    if($DIE =~ /ascended/ || $DIE =~ /昇天した/){  # 昇天
	$ADD .= "<a href=/misc/roguelike/ascend.html>昇天</a>し";
	if($SEX =~ /F/){
	    $ADD .= "女";
	}
#	$ADD .= "神となった．";
	$ADD .= "神となった";
    }else{                   # 死
	# 場所「ーの」
	if($PLACE == 0){	    $ADD .= "運命の大迷宮の";
	}elsif($PLACE == 1){	    $ADD .= "ゲヘナの";
	}elsif($PLACE == 2){	    $ADD .= "ノームの鉱山の";
	}elsif($PLACE == 3){	    $ADD .= "クエストの";
	}elsif($PLACE == 4){	    $ADD .= "フォートローディオスの";
	}elsif($PLACE == 5){	    $ADD .= "ヴラド候の塔の";
	}elsif($PLACE == 6){	    $ADD .= "最終試練の";
	}
	# 階「ーで」
	if($LEVEL < 0){
	    if($LEVEL == -5){		$ADD .= "命";
	    }elsif($LEVEL == -4){	$ADD .= "水";
	    }elsif($LEVEL == -3){	$ADD .= "火";
	    }elsif($LEVEL == -2){	$ADD .= "風";
	    }elsif($LEVEL == -1){	$ADD .= "地";
	    }
	    $ADD .= "の精霊界にて";
	}else{
	    $ADD .= "地下" . $LEVEL . "階にて";
	    if($LEVEL != $LMAX){
		$ADD .= "[最大地下" . $LMAX . "階]";
	    }
	}
	# 死に方
	if($DIE =~ /quit/){
	    $ADD .= "抜けた";
	}else{
	    $ADD .= $DIE;
	}
    }
    $WAY .= $ADD . "．";
    $LEN = length($WAY);
    if(length($WAY) > 80 && $WAY =~ /にて/){
	if($WAY =~ /\]/){
	    @WAY2 = split(/\]/, $WAY);
	    $WAY2[0] .= "]";
	}else{
	    @WAY2 = split(/にて/, $WAY);
	    $WAY2[0] .= "にて";
	}
 	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>
to know who's who.
<hr><address>
roguelike-admin<font color="palegreen">@</font>lab3.kuis.kyoto-u.ac.jp
</address>
</BODY></HTML>
EofPage

