chiark / gitweb /
remove parser for ship info from notes file (@-thing)
[ypp-sc-tools.web-live.git] / ypp-restock-rum
index 7587a3c79bf1d0ec7c062219c26126d617c19129..a8791508840295b8e3700fa849d3df5be6413335 100755 (executable)
@@ -26,7 +26,7 @@ In price, missing entries mean the commodity is unavailable.
 END
 ;
 
-our (@vals) = qw(swill grog fine shot);
+our (@kinds) = qw(Swill Grog Fine Shot);
 our (@proofs) = qw(40 60 100);
 
 sub parse_info ($$$\@$) {
@@ -58,21 +58,21 @@ sub parse_info ($$$\@$) {
     my $i=0;
     foreach $_ (@v) {
        $_ = $default if !length;
-       m/^0*(\d+)$/ or badusage("$what: $vals[$i] \`$_': bad syntax");
+       m/^0*(\d+)$/ or badusage("$what: $kinds[$i] \`$_': bad syntax");
        $_= $1;
        $i++;
     }
     @$storeary = @v;
 }
 
-our (@haves,@want,@price);
+our (@have,@want,@price);
 
 sub parse_args () {
     @ARGV or badusage("need some information to go on");
     while (@ARGV) {
        $_ = shift @ARGV;
        if (m/^have$/) {
-           parse_info(1,0,1, @{ $haves[@haves] }, 'BUG');
+           parse_info(1,0,1, @{ $have[@have] }, 'have');
        } elsif (m/^want$/) {
            parse_info(1,0,0, @want, 'want');
        } elsif (m/^price$/) {
@@ -85,124 +85,172 @@ sub parse_args () {
 
 sub badusage ($) {
     my ($m) = @_;
-    print STDERR "bad usage: $m\n\n$usage\n";
+    print STDERR "\nbad usage: $m\n\n$usage\n";
     exit 16;
 }
 
-parse_args();
-use Data::Dumper;
-print Dumper({ Have => \@haves, Want => \@want, Price => \@price });
+our $ff = '%6.1f';
+
+sub valid ($) {
+    my ($x) = @_;
+    defined $x and $x>0 and $x<1e4;
+}    
+
+sub prvff ($$\@$) {
+    my ($what, $format, $ary, $unit) = @_;
+    printf("%-25s", "$what:");
+    for my $i (qw(0 1 2 3)) {
+       my $x= $ary->[$i];
+       my $y= valid($x) ? sprintf $format, $x : '    ';
+       printf " %-9s", $y;
+    }
+    printf "  %s\n", $unit;
+}
+
+sub pr ($\@$) {
+    my ($what, $ary, $unit) = @_;
+    prvff($what, '%4d  ', @$ary, $unit);
+}
+
+sub prf ($\@$) {
+    my ($what, $ary, $unit) = @_;
+    prvff($what, $ff, @$ary, $unit);
+}
+
+sub pr1 ($$) {
+    my ($k,$v) = @_;
+    printf "%-20s %s\n", "$k:", $v;
+}
+sub fmt_stock_index ($) {
+    my ($si) = @_;
+    @have==1 ? '' : ' #'.$si;
+}
 
 our @norm_price;
-our $best;
+our ($best, $best_norm_price);
+
+sub print_inputs () {
+    printf("%25s",'');
+    map { printf " %5s    ", $_ } @kinds;
+    print "\n\n";
+    pr('prices', @price, 'poe ea.') if valid(@price);
+    pr('target stocks', @want, 'units') if valid(@want);
+    my $si=0; for my $stocks (@have) {
+       pr('actual stocks'.fmt_stock_index(++$si),
+          @$stocks, 'units');
+    }
+    print "\n";
+}
 
 sub compute_cheapest_rum() {
     return unless @price;
 
+    my (@perorder) = map { $_*10 } @price;
+    prf('equiv. ordering price', @perorder, 'poe/order');
+
     $best= undef;
-    my $best_norm_price= 1e5;
+    $best_norm_price= 1e5;
     for my $i (qw(0 1 2)) {
        next unless $price[$i];
-       $norm_price[$i] = $price[$i] * $proof[$i] / $proof[$i];
+       $norm_price[$i] = $price[$i] * 100 / $proofs[$i];
        if ($norm_price[$i] <= $best_norm_price) {
            $best= $i;
+           $best_norm_price= $norm_price[$i];
        }
     };
     prf('normalised prices', @norm_price, 'poe/fine');
+
     if (defined $best) {
-       printf "best is %s\n\n", $best;
-    } else {
-       die "no rum available ?\n";
+       printf "best is %-10s%*s^^\n",
+           $kinds[$best],
+           $best*10+10, '';
+       my (@bestperorder) = map {
+           $best_norm_price * $proofs[$_] / 100 * 10;
+       } qw(0 1 2);
+       #push @bestperorder, $perorder[3];
+       prf('best is equiv. ordering', @bestperorder, 'poe/order');
     }
+    print "\n";
 }
 
+sub pr1s ($) {
+    my ($x) = @_;
+    if (defined $x and $x) {
+       printf ' %9.1f', $x;
+    } else {
+       printf "          ";
+    }
+}
 
+sub compute_stock_values() {
+    return unless @have;
+    print <<END
+                 Rum      Rum     Shot    Shot     total      Profit    Profit
+                equiv.   value   stocks   value    value      this leg   total
+END
+;
 
-__DATA__
+    my $initial_value;
+    my $last_value;
+    my $si=0; for my $stocks (@have) {
+       my $stock_rum= 0;
+       foreach my $i (qw(0 1 2)) {
+           $stock_rum += $stocks->[$i] * $proofs[$i] / 100;
+       }
+       my $rum_value= defined($best) ? $stock_rum * $best_norm_price : 0;
+       my $shot_value= valid($price[3]) ? $stocks->[3] * $price[3] : 0;
+       my $total_value= $rum_value + $shot_value;
 
-our $ff = '%6.1f';
+       printf "%-10s ", 'stocks'.fmt_stock_index(++$si).':';
+       pr1s($stock_rum);
+       pr1s($rum_value);
+       printf "%6d", $stocks->[3];
+       pr1s($shot_value);
+       pr1s($total_value);
+       
+       if (defined $last_value) {
+           printf(" %10.1f %10.1f",
+                  $total_value - $last_value,
+                  $total_value - $initial_value);
+       }
+       $initial_value= $total_value unless defined $initial_value;
+       $last_value= $total_value;
+       print "\n";
+    }
+    print <<END
+                 fine      poe    units    poe       poe     delta-poe     poe
 
-sub prvff ($$\%$) {
-    my ($what, $format, $ary, $unit) = @_;
-    printf("%-40s", "$what:");
-    map {
-       my $x= $ary->{$_};
-       my $y= defined $x ? sprintf $format, $x : '   x';
-       printf " %-10s", $y;
-    } @rums;
-    printf "  %s\n", $unit;
+END
+;
 }
 
-sub pr ($\%$) {
-    my ($what, $ary, $unit) = @_;
-    prvff($what, '%4d  ', %$ary, $unit);
-}
+parse_args();
+print_inputs();
+compute_cheapest_rum();
+compute_stock_values();
 
-sub prf ($\%$) {
-    my ($what, $ary, $unit) = @_;
-    prvff($what, $ff, %$ary, $unit);
-}
+__DATA__
 
-sub pr1 ($$) {
-    my ($k,$v) = @_;
-    printf "%-20s %s\n", "$k:", $v;
-}
+#    if (defined $price{Swill}) {
+#      map { $price{$_}= undef if $price{$_} eq 'x' } @rums;
+#    }
+#    if (defined $have{Swill}) {
+#      $have_proof= 0;
+#      map { $have_proof += $have{$_} * $proof{$_} } @rums;
+#    }
 
 our ($best);
 
 our $have_proof;
 
-sub print_inputs () {
-    printf("%40s",'');
-    map { printf " %5s     ", $_ } @rums;
-    print "\n\n";
-    if (defined $price{Swill}) {
-       map { $price{$_}= undef if $price{$_} eq 'x' } @rums;
-       pr('prices', %price, 'poe ea.');
-    }
-    if (defined $have{Swill}) {
-       $have_proof= 0;
-       map { $have_proof += $have{$_} * $proof{$_} } @rums;
-       pr('stock on board', %have, 'rum');
-    }
-}
-
 our ($need_proof, %need, %buy);
 
 sub compute_restock_requirements () {
     if ($ship =~ m/^\d+/) {
        $need{Fine} = $ship;
     } else {
-       $ship =~ y/_/ /;
-       open F, "/home/ian/private/puzzle-pirates" or die $!;
-       my $this_ship= 0;
-       my $the_ship;
-       while (<F>) {
-           if (!m/\S/ || m/^\s*\#/) {
-               $this_ship= 0;
-               next;
-           }
-           if (!m/^\@/) {
-               next;
-           }
-           if (m/^\@(( [A-Z][-a-z]+){2,})\s*$/) {
-               $this_ship= (uc $1 eq uc " $ship" or
-                            uc $+ eq uc " $ship");
-               $the_ship= $1;
-               next;
-           }
-           next unless $this_ship;
-           if (m/^\@\s+(\d+)\s+fine\s*/) {
-               $need{Fine} = $1;
-               last;
-           }
-       }
-       die $! if F->error;
-       die "unknown ship $ship" unless defined $need{Fine};
-       if (defined $ship) {
-           pr1("vessel",$the_ship);
-       }
     }
+
     pr1('desired stock level', sprintf("%4d fine rum", $need{Fine}));
     $need_proof= $need{Fine} * $proof{Fine} - $have_proof;
     map {