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