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));
36 # $commod{'Hemp'}{Buy|Sell}{'stall'}{Stall}
37 # $commod{'Hemp'}{Buy|Sell}{'stall'}{Price}
38 # $commod{'Hemp'}{Buy|Sell}{'stall'}{Qty}
39 # $commod{'Hemp'}{Hold}
42 our ($commod,$stall,%commod);
44 @ARGV==1 or die "You probably don't want to run this program directly.\n";
45 our ($mode) = shift @ARGV;
47 # ./yppsc-commod-processor tsv <t |less -x40,80,90,100,110
52 my ($price,$qty) = @v[$c..$c+1];
53 return if !length($price) && !length($qty);
54 die "$_ ?" unless length($price) && length($qty);
55 $commod{$commod}{$bs}{$stall}= {
65 #print STDERR "[".join("|",@v)."]\n";
66 ($commod,$stall) = @v;
69 $commod{$commod}{Hold}= $v[6]+0 if @v>6;
75 my ($commod,$bs,$sortmul) = @_;
76 my $ary= $current->{$bs};
79 foreach my $stall (sort {
80 $sortmul * ($ary->{$a}{Price} <=> $ary->{$b}{Price});
82 push @$r, $ary->{$stall};
87 sub bs_p_bestprice ($) {
90 printf("| %-25.25s %4d", $l->[0]{Stall}, $l->[0]{Price}) or die $!;
92 printf("| %25s %4s","","") or die $!;
96 sub main__arbitrage () {
98 foreach $commod (sort keys %commod) {
99 $current= $commod{$commod};
100 my @buys= @{ bs_p($commod,Buy, -1) };
101 my @sells= @{ bs_p($commod,Sell,+1) };
107 #print Dumper($commod,\@buys,\@sells);
110 my $pricediff= $buys[0]{Price} - $sells[0]{Price};
111 last unless $pricediff > 0;
113 sub arb_check_qty (\@) {
115 my $vqty= $verbs->[0]{Qty};
116 return if $vqty =~ m/^\>/;
117 $qty= $vqty if $qty > $vqty;
119 my $verb= shift @$verbs;
121 arb_check_qty(@buys);
122 arb_check_qty(@sells);
124 my $tprofit= $qty*$pricediff;
129 sprintf("%-13.13s| %-19.19s %4d| %-19.19s %4d|%3d x%3d =%3d\n",
131 $sells[0]{Stall},$sells[0]{Price},
132 $buys[0]{Stall},$buys[0]{Price},
133 $qty, $pricediff, $tprofit);
134 sub arb_subtract_qty (\@) {
136 my $verb= shift @$verbs;
137 my $vqty= $verb->{Qty};
140 Stall => $verb->{Stall},
141 Price => $verb->{Price},
145 arb_subtract_qty(@buys);
146 arb_subtract_qty(@sells);
150 sprintf("%-13.13s| %19s %4s| %19s %4s|%3d %4d\n",
151 $commod, '','', '','', $cqty, $profit);
152 push @arbs, { Profit => $profit, Info => $info };
157 print "No arbitrage opportunities.\n" or die $!;
161 =============+=========================+=========================+=============
164 print <<END or die $!;
166 commodity | seller price| buyer price| qty ea prof
170 foreach my $arb (sort {
171 $b->{Profit} <=> $a->{Profit};
173 print $div,$arb->{Info} or die $1;
175 -------------+-------------------------+-------------------------+-------------
177 $allprofit += $arb->{Profit};
179 print $bigdiv or die $!;
180 printf("%-13.13s %19s %4s %19s %4s %-5s %7d\n",
181 '', '','', '','', 'TOTAL', $allprofit)
185 sub main__bestprices () {
186 foreach $commod (sort keys %commod) {
187 $current= $commod{$commod};
188 my $buys= bs_p($commod,Buy, -1);
189 my $sells= bs_p($commod,Sell,+1);
190 printf("%-15.15s", $commod) or die $!;
191 bs_p_bestprice($buys);
192 bs_p_bestprice($sells);
193 print("\n") or die $!;
199 if (exists $current->{$bs}{$stall}) {
200 my $si= $current->{$bs}{$stall};
201 printf($f "\t%d\t%s", $si->{Price}, $si->{Qty}) or die $!;
203 printf($f "\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($f "%s\t%s", $commod, $stall) or die $!;
218 print($f "\n") or die $!;
221 $f->error and die $!;
230 our ($pctb) = $ENV{'YPPSC_YARRG_PCTB'};
232 our ($ua)= http_useragent("commod-results-processor $mode");
234 sub refresh_commodmap() {
237 my $resp= $ua->get("$pctb/commodmap.php?version=2");
238 die $resp->status_line unless $resp->is_success;
244 my $o= new IO::File "_commodmap.tsv.tmp",'w' or die $!;
245 undef %pctb_commodmap;
247 my $xp= new XML::Parser
252 #print STDERR "START [$_] intag=$intag icm=$incommodmap\n";
253 if (m/^commodmap$/i) {
256 } elsif (m/^(?:name|index)$/i) {
258 $intag=lc($_) if $incommodmap;
259 #print STDERR "START RECOGNISED $intag icm=$incommodmap\n";
261 #print STDERR "START UNRECOGNISED\n";
266 #print STDERR "END [$_] intag=$intag icm=$incommodmap\n";
267 if (m/^commodmap$/i) {
269 die unless exists $got{'name'};
270 die unless exists $got{'index'};
271 die unless $got{'index'} =~ m/^\s*([1-9]\d{0,3})\s*$/;
274 s/^\s+//; s/\s+$//; s/\n/ /g; s/\s+/ /;
275 die "$_ ?" if exists $pctb_commodmap{$_};
276 $pctb_commodmap{$_}= $index;
277 print $o "$_\t$index\n" or die $!;
278 } elsif (lc $_ eq $intag) {
279 $got{$intag}= $cdata;
283 #print STDERR "CHAR [$_[1]] intag=$intag icm=$incommodmap\n";
287 my $content= $resp->content;
289 # print STDERR "[[[$content]]]\n";
290 $xp->parse($content);
292 rename "_commodmap.tsv.tmp","_commodmap.tsv" or die $!;
297 sub read_newcommods ($) {
299 if (!open NC, "< $file") {
300 $!==&ENOENT or die $!;
304 chomp; s/^\s*//; s/\s+$//;
309 NC->error and die $!;
313 sub refresh_newcommods() {
314 my $master= fetch_with_rsync('newcommods');
315 read_newcommods($master);
316 read_newcommods('_local-newcommods.txt');
319 our (%stallmap, @stallmap);
322 my ($bs,$sortmul) = @_;
327 sort { $pctb_commodmap{$a} <=> $pctb_commodmap{$b} }
328 grep { exists $pctb_commodmap{$_} }
331 #print STDERR "COMMOD $commod\n";
332 $current= $commod{$commod};
333 my $l= bs_p($commod,$bs,$sortmul);
335 #print STDERR "COMMOD $commod has ".scalar(@$l)."\n";
336 $o .= writeint($pctb_commodmap{$commod});
337 $o .= writeint(scalar @$l);
338 foreach my $cs (@$l) {
339 $stall= $cs->{Stall};
340 my $stallix= $stallmap{$stall};
341 if (!defined $stallix) {
342 push @stallmap, $stall;
343 $stallmap{$stall}= $stallix= @stallmap;
344 #print STDERR "STALL DEF $stallix $stall\n";
348 $o .= writeint($stallix, $cs->{Price}, $qty+0);
352 #print STDERR "COMMOD $commod COUNT WAS $count\n";
357 sub writeint { return pack 'v*', @_; }
359 our (%stalltypetoabbrevmap)= qw(
369 sub genmarketdata () {
370 our $version= '005b';
372 parse_pctb_commodmap();
373 my @missing= grep { !exists $pctb_commodmap{$_} } keys %commod;
376 refresh_newcommods();
378 foreach $commod (sort keys %commod) {
379 next if exists $pctb_commodmap{$commod};
380 if (exists $newcommods{$commod}) {
381 printf STDERR "Ignoring new commodity \`%s'!\n", $commod;
383 printf STDERR "Unknown commodity \`%s'!\n", $commod;
387 die "$missing unknown commoditi(es).".
388 " See README (search for \`newcommods').\n"
393 $ob .= bs_gen_md(Buy, -1);
394 $ob .= bs_gen_md(Sell,+1);
396 my $ot= sprintf("$version\n".
399 foreach $stall (@stallmap) {
401 if ($st =~ m/^(\S+)\'s (\S+) Stall$/) {
402 my $stkind= $stalltypetoabbrevmap{$2};
403 if (defined $stkind) {
406 warn "unknown stall type $2 in $st\n";
414 sub main__genmarketdata () {
415 my $o= genmarketdata();
419 sub save_upload_html ($$) {
420 my ($which, $resptxt) = @_;
421 open R, ">./_upload-$which.html" or die $!;
422 print R $resptxt or die $!;
428 my $tf= pipethrough_prep();
429 print $tf $raw or die $!;
430 return pipethrough_run($tf,undef,'gzip','gzip');
433 sub main__uploadyarrg () {
436 parse_info_clientside();
438 $o{'ocean'}= $ENV{'YPPSC_OCEAN'} or die;
439 $o{'island'}= $ENV{'YPPSC_ISLAND'} or die;
440 $o{'timestamp'}= $ENV{'YPPSC_DATA_TIMESTAMP'} or die;
442 my $tf= pipethrough_prep();
444 my $oz= pipethrough_run_gzip($tf);
445 $o{'data'}= [ undef, 'deduped.tsv.gz',
446 Content_Type => 'application/octet-stream',
449 my $respcontent= yarrgpostform($ua, \%o);
450 $respcontent =~ m/^OK\b/ or die "$respcontent ?";
451 $respcontent =~ s/^/ /mg;
452 print $respcontent,"\n";
455 sub main__uploadpctb () {
456 my $ocean= $ENV{'YPPSC_OCEAN'}; die unless $ocean;
457 my $island= $ENV{'YPPSC_ISLAND'}; die unless $island;
459 my $o= genmarketdata();
461 my $url= "$pctb/upload.php";
463 'marketdata' => [ undef, "marketdata.gz",
464 Content_Type => 'application/gzip',
469 print STDERR "Uploading data to $pctb...\n";
471 my $resp= $ua->post("$url", Content => $content,
472 Content_Type => 'form-data');
473 die $resp->status_line unless $resp->is_success;
475 my $resptxt= $resp->content();
476 save_upload_html('1', $resptxt);
478 open R, ">./_upload-1.html" or die $!;
479 print R $resptxt or die $!;
482 my @filenames= $resptxt =~
483 m/input\s+type="hidden"\s+name="filename"\s+value=\"([_.0-9a-z]+)\"/ig;
486 my @forcerls= $resptxt =~
487 m/input\s+type="hidden"\s+name="forcereload"\s+value=\"([1-9]\d+)\"/ig;
490 my $filename= $filenames[0];
491 my $forcerl= $forcerls[0];
493 $ocean= ucfirst lc $ocean;
494 my @oceanids= $resptxt =~
495 m/\<option value\=\"(\d+)\"\>$ocean\<\/option\>/;
496 @oceanids==1 or die "@oceanids ?";
500 m/^islands\[\d+\]\[\d+\]\=new\s+option\(\"(.*)\"\,(\d+)\)\s*$/mig
502 next unless $1 eq $island;
505 defined $islandid or die;
507 die "@filenames ?" if grep { $_ ne $filename } @filenames;
508 die "@forcerls ?" if grep { $_ ne $forcerl } @forcerls;
513 print STDERR "Setting ocean and island...\n";
515 my $siurl= ($url . "?action=setisland".
516 "&filename=$filename".
517 "&forcereload=$forcerl".
518 "&ocean=$oceanids[0]".
519 "&island=$islandid");
520 $resp= $ua->get($siurl);
522 die $resp->status_line unless $resp->is_success;
524 $resptxt= $resp->content();
525 save_upload_html('2', $resptxt);
527 die unless $resptxt =~ m/your uploaded data has been processed/i;
528 die unless $resptxt =~ m/your data has been integrated into the database/i;
530 $resptxt =~ s/\<a href=\"about:\w+\"\>[^<>]+\<\/a\>//g;
531 save_upload_html('3', $resptxt);
533 print "\n" or die $!;
534 system('w3m -T text/html -dump < _upload-3.html');
536 print "\n" or die $!;
542 close(STDOUT) or die $!;