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