chiark / gitweb /
remove parser for ship info from notes file (@-thing)
[ypp-sc-tools.db-live.git] / ypp-restock-rum
1 #!/usr/bin/perl -w
2 use strict qw(vars);
3 use IO::Handle;
4 use POSIX;
5
6 my $usage = <<END
7 usage:
8   .../ypp-restock-rum [<information> ...]
9
10 where <information> is
11
12   have  [<swill> <grog>] <fine> [<hold_shot>+<loaded_shot>]
13   want  [<swill> <grog>] <fine> [<shot>]
14   price  <swill> <grog>  <fine> [<shot>]
15
16 Each of which may appear only once, except \`have' which may appear
17 more than once which case we calculate the differences and the profit
18 for each one.
19
20 If /<shot> is not specified at all, relevant information about shot is
21 not reported.  For have and want, specifying an empty string means
22 zero.  Entirely Missing <swill>/<grog>/ is taken as if both were
23 specified and zero.
24
25 In price, missing entries mean the commodity is unavailable.
26 END
27 ;
28
29 our (@kinds) = qw(Swill Grog Fine Shot);
30 our (@proofs) = qw(40 60 100);
31
32 sub parse_info ($$$\@$) {
33     my ($omitswgok,$default,$multishot, $storeary, $what) = @_;
34     @ARGV or badusage("missing value for information argument \`$_'");
35     badusage("$what: specified more than once")
36         if defined $storeary->[2];
37     my (@v) = ();
38     while (@ARGV and $ARGV[0] =~ m/^\d/) {
39         $_ = shift @ARGV;
40         push @v, $_;
41     }
42     if (@v==1 or @v==2) {
43         badusage("$what: swill and grog amounts must be specified")
44             unless $omitswgok;
45         @v=($default,$default,@v);
46     }
47     if ($multishot and @v==4 and length $v[3]) {
48         $v[3] =~ m/^0*(\d+)\+0*(\d+)$/ or
49             badusage("$what: shot must be specified as <hold>+<loaded>");
50         $v[3] = $1 + $2;
51     }
52     if (@v==3) {
53         push @v, $default;
54     }
55     if (@v != 4) {
56         badusage("$what: invalid syntax (wrong number of /s)");
57     }
58     my $i=0;
59     foreach $_ (@v) {
60         $_ = $default if !length;
61         m/^0*(\d+)$/ or badusage("$what: $kinds[$i] \`$_': bad syntax");
62         $_= $1;
63         $i++;
64     }
65     @$storeary = @v;
66 }
67
68 our (@have,@want,@price);
69
70 sub parse_args () {
71     @ARGV or badusage("need some information to go on");
72     while (@ARGV) {
73         $_ = shift @ARGV;
74         if (m/^have$/) {
75             parse_info(1,0,1, @{ $have[@have] }, 'have');
76         } elsif (m/^want$/) {
77             parse_info(1,0,0, @want, 'want');
78         } elsif (m/^price$/) {
79             parse_info(0,1e6,0, @price, 'price');
80         } else {
81             badusage("unknown information argument \`$_'");
82         }
83     }
84 }
85
86 sub badusage ($) {
87     my ($m) = @_;
88     print STDERR "\nbad usage: $m\n\n$usage\n";
89     exit 16;
90 }
91
92 our $ff = '%6.1f';
93
94 sub valid ($) {
95     my ($x) = @_;
96     defined $x and $x>0 and $x<1e4;
97 }    
98
99 sub prvff ($$\@$) {
100     my ($what, $format, $ary, $unit) = @_;
101     printf("%-25s", "$what:");
102     for my $i (qw(0 1 2 3)) {
103         my $x= $ary->[$i];
104         my $y= valid($x) ? sprintf $format, $x : '    ';
105         printf " %-9s", $y;
106     }
107     printf "  %s\n", $unit;
108 }
109
110 sub pr ($\@$) {
111     my ($what, $ary, $unit) = @_;
112     prvff($what, '%4d  ', @$ary, $unit);
113 }
114
115 sub prf ($\@$) {
116     my ($what, $ary, $unit) = @_;
117     prvff($what, $ff, @$ary, $unit);
118 }
119
120 sub pr1 ($$) {
121     my ($k,$v) = @_;
122     printf "%-20s %s\n", "$k:", $v;
123 }
124 sub fmt_stock_index ($) {
125     my ($si) = @_;
126     @have==1 ? '' : ' #'.$si;
127 }
128
129 our @norm_price;
130 our ($best, $best_norm_price);
131
132 sub print_inputs () {
133     printf("%25s",'');
134     map { printf " %5s    ", $_ } @kinds;
135     print "\n\n";
136     pr('prices', @price, 'poe ea.') if valid(@price);
137     pr('target stocks', @want, 'units') if valid(@want);
138     my $si=0; for my $stocks (@have) {
139         pr('actual stocks'.fmt_stock_index(++$si),
140            @$stocks, 'units');
141     }
142     print "\n";
143 }
144
145 sub compute_cheapest_rum() {
146     return unless @price;
147
148     my (@perorder) = map { $_*10 } @price;
149     prf('equiv. ordering price', @perorder, 'poe/order');
150
151     $best= undef;
152     $best_norm_price= 1e5;
153     for my $i (qw(0 1 2)) {
154         next unless $price[$i];
155         $norm_price[$i] = $price[$i] * 100 / $proofs[$i];
156         if ($norm_price[$i] <= $best_norm_price) {
157             $best= $i;
158             $best_norm_price= $norm_price[$i];
159         }
160     };
161     prf('normalised prices', @norm_price, 'poe/fine');
162
163     if (defined $best) {
164         printf "best is %-10s%*s^^\n",
165             $kinds[$best],
166             $best*10+10, '';
167         my (@bestperorder) = map {
168             $best_norm_price * $proofs[$_] / 100 * 10;
169         } qw(0 1 2);
170         #push @bestperorder, $perorder[3];
171         prf('best is equiv. ordering', @bestperorder, 'poe/order');
172     }
173     print "\n";
174 }
175
176 sub pr1s ($) {
177     my ($x) = @_;
178     if (defined $x and $x) {
179         printf ' %9.1f', $x;
180     } else {
181         printf "          ";
182     }
183 }
184
185 sub compute_stock_values() {
186     return unless @have;
187     print <<END
188                  Rum      Rum     Shot    Shot     total      Profit    Profit
189                 equiv.   value   stocks   value    value      this leg   total
190 END
191 ;
192
193     my $initial_value;
194     my $last_value;
195     my $si=0; for my $stocks (@have) {
196         my $stock_rum= 0;
197         foreach my $i (qw(0 1 2)) {
198             $stock_rum += $stocks->[$i] * $proofs[$i] / 100;
199         }
200         my $rum_value= defined($best) ? $stock_rum * $best_norm_price : 0;
201         my $shot_value= valid($price[3]) ? $stocks->[3] * $price[3] : 0;
202         my $total_value= $rum_value + $shot_value;
203
204         printf "%-10s ", 'stocks'.fmt_stock_index(++$si).':';
205         pr1s($stock_rum);
206         pr1s($rum_value);
207         printf "%6d", $stocks->[3];
208         pr1s($shot_value);
209         pr1s($total_value);
210         
211         if (defined $last_value) {
212             printf(" %10.1f %10.1f",
213                    $total_value - $last_value,
214                    $total_value - $initial_value);
215         }
216         $initial_value= $total_value unless defined $initial_value;
217         $last_value= $total_value;
218         print "\n";
219     }
220     print <<END
221                  fine      poe    units    poe       poe     delta-poe     poe
222
223 END
224 ;
225 }
226
227 parse_args();
228 print_inputs();
229 compute_cheapest_rum();
230 compute_stock_values();
231
232 __DATA__
233
234 #    if (defined $price{Swill}) {
235 #       map { $price{$_}= undef if $price{$_} eq 'x' } @rums;
236 #    }
237 #    if (defined $have{Swill}) {
238 #       $have_proof= 0;
239 #       map { $have_proof += $have{$_} * $proof{$_} } @rums;
240 #    }
241
242 our ($best);
243
244 our $have_proof;
245
246 our ($need_proof, %need, %buy);
247
248 sub compute_restock_requirements () {
249     if ($ship =~ m/^\d+/) {
250         $need{Fine} = $ship;
251     } else {
252     }
253
254     pr1('desired stock level', sprintf("%4d fine rum", $need{Fine}));
255     $need_proof= $need{Fine} * $proof{Fine} - $have_proof;
256     map {
257         $buy{$_} = $need_proof / $proof{$_};
258     } @rums;
259     pr1("stock equivalent", sprintf "$ff fine rum", $have_proof / $proof{Fine});
260     pr1("restock equivalent", sprintf "$ff fine rum", $need_proof / $proof{Fine});
261     prf('would need', %buy, 'rum');
262 }
263
264 sub compute_restock_cheapest_rum() {
265     my %bill;
266     map {
267         $bill{$_} = $buy{$_} * $price{$_} if defined $price{$_};
268     } @rums;
269     prf('nominal bill', %bill, 'poe');
270     print "\n";
271     if ($need_proof < 0) {
272         printf "stocks are sufficient";
273     } else {
274         my $buy= ceil($buy{$best});
275         printf "buy %d %s at %d poe each for %d poe",
276             $buy, $best, $price{$best}, $buy * $price{$best};
277     }
278     print "\n\n";
279 }
280
281 main();