chiark / gitweb /
Better arbitrage display
[ypp-sc-tools.web-live.git] / pctb / yppsc-commod-processor
1 #!/usr/bin/perl -w
2
3 # helper program for processing commodity output
4
5 # This is part of ypp-sc-tools, a set of third-party tools for assisting
6 # players of Yohoho Puzzle Pirates.
7 #
8 # Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
9 #
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.
14 #
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.
19 #
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/>.
22 #
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.
26
27
28 use strict (qw(vars));
29 use Data::Dumper;
30
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}
35
36 our @v;
37 our ($commod,$stall,%commod);
38
39 @ARGV==1 or die "You probably don't want to run this program directly.\n";
40 our ($mode) = shift @ARGV;
41
42 # ./yppsc-commod-processor tsv <t |less -x40,80,90,100,110
43
44 sub bs_read ($$) {
45     my ($bs,$c) = @_;
46     return if @v <= $c;
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}= {
51         Stall => $stall,
52         Price => $price,
53         Qty => $qty,
54     };
55 }
56
57 while (<>) {
58     chomp;
59     @v= split /\t/;
60 #print STDERR "[".join("|",@v)."]\n";
61     ($commod,$stall) = @v;
62     bs_read(Buy,  2);
63     bs_read(Sell, 4);
64     $commod{$commod}{Hold}= $v[6]+0 if @v>6;
65 }
66
67 our $current;
68
69 sub bs_p ($$$) {
70     my ($commod,$bs,$sortmul) = @_;
71     my $ary= $current->{$bs};
72     my $r= [ ];
73 #print Dumper($ary);
74     foreach my $stall (sort {
75         $sortmul * ($ary->{$a}{Price} <=> $ary->{$b}{Price});
76     } keys %$ary) {
77         push @$r, $ary->{$stall};
78     }
79     return $r;
80 }
81
82 sub bs_p_bestprice ($) {
83     my ($l) = @_;
84     if (@$l) {
85         printf("| %-25.25s %4d", $l->[0]{Stall}, $l->[0]{Price}) or die $!;
86     } else {
87         printf("| %25s %4s","","") or die $!;
88     }
89 }
90
91 our $arbitrage_only= 0;
92
93 sub main__arbitrage () {
94     my @arbs= ();
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) };
99         my $profit= 0;
100         my $cqty= 0;
101         my $info= '';
102         my $arbs= [];
103         for (;;) {
104 #print Dumper($commod,\@buys,\@sells);
105             last unless @buys;
106             last unless @sells;
107             my $pricediff= $buys[0]{Price} - $sells[0]{Price};
108             last unless $pricediff > 0;
109             our $qty= 1000;
110             sub arb_check_qty (\@) {
111                 my ($verbs) = @_;
112                 my $vqty= $verbs->[0]{Qty};
113                 return if $vqty =~ m/^\>/;
114                 $qty= $vqty if $qty > $vqty;
115                 return if $vqty;
116                 my $verb= shift @$verbs;
117             }
118             arb_check_qty(@buys);
119             arb_check_qty(@sells);
120             next unless $qty;
121             my $tprofit= $qty*$pricediff;
122             $profit += $tprofit;
123             $cqty += $qty;
124             
125             $info.=
126                 sprintf("%-13.13s| %-19.19s %4d| %-19.19s %4d|%3d x%3d =%3d\n",
127                        $commod,
128                        $buys[0]{Stall},$buys[0]{Price},
129                        $sells[0]{Stall},$sells[0]{Price},
130                        $qty, $pricediff, $tprofit);
131             sub arb_subtract_qty (\@) {
132                 my ($verbs) = @_;
133                 my $verb= shift @$verbs;
134                 unshift @$verbs, {
135                     Stall => $verb->{Stall},
136                     Price => $verb->{Price},
137                     Qty => $verb->{Qty} - $qty
138                 };
139             }
140             arb_subtract_qty(@buys);
141             arb_subtract_qty(@sells);
142         }
143         next unless $profit;
144         $info.=
145             sprintf("%-13.13s| %19s %4s| %19s %4s|%3d      %4d\n",
146                     $commod, '','', '','', $cqty, $profit);
147         push @arbs, { Profit => $profit, Info => $info };
148     }
149     my $allprofit;
150
151     return unless @arbs;
152     my $bigdiv= <<END;
153 =============+=========================+=========================+=============
154 END
155
156     print <<END or die $!;
157
158 commodity    | seller             price| buyer              price| qty  ea prof
159 END
160
161     my $div= $bigdiv;
162     foreach my $arb (sort {
163         $b->{Profit} <=> $a->{Profit};
164     } @arbs) {
165         print $div,$arb->{Info} or die $1;
166         $div= <<END;
167 -------------+-------------------------+-------------------------+-------------
168 END
169         $allprofit += $arb->{Profit};
170     }
171     print $bigdiv or die $!;
172     printf("%-13.13s  %19s %4s  %19s %4s %-5s %7d\n",
173            '', '','', '','', 'TOTAL', $allprofit)
174         or die $!;
175 }
176
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) {
183         }
184         printf("%-15.15s", $commod) or die $!;
185         bs_p_bestprice($buys);
186         bs_p_bestprice($sells);
187         print("\n") or die $!;
188     }
189 }
190
191 sub bs_p_tsv ($) {
192     my ($bs) = @_;
193     if (exists $current->{$bs}{$stall}) {
194         my $si= $current->{$bs}{$stall};
195         printf("\t%d\t%s", $si->{Price}, $si->{Qty}) or die $!;
196     } else {
197         printf("\t\t") or die $!;
198     }
199 }
200
201 sub main__tsv () {
202     foreach $commod (sort keys %commod) {
203         $current= $commod{$commod};
204         my %stalls;
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 $!;
209             bs_p_tsv(Buy);
210             bs_p_tsv(Sell);
211             print("\n") or die $!;
212         }
213     }
214 }
215
216 sub main__upload () {
217     die "\nUploading not yet implemented, sorry.\n";
218 }
219
220 $mode =~ s/\-//;
221 &{"main__$mode"};
222 close(STDOUT) or die $!;