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