chiark / gitweb /
appears to make a marketdata file
authorIan Jackson <ian@liberator.relativity.greenend.org.uk>
Mon, 8 Jun 2009 02:00:40 +0000 (03:00 +0100)
committerIan Jackson <ian@liberator.relativity.greenend.org.uk>
Mon, 8 Jun 2009 02:00:40 +0000 (03:00 +0100)
pctb/example [new file with mode: 0644]
pctb/yppsc-commod-processor

diff --git a/pctb/example b/pctb/example
new file mode 100644 (file)
index 0000000..b11651d
--- /dev/null
@@ -0,0 +1,85 @@
+
+
+
+Version number (3 ascii digits) followed
+by LF (i.e. 0x0A).  Current version is\
+ 005
+
+  0000   30 30 35 0a                                        005.
+
+# of shops/stalls (in ascii)  + LF
+  0000               31 0a                                      1.           
+
+list of shop names in ascii (without the
+possessive part and by specifying the \
+shop type in an abbreviated way which is
+mentioned below).  The shop names are
+separated \ by line feeds (0x0A).  To
+specify the shop type append the "^"
+character and then \ one of the
+following characters depending upon the
+shop type:
+   Apothecary = A
+   Distillery = D
+   Furnisher = F
+   Iron Monger = I
+   Shipyard = S
+   Tailor = T
+   Weavery = W
+
+  0000                     4f 6c  64 20 49 72 6f 6e 73 69         Ol d Ironsi
+  0010   64 65 73 0a                                        des.              
+
+# of buy offers (in binary)
+  0010               05 00                                 
+
+commodIndex offerCount                                       buy 0/5
+  0010                     0a 00  01 00                      Grog x 1
+[shopIndex val amt]                   
+  0010                                  01 00 1d 00 75 00    OI 29 qty 75
+
+commodIndex offerCount                                       buy 1/5
+  0020   0b 00 01 00                                         Fine x 1
+[shopIndex val amt]... (all in binar\ y)
+  0020               01 00 33 00  e9 03                      OI 51 qty 0x3e9
+
+commodIndex offerCount                                       buy 2/5
+  0020                                  0c 00 01 00          Small shot x 1
+[shopIndex val amt]
+  0020                                              01 00    OI
+  0030   0f 00 e9 03                                         15 qty 0x3e9
+
+commodIndex offerCount                                       buy 3/5
+  0030               0d 00 01 00                             Medium shot x 1
+[shopIndex val amt]
+  0030                            01 00 1c 00 e9 03          OI 28 qty 0x3e9
+
+commodIndex offerCount                                       buy 4/5
+  0030                                              0e 00    Large shot
+  0040   01 00                                                          x 1
+[shopIndex val amt]
+  0040         01 00 26 00 e9 03                             OI 38 0x3e9
+
+
+# of sell offers (in binary)
+  0040                            04 00
+
+commodIndex offerCount                                       sell 0/4
+  0040                                  09 00 01 00          Swill x 1
+[shopIndex val amt]
+  0040                                              01 00    OI 
+  0050   17 00 e8 03                                         23 0x3e8
+commodIndex offerCount                                       sell 1/4
+  0050               0a 00 01 00                             Grog x 1
+[shopIndex val amt]
+  0050                            01 00 1e 00 73 03          OI 30 qty 0x373
+commodIndex offerCount                                       sell 2/4
+  0050                                              0c 00    Small shot
+  0060   01 00                                               x 1
+[shopIndex val amt]
+  0060         01 00 10 00 07 00                             OI 16 qty 7
+commodIndex offerCount                                       sell 3/4
+  0060                            0d 00 01 00                Medium shot x 1
+[shopIndex val amt]
+  0060                                        01 00 1e 00    OI 30
+  0070   02 00                                               qty 2
index 911242c82a5f3a6963e22a7079ce5e2ab86808d4..2083be17221c1037db72151526b999b9b310ea88 100755 (executable)
@@ -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);
@@ -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,7 +281,7 @@ 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;
@@ -293,16 +290,48 @@ print STDERR "CHAR [$_[1]] intag=$intag icm=$incommodmap\n";
     # 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 @$l\n";
+
+       $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;
+           }
+           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 +343,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$!;
 }