#!/usr/exp/bin/perl

# nhp - nethack player high scores (version 1.0)
#
# Copyright nhk (C) 1993 by John J. Chew, III <jjchew@math.utoronto.ca>
# Modified to nhp by Boudewijn Wayers <dedos4@win.tue.nl>
# This program may be distributed freely as long as it is not modified.
#
# DESCRIPTION
#
# This program lists high scores for players, sorted by the number of
# games they have played, or by the total value of their final scores.  
#
# BUGS
#
# - nhk has been tested only on our local files and may not correctly
#   format unusual killers.  Please report bugs to the address above.
# - nhk stands, for historical reasons, for NetHack Killers.  It does
#   not stand for Nihon Housou Kyoku.
#
# The output looks like:
#
# 17 2660884  156522 javier
# 35  363712   10391 severi
# 11  348840   31712 Ace
# 18  210759   11708 Kroisos
# 11  162996   14817 Efembe
#  8   20603    2575 bloo

# configuration variables

$CV_DEFAULT_DIRECTORY = '/home/svin04d/dedos4/bin/nethackdir';
$CV_LOGFILE_NAME = 'logfile';
$CV_RECORD_NAME = 'record';

sub usage {
  warn "Usage: $0 [ -f file | -l | -r ] [ -n | -s ]\n\n";
  warn "  -f  use indicated file\n";
  warn "  -l  use logfile\n";
  warn "  -r  use record (high score file) [default]\n";
  warn "  -n  sort by number of games played\n";
  warn "  -s  sort by aggregate score [default]\n";
  exit 1;
  }

require 'getopts.pl';
&Getopts('f:lnrs') || &usage;
(defined $opt_f) && $opt_l && $opt_r && &usage;
$opt_n && $opt_s && &usage;
$opt_n || $opt_s || ($opt_s = 1);
$FILE = (defined $opt_f) ? $opt_f : 
  $CV_DEFAULT_DIRECTORY . '/' . ($opt_l ? $CV_LOGFILE_NAME : $CV_RECORD_NAME);

(defined $opt_f) || $opt_l || ($opt_r = 1);

open(FILE, "<$FILE") || die "$0: cannot open $FILE";
@file = <FILE>;
close(FILE);

$killlen = 1;
$scorelen = 1;
for $_ (@file) {
  chop;
  # date uid deathdnum deathlev maxlvl hp maxhp points plchar sex name death
  split(/ /, $_, 10); $_ = $_[9]; 
  s/, the shopkeeper//;
  s/,.*//;
  $count{$_}++;
  $killlen = length($count{$_}) if length($count{$_}) > $killlen;
  $score{$_} += $_[7];
  $scorelen = length($score{$_}) if length($score{$_}) > $scorelen;
  }

sub mysort { 
  ($A = $a) =~ tr/A-Z/a-z/; ($B = $b) =~ tr/A-Z/a-z/;
  (($opt_n) ? $count{$b} <=> $count{$a} : $score{$b} <=> $score{$a}) 
    || $A cmp $B;
  }

for $key (sort mysort keys %count)
  {
  printf "%${killlen}d %${scorelen}d %${scorelen}d %s\n", $count{$key}, $score{$key}, $score{$key}/$count{$key}, $key;
  }

exit 0;
__END__

