X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~yarrgweb/git?a=blobdiff_plain;f=pctb%2Fdecode-pctb-marketdata;fp=pctb%2Fdecode-pctb-marketdata;h=d798aad03a1e8de8bc274e5de9837c5635801848;hb=e888c1dd3476ca49bccf82b93b4a3633587d400d;hp=0000000000000000000000000000000000000000;hpb=6a3c0962283d32bc6e5f6c47c929baf37ddc642f;p=ypp-sc-tools.db-live.git diff --git a/pctb/decode-pctb-marketdata b/pctb/decode-pctb-marketdata new file mode 100755 index 0000000..d798aad --- /dev/null +++ b/pctb/decode-pctb-marketdata @@ -0,0 +1,97 @@ +#!/usr/bin/perl + +use IO::Handle; + +open CM, "#commodmap#.tsv" or die $!; + +$debug=1 if @ARGV; + +while () { + m/^(\S.*\S)\t(\d+)$/ or die; + $commodmap[$2]= $1; +} +die $! if CM->error; + +%stallkinds= qw(A Apothecary + D Distilling + F Furnishing + I Ironworking + S Shipbuilding + T Tailor + W Weaving); + +sub getline ($) { + my ($w)= @_; + $!=0; my $l= ; die $! unless defined $l; + die $! if STDIN->error; + die unless chomp $l; + print "GOT FOR $w LINE [$l]\n" + if $debug; + return $l; +} + +sub getint ($) { + my ($w)= @_; + my $b; + my $t= tell STDIN; $t>=0 or die $!; + my $r= read STDIN,$b,2; die $! if STDIN->error; + die unless $r==2; + my $v= scalar unpack "v", $b; + printf "GOT AT 0x%x INT FOR %s VALUE %d 0x%x\n", $t, $w, $v, $v + if $debug; + return $v; +} + +sub inmap($\@$) { + my ($what,$ary,$ix) = @_; + my $got= $ary->[$ix]; + return $got if defined $got; + die "$what $ix ?"; +} + + +printf "# Version: \"%s\"\n", getline("version"); +$nstalls= getline("nstalls")+0; + +while (@stalls < $nstalls) { + $_= getline("stall name ".(@stalls+1)); + if (s/\^([A-Z])$//) { + $kind= $1; + $sk= $stallkinds{$kind}; + die "kind $kind in $_ ?" unless defined $sk; + $_ .= "'s $sk Stall"; + } + push @stalls, $_; +} +unshift @stalls, undef; + +$|=1; + +foreach $bs qw(Buy Sell) { + $alloffers_want= getint("Buy ncommods"); + $alloffers_done=0; + while ($alloffers_done < $alloffers_want) { + $commodix= getint("Buy $alloffers_done/$alloffers_want commodix"); + $offers= getint("Buy $commodnum offers"); + for ($offernum=0; $offernum<$offers; $offernum++) { + $stallix= getint("Buy $commodnum $offernum stallix"); + $price= getint("Buy $commodnum $offernum price"); + $qty= getint("Buy $commodnum $offernum qty"); + printf("%s\t%s\t%s", + $bs, + inmap('commod',@commodmap,$commodix), + inmap('stall',@stalls,$stallix)) or die $!; + if ($bs eq 'Sell') { print "\t\t" or die $!; } + printf("\t%d\t%d", $price, $qty) or die $!; + if ($bs eq 'Buy') { print "\t\t" or die $!; } + print "\n" or die $!; + $alloffers_done++; + die if $alloffers_done > $alloffers_want; + } + } +} + +$r= read STDIN,$b,1; +STDIN->error and die $!; +STDIN->eof or die; +$b and die;