chiark / gitweb /
41eda49db3359e0462a330245c3ce1468fe9b86d
[ypp-sc-tools.web-live.git] / yarrg / web / lookup
1 %# This is part of the YARRG website.  YARRG is a tool and website
2 %# for assisting players of Yohoho Puzzle Pirates.
3 %#
4 %# Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
5 %# Copyright (C) 2009 Clare Boothby
6 %#
7 %#  YARRG's client code etc. is covered by the ordinary GNU GPL (v3 or later).
8 %#  The YARRG website is covered by the GNU Affero GPL v3 or later, which
9 %#   basically means that every installation of the website will let you
10 %#   download the source.
11 %#
12 %# This program is free software: you can redistribute it and/or modify
13 %# it under the terms of the GNU Affero General Public License as
14 %# published by the Free Software Foundation, either version 3 of the
15 %# License, or (at your option) any later version.
16 %#
17 %# This program is distributed in the hope that it will be useful,
18 %# but WITHOUT ANY WARRANTY; without even the implied warranty of
19 %# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 %# GNU Affero General Public License for more details.
21 %#
22 %# You should have received a copy of the GNU Affero General Public License
23 %# along with this program.  If not, see <http://www.gnu.org/licenses/>.
24 %#
25 %# Yohoho and Puzzle Pirates are probably trademarks of Three Rings and
26 %# are used without permission.  This program is not endorsed or
27 %# sponsored by Three Rings.
28 %#
29 %#
30 %# This Mason component generates the main `lookup' page, including
31 %# all the entry boxes etc. for every query.
32 %#
33 <%perl>
34 my %a;
35 my %ahtml;
36 my @vars;
37
38 # for output:
39 my @archipelagoes;
40 my @islandids;
41 my %islandid2;
42
43 #---------- "mode" argument parsing and mode menu at top of page ----------
44
45 # for debugging, invoke as
46 #  http://www.chiark.greenend.org.uk/ucgi/~clareb/mason/pirates/pirate-route?debug=1
47
48 @vars= ({       Name => 'Ocean',
49                 Before => 'Ocean: ',
50                 CmpCanon => sub { ucfirst lc $_[0] },
51                 Values => [ ocean_list() ]
52         }, {    Name => 'Dropdowns',
53                 Before => 'Interface: ',
54                 CmpCanon => sub { !!$_[0] },
55                 Values => [     [ 0, 'Type in names' ],
56                                 [ 4, 'Select from menus' ] ]
57         }, {    Name => 'Query',
58                 Before => 'Query: ',
59                 Values => [     [ 'route', 'Trades for route' ],
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                 $a{$name}= $ARGS{$lname};
74                 my @html= grep { $_->[0] eq $a{$name} } @{ $var->{Values} };
75                 $ahtml{$name}= @html==1 ? $html[0][1] : '???';
76         } else {
77                 $a{$name}= $var->{Values}[0][0];
78                 $ahtml{$name}= $var->{Values}[0][1];
79         }
80 }
81
82 </%perl>
83 <html><head><title><% ucfirst $ahtml{Query} %> - YARRG</title></head><body>
84 <& webcopyright &>
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}}($a{$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                         print '<a href="',$quri->(%qf),'">';
134                         $after= '</a>';
135                 }
136                 print $html, $after;
137                 $cvalix++;
138         }
139         print "<p>\n\n";
140 }
141
142 #---------- initial checks, startup, main entry form ----------
143
144 dbw_connect($a{Ocean});
145
146 </%perl>
147 <%args>
148 $debug => 0
149 $routestring => ''
150 </%args>
151
152 <hr>
153
154 %########### query `route' ##########
155 % if ($a{Query} eq 'route') {
156
157 <h1>Specify route</h1>
158 <form action="<% $quri->() %>" method="get">
159
160 %#---------- textbox, user enters route as string ----------
161 % if (!$a{Dropdowns}) {
162
163 Enter route (islands, or archipelagoes, separated by |s or commas;
164  abbreviations are OK):<br/>
165
166 <script type="text/javascript">
167 tr_uri= "routetextstring?format=json&type=text/xml"
168                 + "&ocean=<% uri_escape($a{Ocean}) %>";
169
170 tr_timeout=false;
171 tr_request=false;
172 tr_done='';
173 tr_needed='';
174 function tr_Later(){
175   window.clearTimeout(tr_timeout);
176   tr_timeout = window.setTimeout(tr_Needed, 500);
177 }
178 function tr_Needed(){
179   window.clearTimeout(tr_timeout);
180   tr_element= document.getElementById('routestring');
181   tr_needed= tr_element.value;
182   tr_Request();
183 }
184 function tr_Request(){
185   if (tr_request || tr_needed==tr_done) return;
186   tr_done= tr_needed;
187   tr_request= new XMLHttpRequest();
188   uri= tr_uri+'&string='+encodeURIComponent(tr_needed);
189   tr_request.open('GET', uri);
190   tr_request.onreadystatechange= tr_Ready;
191   tr_request.send(null);
192 }
193 function tr_Ready() {
194   if (tr_request.readyState != 4) return;
195   if (tr_request.status == 200) {
196     response= tr_request.responseText;
197     eval('results='+response);
198     toedit= document.getElementById('routeresults');
199     toedit.innerHTML= results.show;
200   }
201   tr_request= false;
202   tr_Request();
203 }
204 window.onload= tr_Needed;
205 </script>
206
207 <input type="text" id="routestring" name="routestring" size=80
208  value="<% $routestring |h %>"
209  onchange="tr_Needed();"
210  onkeyup="tr_Later();"><br>
211 <div id="routeresults">&nbsp;</div><br/>
212
213 % } else { #---------- dropdowns, user selects from menus ----------
214
215 <%perl>
216 my ($sth,$row);;
217 my @archlistdata;
218 my %islandlistdata;
219 $islandlistdata{'none'}= [ [ "none", "Select island..." ] ];
220
221 my $optionlistmap= sub {
222         my ($optlist, $selected) = @_;
223         my $out='';
224         foreach my $entry (@$optlist) {
225                 $out.= sprintf('<option value="%s" %s>%s</option>',
226                         encode_entities($entry->[0]),
227                         defined $selected && $entry->[0] eq $selected
228                                 ? 'selected' : '',
229                         encode_entities($entry->[1]));
230         }
231         return $out;
232 };
233
234 my $dbh= dbw_connect($a{Ocean});
235
236 $sth= $dbh->prepare("SELECT DISTINCT archipelago FROM islands
237                             ORDER BY archipelago;");
238 $sth->execute();
239
240 while ($row=$sth->fetchrow_arrayref) {
241         my ($arch)= @$row;
242         push @archlistdata, [ $arch, $arch ];
243         $islandlistdata{$arch}= [ [ "none", "Whole arch" ] ];
244 }
245
246 $sth= $dbh->prepare("SELECT islandid,islandname,archipelago
247                             FROM islands
248                             ORDER BY islandname;");
249 $sth->execute();
250
251 while ($row=$sth->fetchrow_arrayref) {
252         my $arch= $row->[2];
253         push @{ $islandlistdata{'none'} }, [ @$row ];
254         push @{ $islandlistdata{$arch} }, [ @$row ];
255         $islandid2{$row->[0]}= { Name => $row->[1], Arch => $arch };
256 }
257
258 my %resetislandlistdata;
259 foreach my $arch (keys %islandlistdata) {
260         $resetislandlistdata{$arch}=
261                 $optionlistmap->($islandlistdata{$arch}, '');
262 }
263
264 </%perl>
265
266 <input type=hidden name=dropdowns value="<% $a{Dropdowns} |h %>">
267
268 <script type="text/javascript">
269 ms_lists= <% to_json(\%resetislandlistdata) %>;
270 function ms_Setarch(dd) {
271   debug('ms_SetArch '+dd+' arch='+arch);
272   var arch= document.getElementsByName('archipelago'+dd).item(0).value;
273   var got= ms_lists[arch];
274   if (got == undefined) return; // unknown arch ?  hrm
275   debug('ms_SetArch '+dd+' arch='+arch+' got ok');
276   var select= document.getElementsByName('islandid'+dd).item(0);
277   select.innerHTML= got;
278   debug('ms_SetArch '+dd+' arch='+arch+' innerHTML set');
279 }
280 </script>
281
282 <table style="table-layout:fixed; width:90%;">
283
284 <tr>
285 %       for my $dd (0..$a{Dropdowns}-1) {
286 <td>
287 <select name="archipelago<% $dd %>" onchange="ms_Setarch(<% $dd %>)">
288 <option value="none">Whole ocean</option>
289 <% $optionlistmap->(\@archlistdata, $ARGS{"archipelago$dd"}) %></select></td>
290 %       }
291 </tr>
292
293 <tr>
294 %       for my $dd (0..$a{Dropdowns}-1) {
295 %               my $arch= $ARGS{"archipelago$dd"};
296 %               $arch= 'none' if !defined $arch;
297 <td>
298 <select name="islandid<% $dd %>">
299 <% $optionlistmap->($islandlistdata{$arch}, $ARGS{"islandid$dd"}) %>
300 </select></td>
301 %       }
302 </tr>
303
304 </table>
305
306 % } #---------- end of dropdowns, now common middle of page code ----------
307
308 <input type=submit name=submit value="Go">
309 </form>
310
311 <%perl>
312 #========== result computations ==========
313
314 my $results_head;
315 $results_head= sub {
316         print "<h1>Results</h1>\n";
317         $results_head= sub { };
318 };
319
320 #---------- result computation - textstring ----------
321 if (!$a{Dropdowns}) {
322   if (length $routestring) {
323         $results_head->();
324         my $rsr= $m->comp('routetextstring',
325                 ocean => $a{Ocean},
326                 string => $routestring,
327                 format => 'return'
328         );
329         if (length $rsr->{Error}) {
330                 print encode_entities($rsr->{Error});
331         } else {
332                 foreach my $entry (@{ $rsr->{Results} }) {
333                         push @archipelagoes,
334                                 defined $entry->[1] ? undef : $entry->[0];
335                         push @islandids, $entry->[1];
336                 } 
337         }
338   }
339
340 } else { #---------- results - dropdowns ----------
341
342 my $argorundef= sub {
343         my ($dd,$base) = @_;
344         my $thing= $ARGS{"${base}${dd}"};
345         $thing= undef if defined $thing and $thing eq 'none';
346         return $thing;
347 };
348
349 for my $dd (0..$a{Dropdowns}-1) {
350         my $arch= $argorundef->($dd,'archipelago');
351         my $island= $argorundef->($dd,'islandid');
352         next unless defined $arch or defined $island;
353         if (defined $island and defined $arch) {
354                 my $ii= $islandid2{$island};
355                 my $iarch= $ii->{Arch};
356                 if ($iarch ne $arch) {
357                         $results_head->();
358 </%perl>
359  Specified archipelago <% $arch %> but
360  island <% $ii->{Name} %>
361  which is in <% $iarch %>; using the island.<br>
362 <%perl>
363                 }
364                 $arch= undef;
365         }
366         push @archipelagoes, $arch;
367         push @islandids, $island;
368 }
369
370 }#---------- result processing, common stuff
371 </%perl>
372
373 % if (@islandids) {
374 %       $results_head->();
375
376 <& routetrade, islandids => \@islandids, archipelagoes => \@archipelagoes &>
377
378 % }
379
380 % } elsif ($a{Query} eq 'age') {
381 % ########### query `age' ##########
382
383 <h1>Market data age</h1>
384 <& dataage, %baseqf, %queryqf &>
385
386 % } ########## end of `age' query ##########
387
388 %#---------- debugging and epilogue ----------
389
390 % if ($debug) {
391 <p>
392 <pre id="debug_log">
393 Debug log:
394 </pre>
395 % }
396
397 <script type="text/javascript">
398 function debug (m) {
399 % if ($debug) {
400   var node= document.getElementById('debug_log');
401   node.innerHTML += "\n" + m + "\n";
402 % }
403 }
404 </script>
405
406 <& footer &>
407
408 <%init>
409 use CommodsWeb;
410 use HTML::Entities;
411 use URI::Escape;
412 use JSON;
413
414 </%init>