#!/usr/bin/perl
############################################################################
# LogStats                                                                 #
#                                                                          #
# Written by Kyle M Forbes (Gepht@Hemlock)                                 #
#                                                                          #
# All code contained in this program is distributed without obligation or  #
# guarentee of performance.                                                #
#                                                                          #
# The LogStats program is a simple tool that can be used to analyze game   #
# connections.  It analyzes the PennMUSH connect.log to determine the      #
# total number of raw connections (connections to all players) and the     #
# number of unique connections (each player counts only once).  Through    #
# this approach, the program can provide information on connections per    #
# day and the total number of players playing the game.                    #
#                                                                          #
# The following information results in the following statistics:           #
#                                                                          #
#       > Connect Gepht                                                    #
#       > QUIT                                                             #
#       > Connect Gepht                                                    #
#       > QUIT                                                             #
#                                                                          #
#       Raw Connections    = 2                                             #
#       Unique Connections = 1                                             #
#                                                                          #
# LogStats breaks down the connection statistics by day and provides a     #
# cumulative summary at the end.  For each 24 hour period, LogStats will   #
# indicate which hour received the largest number of raw connections by    #
# placing a '<' next to the time.  Hourly statistics are given as          #
# percentages of total raw connections for that day.                       #
#                                                                          #
# I hope you find this program beneficial to the administration of your    #
# game!  Please send all comments and enhancements to Gepht by addressing  #
# your email to hemlock@hemlock.unl.edu                                    #
#                                                                          #
############################################################################

# Set this to point to your "connect.log" file.
$GAMELOG = "/home/hemlock/hemlock/game/log/connect.log";


################## Do not change anything below this line ###################
$connections = 0;
$today = 0;
$unique = 0;

@DAYLIST = ("Sun", "Mon","Tue","Wed","Thu","Fri","Sat");
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$TODAY = $mday;

# Let's do it.
&parse_it($GAMELOG);


sub parse_it {
  local($log) = @_;
  unless(open(FP, $log)) {
    print "Couldn't open log file \"$log\".\n";
  } else {
    $curlist[0] = "#-1";
    $curuniqueptr = 0;
    $hptr = 0;
    while ($hptr < 24) {
      @hourlist[$hptr] = 0;
      $hptr += 1;
    }
    $iter = 0;
    print "+- LogStats per Day -+\n";
    while (<FP>) {
      $last = $_;
      $iter += 1;
      if ($iter == 1) {
        @dates = split(/ /, $_);
        $start = $dates[0];
        ($month, $day) = split(/\//, $start);
        $stime = $dates[1]; 
      }
      if ($_ =~ /Connected to/i ) {
        @words = split(/\(/,$_);
        @stuff = split(/ /,$words[0]);
        $ttime = $stuff[1];
        @hour  = split(/:/,$stuff[1]);
        @dayl   = split(/\//,$stuff[0]);
        if ($hour[0] == "00" && $dayl[1] != $day) {
          $hptr = 0;
          $tophour = 0;
          while($hourlist[$hptr] ne '') {
            if ($hourlist[$hptr] > $hourlist[$tophour]) {
               $tophour = $hptr;
            }
            $hptr += 1;
          }
          $iday = $wday - ($mday - $day);
          if ($iday < 0) {
            $iday += 7;
          }
          print "Date: $DAYLIST[$iday], $month/$day\n";
          $day = $dayl[1];
          $hptr = 0;
          print "    12 - 01 - 02 - 03 - 04 - 05 - 06 - 07 - 08 - 09 - 10 - 11\n";
          print "AM: ";
          $hptr = 0;
          $sum = 0;
          while($hptr < 12) {
            $sum += $hourlist[$hptr];
            $var = 100 * $hourlist[$hptr] / $today;
            if ($hptr == $tophour) {
              $str = sprintf("%2.0f%%<", $var);
            } else {
              $str = sprintf("%2.0f%%", $var);
            }
            printf "%-5s", $str;
            $hptr += 1;
          }
          print " = $sum\nPM: ";
          $sum = 0;
          while($hourlist[$hptr] ne '') {
            $sum += $hourlist[$hptr];
            $var = 100 * $hourlist[$hptr] / $today;
            if ($hptr == $tophour) {
              $str = sprintf("%2.0f%%<", $var);
            } else {
              $str = sprintf("%2.0f%%", $var);
            }
            printf "%-5s", $str;
            $hptr += 1;
          }
          print " = $sum\n";
          print "Unique this date: $curunique\n";
          $curlist[0] = "#-1";
          $curunique = 0; 
          $hptr = 0;
          while ($hptr < 24) {
            $hourlist[$hptr] = 0;
            $hptr += 1;
          }
          $today = 0;
        }
        $connections += 1;
        $today += 1;
        $hourlist[$hour[0]] += 1;
        $found = 0;
        $i = 0;
        @blah = split(/\)/,$words[1]);
        while (@list[$i] ne '') {
          if (@list[$i] eq $blah[0]) {
            $found = 1;
          }
          $i += 1;
        }
        if ($found == 0) {
          @list[$unique] = $blah[0];
          $unique += 1;
        }
        $found = 0;
        $i = 0;
        while ($curlist[$i] ne "#-1") {
          if (@curlist[$i] eq $blah[0]) {
            $found = 1;
          }
          $i += 1;
        }
        if ($found == 0) {
          @curlist[$i] = $blah[0];
          $curunique += 1;
          $i += 1;
          @curlist[$i] = "#-1";
        }
      }
    }
    $hptr = 0;
    $tophour = 0;
    while($hourlist[$hptr] ne '') {
      if ($hourlist[$hptr] > $hourlist[$tophour]) {
        $tophour = $hptr;
      }
      $hptr += 1;
    }
    $iday = $wday - ($mday - $day);
    if ($iday < 0) {
      $iday += 7;
    }
    print "Date: $DAYLIST[$iday], $month/$day\n";
    $day = $dayl[1];
    $hptr = 0;
    print "    12 - 01 - 02 - 03 - 04 - 05 - 06 - 07 - 08 - 09 - 10 - 11\n";
    print "AM: ";
    $hptr = 0;
    $sum = 0;
    while($hptr < 12) {
      $sum += $hourlist[$hptr];
      if ($connections == 0) {
        $connections = 1;
      }
      $var = 100 * $hourlist[$hptr] / $today;
      if ($hptr == $tophour) {
        $str = sprintf("%2.0f%%<", $var);
      } else {
        $str = sprintf("%2.0f%%", $var);
      }
      printf "%-5s", $str;
      $hptr += 1;
    }
    print " = $sum\nPM: ";
    $sum = 0;
    while($hourlist[$hptr] ne '') {
      $sum += $hourlist[$hptr];
      $var = 100 * $hourlist[$hptr] / $today;
      if ($hptr == $tophour) {
        $str = sprintf("%2.0f%%<", $var);
      } else {
        $str = sprintf("%2.0f%%", $var);
      }
      printf "%-5s", $str;
      $hptr += 1;
    }
    print " = $sum\n";
    print "Unique this date: $curunique\n";
    @dates = split(/ /, $last);
    $end = $dates[0];
    $etime = $dates[1]; 
    close(FP);
    print "\n+- Cumulative Stats -+\n";
    print "Log starts on $start at $stime\n"; 
    print "Log ends   on $end at $etime\n"; 
    print "Total connections         : $connections\n";
    print "Total unique              : $unique\n";
  }
}
