chiark / gitweb /
swap require_rectangle_r and require_rectangle so we can have FOR_P_RECT
[ypp-sc-tools.main.git] / pctb / database-info-fetch
1 #!/usr/bin/perl -w
2
3 # helper program for determining pixmap resolution options
4
5 # This is part of ypp-sc-tools, a set of third-party tools for assisting
6 # players of Yohoho Puzzle Pirates.
7 #
8 # Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
9 #
10 # This program is free software: you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation, either version 3 of the License, or
13 # (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 #
23 # Yohoho and Puzzle Pirates are probably trademarks of Three Rings and
24 # are used without permission.  This program is not endorsed or
25 # sponsored by Three Rings.
26
27 use strict (qw(vars));
28 use LWP::UserAgent;
29 use JSON;
30 use Data::Dumper;
31
32 @ARGV==1 or die "You probably don't want to run this program directly.\n";
33 our ($which) = shift @ARGV;
34
35 $which =~ s/\W//g;
36
37 our ($pctb) = $ENV{'YPPSC_PCTB_PCTB'};  die unless $pctb;
38 our ($ua)= LWP::UserAgent->new;
39 our $jsonresp;
40
41 sub jparsetable ($$) {
42     my ($jobj,$wh) = @_;
43     my $jtab= $jobj->{$wh};
44     die "$jsonresp $wh ?" unless defined $jtab;
45     my $cns= $jtab->{'colNames'};  die "$jsonresp $wh ?" unless defined $cns;
46     my $ad= $jtab->{'arrayData'};  die "$jsonresp $wh ?" unless defined $ad;
47     my @o=();
48     foreach my $ai (@$ad) {
49         @$ai == @$cns or die "$jsonresp $wh ".scalar(@o)."?";
50         my $v= { };
51         for (my $i=0; $i<@$cns; $i++) {
52             $v->{$cns->[$i]} = $ai->[$i];
53         }
54         push @o, $v;
55     }
56     return @o;
57 }
58 sub sort_by_name {
59     sort {
60         $a->{'name'} cmp $b->{'name'};
61     } @_;
62 }
63
64 sub p ($) { print $_[0] or die $!; }
65 sub ptcl ($) {
66     local ($_) = @_;
67     die "$_ $& ?" if m/[^-+'"# 0-9a-z]/i;
68     p("{$_[0]}");
69 }
70
71 sub get_arches_islands () {
72     my $ocean= $ENV{'YPPSC_OCEAN'};  die unless $ocean;
73     my $url= "$pctb/islands.php?oceanName=".uc $ocean;
74     my $resp= $ua->get($url);
75     die $resp->status_line unless $resp->is_success;
76     $jsonresp= $resp->content;
77     my $jobj= jsonToObj($resp->content);
78     my $arches= [ jparsetable($jobj, 'arches') ];
79     my $islands= [ jparsetable($jobj, 'islands') ];
80     return ($arches,$islands);
81 }
82
83 sub main__island () {
84     my ($arches, $islands) = get_arches_islands();
85 #    print Dumper(\@arches, \@islands);
86     my $islands_done=0;
87     foreach my $arch (sort_by_name(@$arches)) {
88 #       print Dumper($arch);
89         my $aname= $arch->{'name'};
90         die "$jsonresp ?" unless defined $aname;
91         ptcl($aname); p(' '); ptcl($aname); p(" {\n");
92         foreach my $island (sort_by_name(@$islands)) {
93             my $iname= $island->{'name'};
94             die "$jsonresp $aname ?" unless defined $iname;
95             next unless $arch->{'id'} == $island->{'arch'};
96             p('    '); ptcl($iname); p(' '); ptcl($iname); p("\n");
97             $islands_done++;
98         }
99         p("}\n");
100     }
101     die "$jsonresp $islands_done ?" unless $islands_done == @$islands;
102 }
103
104 sub main__sunshinewidget () {
105     print <<END
106 Land {On land} {
107     Crew   Crew
108     Shoppe Shoppe
109     Ye     Ye
110     Booty  Booty
111     Ahoy!  Ahoy!
112 }
113 Vessel {On board a ship} {
114     Crew   Crew
115     Vessel Vessel
116     Ye     Ye
117     Booty  Booty
118     Ahoy!  Ahoy!
119 }
120 END
121     or die $!;
122 }
123
124 &{"main__$which"}();