chiark / gitweb /
schema creator in Perl
[ypp-sc-tools.main.git] / pctb / Commods.pm
1 # This is part of ypp-sc-tools, a set of third-party tools for assisting
2 # players of Yohoho Puzzle Pirates.
3 #
4 # Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 #
19 # Yohoho and Puzzle Pirates are probably trademarks of Three Rings and
20 # are used without permission.  This program is not endorsed or
21 # sponsored by Three Rings.
22
23 package Commods;
24 use IO::File;
25 use HTTP::Request::Common ();
26
27 use strict;
28 use warnings;
29
30 BEGIN {
31     use Exporter ();
32     our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
33     $VERSION     = 1.00;
34     @ISA         = qw(Exporter);
35     @EXPORT      = qw(&parse_masters %oceans %commods %clients
36                       &parse_pctb_commodmap %pctb_commodmap @pctb_commodmap
37                       &get_our_version &check_tsv_line
38                       &pipethrough_prep &pipethrough_run
39                       &pipethrough_run_along &pipethrough_run_finish
40                       &pipethrough_run_gzip
41                       &cgipostform);
42     %EXPORT_TAGS = ( );
43
44     @EXPORT_OK   = qw();
45 }
46
47 our %oceans; # eg $oceans{'Midnight'}{'Ruby'}{'Eta Island'}= $sources;
48 our %commods; # eg $commods{'Fine black cloth'}= $sources;
49 our %clients; # eg $clients{'ypp-sc-tools'}= [ qw(last-page) ];
50 # $sources = 's[l]b';
51 #       's' = Special Circumstances; 'l' = local ; B = with Bleach
52
53 our (%pctb_commodmap,@pctb_commodmap);
54
55 my %colours; # eg $colours{'c'}{'black'}= $sources
56 my @rawcm; # eg $rawcm[0]='fine rum'; $rawcm[1]='fine %c cloth'
57
58 sub parse_master_master1 ($$) {
59     my ($mmfn,$src)= @_;
60     my $mm= new IO::File $mmfn, 'r' or die "$mmfn $!";
61     my @ctx= ();
62     while (<$mm>) {
63         next if m/^\s*\#/;
64         next unless m/\S/;
65         s/\s+$//;
66         if (m/^\%(\w+)$/) {
67             my $colourkind= $1;
68             @ctx= (sub { $colours{$colourkind}{lc $_} .= $src; });
69         } elsif (m/^commods$/) {
70             @ctx= (sub { push @rawcm, lc $_; });
71         } elsif (m/^ocean (\w+)$/) {
72             my $ocean= $1;
73             @ctx= (sub {
74                 $ocean or die; # ref to $ocean needed to work
75                                # around a perl bug
76                 my $arch= $_;
77                 $ctx[1]= sub {
78                     $oceans{$ocean}{$arch}{$_} .= $src;
79                 };
80             });
81         } elsif (m/^client (\S+.*\S)$/) {
82             my $client= $1;
83             $clients{$client}= [ ];
84             @ctx= (sub {
85                 my $bug= $_;
86                 push @{ $clients{$client} }, $bug;
87             });
88         } elsif (s/^ +//) {
89             my $indent= length $&;
90             die "wrong indent $indent" unless defined $ctx[$indent-1];
91             &{ $ctx[$indent-1] }();
92         } else {
93             die "bad syntax";
94         }
95     }
96     $mm->error and die $!;
97     close $mm or die $!;
98
99 #print Dumper(\%oceans);
100 #print Dumper(\@rawcm);
101         
102     %commods= ();
103     my $ca;
104     $ca= sub {
105         my ($s,$ss) = @_;
106 #print "ca($s)\n";
107         if ($s !~ m/\%(\w+)/) { $commods{ucfirst $s} .= $ss; return; }
108         die "unknown $&" unless defined $colours{$1};
109         foreach my $c (keys %{ $colours{$1} }) {
110             &$ca($`.$c.$', $ss .'%'. $colours{$1}{$c});
111         }
112     };
113     foreach (@rawcm) { &$ca($_,$src); }
114 }
115
116 sub parse_masters () {
117     parse_master_master1('master-master.txt','s');
118 }
119
120 sub parse_pctb_commodmap () {
121     undef %pctb_commodmap;
122     foreach my $commod (keys %commods) { $commods{$commod} =~ s/b//; }
123
124     my $c= new IO::File '_commodmap.tsv' or die $!;
125     if (!$c) { $!==&ENOENT or die $!; return 0; }
126
127     while (<$c>) {
128         m/^(\S.*\S)\t(\d+)\n$/ or die "$_";
129         die if defined $pctb_commodmap{$1};  $pctb_commodmap{$1}= $2;
130         die if defined $pctb_commodmap[$2];  $pctb_commodmap[$2]= $1;
131         $commods{$1} .= 'b';
132     }
133     $c->error and die $!;
134     close $c or die $!;
135     return 1;
136 }
137
138 sub get_our_version ($$) {
139     my ($aref,$prefix) = @_;
140     $aref->{"${prefix}name"}= 'ypp-sc-tools yarrg';
141     $aref->{"${prefix}fixes"}= 'lastpage';
142
143     my $version= `git-describe --tags HEAD`; $? and die $?;
144     chomp($version);
145     $aref->{"${prefix}version"}= $version;
146     return $aref;
147 }
148
149 sub pipethrough_prep () {
150     my $tf= IO::File::new_tmpfile() or die $!;
151     return $tf;
152 }
153
154 sub pipethrough_run_along ($$$@) {
155     my ($tf, $childprep, $cmd, @a) = @_;
156     $tf->flush or die $!;
157     $tf->seek(0,0) or die $!;
158     my $fh= new IO::File;
159     my $child= $fh->open("-|"); defined $child or die $!;
160     if (!$child) {
161         open STDIN, "<&", $tf;
162         &$childprep() if defined $childprep;
163         exec $cmd @a; die $!;
164     }
165     return $fh;
166 }
167 sub pipethrough_run_finish ($) {
168     my ($fh)= @_;
169     $fh->error and die $!;
170     close $fh or die "$! $?";  die $? if $?;
171 }
172
173 sub pipethrough_run ($$$@) {
174     my ($tf, $childprep, $cmd, @a) = @_;
175     my $pt= pipethrough_run_along($tf,$childprep,$cmd,@a);
176     my $r;
177     { undef $/; $!=0; $r= <$pt>; }
178     defined $r or die $!;
179     pipethrough_run_finish($pt);
180     return $r;
181 }
182 sub pipethrough_run_gzip ($) {
183     pipethrough_run($_[0],undef,'gzip','gzip');
184 }
185
186 sub cgipostform ($$$) {
187     my ($ua, $url, $form) = @_;
188     my $req= HTTP::Request::Common::POST($url,
189                                          Content => $form,
190                                          Content_Type => 'form-data');
191     if ($url =~ m,^\.?/,) {
192         my $tf= pipethrough_prep();
193         print $tf $req->content() or die $!;
194 #print STDERR "[[[",$req->content(),"]]]";
195         my $out= pipethrough_run($tf, sub {
196             $ENV{'REQUEST_METHOD'}= 'POST';
197             $ENV{'QUERY_STRING'}= '';
198             $ENV{'PATH_TRANSLATED'}= $url;
199             $ENV{'PATH_INFO'}= '';
200             $ENV{'HTTP_HOST'}= 'localhost';
201             $ENV{'REMOTE_ADDR'}= '127.0.0.1';
202             $ENV{'GATEWAY_INTERFACE'}= 'CGI/1.1';
203             $ENV{'DOCUMENT_ROOT'}= '.';
204             $ENV{'SCRIPT_FILENAME'}= $url;
205             $ENV{'SCRIPT_NAME'}= $url;
206             $ENV{'HTTP_USER_AGENT'}= 'Commods.pm local test';
207
208             foreach my $f (qw(Content_Length Content_Type)) {
209                 $ENV{uc $f}= $req->header($f);
210             }
211 #system 'printenv >&2';
212         }, "$url", "$url");
213         $out =~ s/\r\n/\n/g;
214         $out =~ m,^Content-Type: text/plain.*\n\n, or die "$out ?";
215         return $';
216     } else {
217         my $resp= $ua->request($url,$req);
218         die $resp->status_line unless $resp->is_success;
219         return $resp->content();
220     }
221 }
222
223 our %check_tsv_done;
224
225 sub check_tsv_line ($$) {
226     my ($l, $bad_data_callback) = @_;
227     my $bad_data= sub { &$bad_data_callback("bad data: line $.: $_[0]"); };
228     
229     chomp($l) or &$bad_data('missing end-of-line');
230
231     $l !~ m/\P{IsPrint}/ or &$bad_data('nonprinting char(s)');
232     $l !~ m/\\/ or &$bad_data('data contains backslashes');
233     my @v= split /\t/, $l, -1;
234     @v==6 or &$bad_data('wrong number of fields');
235     my ($commod,$stall) = @v;
236
237     !keys %commods or
238         defined $commods{$commod} or
239         &$bad_data("unknown commodity \`$commod'");
240     
241     $stall =~ m/^\p{IsUpper}|^[0-9]/ or &$bad_data("stall not capitalised");
242     !exists $check_tsv_done{$commod,$stall} or &$bad_data("repeated data");
243     $check_tsv_done{$commod,$stall}= 1;
244     foreach my $i (2..5) {
245         my $f= $v[$i];
246         $f =~ m/^(|0|[1-9][0-9]{0,5}|\>1000)$/ or &$bad_data("bad field $i");
247         ($i % 2) or ($f !~ m/\>/) or &$bad_data("> in field $i price");
248     }
249     return @v;
250 }
251
252 1;