#!/usr/bin/perl

#Figure decay times for items that were picked from corpse and decayed
#usage examples: 
# -death.pl q;death.pl 'item'
# -death.pl 50 p
# -death.pl Rando q;death.pl 50 Rando p

$MYNAME=$0;
$debug=1;

$maxplaytime=0;
$minplaytime=9999999;

$name=Gilran;
$item="";

while(defined($ARGV[0])) {
    if(defined($ARGV[1])) {
	$name=shift;next;
    }
    if($ARGV[0] eq "q") {
	qx{ls -lrt | grep $name |cut -c56-|tr ' ' '\012' >.nil};
	qx{grep -E "You have been playing for|You get.*from the corpse|decays in your hands" `cat .nil` |grep -v " coins " > .decay.$name};
	exit(0);
    } else {
	$item=shift;
    }
}
if("$item" eq "") {
    print STDERR "Use: '$MYNAME q' to create database, '$MYNAME \"item name\"' to analyze\n";
    exit(0);
}
qx(cat ????/.decay.$name .decay.$name >.nil);

$playtime=0;$prevplaytime=0;
$lasteventtime=0;$lastgettime=0;$lastdecaytime=0;
open(FILE,"<.nil") || do_die("Couldn't open the database.");
while(<FILE>) {
    if(/says|shouts|tells/) {next;}
    if(/playing for (\d+) day\(s\), (\d+) hour\(s\) and (\d+) minute\(s\)/) {
	$match++;
	$prevplaytime=$playtime;
	$playtime=$1*24*60+$2*60+$3;	
    }
    if(/ ($item) /) {
	/.*\:([A-Z].*\.)/;$event=$1;
	$match++;
	$type="Other";
	if($event=~ / get /) {$type="Get"}
	if($event=~ / decay/) {$type="Decay"}
	print "$type:\t$event\n";
	print "\tTicks $prevplaytime-$playtime, from last event: ".($playtime-$lasteventtime);
	if("$type" eq "Get") {print " from last Decay ".($playtime-$lastdecaytime)."\n";}
	elsif("$type" eq "Decay") {print " from last Get ".($playtime-$lastgettime)."\n";}
	else {print "\n";}
				   

	$lasteventtime=$playtime;
	if("$type" eq "Get") {$lastgettime=$playtime;}
	if("$type" eq "Decay") {$lastdecaytime=$playtime;}
    }
}
exit;
show();


exit;

sub handle {
    if($exp==$undef) {$exp=0;print "Undef exp for $mob\n";}
    if("$mob" eq "") {$mob="unknown";print "Undef mob for $exp\n";}
    $total+=$exp;
    $count++;
    if(!defined($mobs{'count'}{$mob})) {$uniq++;}

    $mobs{'exp'}{$mob}+=$exp; $mobs{'count'}{$mob}++;
    $rate{'exp'}{$playtime}+=$exp; $rate{'count'}{$playtime}+=1;
    if($playtime>$maxplaytime) {$maxplaytime=$playtime;}
    if($playtime<$minplaytime) {$minplaytime=$playtime;}

    if(!defined($high) || abs($exp)>abs($high)) {$high=$exp;$highmob=$mob;}
    if(!defined($mobs{'high'}{$mob}) || abs($exp)>abs($mobs{'high'}{$mob})) {
	$mobs{'high'}{$mob}=$exp; }

    $mob="";$exp=$undef;
}

sub show {
    print "Total exp $total from $count kills of $uniq different mobs.\n";
    print "Highest was $high exp from $highmob.\n";
    print "Average exp per kill is ".int($total/$count).".\n";
    print "You've received $other exp from $othercount quests\n";
    
    
    
    print "Most killed mobs:\n";
    $loop=1;
    foreach $key (sort { $mobs{'count'}{$b} <=> $mobs{'count'}{$a} } keys %{$mobs{'count'}}) {
	printf("%6d %s (total %d, max %d)\n", 
	       $mobs{'count'}{$key}, $key,$mobs{'exp'}{$key},$mobs{'high'}{$key});
	if($loop++>=$amount) {last;}
    }
    
    print "Most expfull mobs:\n";
    $loop=1;
    foreach $key (sort { $mobs{'exp'}{$b} <=> $mobs{'exp'}{$a} } keys %{$mobs{'exp'}}) {
	printf("%6d %s (total %d, max %d)\n", 
	       $mobs{'count'}{$key}, $key,$mobs{'exp'}{$key},$mobs{'high'}{$key});
	if($loop++>=$amount) {last;}
    }
    
    print "Highest exp mobs:\n";
    $loop=1;
    foreach $key (sort { $mobs{'high'}{$b} <=> $mobs{'high'}{$a} } keys %{$mobs{'high'}}) {
	printf("%6d %s (total %d, max %d)\n", 
	       $mobs{'count'}{$key}, $key,$mobs{'exp'}{$key},$mobs{'high'}{$key});
	if($loop++>=$amount) {last;}
    }

    print "Most expfull hours:\n";
    $loop=1;
    foreach $key (sort { $rate{'exp'}{$b} <=> $rate{'exp'}{$a} } keys %{$rate{'exp'}}) {
	printf("%6sth hour: %3d kills for %8d exp\n",
	       $key,$rate{'count'}{$key},$rate{'exp'}{$key});
	if($loop++>=$amount) {last;}
    }


    print "Most killfull hours:\n";
    $loop=1;
    foreach $key (sort { $rate{'count'}{$b} <=> $rate{'count'}{$a} } keys %{$rate{'count'}}) {
	printf("%6sth hour: %3d kills for %8d exp\n",
	       $key,$rate{'count'}{$key},$rate{'exp'}{$key});
	if($loop++>=$amount) {last;}
    }

    print "Exp-history by hours:\n";    
    $loop=$maxplaytime-$amount;
    if($loop<$minplaytime) {$loop=$minplaytime;}

    $sum=0;$lastsum=0;$sumcount=0;$divider=0;$timespan=10;
    foreach $key ($loop .. $maxplaytime) {
	$sum+=$rate{'exp'}{$key};
	$sumcount++;if($divider<$timespan){$divider++;}
	$lastsum+=$rate{'exp'}{$key};
	if($key>($loop+$timespan)) {$lastsum-=$rate{'exp'}{$key-$timespan};}

	printf("%6sth hour: %3d kills for %8d exp, avg %7d, last10avg %7d.\n",
	       $key,$rate{'count'}{$key},$rate{'exp'}{$key},
	       int($sum/($sumcount)),int($lastsum/$divider));
    }


}




# foreach $key (sort { $hash{$b} <=> $hash{$a} } keys %hash) {
#     printf "%4d %s\n", $hash{$key}, $key;
# }
    


#grep "death cry" files* >.nil_rip
#nil_death.pl <.nil_rip
#while(<>) {
#    if(/(.*?):The death cry of (\w+) echoes/) {
#	print "-$2-\t$1\n";
#    }
#}
