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 sub main__arbitrage () {
97 foreach $commod (sort keys %commod) {
98 $current= $commod{$commod};
99 my @buys= @{ bs_p($commod,Buy, -1) };
100 my @sells= @{ bs_p($commod,Sell,+1) };
106 #print Dumper($commod,\@buys,\@sells);
109 my $pricediff= $buys[0]{Price} - $sells[0]{Price};
110 last unless $pricediff > 0;
112 sub arb_check_qty (\@) {
114 my $vqty= $verbs->[0]{Qty};
115 return if $vqty =~ m/^\>/;
116 $qty= $vqty if $qty > $vqty;
118 my $verb= shift @$verbs;
120 arb_check_qty(@buys);
121 arb_check_qty(@sells);
123 my $tprofit= $qty*$pricediff;
128 sprintf("%-13.13s| %-19.19s %4d| %-19.19s %4d|%3d x%3d =%3d\n",
130 $buys[0]{Stall},$buys[0]{Price},
131 $sells[0]{Stall},$sells[0]{Price},
132 $qty, $pricediff, $tprofit);
133 sub arb_subtract_qty (\@) {
135 my $verb= shift @$verbs;
137 Stall => $verb->{Stall},
138 Price => $verb->{Price},
139 Qty => $verb->{Qty} - $qty
142 arb_subtract_qty(@buys);
143 arb_subtract_qty(@sells);
147 sprintf("%-13.13s| %19s %4s| %19s %4s|%3d %4d\n",
148 $commod, '','', '','', $cqty, $profit);
149 push @arbs, { Profit => $profit, Info => $info };
154 print "No arbitrage opportunities.\n" or die $!;
158 =============+=========================+=========================+=============
161 print <<END or die $!;
163 commodity | seller price| buyer price| qty ea prof
167 foreach my $arb (sort {
168 $b->{Profit} <=> $a->{Profit};
170 print $div,$arb->{Info} or die $1;
172 -------------+-------------------------+-------------------------+-------------
174 $allprofit += $arb->{Profit};
176 print $bigdiv or die $!;
177 printf("%-13.13s %19s %4s %19s %4s %-5s %7d\n",
178 '', '','', '','', 'TOTAL', $allprofit)
182 sub main__bestprices () {
183 foreach $commod (sort keys %commod) {
184 $current= $commod{$commod};
185 my $buys= bs_p($commod,Buy, -1);
186 my $sells= bs_p($commod,Sell,+1);
187 printf("%-15.15s", $commod) or die $!;
188 bs_p_bestprice($buys);
189 bs_p_bestprice($sells);
190 print("\n") or die $!;
196 if (exists $current->{$bs}{$stall}) {
197 my $si= $current->{$bs}{$stall};
198 printf("\t%d\t%s", $si->{Price}, $si->{Qty}) or die $!;
200 printf("\t\t") or die $!;
205 foreach $commod (sort keys %commod) {
206 $current= $commod{$commod};
208 map { $stalls{$_}=1; } keys %{ $current->{Buy} };
209 map { $stalls{$_}=1; } keys %{ $current->{Sell} };
210 foreach $stall (sort keys %stalls) {
211 printf("%s\t%s", $commod, $stall) or die $!;
214 print("\n") or die $!;
221 our ($pctb) = $ENV{'YPPSC_PCTB_PCTB'};
223 our ($ua)= LWP::UserAgent->new;
225 sub load_commodmap() {
227 my $c= new IO::File "#commodmap#.tsv";
228 if (!$c) { $!==&ENOENT or die $!; return; }
230 m/^(\S.*\S)\t(\d+)\n$/ or die "$_";
233 $c->error and die $!;
237 sub refresh_commodmap() {
240 my $resp= $ua->get("$pctb/commodmap.php?version=2");
241 die $resp->status_line unless $resp->is_success;
247 my $o= new IO::File "#commodmap#.tsv.tmp",'w' or die $!;
250 my $xp= new XML::Parser
255 #print STDERR "START [$_] intag=$intag icm=$incommodmap\n";
256 if (m/^commodmap$/i) {
259 } elsif (m/^(?:name|index)$/i) {
261 $intag=lc($_) if $incommodmap;
262 #print STDERR "START RECOGNISED $intag icm=$incommodmap\n";
264 #print STDERR "START UNRECOGNISED\n";
269 #print STDERR "END [$_] intag=$intag icm=$incommodmap\n";
270 if (m/^commodmap$/i) {
272 die unless exists $got{'name'};
273 die unless exists $got{'index'};
274 die unless $got{'index'} =~ m/^\s*([1-9]\d{0,3})\s*$/;
277 s/^\s+//; s/\s+$//; s/\n/ /g; s/\s+/ /;
278 die "$_ ?" if exists $commodmap{$_};
279 $commodmap{$_}= $index;
280 print $o "$_\t$index\n" or die $!;
281 } elsif (lc $_ eq $intag) {
282 $got{$intag}= $cdata;
286 #print STDERR "CHAR [$_[1]] intag=$intag icm=$incommodmap\n";
290 my $content= $resp->content;
292 # print STDERR "[[[$content]]]\n";
293 $xp->parse($content);
295 rename "#commodmap#.tsv.tmp","#commodmap#.tsv" or die $!;
300 sub read_newcommods ($) {
302 if (!open NC, "< $file") {
303 $!==&ENOENT or die $!;
307 chomp; s/^\s*//; s/\s+$//;
312 NC->error and die $!;
316 sub refresh_newcommods() {
317 my $rsync= $ENV{'YPPSC_PCTB_RSYNC'};
318 $rsync= 'rsync' if !defined $rsync;
320 my $local= "#master-newcommods#.txt";
321 my $src= $ENV{'YPPSC_PCTB_DICT_UPDATE'};
323 my $remote= "$src/master-newcommods.txt";
324 $!=0; system 'rsync','-Lt','--',$remote,$local;
325 die "$? $!" if $! or $?;
327 read_newcommods($local);
328 read_newcommods('#local-newcommods#.txt');
331 our (%stallmap, @stallmap);
334 my ($bs,$sortmul) = @_;
339 sort { $commodmap{$a} <=> $commodmap{$b} }
340 grep { exists $commodmap{$_} }
343 #print STDERR "COMMOD $commod\n";
344 $current= $commod{$commod};
345 my $l= bs_p($commod,$bs,$sortmul);
347 #print STDERR "COMMOD $commod has ".scalar(@$l)."\n";
348 $o .= writeint($commodmap{$commod});
349 $o .= writeint(scalar @$l);
350 foreach my $cs (@$l) {
351 $stall= $cs->{Stall};
352 my $stallix= $stallmap{$stall};
353 if (!defined $stallix) {
354 push @stallmap, $stall;
355 $stallmap{$stall}= $stallix= @stallmap;
356 #print STDERR "STALL DEF $stallix $stall\n";
360 $o .= writeint($stallix, $cs->{Price}, $qty+0);
364 #print STDERR "COMMOD $commod COUNT WAS $count\n";
369 sub writeint { return pack 'v*', @_; }
371 our (%stalltypetoabbrevmap)= qw(
381 sub genmarketdata () {
382 our $version= '005b';
385 my @missing= grep { !exists $commodmap{$_} } keys %commod;
388 refresh_newcommods();
390 foreach $commod (sort keys %commod) {
391 next if exists $commodmap{$commod};
392 if (exists $newcommods{$commod}) {
393 printf STDERR "Ignoring new commodity \`%s'!\n", $commod;
395 printf STDERR "Unknown commodity \`%s'!\n", $commod;
399 die "$missing unknown commoditi(es).".
400 " See README (search for \`newcommods').\n"
405 $ob .= bs_gen_md(Buy, -1);
406 $ob .= bs_gen_md(Sell,+1);
408 my $ot= sprintf("$version\n".
411 foreach $stall (@stallmap) {
413 if ($st =~ m/^(\S+)\'s (\S+) Stall$/) {
414 my $stkind= $stalltypetoabbrevmap{$2};
415 if (defined $stkind) {
418 warn "unknown stall type $2 in $st\n";
426 sub main__genmarketdata () {
427 my $o= genmarketdata();
431 sub save_upload_html ($$) {
432 my ($which, $resptxt) = @_;
433 open R, ">./#upload-$which#.html" or die $!;
434 print R $resptxt or die $!;
438 sub main__upload () {
439 my $ocean= $ENV{'YPPSC_OCEAN'}; die unless $ocean;
440 my $island= $ENV{'YPPSC_ISLAND'}; die unless $island;
442 my $o= genmarketdata();
444 my $url= "$pctb/upload.php";
446 'marketdata' => [ undef, "marketdata.gz",
447 Content_Type => 'application/gzip',
452 print STDERR "Uploading data to $pctb...\n";
454 my $resp= $ua->post("$url", Content => $content,
455 Content_Type => 'form-data');
456 die $resp->status_line unless $resp->is_success;
458 my $resptxt= $resp->content();
459 save_upload_html('1', $resptxt);
461 open R, ">./#upload-1#.html" or die $!;
462 print R $resptxt or die $!;
465 my @filenames= $resptxt =~
466 m/input\s+type="hidden"\s+name="filename"\s+value=\"([_.0-9a-z]+)\"/ig;
469 my @forcerls= $resptxt =~
470 m/input\s+type="hidden"\s+name="forcereload"\s+value=\"([1-9]\d+)\"/ig;
473 my $filename= $filenames[0];
474 my $forcerl= $forcerls[0];
476 $ocean= ucfirst lc $ocean;
477 my @oceanids= $resptxt =~
478 m/\<option value\=\"(\d+)\"\>$ocean\<\/option\>/;
479 @oceanids==1 or die "@oceanids ?";
483 m/^islands\[\d+\]\[\d+\]\=new\s+option\(\"(.*)\"\,(\d+)\)\s*$/mig
485 next unless $1 eq $island;
488 defined $islandid or die;
490 die "@filenames ?" if grep { $_ ne $filename } @filenames;
491 die "@forcerls ?" if grep { $_ ne $forcerl } @forcerls;
496 print STDERR "Setting ocean and island...\n";
498 my $siurl= ($url . "?action=setisland".
499 "&filename=$filename".
500 "&forcereload=$forcerl".
501 "&ocean=$oceanids[0]".
502 "&island=$islandid");
503 $resp= $ua->get($siurl);
505 die $resp->status_line unless $resp->is_success;
507 $resptxt= $resp->content();
508 save_upload_html('2', $resptxt);
510 die unless $resptxt =~ m/your uploaded data has been processed/i;
511 die unless $resptxt =~ m/your data has been integrated into the database/i;
513 $resptxt =~ s/\<a href=\"about:\w+\"\>[^<>]+\<\/a\>//g;
514 save_upload_html('3', $resptxt);
516 print "\n" or die $!;
517 system('w3m -T text/html -dump < \#upload-3#.html');
519 print "\n" or die $!;
525 close(STDOUT) or die $!;