chiark / gitweb /
Fix up search too long error message
[ypp-sc-tools.main.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 %styles;
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                                 [ 'offers', 'Offers at location' ],
61                                 [ 'routesearch', 'Find profitable route' ],
62                                 [ 'age', 'Data age' ] ]
63         }, {    Name => 'BuySell',
64                 Before => '',
65                 Values => [     [ 'buy_sell',   'Buy and sell' ],
66                                 [ 'sell_buy',   'Sell and buy' ],
67                                 [ 'buy',        'Buy offers only' ],
68                                 [ 'sell',       'Sell offers only' ],
69                         ],
70                 QuerySpecific => 1,
71         }, {    Name => 'ShowBlank',
72                 Before => '',
73                 Values => [     [ 0, 'Omit islands with no offers' ],
74                                 [ 'show', 'Show all islands' ],
75                         ],
76                 QuerySpecific => 1,
77         }, {    Name => 'ShowStalls',
78                 Before => '',
79                 Values => [     [ 0, 'Show total quantity at each price' ],
80                                 [ 1, 'Show individual stalls' ],
81                                 [ 2, "Also be cautious about stalls'".
82                                         " poe reserves",
83                                         '<a href="docs#poelimit">[?]</a>' ]
84                         ],
85                 QuerySpecific => 1,
86         }, {    Name => 'RouteSearchType',
87                 Before => 'Type of routes to search for: ',
88                 Values => [     [ 0, 'Open-ended' ],
89                                 [ 1, 'Circular' ],
90                                 [ 2, 'Specific destination' ],
91                         ],
92                 QuerySpecific => 1,
93         });
94
95 foreach my $var (@vars) {
96         my $name= $var->{Name};
97         my $lname= lc $name;
98         $var->{Before}= '' unless exists $var->{Before};
99         $var->{CmpCanon}= sub { $_[0]; } unless exists $var->{CmpCanon};
100         foreach my $val (@{ $var->{Values} }) {
101                 next if ref $val;
102                 $val= [ $val, encode_entities($val) ];
103         }
104         if (exists $ARGS{$lname}) {
105                 $styles{$name}= $ARGS{$lname};
106                 my @html= grep { $_->[0] eq $styles{$name} }
107                                 @{ $var->{Values} };
108                 $ahtml{$name}= @html==1 ? $html[0][1] : '???';
109         } else {
110                 $styles{$name}= $var->{Values}[0][0];
111                 $ahtml{$name}= $var->{Values}[0][1];
112         }
113 }
114
115 </%perl>
116
117 <%shared>
118 my %baseqf;
119 my %queryqf;
120 </%shared>
121
122 <%method formhidden>
123 <%args>
124 $ours
125 </%args>
126 % foreach my $n (keys %baseqf, keys %queryqf) {
127 %       next if $ours->($n);
128 %       my $v= exists $baseqf{$n} ? $baseqf{$n} : $queryqf{$n};
129 <input type=hidden name=<% $n %> value="<% $v |h %>">
130 % }
131 </%method>
132
133 <html lang="en"><head><title><% ucfirst $ahtml{Query} %> - YARRG</title>
134 <style type="text/css">
135 <& style.css &>
136   body { margin: 0; color: #000000; background: #c5c7ae; }
137   div.query { background: #d7d8b3; padding: 1em; }
138   div.results { padding: 1em; }
139   table.data { background: #b5b686; }
140   tr.datarow0 { background: #ffffff; }
141   tr.datarow1 { background: #e3e3e3; }
142 </style>
143 <&| script &>
144   function register_onload(f) {
145     var previous_onload= window.onload;
146     window.onload= function() {
147       if (previous_onload) previous_onload();
148       f();
149     };
150   }
151 </&script>
152 </head><body>
153
154 % if (!printable($m)) {
155 <div class="navoptbar">
156 <& navbar &>
157 <p>
158 % }
159 <%perl>
160
161 foreach my $var (@vars) {
162         my $lname= lc $var->{Name};
163         next unless exists $ARGS{$lname};
164         $baseqf{$lname}= $ARGS{$lname};
165 }
166
167 foreach my $var (keys %ARGS) {
168         next unless $var =~
169                 m/^(?: (?:route|commod|capacity|capital|minprofit
170                          |island|dest)string |
171                         lossperleague | distance |
172                         commodid |
173                         islandid \d |
174                         archipelago \d |
175                         debug |
176                         [RT]\w+
177                     )$/x;
178         my $val= $ARGS{$var};
179         next if $val eq 'none';
180         $queryqf{$var}= $val;
181 }
182
183 my $quri= sub {
184         my $uri= URI->new('lookup');
185         $uri->query_form(@_);
186         $uri->path_query();
187 };
188
189 my $prselector_core= sub {
190         my ($var)= @_;
191         return if printable($m);
192         my $name= $var->{Name};
193         my $lname= lc $var->{Name};
194         my $delim= $var->{Before};
195         my $canon= &{$var->{CmpCanon}}($styles{$name});
196         my $cvalix= 0;
197         foreach my $valr (@{ $var->{Values} }) {
198                 print $delim;  $delim= "\n|\n";
199                 my ($value,$html,$finally) = @$valr;
200                 my $iscurrent= &{$var->{CmpCanon}}($value) eq $canon;
201                 my $after;
202                 if ($iscurrent) {
203                         print '<b>';
204                         $after= '</b>';
205                 } else {
206                         my %qf= (%baseqf,%queryqf);
207                         delete $qf{$lname};
208                         delete $qf{$_} foreach grep { m/^[A-Z]/ } keys %qf;
209                         $qf{$lname}= $value if $cvalix;
210 </%perl>
211 <a href="<% $quri->(%qf) |h %>">
212 <%perl>
213                         $after= '</a>';
214                 }
215                 print $html, $after;
216                 print " ", $finally if defined $finally;
217                 $cvalix++;
218         }
219         print "<p>\n\n";
220 };
221
222 my $prselector= sub {
223         my ($name)= @_;
224         foreach my $var (@vars) {
225                 if ($var->{Name} eq $name) {
226                         $prselector_core->($var);
227                         return;
228                 }
229         }
230         die $name;
231 };
232
233 foreach my $var (@vars) {
234         next if $var->{QuerySpecific};
235         $prselector_core->($var);
236 }
237
238 #---------- initial checks, startup, main entry form ----------
239
240 die if $styles{Query} =~ m/[^a-z]/;
241
242 my $mydbh;
243 my $dbh= ($mydbh= dbw_connect($styles{Ocean}));
244
245 my $results_head_done=0;
246 my $someresults= sub {
247         return if $results_head_done;
248         $results_head_done=1;
249         my ($h)= @_;
250         $h= 'Results' if !$h;
251         print "\n<h1>$h</h1>\n";
252 };
253
254 </%perl>
255 <%args>
256 $debug => 0
257 </%args>
258
259 % if (!printable($m)) {
260 </div>
261 <hr>
262 % }
263
264 <& "query_$styles{Query}", %baseqf, %queryqf, %styles,
265     quri => $quri, dbh => $dbh,
266     baseqf => \%baseqf, queryqf => \%queryqf, allargs => \%ARGS,
267     prselector => $prselector,
268     someresults => $someresults,
269     emsgokorprint => sub {
270         my ($emsg) = @_;
271         return 1 unless defined $emsg and length $emsg;
272         $someresults->();
273         print $emsg;
274         return 0;
275     }
276  &>
277
278 %#---------- debugging and epilogue ----------
279
280 % if ($debug) {
281 <div class="results">
282 <pre id="debug_log">
283 Debug log:
284 </pre>
285 </div>
286 % }
287
288 <&| script &>
289 function debug (m) {
290 % if ($debug) {
291   var node= document.getElementById('debug_log');
292   node.innerHTML += "\n" + m + "\n";
293 % }
294 }
295 </&script>
296
297 <& footer &>
298
299 <%init>
300 use CommodsWeb;
301 use HTML::Entities;
302 use URI::Escape;
303
304 </%init>
305 <%cleanup>
306
307 $mydbh->rollback() if $mydbh;
308
309 </%cleanup>