X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~yarrgweb/git?p=ypp-sc-tools.db-test.git;a=blobdiff_plain;f=ypp-restock-rum;h=ce76aa794d8eeb11189b0c5d4a8a8e05df829e6f;hp=98910ebc63581b6cb00d24fa436c75ffc9631d00;hb=ed817cf1be071a52992d8dd9fabc2437472d8085;hpb=2d35f1d97444ff9c38cca026c4b5b92f567fe59d diff --git a/ypp-restock-rum b/ypp-restock-rum index 98910eb..ce76aa7 100755 --- a/ypp-restock-rum +++ b/ypp-restock-rum @@ -26,7 +26,7 @@ In price, missing entries mean the commodity is unavailable. END ; -our (@kinds) = qw(swill grog fine shot); +our (@kinds) = qw(Swill Grog Fine Shot); our (@proofs) = qw(40 60 100); sub parse_info ($$$\@$) { @@ -65,18 +65,18 @@ sub parse_info ($$$\@$) { @$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$/) { - parse_info(0,1e6,0, @price, 'price'); + parse_info(0,1e7,0, @price, 'price'); } else { badusage("unknown information argument \`$_'"); } @@ -85,19 +85,24 @@ 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; } our $ff = '%6.1f'; +sub valid ($) { + my ($x) = @_; + defined $x and $x and $x<1e5; +} + sub prvff ($$\@$) { my ($what, $format, $ary, $unit) = @_; - printf("%-40s", "$what:"); - for my $i (qw(0 1 2)) { + printf("%-25s", "$what:"); + for my $i (qw(0 1 2 3)) { my $x= $ary->[$i]; - my $y= $x>0 && $x<1e5 ? sprintf $format, $x : ' x'; - printf " %-10s", $y; + my $y= valid($x) ? sprintf $format, $x : ' '; + printf " %-9s", $y; } printf " %s\n", $unit; } @@ -116,103 +121,200 @@ sub pr1 ($$) { my ($k,$v) = @_; printf "%-20s %s\n", "$k:", $v; } +sub fmt_stock_index ($) { + my ($si) = @_; + @have==1 ? '' : ' #'.$si; +} +sub rum_total (\@) { + my ($rums) = @_; + my $total= 0; + foreach my $i (qw(0 1 2)) { + $total += $rums->[$i] * $proofs[$i] / 100; + } + return $total; +} 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= 1e6; for my $i (qw(0 1 2)) { next unless $price[$i]; - $norm_price[$i] = $price[$i] * $proofs[$i] / $proofs[$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", $kinds[$best]; + 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 (valid($x)) { + printf ' %9.1f', $x; } else { - die "no rum available ?\n"; + printf " "; } } -parse_args(); -use Data::Dumper; -print Dumper({ Have => \@haves, Want => \@want, Price => \@price }); -compute_cheapest_rum(); +sub compute_stock_values() { + return unless @have; + print <[3] * $price[3] : 0; + my $total_value= $rum_value + $shot_value; -our ($best); + 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 <) { - 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; - } + return unless @want; + + my $rum_want= rum_total(@want); + + my $stocks= @have ? $have[-1] : [qw(0 0 0 0)]; + my $rum_have= rum_total(@$stocks); + + pr2rs('desired stock level', $rum_want, $want[3]); + + my $rum_need = $rum_want - $rum_have; + my $shot_need = $want[3] - $stocks->[3]; + + if (@have) { + pr2rs('current stock', $rum_have, $stocks->[3]); + pr2rs('restock requirement', $rum_need, $shot_need); + print "\n"; + } + + if (@price) { + my ($rum_buy,$shot_buy) = ('',''); + my ($rum_bill,$shot_bill) = qw(0 0); + if (defined $best and $rum_need > 0) { + my $rum_qty= $rum_need * 100 / $proofs[$best]; + $rum_qty= ceil($rum_qty); + $rum_buy= sprintf('%12s %-11s ',"** $rum_qty","$kinds[$best] **"); + $rum_bill= $rum_qty * $price[$best]; + } + if ($shot_need > 0 and valid($price[3])) { + $shot_buy= sprintf('%7s shot **', "** $shot_need"); + $shot_bill= $shot_need * $price[3]; } - die $! if F->error; - die "unknown ship $ship" unless defined $need{Fine}; - if (defined $ship) { - pr1("vessel",$the_ship); + if (length($rum_buy) or length($shot_buy)) { + pr3('for', + $rum_bill ? sprintf("%12d poe ", $rum_bill) : '', + $shot_bill ? sprintf("%10d poe", $shot_bill) : '', + sprintf("%7d total", $rum_bill + $shot_bill)); + pr2('BUY', $rum_buy, $shot_buy); + } else { + print "stocks are sufficient.\n"; } + print "\n"; } - pr1('desired stock level', sprintf("%4d fine rum", $need{Fine})); - $need_proof= $need{Fine} * $proof{Fine} - $have_proof; - map { - $buy{$_} = $need_proof / $proof{$_}; - } @rums; - pr1("stock equivalent", sprintf "$ff fine rum", $have_proof / $proof{Fine}); - pr1("restock equivalent", sprintf "$ff fine rum", $need_proof / $proof{Fine}); - prf('would need', %buy, 'rum'); } +parse_args(); +print_inputs(); +compute_cheapest_rum(); +compute_stock_values(); +compute_restock_requirements(); + +__DATA__ + +# 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; + +our ($need_proof, %need, %buy); + + sub compute_restock_cheapest_rum() { my %bill; map {