chiark / gitweb /
a118de0905a4d1e58cd500fcc732b20811f847ef
[ypp-sc-tools.web-live.git] / yarrg / web / lookup
1 <%doc>
2
3  This is part of the YARRG website.  YARRG is a tool and website
4  for assisting players of Yohoho Puzzle Pirates.
5
6  Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
7  Copyright (C) 2009 Clare Boothby
8
9   YARRG's client code etc. is covered by the ordinary GNU GPL (v3 or later).
10   The YARRG website is covered by the GNU Affero GPL v3 or later, which
11    basically means that every installation of the website will let you
12    download the source.
13
14  This program is free software: you can redistribute it and/or modify
15  it under the terms of the GNU Affero General Public License as
16  published by the Free Software Foundation, either version 3 of the
17  License, or (at your option) any later version.
18
19  This program is distributed in the hope that it will be useful,
20  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  GNU Affero General Public License for more details.
23
24  You should have received a copy of the GNU Affero General Public License
25  along with this program.  If not, see <http://www.gnu.org/licenses/>.
26
27  Yohoho and Puzzle Pirates are probably trademarks of Three Rings and
28  are used without permission.  This program is not endorsed or
29  sponsored by Three Rings.
30
31
32  This Mason component generates the main `lookup' page, including
33  all the entry boxes etc. for every query.
34
35
36 </%doc>
37 <%perl>
38 my %ahtml;
39 my @vars;
40 my %styleqf;
41
42 #---------- "mode" argument parsing and mode menu at top of page ----------
43
44 # for debugging, invoke as
45 #  http://www.chiark.greenend.org.uk/ucgi/~clareb/mason/pirates/pirate-route?debug=1
46
47 @vars= ({       Name => 'Ocean',
48                 Before => 'Ocean: ',
49                 CmpCanon => sub { ucfirst lc $_[0] },
50                 Values => [ ocean_list() ]
51         }, {    Name => 'Dropdowns',
52                 Before => 'Interface: ',
53                 CmpCanon => sub { !!$_[0] },
54                 Values => [     [ 0, 'Type in names' ],
55                                 [ 4, 'Select from menus' ] ]
56         }, {    Name => 'Query',
57                 Before => 'Query: ',
58                 Values => [     [ 'route', 'Trades for route' ],
59                                 [ 'commod', 'Prices for commodity' ],
60                                 [ 'age', 'Data age' ] ]
61         });
62
63 foreach my $var (@vars) {
64         my $name= $var->{Name};
65         my $lname= lc $name;
66         $var->{Before}= '' unless exists $var->{Before};
67         $var->{CmpCanon}= sub { $_[0]; } unless exists $var->{CmpCanon};
68         foreach my $val (@{ $var->{Values} }) {
69                 next if ref $val;
70                 $val= [ $val, encode_entities($val) ];
71         }
72         if (exists $ARGS{$lname}) {
73                 $styleqf{$name}= $ARGS{$lname};
74                 my @html= grep { $_->[0] eq $styleqf{$name} }
75                                 @{ $var->{Values} };
76                 $ahtml{$name}= @html==1 ? $html[0][1] : '???';
77         } else {
78                 $styleqf{$name}= $var->{Values}[0][0];
79                 $ahtml{$name}= $var->{Values}[0][1];
80         }
81 }
82
83 </%perl>
84 <html><head><title><% ucfirst $ahtml{Query} %> - YARRG</title></head><body>
85
86 <a href="<% $m->current_comp()->name() |u %>">YARRG</a> -
87  Yet Another Revenue Research Gatherer
88 |
89 <a href="docs">documentation</a>
90 <p>
91 <%perl>
92
93 my %baseqf;
94 foreach my $var (@vars) {
95         my $lname= lc $var->{Name};
96         next unless exists $ARGS{$lname};
97         $baseqf{$lname}= $ARGS{$lname};
98 }
99
100 my %queryqf;
101 foreach my $var (keys %ARGS) {
102         next unless $var =~
103                 m/^(?:routestring|islandid\d|archipelago\d|debug)$/;
104         my $val= $ARGS{$var};
105         next if $val eq 'none';
106         $queryqf{$var}= $val;
107 }
108
109 my $quri= sub {
110         my $uri= URI->new('lookup');
111         $uri->query_form(@_);
112         $uri->path_query();
113 };
114
115 foreach my $var (@vars) {
116         my $name= $var->{Name};
117         my $lname= lc $var->{Name};
118         my $delim= $var->{Before};
119         my $canon= &{$var->{CmpCanon}}($styleqf{$name});
120         my $cvalix= 0;
121         foreach my $valr (@{ $var->{Values} }) {
122                 print $delim;  $delim= "\n|\n";
123                 my ($value,$html) = @$valr;
124                 my $iscurrent= &{$var->{CmpCanon}}($value) eq $canon;
125                 my $after;
126                 if ($iscurrent) {
127                         print '<b>';
128                         $after= '</b>';
129                 } else {
130                         my %qf= (%baseqf,%queryqf);
131                         delete $qf{$lname};
132                         $qf{$lname}= $value if $cvalix;
133 </%perl>
134 <a href="<% $quri->(%qf) |h %>">
135 <%perl>
136                         $after= '</a>';
137                 }
138                 print $html, $after;
139                 $cvalix++;
140         }
141         print "<p>\n\n";
142 }
143
144 #---------- initial checks, startup, main entry form ----------
145
146 die if $styleqf{Query} =~ m/[^a-z]/;
147
148 dbw_connect($styleqf{Ocean});
149
150 </%perl>
151 <%args>
152 $debug => 0
153 </%args>
154
155 <hr>
156
157 <& "query_$styleqf{Query}", %baseqf, %queryqf, %styleqf, quri => $quri &>
158
159 <p>
160
161 %#---------- debugging and epilogue ----------
162
163 % if ($debug) {
164 <p>
165 <pre id="debug_log">
166 Debug log:
167 </pre>
168 % }
169
170 <&| script &>
171 function debug (m) {
172 % if ($debug) {
173   var node= document.getElementById('debug_log');
174   node.innerHTML += "\n" + m + "\n";
175 % }
176 }
177 </&script>
178
179 <& footer &>
180
181 <%init>
182 use CommodsWeb;
183 use HTML::Entities;
184 use URI::Escape;
185
186 </%init>