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));
37 # $commod{'Hemp'}{Buy|Sell}{'stall'}{Stall}
38 # $commod{'Hemp'}{Buy|Sell}{'stall'}{Price}
39 # $commod{'Hemp'}{Buy|Sell}{'stall'}{Qty}
40 # $commod{'Hemp'}{Hold}
43 our ($commod,$stall,%commod);
45 @ARGV==1 or die "You probably don't want to run this program directly.\n";
46 our ($mode) = shift @ARGV;
48 # ./yppsc-commod-processor tsv <t |less -x40,80,90,100,110
53 my ($price,$qty) = @v[$c..$c+1];
54 return if !length($price) && !length($qty);
55 die "$_ ?" unless length($price) && length($qty);
56 $commod{$commod}{$bs}{$stall}= {
66 #print STDERR "[".join("|",@v)."]\n";
67 ($commod,$stall) = @v;
70 $commod{$commod}{Hold}= $v[6]+0 if @v>6;
76 my ($commod,$bs,$sortmul) = @_;
77 my $ary= $current->{$bs};
80 foreach my $stall (sort {
81 $sortmul * ($ary->{$a}{Price} <=> $ary->{$b}{Price});
83 push @$r, $ary->{$stall};
88 sub bs_p_bestprice ($) {
91 printf("| %-25.25s %4d", $l->[0]{Stall}, $l->[0]{Price}) or die $!;
93 printf("| %25s %4s","","") or die $!;
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 printf("%-15.15s", $commod) or die $!;
190 bs_p_bestprice($buys);
191 bs_p_bestprice($sells);
192 print("\n") or die $!;
198 if (exists $current->{$bs}{$stall}) {
199 my $si= $current->{$bs}{$stall};
200 printf("\t%d\t%s", $si->{Price}, $si->{Qty}) or die $!;
202 printf("\t\t") or die $!;
207 foreach $commod (sort keys %commod) {
208 $current= $commod{$commod};
210 map { $stalls{$_}=1; } keys %{ $current->{Buy} };
211 map { $stalls{$_}=1; } keys %{ $current->{Sell} };
212 foreach $stall (sort keys %stalls) {
213 printf("%s\t%s", $commod, $stall) or die $!;
216 print("\n") or die $!;
222 our ($pctb) = $ENV{'YPPSC_PCTB_PCTB'};
224 our ($ua)= LWP::UserAgent->new;
226 sub refresh_commodmap() {
229 my $resp= $ua->get("$pctb/commodmap.php?version=2");
230 die $resp->status_line unless $resp->is_success;
236 my $o= new IO::File "_commodmap.tsv.tmp",'w' or die $!;
237 undef %pctb_commodmap;
239 my $xp= new XML::Parser
244 #print STDERR "START [$_] intag=$intag icm=$incommodmap\n";
245 if (m/^commodmap$/i) {
248 } elsif (m/^(?:name|index)$/i) {
250 $intag=lc($_) if $incommodmap;
251 #print STDERR "START RECOGNISED $intag icm=$incommodmap\n";
253 #print STDERR "START UNRECOGNISED\n";
258 #print STDERR "END [$_] intag=$intag icm=$incommodmap\n";
259 if (m/^commodmap$/i) {
261 die unless exists $got{'name'};
262 die unless exists $got{'index'};
263 die unless $got{'index'} =~ m/^\s*([1-9]\d{0,3})\s*$/;
266 s/^\s+//; s/\s+$//; s/\n/ /g; s/\s+/ /;
267 die "$_ ?" if exists $pctb_commodmap{$_};
268 $pctb_commodmap{$_}= $index;
269 print $o "$_\t$index\n" or die $!;
270 } elsif (lc $_ eq $intag) {
271 $got{$intag}= $cdata;
275 #print STDERR "CHAR [$_[1]] intag=$intag icm=$incommodmap\n";
279 my $content= $resp->content;
281 # print STDERR "[[[$content]]]\n";
282 $xp->parse($content);
284 rename "_commodmap.tsv.tmp","_commodmap.tsv" or die $!;
289 sub read_newcommods ($) {
291 if (!open NC, "< $file") {
292 $!==&ENOENT or die $!;
296 chomp; s/^\s*//; s/\s+$//;
301 NC->error and die $!;
305 sub refresh_newcommods() {
306 my $rsync= $ENV{'YPPSC_PCTB_RSYNC'};
307 $rsync= 'rsync' if !defined $rsync;
309 my $local= "_master-newcommods.txt";
310 my $src= $ENV{'YPPSC_PCTB_DICT_UPDATE'};
312 my $remote= "$src/master-newcommods.txt";
313 $!=0; system 'rsync','-Lt','--',$remote,$local;
314 die "$? $!" if $! or $?;
316 read_newcommods($local);
317 read_newcommods('_local-newcommods.txt');
320 our (%stallmap, @stallmap);
323 my ($bs,$sortmul) = @_;
328 sort { $pctb_commodmap{$a} <=> $pctb_commodmap{$b} }
329 grep { exists $pctb_commodmap{$_} }
332 #print STDERR "COMMOD $commod\n";
333 $current= $commod{$commod};
334 my $l= bs_p($commod,$bs,$sortmul);
336 #print STDERR "COMMOD $commod has ".scalar(@$l)."\n";
337 $o .= writeint($pctb_commodmap{$commod});
338 $o .= writeint(scalar @$l);
339 foreach my $cs (@$l) {
340 $stall= $cs->{Stall};
341 my $stallix= $stallmap{$stall};
342 if (!defined $stallix) {
343 push @stallmap, $stall;
344 $stallmap{$stall}= $stallix= @stallmap;
345 #print STDERR "STALL DEF $stallix $stall\n";
349 $o .= writeint($stallix, $cs->{Price}, $qty+0);
353 #print STDERR "COMMOD $commod COUNT WAS $count\n";
358 sub writeint { return pack 'v*', @_; }
360 our (%stalltypetoabbrevmap)= qw(
370 sub genmarketdata () {
371 our $version= '005b';
373 parse_pctb_commodmap();
374 my @missing= grep { !exists $pctb_commodmap{$_} } keys %commod;
377 refresh_newcommods();
379 foreach $commod (sort keys %commod) {
380 next if exists $pctb_commodmap{$commod};
381 if (exists $newcommods{$commod}) {
382 printf STDERR "Ignoring new commodity \`%s'!\n", $commod;
384 printf STDERR "Unknown commodity \`%s'!\n", $commod;
388 die "$missing unknown commoditi(es).".
389 " See README (search for \`newcommods').\n"
394 $ob .= bs_gen_md(Buy, -1);
395 $ob .= bs_gen_md(Sell,+1);
397 my $ot= sprintf("$version\n".
400 foreach $stall (@stallmap) {
402 if ($st =~ m/^(\S+)\'s (\S+) Stall$/) {
403 my $stkind= $stalltypetoabbrevmap{$2};
404 if (defined $stkind) {
407 warn "unknown stall type $2 in $st\n";
415 sub main__genmarketdata () {
416 my $o= genmarketdata();
420 sub save_upload_html ($$) {
421 my ($which, $resptxt) = @_;
422 open R, ">./_upload-$which.html" or die $!;
423 print R $resptxt or die $!;
427 sub main__upload () {
428 my $ocean= $ENV{'YPPSC_OCEAN'}; die unless $ocean;
429 my $island= $ENV{'YPPSC_ISLAND'}; die unless $island;
431 my $o= genmarketdata();
433 my $url= "$pctb/upload.php";
435 'marketdata' => [ undef, "marketdata.gz",
436 Content_Type => 'application/gzip',
441 print STDERR "Uploading data to $pctb...\n";
443 my $resp= $ua->post("$url", Content => $content,
444 Content_Type => 'form-data');
445 die $resp->status_line unless $resp->is_success;
447 my $resptxt= $resp->content();
448 save_upload_html('1', $resptxt);
450 open R, ">./_upload-1.html" or die $!;
451 print R $resptxt or die $!;
454 my @filenames= $resptxt =~
455 m/input\s+type="hidden"\s+name="filename"\s+value=\"([_.0-9a-z]+)\"/ig;
458 my @forcerls= $resptxt =~
459 m/input\s+type="hidden"\s+name="forcereload"\s+value=\"([1-9]\d+)\"/ig;
462 my $filename= $filenames[0];
463 my $forcerl= $forcerls[0];
465 $ocean= ucfirst lc $ocean;
466 my @oceanids= $resptxt =~
467 m/\<option value\=\"(\d+)\"\>$ocean\<\/option\>/;
468 @oceanids==1 or die "@oceanids ?";
472 m/^islands\[\d+\]\[\d+\]\=new\s+option\(\"(.*)\"\,(\d+)\)\s*$/mig
474 next unless $1 eq $island;
477 defined $islandid or die;
479 die "@filenames ?" if grep { $_ ne $filename } @filenames;
480 die "@forcerls ?" if grep { $_ ne $forcerl } @forcerls;
485 print STDERR "Setting ocean and island...\n";
487 my $siurl= ($url . "?action=setisland".
488 "&filename=$filename".
489 "&forcereload=$forcerl".
490 "&ocean=$oceanids[0]".
491 "&island=$islandid");
492 $resp= $ua->get($siurl);
494 die $resp->status_line unless $resp->is_success;
496 $resptxt= $resp->content();
497 save_upload_html('2', $resptxt);
499 die unless $resptxt =~ m/your uploaded data has been processed/i;
500 die unless $resptxt =~ m/your data has been integrated into the database/i;
502 $resptxt =~ s/\<a href=\"about:\w+\"\>[^<>]+\<\/a\>//g;
503 save_upload_html('3', $resptxt);
505 print "\n" or die $!;
506 system('w3m -T text/html -dump < _upload-3.html');
508 print "\n" or die $!;
514 close(STDOUT) or die $!;