chiark / gitweb /
antialiasing text conversion: dictionary-update-receiver and approval seems to work
[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;