chiark / gitweb /
68cf060692d4da167649a57802f66ac961c131cb
[ypp-sc-tools.db-test.git] / pctb / decode-pctb-marketdata
1 #!/usr/bin/perl
2
3 use IO::Handle;
4
5 open CM, "_commodmap.tsv" or die $!;
6
7 $debug=1 if @ARGV;
8
9 while (<CM>) {
10     m/^(\S.*\S)\t(\d+)$/ or die;
11     $commodmap[$2]= $1;
12 }
13 die $! if CM->error;
14
15 %stallkinds= qw(A Apothecary
16                 D Distilling
17                 F Furnishing
18                 I Ironworking
19                 S Shipbuilding
20                 T Tailor
21                 W Weaving);
22
23 sub getline ($) {
24     my ($w)= @_;
25     $!=0; my $l= <STDIN>; die $! unless defined $l;
26     die $! if STDIN->error;
27     die unless chomp $l;
28     print "GOT FOR $w LINE [$l]\n"
29         if $debug;
30     return $l;
31 }
32
33 sub getint ($) {
34     my ($w)= @_;
35     my $b;
36     my $t= tell STDIN; $t>=0 or die $!;
37     my $r= read STDIN,$b,2; die $! if STDIN->error;
38     die unless $r==2;
39     my $v= scalar unpack "v", $b;
40     printf "GOT AT 0x%x INT FOR %s VALUE %d 0x%x\n", $t, $w, $v, $v
41         if $debug;
42     return $v;
43 }
44
45 sub inmap($\@$) {
46     my ($what,$ary,$ix) = @_;
47     my $got= $ary->[$ix];
48     return $got if defined $got;
49     die "$what $ix ?";
50 }
51     
52
53 printf "# Version: \"%s\"\n", getline("version");
54 $nstalls= getline("nstalls")+0;
55
56 while (@stalls < $nstalls) {
57     $_= getline("stall name ".(@stalls+1));
58     if (s/\^([A-Z])$//) {
59         $kind= $1;
60         $sk= $stallkinds{$kind};
61         die "kind $kind in $_ ?" unless defined $sk;
62         $_ .= "'s $sk Stall";
63     }
64     push @stalls, $_;
65 }
66 unshift @stalls, undef;
67
68 $|=1;
69
70 foreach $bs qw(Buy Sell) {
71     $alloffers_want= getint("Buy ncommods");
72     $alloffers_done=0;
73     while ($alloffers_done < $alloffers_want)  {
74         $commodix= getint("Buy $alloffers_done/$alloffers_want commodix");
75         $offers= getint("Buy $commodnum offers");
76         for ($offernum=0; $offernum<$offers; $offernum++) {
77             $stallix= getint("Buy $commodnum $offernum stallix");
78             $price= getint("Buy $commodnum $offernum price");
79             $qty= getint("Buy $commodnum $offernum qty");
80             printf("%s\t%s\t%s",
81                    $bs,
82                    inmap('commod',@commodmap,$commodix),
83                    inmap('stall',@stalls,$stallix)) or die $!;
84             if ($bs eq 'Sell') { print "\t\t" or die $!; }
85             printf("\t%d\t%d", $price, $qty) or die $!;
86             if ($bs eq 'Buy') { print "\t\t" or die $!; }
87             print "\n" or die $!;
88             $alloffers_done++;
89             die if $alloffers_done > $alloffers_want;
90         }
91     }
92 }
93
94 $r= read STDIN,$b,1;
95 STDIN->error and die $!;
96 STDIN->eof or die;
97 $b and die;
98
99
100
101
102 #
103 #
104 #Version number (3 ascii digits) followed
105 #by LF (i.e. 0x0A).  Current version is\
106 # 005
107 #
108 #  0000   30 30 35 0a                                        005.
109 #
110 ## of shops/stalls (in ascii)  + LF
111 #  0000               31 0a                                      1.           
112 #
113 #list of shop names in ascii (without the
114 #possessive part and by specifying the \
115 #shop type in an abbreviated way which is
116 #mentioned below).  The shop names are
117 #separated \ by line feeds (0x0A).  To
118 #specify the shop type append the "^"
119 #character and then \ one of the
120 #following characters depending upon the
121 #shop type:
122 #   Apothecary = A
123 #   Distillery = D
124 #   Furnisher = F
125 #   Iron Monger = I
126 #   Shipyard = S
127 #   Tailor = T
128 #   Weavery = W
129 #
130 #  0000                     4f 6c  64 20 49 72 6f 6e 73 69         Ol d Ironsi
131 #  0010   64 65 73 0a                                        des.              
132 #
133 ## of buy offers (in binary)
134 #  0010               05 00                                 
135 #
136 #commodIndex offerCount                                       buy 0/5
137 #  0010                     0a 00  01 00                      Grog x 1
138 #[shopIndex val amt]                   
139 #  0010                                  01 00 1d 00 75 00    OI 29 qty 75
140 #
141 #commodIndex offerCount                                       buy 1/5
142 #  0020   0b 00 01 00                                         Fine x 1
143 #[shopIndex val amt]... (all in binar\ y)
144 #  0020               01 00 33 00  e9 03                      OI 51 qty 0x3e9
145 #
146 #commodIndex offerCount                                       buy 2/5
147 #  0020                                  0c 00 01 00          Small shot x 1
148 #[shopIndex val amt]
149 #  0020                                              01 00    OI
150 #  0030   0f 00 e9 03                                         15 qty 0x3e9
151 #
152 #commodIndex offerCount                                       buy 3/5
153 #  0030               0d 00 01 00                             Medium shot x 1
154 #[shopIndex val amt]
155 #  0030                            01 00 1c 00 e9 03          OI 28 qty 0x3e9
156 #
157 #commodIndex offerCount                                       buy 4/5
158 #  0030                                              0e 00    Large shot
159 #  0040   01 00                                                          x 1
160 #[shopIndex val amt]
161 #  0040         01 00 26 00 e9 03                             OI 38 0x3e9
162 #
163 #
164 ## of sell offers (in binary)
165 #  0040                            04 00
166 #
167 #commodIndex offerCount                                       sell 0/4
168 #  0040                                  09 00 01 00          Swill x 1
169 #[shopIndex val amt]
170 #  0040                                              01 00    OI 
171 #  0050   17 00 e8 03                                         23 0x3e8
172 #commodIndex offerCount                                       sell 1/4
173 #  0050               0a 00 01 00                             Grog x 1
174 #[shopIndex val amt]
175 #  0050                            01 00 1e 00 73 03          OI 30 qty 0x373
176 #commodIndex offerCount                                       sell 2/4
177 #  0050                                              0c 00    Small shot
178 #  0060   01 00                                               x 1
179 #[shopIndex val amt]
180 #  0060         01 00 10 00 07 00                             OI 16 qty 7
181 #commodIndex offerCount                                       sell 3/4
182 #  0060                            0d 00 01 00                Medium shot x 1
183 #[shopIndex val amt]
184 #  0060                                        01 00 1e 00    OI 30
185 #  0070   02 00                                               qty 2
186 #