chiark / gitweb /
c45b56a06690f51173d15a918b17121c8dc647ea
[ypp-sc-tools.db-live.git] / yarrg / web / query_routesearch
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 core of the `routesearch' query.
33
34
35 </%doc>
36 <%args>
37 $quri
38 $dbh
39 $queryqf
40 $islandstring => '';
41 $capacitystring => '';
42 $lossperleague => '';
43 $capitalstring => '';
44 $distance => '';
45 $emsgokorprint
46 </%args>
47
48 <%perl>
49 use BSD::Resource;
50
51 my $emsg;
52 my @warningfs;
53 my @islandids;
54
55 my $maxmaxdist=35;
56 my $maxcpu=90;
57 my $concur_lim=5;
58
59 my $qa= \%ARGS;
60 my $routeparams= { EmsgRef => \$emsg, SayRequiredCapacity => 1 };
61 my $maxdist;
62 my $maxcountea=15;
63
64 </%perl>
65
66 <h1>Find most profitable routes and trades</h1>
67
68 % if ($qa->{Dropdowns}) {
69 This feature is not available from the "drop down menus" interface.
70 % } else {
71
72 <form action="<% $quri->() |h %>" method="get">
73
74 <& enter_route, qa=>$qa, dbh=>$dbh, emsg_r=>\$emsg, warningfs_r=>\@warningfs,
75         enterwhat => 'Enter starting point(s)',
76         islandids_r => \@islandids, archipelagoes_r => undef
77  &>
78
79 <&| enter_advrouteopts, qa=>$qa, dbh=>$dbh, routeparams=>$routeparams &>
80 <td>
81 &nbsp;
82 &nbsp;
83 <td>
84  Maximum distance:
85  <&| qtextstring, qa => $qa, dbh => $dbh, prefix => 'ml',
86     thingstring => 'distance', emsgstore => \$emsg,
87     onresults => sub { ($maxdist)= @_; } &>
88    size=10
89  </&>
90 </&>
91
92 <input type=submit name=submit value="Go">
93 % my $ours= sub { $_[0] =~ m/^lossperleague|^islandstring|^capitalstring|^capacitystring|^distance/; };
94 <& "lookup:formhidden", ours => $ours &>
95
96 % }
97
98 </form>
99 <%perl>
100
101 if (!$emsg && $maxdist > $maxmaxdist) {
102         $emsg= "Searching for routes of more than $maxmaxdist leagues is not".
103                 " supported, sorry.";
104 }
105
106 $emsgokorprint->($emsg) or return;
107 @islandids or return;
108 defined $routeparams->{MaxMass} or defined $routeparams->{MaxVolume} or return;
109
110 #---------- prepare island names ----------
111
112 my $islandname_stmt= $dbh->prepare(<<END);
113         SELECT islandname, archipelago
114           FROM islands
115          WHERE islandid = ?
116 END
117
118 my $isleinfo = sub {
119         my ($id) = @_;
120         $islandname_stmt->execute($id);
121         my $row= $islandname_stmt->fetchrow_hashref();
122         local $_= $row->{'islandname'};
123         s/ Island$//;
124         return $_, $row->{'islandname'}, $row->{'archipelago'};
125 };
126
127 #---------- compute the results ----------
128
129 my @rsargs= ($concur_lim, '-DN');
130 my $concur_fail;
131
132 foreach my $k (qw(MaxMass MaxVolume MaxCapital)) {
133         my $v= $routeparams->{$k};
134         push @rsargs, (defined $v ? $v : -1);
135 }
136 push @rsargs, defined $routeparams->{LossPerLeaguePct}
137         ? $routeparams->{LossPerLeaguePct}*0.01 : 1e-9;
138 push @rsargs, '0';
139 push @rsargs, 'search',$maxdist, $maxcountea,$maxcountea, 'any', @islandids;
140
141 m/[^-.0-9a-zA-Z]/ and die "$_ $& ?" foreach @rsargs;
142
143 if ($qa->{'debug'}) {
144 </%perl>
145 [[ <% "@rsargs" |h %> ]]<br><pre>
146 <%perl>
147 }
148
149 unshift @rsargs,
150         sourcebasedir().'/yarrg/routesearch',
151         '-d', dbw_filename($qa->{'Ocean'}),
152         '-C', webdatadir().'/_concur.', '.lock';
153
154 # touch _concur.0{0,1,2,3,4}.lock
155 # really chgrp www-data _concur.0?.lock
156
157 my %results; # $results{$ap}{"5 6 9 10"} = { stuff }
158
159 my $fh= new IO::File;
160 my $child= $fh->open("-|"); defined $child or die $!;
161 if (!$child) {
162         my $cpu= BSD::Resource::RLIMIT_CPU;
163         my ($soft,$hard)= getrlimit($cpu);
164         setrlimit($cpu,$maxcpu,$hard) or die $! if $hard<=$maxcpu;
165         exec @rsargs;
166         die $!;
167 }
168
169 while (<$fh>) {
170         chomp;
171         if ($qa->{'debug'}) {
172 </%perl>
173 <% $_ |h %>
174 <%perl>
175         }
176         next unless m/^\s*\@/;
177         if (m/^\@\@\@ concurrency limit exceeded/) {
178                 $concur_fail= 1;
179                 last;
180         }
181         die unless m/^ \@ *\d+ ([ap])\# *\d+ \|.*\| *(\d+)lg *\| *\d+ +(\d+) +(\d+) *\| ([0-9 ]+)$/;
182         my ($ap,$isles) = (uc $1,$5);
183         next if $results{$ap} && %{$results{$ap}} >= $maxcountea;
184         my $item= { A => $3, P => $4, Leagues => $2 };
185         my (@i, @fi, @a);
186         foreach (split / /, $isles) {
187                 my ($name,$fullname,$arch)= $isleinfo->($_);
188                 push @i, $name;
189                 push @fi, $fullname;
190                 push @a, $arch unless @a && $a[-1] eq $arch;
191         }
192         $item->{Isles}= [ @i ];
193         $item->{Archs}= [ @a ];
194         $item->{Start}= $i[0];
195         $item->{Finish}= $i[-1];
196         $item->{Vias}= [ ];
197         my $i;
198         for ($i=1; $i < @i-1; $i++) {
199                 push @{ $item->{Vias} }, $i[$i];
200         }
201         my %linkqf= %$queryqf;
202         delete $linkqf{'query'};
203         $linkqf{'routestring'}= join ', ', @fi;
204         $item->{Url}= $quri->(%linkqf);
205         $results{$ap}{$isles}= $item;
206 }
207
208 if ($qa->{'debug'}) {
209         print "</pre>\n";
210 }
211
212 $!=0;
213 if (!close $fh) {
214         die $! if $!;
215         die $? if $? != 24; # SIGXCPU but not in POSIX.pm :-/
216 </%perl>
217 <h2>Search took too long and was terminated</h2>
218
219 Sorry, but your query resulted in a search that took too long.
220 Searches are limited to <% $maxcpu |h %> seconds of CPU time to
221 avoid them consuming excessive resources on the server system, and to
222 make sure that shorter searches can still happen.
223
224 <p>
225 Please try a search with a smaller minimum distance, or place more
226 restrictions on the route.
227
228 <%perl>
229         return;
230 }
231
232 if ($concur_fail) {
233 </%perl>
234 <h2>Server too busy</h2>
235
236 Sorry, but there are already <% $concur_lim |h %> route searches
237 running.  We limit the number which can run at once to avoid
238 overloading the server system and to make sure that the rest of the
239 YARRG website still runs quickly.
240 <p>
241
242 If you submitted several searches and gave up on them (eg by hitting
243 `back' or `stop' in your browser), be aware that that doesn't
244 generally stop the search process at the server end.  So it's best to
245 avoid asking for large searches that you're not sure about.
246
247 <p>
248 Otherwise, please try later.  Searches are limited to <% $maxcpu |h %>
249 seconds of CPU time so more processing resources should be available soon.
250
251 <%perl>
252         return;
253 }
254
255 </%perl>
256 % foreach my $ap (qw(A P)) {
257 %       if ($ap eq 'A') {
258 <h2>Best routes for total profit</h2>
259 %       } else {
260 <h2>Best routes for profit per league</h2>
261 %       }
262 <table rules=groups>
263 <colgroup span=2>
264 <colgroup span=1>
265 <colgroup span=1>
266 <colgroup span=3>
267 <tbody>
268 <tr>
269 <th colspan=2>Profit
270 <th>Dist.
271 <th>Archipelagoes
272 <th colspan=3>Route
273 <tr>
274 <th>Abs.
275 <th>Per.lg.
276 <th>
277 <th>
278 <th>Start
279 <th>Via
280 <th>Finish
281 <tbody>
282 %       my $datarow=0;
283 %       foreach my $isles (sort {
284 %                       $results{$ap}{$b}{$ap} <=>
285 %                       $results{$ap}{$a}{$ap}
286 %               } keys %{$results{$ap}}) {
287 %               my $item= $results{$ap}{$isles};
288 <tr class="datarow<% $datarow %>">
289 <td align=right><% $item->{A} |h %>
290 <td align=right><% $item->{P} |h %>
291 <td align=right><% $item->{Leagues} |h %>
292 <td align=left><a href="<% $item->{Url} |h %>"><% join ', ', @{ $item->{Archs} } |h %></a>
293 <td align=left><% $item->{Start} |h %>,
294 <td align=left><% join ' ', map { $_.',' } @{ $item->{Vias} } |h %>
295 <td align=left><% $item->{Finish} |h %>
296 </td>
297 %               $datarow ^= 1;
298 %       } # $isles
299 </table>
300 % } # $ap
301
302 <p>
303
304 <h2>Notes</h2>
305
306 Per league values count each island visited as one
307 (additional) league; the `Dist.' column is however the actual distance
308 to be sailed.  All profit figures are somewhat approximate; get a
309 complete trading plan for a route for accurate information.
310
311 <%perl>
312
313
314 </%perl>