chiark / gitweb /
Query planner and dumpqueryresults doc strings; dumpqueryresults improved
[ypp-sc-tools.db-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                                 [ 'age', 'Data age' ] ]
61         }, {    Name => 'BuySell',
62                 Before => '',
63                 Values => [     [ 'buy_sell',   'Buy and sell' ],
64                                 [ 'sell_buy',   'Sell and buy' ],
65                                 [ 'buy',        'Buy offers only' ],
66                                 [ 'sell',       'Sell offers only' ],
67                         ],
68                 QuerySpecific => 1,
69         }, {    Name => 'ShowBlank',
70                 Before => '',
71                 Values => [     [ 'omit', 'Omit islands with no offers' ],
72                                 [ 'show', 'Show all islands' ],
73                         ],
74                 QuerySpecific => 1,
75         });
76
77 foreach my $var (@vars) {
78         my $name= $var->{Name};
79         my $lname= lc $name;
80         $var->{Before}= '' unless exists $var->{Before};
81         $var->{CmpCanon}= sub { $_[0]; } unless exists $var->{CmpCanon};
82         foreach my $val (@{ $var->{Values} }) {
83                 next if ref $val;
84                 $val= [ $val, encode_entities($val) ];
85         }
86         if (exists $ARGS{$lname}) {
87                 $styles{$name}= $ARGS{$lname};
88                 my @html= grep { $_->[0] eq $styles{$name} }
89                                 @{ $var->{Values} };
90                 $ahtml{$name}= @html==1 ? $html[0][1] : '???';
91         } else {
92                 $styles{$name}= $var->{Values}[0][0];
93                 $ahtml{$name}= $var->{Values}[0][1];
94         }
95 }
96
97 </%perl>
98
99 <%shared>
100 my %baseqf;
101 my %queryqf;
102 </%shared>
103
104 <%method formhidden>
105 <%args>
106 $ours
107 </%args>
108 % foreach my $n (keys %baseqf, keys %queryqf) {
109 %       next if $ours->($n);
110 %       my $v= exists $baseqf{$n} ? $baseqf{$n} : $queryqf{$n};
111 <input type=hidden name=<% $n %> value="<% $v |h %>">
112 % }
113 </%method>
114
115 <html><head><title><% ucfirst $ahtml{Query} %> - YARRG</title></head><body>
116
117 <a href="<% $m->current_comp()->name() |u %>">YARRG</a> -
118  Yet Another Revenue Research Gatherer
119 |
120 <a href="docs">documentation</a>
121 <p>
122 <%perl>
123
124 foreach my $var (@vars) {
125         my $lname= lc $var->{Name};
126         next unless exists $ARGS{$lname};
127         $baseqf{$lname}= $ARGS{$lname};
128 }
129
130 foreach my $var (keys %ARGS) {
131         next unless $var =~
132                 m/^(?:(?:route|commod)string|islandid\d|archipelago\d|debug)$/;
133         my $val= $ARGS{$var};
134         next if $val eq 'none';
135         $queryqf{$var}= $val;
136 }
137
138 my $quri= sub {
139         my $uri= URI->new('lookup');
140         $uri->query_form(@_);
141         $uri->path_query();
142 };
143
144 my $prselector_core= sub {
145         my ($var)= @_;
146         my $name= $var->{Name};
147         my $lname= lc $var->{Name};
148         my $delim= $var->{Before};
149         my $canon= &{$var->{CmpCanon}}($styles{$name});
150         my $cvalix= 0;
151         foreach my $valr (@{ $var->{Values} }) {
152                 print $delim;  $delim= "\n|\n";
153                 my ($value,$html) = @$valr;
154                 my $iscurrent= &{$var->{CmpCanon}}($value) eq $canon;
155                 my $after;
156                 if ($iscurrent) {
157                         print '<b>';
158                         $after= '</b>';
159                 } else {
160                         my %qf= (%baseqf,%queryqf);
161                         delete $qf{$lname};
162                         $qf{$lname}= $value if $cvalix;
163 </%perl>
164 <a href="<% $quri->(%qf) |h %>">
165 <%perl>
166                         $after= '</a>';
167                 }
168                 print $html, $after;
169                 $cvalix++;
170         }
171         print "<p>\n\n";
172 };
173
174 my $prselector= sub {
175         my ($name)= @_;
176         foreach my $var (@vars) {
177                 if ($var->{Name} eq $name) {
178                         $prselector_core->($var);
179                         return;
180                 }
181         }
182         die $name;
183 };
184
185 foreach my $var (@vars) {
186         next if $var->{QuerySpecific};
187         $prselector_core->($var);
188 }
189
190 #---------- initial checks, startup, main entry form ----------
191
192 die if $styles{Query} =~ m/[^a-z]/;
193
194 my $mydbh;
195 my $dbh= ($mydbh= dbw_connect($styles{Ocean}));
196
197 my $results_head_done=0;
198 my $someresults= sub {
199         return if $results_head_done;
200         $results_head_done=1;
201         print "\n<h1>Results</h1>\n";
202 };
203
204 </%perl>
205 <%args>
206 $debug => 0
207 </%args>
208
209 <hr>
210
211 <& "query_$styles{Query}", %baseqf, %queryqf, %styles,
212     quri => $quri, dbh => $dbh,
213     prselector => $prselector,
214     someresults => $someresults,
215     emsgokorprint => sub {
216         my ($emsg) = @_;
217         return 1 unless defined $emsg and length $emsg;
218         $someresults->();
219         print $emsg;
220         return 0;
221     }
222  &>
223
224 <p>
225
226 %#---------- debugging and epilogue ----------
227
228 % if ($debug) {
229 <p>
230 <pre id="debug_log">
231 Debug log:
232 </pre>
233 % }
234
235 <&| script &>
236 function debug (m) {
237 % if ($debug) {
238   var node= document.getElementById('debug_log');
239   node.innerHTML += "\n" + m + "\n";
240 % }
241 }
242 </&script>
243
244 <& footer &>
245
246 <%init>
247 use CommodsWeb;
248 use HTML::Entities;
249 use URI::Escape;
250
251 </%init>
252 <%cleanup>
253
254 $mydbh->rollback() if $mydbh;
255
256 </%cleanup>