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

use strict;

my $delta=30;
my $threshold=0;
my $your="Abbi";
my $yours=$your."'s";

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

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

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);
my ($start_time, $end_time);
my $stupidMidnightTricks=0;
while($line=<IN>) {
    chomp($line);
    next if $line=~/^\s*$/;
    ($timestamp, $action)=split("  ", $line);
    if (!defined($start_time)) {
	$start_time=$timestamp;
	my ($date, $time)=split(" ", $timestamp);
	if ($time ge "12:00:00.000") {
	    $stupidMidnightTricks=1;
	}
    }
    if ($stupidMidnightTricks) {
	$timestamp=~s/ 00:/ 24:/;
	$timestamp=~s/ 01:/ 25:/;
	$timestamp=~s/ 02:/ 26:/;
	$timestamp=~s/ 03:/ 27:/;
	$timestamp=~s/ 04:/ 28:/;
	$timestamp=~s/ 05:/ 29:/;
    }
    @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 %peopleActivity;

# Say Abbi's activity is "___XXXX_X_XXX_X";
# $peopleActivity{"Abbi"}=["_", "_", "_", "_", "X", "X"...]
# so now, I can do a N-person-pass on each time slice and throw it out.
# oh my god, this is horrible brute forcing.  please kill me.

my $numSlices;
my @tmparray;
foreach $name (sort keys %people) {
    # print "$name";
    # print " "x(12-length($name));
    @data=@{$people{$name}};
    $awake=0;
    $spaced=0;
    @tmparray=();
    # stupid, but it works.
    $numSlices=0;
    for ($t=&getsec($start_time)+$delta; $t<&getsec($end_time); $t+=$delta) {
	$numSlices++;
#	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";
	    push @{$peopleActivity{$name}}, "X";
	    while(&getsec($data[0])<$t && $#data>0) {
#		print "\n$data[0] shifting" if $name eq "Trinn";
		shift @data;
	    }
	} else {
	    # print "_";
	    push @{$peopleActivity{$name}}, "_";
	    $spaced++;
	}
    }
    # print "\n";
#    print "$name\tawake: $awake\tspaced: $spaced\n";
}

#print "name: $name\n";
#print "ref activity: ".ref($peopleActivity{$name})."\n";
print "numSlices: $numSlices\n";

my $i;
# how many people are in this raid?
my $numPeople=scalar(keys %peopleActivity)+1;
print "numPeople: $numPeople\n";
# how many people are active in this slice?
my $activeCount;

open(OUT, ">$file-processed.txt") || die "Fuck!\n";
print OUT "start_time: $start_time\n";
print OUT "end_time: $end_time\n";
print OUT "numPeople: $numPeople\n";
print OUT "numSlices: $numSlices\n";
foreach $name (sort keys %peopleActivity) {
    print OUT "$name\t".join(",", @{$peopleActivity{$name}})."\n";
}
close(OUT);
exit;

# gawd, top-down programming at its worst.
# OK, now we're finding out all the indices we want to report.  We'll
# store them all here.
my @activeIndices;
my $numberDroppedSlices=0;
for($i=0;$i<=$numSlices;$i++) {
    $activeCount=0;
    foreach $name (keys %peopleActivity) {
	$activeCount++ if (defined($peopleActivity{$name}[$i]) &&
			   $peopleActivity{$name}[$i] eq "X");
    }
    if ($activeCount > $numPeople * $threshold) {
	$activeIndices[$i]=1;
    } else {
	$numberDroppedSlices++;
    }
}
print "thresh: ".($numPeople*$threshold)."\n";

# now that we have all the active indices, now we can do row-by-row
# printing.  *sob*  N^2 programming!  Yes!  Everyone's dream!

print "numberDroppedSlices: $numberDroppedSlices\n";

foreach $name (sort keys %peopleActivity) {
    print "$name";
    print " "x(12-length($name));
    for ($i=0;$i<=$#{$peopleActivity{$name}};$i++) {
	print $peopleActivity{$name}[$i] if defined($activeIndices[$i]);
#	print $peopleActivity{$name}[$i];
    }
    print "\n";
}

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

