view nun/tasks/findreqs.pl @ 355:6f4baa8abf82 misc

Make this script a bit better. Also fix the ASCII table width for tasks that have a longer name than we anticipated.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 27 Jun 2018 14:40:27 +0300
parents 992d8c1c72e6
children 102ac4f0bd22
line wrap: on
line source

#!/usr/bin/perl -w
use strict;
use warnings;
my %taskList = ();
my @tmpList = ();
my $wasFound = 0;
my $n;
my $nline = 0;

### Process and parse input logfile
while (defined(my $line = <STDIN>))
{
  $nline++;
  chomp($line);

  if ($line =~ /^New holy task '(.+)' is now available to you\.$/)
  {
    push(@tmpList, $1);
    $wasFound = 1;
    $n = 0;
  }
  elsif ($line =~ /^You now have '(.+)' at ([0-9]+)\% without special bonuses\.$/ && $wasFound)
  {
    my $tmpName = $1;
    my $tmpPercentage = $2;
    $n = 10;

    for my $taskName (@tmpList)
    {
      if (defined($taskList{$taskName}{$tmpName}))
      {
        if ($tmpPercentage < $taskList{$taskName}{$tmpName})
        {
          $taskList{$taskName}{$tmpName} = $tmpPercentage;
        }
      }
      else
      {
        $taskList{$taskName}{$tmpName} = $tmpPercentage;
      }
    }
  }

  if ($n++ >= 6)
  {
    $wasFound = 0;
    for my $taskName (@tmpList)
    {
      if (!defined($taskList{$taskName}))
      {
        $taskList{$taskName} = ();
      }
    }
    @tmpList = ();
  }
}


sub print_separator()
{
  print "|------------------------------------------------------------------------|\n";
}


### Print out the results
$n = 0;
if ($#ARGV >= 0) {
  if ($ARGV[0] eq "-tasks")
  {
    for my $taskName (sort keys %taskList)
    {
      print $taskName."\n";
    }
  }
  elsif ($ARGV[0] eq "-dump")
  {
    for my $taskName (sort keys %taskList)
    {
      print $taskName."|";
      for my $skillName (sort keys %{$taskList{$taskName}})
      {
        print $skillName."=".$taskList{$taskName}{$skillName}."|";
      }
      print "\n";
    }
  }
}
else
{
  my $sepLine = 0;
  print  ",------------------------------------------------------------------------.\n";
  print  "| Task name                            | Skill/spell               | Min |\n";
  print  "+--------------------------------------+---------------------------+-----+\n";
  for my $taskName (sort keys %taskList)
  {
    my $nreqs = scalar keys(%{$taskList{$taskName}});
    
    if ($nreqs == 0)
    {
      # No requirement data for task
      print_separator() if ($sepLine);
      printf "| %-36s | %-31s |\n", $taskName, "UNKNOWN or no requirements";
      print_separator();
      $sepLine = 1;
    }
    elsif ($nreqs == 1)
    {
      for my $skillName (sort keys %{$taskList{$taskName}})
      {
        printf "| %-36s | %-25s | %3d |\n", $taskName, $skillName, $taskList{$taskName}{$skillName};
      }
      $sepLine = 1;
    }
    else
    {
      print_separator() if ($sepLine);
      printf "| %-70s |\n", $taskName;
      for my $skillName (sort keys %{$taskList{$taskName}})
      {
        printf "| %-36s | %-25s | %3d |\n", "", $skillName, $taskList{$taskName}{$skillName};
      }
      print_separator();
      $sepLine = 0;
    }

    $n++;
  }
  print  "|-----------------+--------------------+---------------------------+-----'\n";
  printf "| %3d tasks total |\n", $n;
  print  "`-----------------'\n";
}