#!/mit/perl5/bin/perl -w

my $delta=4;
#my $threshold=0.7;
my $your="Thanaomira";
my $yours=$your."'s";

use strict;

my @activity=(" begins to cast",
	      " begins to perform",
	      " begin to cast",
	      " begin to perform",
	      "\\'s [\\w ]+ hits",
	      "\\'s [\\w ]+ crits",
	      "\\'s [\\w ]+ was resisted by",
	      "\\'s [\\w ]+ heals",
	      " hits",
	      " misses",
	      " crits");
my %people;
my $file=$ARGV[0];

open(IN, $file) || die "Can't open '$file'!\n";
my ($line, $timestamp, $action, @name, $name, $regex, $activity, $date, $time);
my ($start_time, $end_time);
while($line=<IN>) {
    chomp($line);
    ($timestamp, $action)=split("  ", $line);
    $start_time=$timestamp if !defined($start_time);
    @name=split(" ", $action);
    $name=$name[0];
    $name=~s/\'s$//;
    if ($name eq "Your") {
	$name=$your;
	$action=~s/Your/$yours/g;
    }
#    print $line if $name eq "Trinn";
    foreach $activity (@activity) {
	$regex=$name.$activity;
	$action=~/^($regex)/;
	if (defined($1)) {
#	    print "\t$timestamp" if $name eq "Trinn";
	    push @{$people{$name}}, $timestamp;
	    last;
	}
    }
#    print "\n" if $name eq "Trinn";
}
close(IN);
$end_time=$timestamp;

my (@data, $t, $awake, $spaced);
print "start: $start_time\tend: $end_time\tdelta: $delta\n";
#my %numbersToPeopleMap;
#my $peopleNumber=0;
#my @timeSlices; # values are a CSV of peopleNumbers that are active.
foreach $name (sort keys %people) {
    print "$name";
    print " "x(12-length($name));
#    $numbersToPeopleMap{$peopleNumber}=$name;
    @data=@{$people{$name}};
#    print "$#data ";
    $awake=0;
    $spaced=0;
    for ($t=&getsec($start_time)+$delta; $t<&getsec($end_time); $t+=$delta) {
#	print "\n$t ".&getsec($data[0])." $data[0] " if $name eq "Trinn";
	if((($t-$delta)<=&getsec($data[0])) && &getsec($data[0])<$t) {
	    $awake++;
	    print "X";
#	    $timeSlices[$t].="$peopleNumber,";
	    while(&getsec($data[0])<$t && $#data>0) {
#		print "\n$data[0] shifting" if $name eq "Trinn";
		shift @data;
	    }
	} else {
#	    $activity{$name}.="_";
	    print "_";
	    $spaced++;
	}
    }
#    $peopleNumber++;
    print "\n";
#    print "$name\tawake: $awake\tspaced: $spaced\n";
}

#foreach (sort keys %numbersToPeopleMap) {
#}

################################################################

sub getsec {
    my ($date, $time)=split(" ", $_[0]);
    my ($h,$m,$s)=split(":", $time);
    return $h*3600+$m*60+$s;
}
