3 This is part of the YARRG website. YARRG is a tool and website
4 for assisting players of Yohoho Puzzle Pirates.
6 Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
7 Copyright (C) 2009 Clare Boothby
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
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.
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.
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/>.
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.
32 This Mason component generates the core of the `trade route' query.
49 %#---------- textbox, user enters route as string ----------
50 % if (!$qa->{Dropdowns}) {
52 <h1>Specify route</h1>
54 Enter route (islands, or archipelagoes, separated by |s or commas;
55 abbreviations are OK):<br>
57 <form action="<% $quri->() |h %>" method="get">
59 <&| qtextstring, qa => $qa, thingstring => 'routestring' &>
63 % } else { #---------- dropdowns, user selects from menus ----------
69 $islandlistdata{'none'}= [ [ "none", "Select island..." ] ];
71 my $optionlistmap= sub {
72 my ($optlist, $selected) = @_;
74 foreach my $entry (@$optlist) {
75 $out.= sprintf('<option value="%s" %s>%s</option>',
76 encode_entities($entry->[0]),
77 defined $selected && $entry->[0] eq $selected
79 encode_entities($entry->[1]));
84 my $dbh= dbw_connect($qa->{Ocean});
86 $sth= $dbh->prepare("SELECT DISTINCT archipelago FROM islands
87 ORDER BY archipelago;");
90 while ($row=$sth->fetchrow_arrayref) {
92 push @archlistdata, [ $arch, $arch ];
93 $islandlistdata{$arch}= [ [ "none", "Whole arch" ] ];
96 $sth= $dbh->prepare("SELECT islandid,islandname,archipelago
98 ORDER BY islandname;");
101 while ($row=$sth->fetchrow_arrayref) {
103 push @{ $islandlistdata{'none'} }, [ @$row ];
104 push @{ $islandlistdata{$arch} }, [ @$row ];
105 $islandid2{$row->[0]}= { Name => $row->[1], Arch => $arch };
108 my %resetislandlistdata;
109 foreach my $arch (keys %islandlistdata) {
110 $resetislandlistdata{$arch}=
111 $optionlistmap->($islandlistdata{$arch}, '');
119 ms_lists= <% to_json_protecttags(\%resetislandlistdata) %>;
120 function ms_Setarch(dd) {
121 debug('ms_SetArch '+dd+' arch='+arch);
122 var arch= document.getElementsByName('archipelago'+dd).item(0).value;
123 var got= ms_lists[arch];
124 if (got == undefined) return; // unknown arch ? hrm
125 debug('ms_SetArch '+dd+' arch='+arch+' got ok');
126 var select= document.getElementsByName('islandid'+dd).item(0);
127 select.innerHTML= got;
128 debug('ms_SetArch '+dd+' arch='+arch+' innerHTML set');
132 <table style="table-layout:fixed; width:90%;">
135 % for my $dd (0..$qa->{Dropdowns}-1) {
137 <select name="archipelago<% $dd %>" onchange="ms_Setarch(<% $dd %>)">
138 <option value="none">Whole ocean</option>
139 <% $optionlistmap->(\@archlistdata, $ARGS{"archipelago$dd"}) %></select></td>
144 % for my $dd (0..$qa->{Dropdowns}-1) {
145 % my $arch= $ARGS{"archipelago$dd"};
146 % $arch= 'none' if !defined $arch;
148 <select name="islandid<% $dd %>">
149 <% $optionlistmap->($islandlistdata{$arch}, $ARGS{"islandid$dd"}) %>
156 % } #---------- end of dropdowns, now common middle of page code ----------
158 <input type=submit name=submit value="Go">
159 % my $ours= sub { $_[0] =~ m/^island|^archipelago|^routestring/; };
160 <& "lookup:formhidden", ours => $ours &>
164 #========== result computations ==========
168 print "<h1>Results</h1>\n";
169 $results_head= sub { };
172 #---------- result computation - textstring ----------
173 if (!$qa->{Dropdowns}) {
174 if (length $routestring) {
176 my ($emsg,$canonstring,$results)= $m->comp('qtextstringcheck',
177 what => 'routestring',
178 ocean => $qa->{Ocean},
179 string => $routestring,
183 print encode_entities($emsg);
185 foreach my $entry (@$results) {
186 push @islandids, $entry->[1];
188 defined $entry->[1] ? undef : $entry->[2];
193 } else { #---------- results - dropdowns ----------
195 my $argorundef= sub {
197 my $thing= $ARGS{"${base}${dd}"};
198 $thing= undef if defined $thing and $thing eq 'none';
202 for my $dd (0..$qa->{Dropdowns}-1) {
203 my $arch= $argorundef->($dd,'archipelago');
204 my $island= $argorundef->($dd,'islandid');
205 next unless defined $arch or defined $island;
206 if (defined $island and defined $arch) {
207 my $ii= $islandid2{$island};
208 my $iarch= $ii->{Arch};
209 if ($iarch ne $arch) {
212 Specified archipelago <% $arch %> but
213 island <% $ii->{Name} %>
214 which is in <% $iarch %>; using the island.<br>
219 push @archipelagoes, $arch;
220 push @islandids, $island;
223 }#---------- result processing, common stuff
229 <& routetrade, islandids => \@islandids, archipelagoes => \@archipelagoes &>