X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~yarrgweb/git?p=ypp-sc-tools.web-live.git;a=blobdiff_plain;f=pctb%2Fyppsc-commod-processor;h=0f2545201d9de876f9ee5c1d88ff1458724f15a3;hp=911242c82a5f3a6963e22a7079ce5e2ab86808d4;hb=5b549365060315270a1028473c12407d286f27d4;hpb=79b7d51e0d3b8481ac4caa3439e0bd59d821887d diff --git a/pctb/yppsc-commod-processor b/pctb/yppsc-commod-processor index 911242c..0f25452 100755 --- a/pctb/yppsc-commod-processor +++ b/pctb/yppsc-commod-processor @@ -92,8 +92,6 @@ sub bs_p_bestprice ($) { } } -our $arbitrage_only= 0; - sub main__arbitrage () { my @arbs= (); foreach $commod (sort keys %commod) { @@ -186,8 +184,6 @@ sub main__bestprices () { $current= $commod{$commod}; my $buys= bs_p($commod,Buy, -1); my $sells= bs_p($commod,Sell,+1); - if ($arbitrage_only) { - } printf("%-15.15s", $commod) or die $!; bs_p_bestprice($buys); bs_p_bestprice($sells); @@ -239,7 +235,7 @@ sub load_commodmap() { sub refresh_commodmap() { my $ua= LWP::UserAgent->new; - my $resp= $ua->get("$pctb/commodmap.php"); + my $resp= $ua->get("$pctb/commodmap.php?version=2"); die $resp->status_line unless $resp->is_success; my $cdata=''; @@ -247,27 +243,28 @@ sub refresh_commodmap() { my $intag=''; my %got; my $o= new IO::File "#commodmap#.tsv.new",'w' or die $!; + undef %commodmap; my $xp= new XML::Parser (Handlers => { Start => sub { $_=$_[1]; -print STDERR "START [$_] intag=$intag icm=$incommodmap\n"; +#print STDERR "START [$_] intag=$intag icm=$incommodmap\n"; if (m/^commodmap$/i) { $incommodmap++; undef %got; } elsif (m/^(?:name|index)$/i) { $cdata=''; $intag=lc($_) if $incommodmap; - print STDERR "START RECOGNISED $intag icm=$incommodmap\n"; - } else { - print STDERR "START UNRECOGNISED\n"; +#print STDERR "START RECOGNISED $intag icm=$incommodmap\n"; +# } else { +#print STDERR "START UNRECOGNISED\n"; } }, End => sub { $_=$_[1]; -print STDERR "END [$_] intag=$intag icm=$incommodmap\n"; +#print STDERR "END [$_] intag=$intag icm=$incommodmap\n"; if (m/^commodmap$/i) { $incommodmap--; die unless exists $got{'name'}; @@ -276,7 +273,7 @@ print STDERR "END [$_] intag=$intag icm=$incommodmap\n"; my $index= $1; $_= $got{'name'}; s/^\s+//; s/\s+$//; s/\n/ /g; s/\s+/ /; - die if exists $commodmap{$_}; + die "$_ ?" if exists $commodmap{$_}; $commodmap{$_}= $index; print $o "$_\t$index\n" or die $!; } elsif (lc $_ eq $intag) { @@ -284,25 +281,55 @@ print STDERR "END [$_] intag=$intag icm=$incommodmap\n"; } }, Char => sub { -print STDERR "CHAR [$_[1]] intag=$intag icm=$incommodmap\n"; +#print STDERR "CHAR [$_[1]] intag=$intag icm=$incommodmap\n"; $cdata .= $_[1]; } }) or die; my $content= $resp->content; - # hacks to strip off drivel that seems to have been added! - $content =~ s/^.*\n(\<\?xml)/$1/s; - $content =~ s/\<\/body\>.*//s; - print STDERR "[[[$content]]]\n"; +# print STDERR "[[[$content]]]\n"; $xp->parse($content); close $o or die $!; rename "#commodmap#.tsv.new","#commodmap#.tsv" or die $!; } - -# -#sub bs_gen_md ($) { -# my ($bs) = @_; - + +our (%stallmap, @stallmap); + +sub bs_gen_md ($$) { + my ($bs,$sortmul) = @_; + my $count= 0; + my $o= ''; + + foreach $commod (sort { + $commodmap{$a} <=> $commodmap{$b} + } keys %commod) { +#print STDERR "COMMOD $commod\n"; + $current= $commod{$commod}; + my $l= bs_p($commod,$bs,$sortmul); + next unless @$l; +#print STDERR "COMMOD $commod has ".scalar(@$l)."\n"; + $o .= writeint($commodmap{$commod}); + $o .= writeint(scalar @$l); + foreach my $cs (@$l) { + $stall= $cs->{Stall}; + my $stallix= $stallmap{$stall}; + if (!defined $stallix) { + push @stallmap, $stall; + $stallmap{$stall}= $stallix= @stallmap; +#print STDERR "STALL DEF $stallix $stall\n"; + } + my $qty= $cs->{Qty}; + $qty =~ s/^\>\s*//; + $o .= writeint($stallix, $cs->{Price}, $qty+0); + $count++; + } + } +#print STDERR "COMMOD $commod COUNT WAS $count\n"; + return + writeint($count).$o; +} + +sub writeint { return pack 'v*', @_; } sub main__genmarketdata () { our $version= '005b'; @@ -314,19 +341,24 @@ sub main__genmarketdata () { my $missing=0; foreach $commod (sort keys %commod) { next if exists $commodmap{$commod}; - print STDERR "Unknown commodity \`%s'!\n"; + printf STDERR "Unknown commodity \`%s'!\n", $commod; $missing++; } - die "$missing unknown commodities. OCR failure?\n" + die "$missing unknown commoditi(es). OCR failure?\n" if $missing; } - - # foreach $commod (sort keys %commod) { -# next if - -# bs_gen_md(Buy); -# bs_gen_md(Sell); + my $o=''; + $o .= bs_gen_md(Buy, -1); + $o .= bs_gen_md(Sell,+1); + + printf("$version\n". + "%d\n", + scalar(@stallmap)) + or die $!; + foreach $stall (@stallmap) { print "$stall\n" or die $!; } + + print $o or die$!; }