1 # This is part of ypp-sc-tools, a set of third-party tools for assisting
2 # players of Yohoho Puzzle Pirates.
4 # Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
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.
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.
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/>.
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.
25 use HTTP::Request::Common ();
33 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
36 @EXPORT = qw(&parse_info_clientside &fetch_with_rsync
37 &parse_info_serverside &parse_info_serverside_ocean
38 %oceans %commods %clients %routes %route_mysteries
39 %vessels %shotname2damage
40 &parse_pctb_commodmap %pctb_commodmap @pctb_commodmap
41 &get_our_version &check_tsv_line
42 &pipethrough_prep &pipethrough_run
43 &pipethrough_run_along &pipethrough_run_finish
45 &cgipostform &yarrgpostform &cgi_get_caller
46 &set_ctype_utf8 $masterinfoversion);
52 our $masterinfoversion= 2; # version we understand
54 our %oceans; # eg $oceans{'Midnight'}{'Ruby'}{'Eta Island'}= $sources;
55 our %clients; # eg $clients{'ypp-sc-tools'}= [ qw(last-page) ];
56 our %routes; # eg $routes{'Midnight'}{'Orca'}{'Tinga'}= $sources NB abbrevs!
57 our %route_mysteries; # eg $route_mysteries{'Midnight'}{'Norse'}= 3
58 our %vessels; # eg $vessels{'War Brig'}{Shot}='medium'
59 # $vessels{'War Brig'}{Volume}= 81000
60 # $vessels{'War Brig'}{Mass}= 54000
61 our %shotname2damage; # eg $shotname2damage{'medium'}= 3;
63 # 's' = Special Circumstances; 'l' = local ; B = with Bleach
66 # eg $commods{'Fine black cloth'}{Srcs}= $sources;
67 # eg $commods{'Fine black cloth'}{Mass}= 700 [g]
68 # eg $commods{'Fine black cloth'}{Volume}= 1000 [ml]
70 our (%pctb_commodmap,@pctb_commodmap);
72 my %colours; # eg $colours{'c'}{'black'}= $sources
73 my (@rawcm, @nocm); # eg $rawcm[0]='fine rum'; $rawcm[1]='fine %c cloth'
76 # when extending the format of source-info in a non-backward
77 # compatible way, be sure to update update-master-info too.
79 sub parse_info1 ($$) {
81 my $mm= new IO::File $mmfn, 'r' or die "$mmfn $!";
89 @ctx= (sub { $colours{$colourkind}{lc $_} .= $src; });
90 } elsif (m/^commods$/) {
91 @ctx= (sub { push @rawcm, lc $_; });
92 } elsif (m/^nocommods$/) {
93 @ctx= (sub { push @nocm, lc $_; });
94 } elsif (m/^ocean (\w+)$/) {
96 keys %{ $oceans{$ocean} };
98 $ocean or die; # ref to $ocean needed to work
101 keys %{ $oceans{$ocean}{$arch} };
103 $oceans{$ocean}{$arch}{$_} .= $src;
106 } elsif (m/^vessels$/) {
108 m/^ ([A-Z][a-z\ ]+[a-z]) \t\s*
109 (small|medium|large) \t\s*
119 } elsif (m/^shot$/) {
121 m/^ ([a-z]+) \s+ (\d+) $/x or die;
122 $shotname2damage{$1}= $2;
124 } elsif (m/^routes (\w+)$/) {
127 m/^(\S[^\t]*\S),\s*(\S[^\t]*\S),\s*([1-9][0-9]{0,2})$/ or die;
128 $routes{$ocean}{$1}{$2}= $3;
130 } elsif (m/^client (\S+.*\S)$/) {
132 $clients{$client}= [ ];
135 push @{ $clients{$client} }, $bug;
138 my $indent= length $&;
139 die "wrong indent $indent" unless defined $ctx[$indent-1];
140 &{ $ctx[$indent-1] }();
145 $mm->error and die $!;
148 #print Dumper(\%oceans);
149 #print Dumper(\@rawcm);
156 if ($s !~ m/\%(\w+)/) {
157 my ($name, $props) = $s =~
158 /^(\S[^\t]*\S)(?:\t+(\S[^\t]*\S))?$/
159 or die "bad commodspec $s";
160 return if grep { $name eq $_ } @nocm;
161 my $ucname= ucfirst $name;
162 $commods{$ucname}{Srcs} .= $ss;
163 my $c= $commods{$ucname};
165 foreach my $prop (defined $props ? split /\s+/, $props : ()) {
166 if ($prop =~ m/^([1-9]\d*)(k?)g$/) {
167 $c->{Mass}= $1 * ($2 ? 1000 : 1);
168 } elsif ($prop =~m/^([1-9]\d*)l$/) {
169 $c->{Volume}= $1 * 1000;
171 die "unknown property $prop for $ucname";
176 die "unknown $&" unless defined $colours{$1};
177 my ($lhs,$pctlet,$rhs)= ($`,$1,$');
178 foreach my $c (keys %{ $colours{$pctlet} }) {
179 &$ca($lhs.$c.$rhs, $ss .'%'. $colours{$pctlet}{$c});
182 foreach (@rawcm) { &$ca($_,$src); }
184 foreach my $on (keys %routes) {
185 my $routes= $routes{$on};
186 my $ocean= $oceans{$on};
187 die unless defined $ocean;
190 foreach my $an (sort keys %$ocean) {
191 my $arch= $ocean->{$an};
192 push @allislands, sort keys %$arch;
194 parse_info_maproutes($on, \@allislands, $routes);
195 foreach my $route (values %$routes) {
196 parse_info_maproutes($on, \@allislands, $route);
201 sub parse_info_clientside () {
202 my $yarrg= $ENV{'YPPSC_YARRG_DICT_UPDATE'};
203 return unless $yarrg;
204 my $master= fetch_with_rsync("info-$masterinfoversion");
205 parse_info1($master,'s');
206 my $local= '_local-info.txt';
208 parse_info1($local,'s');
210 die "$local $!" unless $! == &ENOENT;
214 sub fetch_with_rsync ($) {
217 my $rsync= $ENV{'YPPSC_YARRG_RSYNC'};
218 $rsync= 'rsync' if !defined $rsync;
220 my $local= "_master-$stem.txt";
221 my $src= $ENV{'YPPSC_YARRG_DICT_UPDATE'};
223 my $remote= "$src/master-$stem.txt";
224 $!=0; system 'rsync','-Lt','--',$remote,$local;
225 die "$? $!" if $! or $?;
230 sub parse_info_maproutes ($$$) {
231 my ($on, $allislands, $routemap) = @_;;
232 foreach my $k (sort keys %$routemap) {
233 my @ok= grep { index($_,$k) >= 0 } @$allislands;
234 die "ambiguous $k" if @ok>1;
236 $route_mysteries{$on}{$k}++;
237 delete $routemap->{$k};
238 } elsif ($ok[0] ne $k) {
239 $routemap->{$ok[0]}= $routemap->{$k};
240 delete $routemap->{$k};
245 sub parse_info_serverside () {
246 parse_info1('source-info.txt','s');
248 sub parse_info_serverside_ocean ($) {
249 my ($oceanname) = @_;
250 die "unknown ocean $oceanname ?" unless exists $oceans{$oceanname};
251 parse_info1("_ocean-".(lc $oceanname).".txt",'s');
254 sub parse_pctb_commodmap () {
255 undef %pctb_commodmap;
256 foreach my $commod (keys %commods) { $commods{$commod}{Srcs} =~ s/b//; }
258 my $c= new IO::File '_commodmap.tsv';
259 if (!$c) { $!==&ENOENT or die $!; return 0; }
262 m/^(\S.*\S)\t(\d+)\n$/ or die "$_";
263 die if defined $pctb_commodmap{$1}; $pctb_commodmap{$1}= $2;
264 die if defined $pctb_commodmap[$2]; $pctb_commodmap[$2]= $1;
265 $commods{$1}{Srcs} .= 'b';
267 $c->error and die $!;
272 sub get_our_version ($$) {
273 my ($aref,$prefix) = @_;
274 $aref->{"${prefix}name"}= 'ypp-sc-tools yarrg';
275 $aref->{"${prefix}fixes"}= 'lastpage checkpager';
277 my $version= `git-describe --tags HEAD || echo 0unknown`; $? and die $?;
279 $aref->{"${prefix}version"}= $version;
281 # clientname "ypp-sc-tools"
282 # clientversion 2.1-g2e06a26 [from git-describe --tags HEAD]
283 # clientfixes "lastpage" [space separated list]
286 sub pipethrough_prep () {
287 my $tf= IO::File::new_tmpfile() or die $!;
291 sub pipethrough_run_along ($$$@) {
292 my ($tf, $childprep, $cmd, @a) = @_;
293 $tf->error and die $!;
294 $tf->flush or die $!;
295 $tf->seek(0,0) or die $!;
296 my $fh= new IO::File;
297 my $child= $fh->open("-|"); defined $child or die $!;
299 open STDIN, "<&", $tf;
300 &$childprep() if defined $childprep;
301 exec $cmd @a; die "@a $!";
305 sub pipethrough_run_finish ($$) {
307 $fh->error and die $!;
308 close $fh or die "$what $! $?"; die $? if $?;
311 sub pipethrough_run ($$$@) {
312 my ($tf, $childprep, $cmd, @a) = @_;
313 my $pt= pipethrough_run_along($tf,$childprep,$cmd,@a);
315 { undef $/; $!=0; $r= <$pt>; }
316 defined $r or die $!;
317 pipethrough_run_finish($pt, "@a");
320 sub pipethrough_run_gzip ($) {
321 pipethrough_run($_[0],undef,'gzip','gzip');
324 sub yarrgpostform ($$) {
325 my ($ua, $form) = @_;
326 my $dest= $ENV{'YPPSC_YARRG_YARRG'};
327 get_our_version($form, 'client');
328 die unless $dest =~ m,/$,;
329 return cgipostform($ua, "${dest}commod-update-receiver", $form);
332 sub cgipostform ($$$) {
333 my ($ua, $url, $form) = @_;
334 my $req= HTTP::Request::Common::POST($url,
336 Content_Type => 'form-data');
337 if ($url =~ m,^\.?/,) {
338 my $tf= pipethrough_prep();
339 print $tf $req->content() or die $!;
340 #print STDERR "[[[",$req->content(),"]]]";
341 my $out= pipethrough_run($tf, sub {
342 $ENV{'REQUEST_METHOD'}= 'POST';
343 $ENV{'QUERY_STRING'}= '';
344 $ENV{'PATH_TRANSLATED'}= $url;
345 $ENV{'PATH_INFO'}= '';
346 $ENV{'HTTP_HOST'}= 'localhost';
347 $ENV{'REMOTE_ADDR'}= '127.0.0.1';
348 $ENV{'GATEWAY_INTERFACE'}= 'CGI/1.1';
349 $ENV{'DOCUMENT_ROOT'}= '.';
350 $ENV{'SCRIPT_FILENAME'}= $url;
351 $ENV{'SCRIPT_NAME'}= $url;
352 $ENV{'HTTP_USER_AGENT'}= 'Commods.pm local test';
354 foreach my $f (qw(Content_Length Content_Type)) {
355 $ENV{uc $f}= $req->header($f);
357 #system 'printenv >&2';
360 $out =~ m,^Content-Type: text/plain.*\n\n, or die "$out ?";
363 my $resp= $ua->request($req);
364 die $resp->status_line."\n".$resp->content."\n "
365 unless $resp->is_success;
366 return $resp->content();
372 sub check_tsv_line ($$) {
373 my ($l, $bad_data_callback) = @_;
374 my $bad_data= sub { &$bad_data_callback("bad data: line $.: $_[0]"); };
376 chomp($l) or &$bad_data('missing end-of-line');
378 $l !~ m/\P{IsPrint}/ or &$bad_data('nonprinting char(s)');
379 my @v= split /\t/, $l, -1;
380 @v==6 or &$bad_data('wrong number of fields');
381 my ($commod,$stall) = @v;
384 defined $commods{$commod} or
385 &$bad_data("unknown commodity \`$commod'");
387 $stall =~ m/^\p{IsUpper}|^[0-9]/ or &$bad_data("stall not capitalised");
388 !exists $check_tsv_done{$commod,$stall} or &$bad_data("repeated data");
389 $check_tsv_done{$commod,$stall}= 1;
390 foreach my $i (2..5) {
392 $f =~ m/^(|0|[1-9][0-9]{0,5}|\>1000)$/ or &$bad_data("bad field $i");
393 ($i % 2) or ($f !~ m/\>/) or &$bad_data("> in field $i price");
396 foreach my $i (2,4) {
397 &$bad_data("price with no qty or vice versa (field $i)")
398 if length($v[$i]) xor length($v[$i+1]);
400 length($v[2]) or length($v[4]) or
401 &$bad_data("commodity entry with no buy or sell offer");
406 sub cgi_get_caller () {
407 my $caller= $ENV{'REMOTE_ADDR'};
408 $caller= 'LOCAL' unless defined $caller;
410 my $fwdf= $ENV{'HTTP_X_FORWARDED_FOR'};
413 $fwdf =~ s/[^0-9.,]/?/g;
419 sub set_ctype_utf8 () {
420 setlocale(LC_CTYPE, "en.UTF-8");