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));
35 # $commod{'Hemp'}{Buy|Sell}{'stall'}{Stall}
36 # $commod{'Hemp'}{Buy|Sell}{'stall'}{Price}
37 # $commod{'Hemp'}{Buy|Sell}{'stall'}{Qty}
38 # $commod{'Hemp'}{Hold}
41 our ($commod,$stall,%commod);
43 @ARGV==1 or die "You probably don't want to run this program directly.\n";
44 our ($mode) = shift @ARGV;
46 # ./yppsc-commod-processor tsv <t |less -x40,80,90,100,110
51 my ($price,$qty) = @v[$c..$c+1];
52 return if !length($price) && !length($qty);
53 die "$_ ?" unless length($price) && length($qty);
54 $commod{$commod}{$bs}{$stall}= {
64 #print STDERR "[".join("|",@v)."]\n";
65 ($commod,$stall) = @v;
68 $commod{$commod}{Hold}= $v[6]+0 if @v>6;
74 my ($commod,$bs,$sortmul) = @_;
75 my $ary= $current->{$bs};
78 foreach my $stall (sort {
79 $sortmul * ($ary->{$a}{Price} <=> $ary->{$b}{Price});
81 push @$r, $ary->{$stall};
86 sub bs_p_bestprice ($) {
89 printf("| %-25.25s %4d", $l->[0]{Stall}, $l->[0]{Price}) or die $!;
91 printf("| %25s %4s","","") or die $!;
95 our $arbitrage_only= 0;
97 sub main__arbitrage () {
99 foreach $commod (sort keys %commod) {
100 $current= $commod{$commod};
101 my @buys= @{ bs_p($commod,Buy, -1) };
102 my @sells= @{ bs_p($commod,Sell,+1) };
108 #print Dumper($commod,\@buys,\@sells);
111 my $pricediff= $buys[0]{Price} - $sells[0]{Price};
112 last unless $pricediff > 0;
114 sub arb_check_qty (\@) {
116 my $vqty= $verbs->[0]{Qty};
117 return if $vqty =~ m/^\>/;
118 $qty= $vqty if $qty > $vqty;
120 my $verb= shift @$verbs;
122 arb_check_qty(@buys);
123 arb_check_qty(@sells);
125 my $tprofit= $qty*$pricediff;
130 sprintf("%-13.13s| %-19.19s %4d| %-19.19s %4d|%3d x%3d =%3d\n",
132 $buys[0]{Stall},$buys[0]{Price},
133 $sells[0]{Stall},$sells[0]{Price},
134 $qty, $pricediff, $tprofit);
135 sub arb_subtract_qty (\@) {
137 my $verb= shift @$verbs;
139 Stall => $verb->{Stall},
140 Price => $verb->{Price},
141 Qty => $verb->{Qty} - $qty
144 arb_subtract_qty(@buys);
145 arb_subtract_qty(@sells);
149 sprintf("%-13.13s| %19s %4s| %19s %4s|%3d %4d\n",
150 $commod, '','', '','', $cqty, $profit);
151 push @arbs, { Profit => $profit, Info => $info };
156 print "No arbitrage opportunities.\n" or die $!;
160 =============+=========================+=========================+=============
163 print <<END or die $!;
165 commodity | seller price| buyer price| qty ea prof
169 foreach my $arb (sort {
170 $b->{Profit} <=> $a->{Profit};
172 print $div,$arb->{Info} or die $1;
174 -------------+-------------------------+-------------------------+-------------
176 $allprofit += $arb->{Profit};
178 print $bigdiv or die $!;
179 printf("%-13.13s %19s %4s %19s %4s %-5s %7d\n",
180 '', '','', '','', 'TOTAL', $allprofit)
184 sub main__bestprices () {
185 foreach $commod (sort keys %commod) {
186 $current= $commod{$commod};
187 my $buys= bs_p($commod,Buy, -1);
188 my $sells= bs_p($commod,Sell,+1);
189 if ($arbitrage_only) {
191 printf("%-15.15s", $commod) or die $!;
192 bs_p_bestprice($buys);
193 bs_p_bestprice($sells);
194 print("\n") or die $!;
200 if (exists $current->{$bs}{$stall}) {
201 my $si= $current->{$bs}{$stall};
202 printf("\t%d\t%s", $si->{Price}, $si->{Qty}) or die $!;
204 printf("\t\t") or die $!;
209 foreach $commod (sort keys %commod) {
210 $current= $commod{$commod};
212 map { $stalls{$_}=1; } keys %{ $current->{Buy} };
213 map { $stalls{$_}=1; } keys %{ $current->{Sell} };
214 foreach $stall (sort keys %stalls) {
215 printf("%s\t%s", $commod, $stall) or die $!;
218 print("\n") or die $!;
225 our ($pctb) = 'http://pctb.ilk.org/';
228 sub load_commodmap() {
230 my $c= new IO::File "#commodmap#.tsv";
231 if (!$c) { $!==&ENOENT or die $!; return; }
233 m/^(\S.*\S)\t(\d+)\n$/ or die "$_";
236 $c->error and die $!;
240 sub refresh_commodmap() {
241 my $ua= LWP::UserAgent->new;
242 my $resp= $ua->get("$pctb/commodmap.php");
243 die $resp->status_line unless $resp->is_success;
249 my $o= new IO::File "#commodmap#.tsv.new",'w' or die $!;
251 my $xp= new XML::Parser
256 print STDERR "START [$_] intag=$intag icm=$incommodmap\n";
257 if (m/^commodmap$/i) {
260 } elsif (m/^(?:name|index)$/i) {
262 $intag=lc($_) if $incommodmap;
263 print STDERR "START RECOGNISED $intag icm=$incommodmap\n";
265 print STDERR "START UNRECOGNISED\n";
270 print STDERR "END [$_] intag=$intag icm=$incommodmap\n";
271 if (m/^commodmap$/i) {
273 die unless exists $got{'name'};
274 die unless exists $got{'index'};
275 die unless $got{'index'} =~ m/^\s*([1-9]\d{0,3})\s*$/;
278 s/^\s+//; s/\s+$//; s/\n/ /g; s/\s+/ /;
279 die if exists $commodmap{$_};
280 $commodmap{$_}= $index;
281 print $o "$_\t$index\n" or die $!;
282 } elsif (lc $_ eq $intag) {
283 $got{$intag}= $cdata;
287 print STDERR "CHAR [$_[1]] intag=$intag icm=$incommodmap\n";
291 my $content= $resp->content;
293 # hacks to strip off drivel that seems to have been added!
294 $content =~ s/^.*\n(\<\?xml)/$1/s;
295 $content =~ s/\<\/body\>.*//s;
296 print STDERR "[[[$content]]]\n";
297 $xp->parse($content);
299 rename "#commodmap#.tsv.new","#commodmap#.tsv" or die $!;
307 sub main__genmarketdata () {
308 our $version= '005b';
311 my @missing= grep { !exists $commodmap{$_} } keys %commod;
315 foreach $commod (sort keys %commod) {
316 next if exists $commodmap{$commod};
317 print STDERR "Unknown commodity \`%s'!\n";
320 die "$missing unknown commodities. OCR failure?\n"
324 # foreach $commod (sort keys %commod) {
333 sub main__upload () {
334 die "\nUploading not yet implemented, sorry.\n";
340 close(STDOUT) or die $!;