3 # helper program for processing commodity output
5 # This is part of ypp-sc-tools, a set of third-party tools for assisting
6 # players of Yohoho Puzzle Pirates.
8 # Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
10 # This program is free software: you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation, either version 3 of the License, or
13 # (at your option) any later version.
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program. If not, see <http://www.gnu.org/licenses/>.
23 # Yohoho and Puzzle Pirates are probably trademarks of Three Rings and
24 # are used without permission. This program is not endorsed or
25 # sponsored by Three Rings.
28 use strict (qw(vars));
31 # $commod{'Hemp'}{Buy|Sell}{'stall'}{Stall}
32 # $commod{'Hemp'}{Buy|Sell}{'stall'}{Price}
33 # $commod{'Hemp'}{Buy|Sell}{'stall'}{Qty}
34 # $commod{'Hemp'}{Hold}
37 our ($commod,$stall,%commod);
39 @ARGV==1 or die "You probably don't want to run this program directly.\n";
40 our ($mode) = shift @ARGV;
42 # ./yppsc-commod-processor tsv <t |less -x40,80,90,100,110
47 my ($price,$qty) = @v[$c..$c+1];
48 return if !length($price) && !length($qty);
49 die "$_ ?" unless length($price) && length($qty);
50 $commod{$commod}{$bs}{$stall}= {
60 #print STDERR "[".join("|",@v)."]\n";
61 ($commod,$stall) = @v;
64 $commod{$commod}{Hold}= $v[6]+0 if @v>6;
70 my ($commod,$bs,$sortmul) = @_;
71 my $ary= $current->{$bs};
74 foreach my $stall (sort {
75 $sortmul * ($ary->{$a}{Price} <=> $ary->{$b}{Price});
77 push @$r, $ary->{$stall};
82 sub bs_p_bestprice ($) {
85 printf("| %-25.25s %4d", $l->[0]{Stall}, $l->[0]{Price}) or die $!;
87 printf("| %25s %4s","","") or die $!;
91 our $arbitrage_only= 0;
93 sub main__arbitrage () {
95 foreach $commod (sort keys %commod) {
96 $current= $commod{$commod};
97 my @buys= @{ bs_p($commod,Buy, -1) };
98 my @sells= @{ bs_p($commod,Sell,+1) };
104 #print Dumper($commod,\@buys,\@sells);
107 my $pricediff= $buys[0]{Price} - $sells[0]{Price};
108 last unless $pricediff > 0;
110 sub arb_check_qty (\@) {
112 my $vqty= $verbs->[0]{Qty};
113 return if $vqty =~ m/^\>/;
114 $qty= $vqty if $qty > $vqty;
116 my $verb= shift @$verbs;
118 arb_check_qty(@buys);
119 arb_check_qty(@sells);
121 my $tprofit= $qty*$pricediff;
126 sprintf("%-13.13s| %-19.19s %4d| %-19.19s %4d|%3d x%3d =%3d\n",
128 $buys[0]{Stall},$buys[0]{Price},
129 $sells[0]{Stall},$sells[0]{Price},
130 $qty, $pricediff, $tprofit);
131 sub arb_subtract_qty (\@) {
133 my $verb= shift @$verbs;
135 Stall => $verb->{Stall},
136 Price => $verb->{Price},
137 Qty => $verb->{Qty} - $qty
140 arb_subtract_qty(@buys);
141 arb_subtract_qty(@sells);
145 sprintf("%-13.13s| %19s %4s| %19s %4s|%3d %4d\n",
146 $commod, '','', '','', $cqty, $profit);
147 push @arbs, { Profit => $profit, Info => $info };
153 =============+=========================+=========================+=============
156 print <<END or die $!;
158 commodity | seller price| buyer price| qty ea prof
162 foreach my $arb (sort {
163 $b->{Profit} <=> $a->{Profit};
165 print $div,$arb->{Info} or die $1;
167 -------------+-------------------------+-------------------------+-------------
169 $allprofit += $arb->{Profit};
171 print $bigdiv or die $!;
172 printf("%-13.13s %19s %4s %19s %4s %-5s %7d\n",
173 '', '','', '','', 'TOTAL', $allprofit)
177 sub main__bestprices () {
178 foreach $commod (sort keys %commod) {
179 $current= $commod{$commod};
180 my $buys= bs_p($commod,Buy, -1);
181 my $sells= bs_p($commod,Sell,+1);
182 if ($arbitrage_only) {
184 printf("%-15.15s", $commod) or die $!;
185 bs_p_bestprice($buys);
186 bs_p_bestprice($sells);
187 print("\n") or die $!;
193 if (exists $current->{$bs}{$stall}) {
194 my $si= $current->{$bs}{$stall};
195 printf("\t%d\t%s", $si->{Price}, $si->{Qty}) or die $!;
197 printf("\t\t") or die $!;
202 foreach $commod (sort keys %commod) {
203 $current= $commod{$commod};
205 map { $stalls{$_}=1; } keys %{ $current->{Buy} };
206 map { $stalls{$_}=1; } keys %{ $current->{Sell} };
207 foreach $stall (sort keys %stalls) {
208 printf("%s\t%s", $commod, $stall) or die $!;
211 print("\n") or die $!;
216 sub main__upload () {
217 die "\nUploading not yet implemented, sorry.\n";
222 close(STDOUT) or die $!;