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 `routesearch' query.
42 $capacitystring => '';
45 $minprofitstring => '';
64 my @maxmaxdist= qw(100 100 100);
69 my $routeparams= { EmsgRef => \$emsg, SayRequiredCapacity => 1 };
76 <h1>Find most profitable routes and trades</h1>
78 % my $searchtype= $ARGS{RouteSearchType};
79 % my $searchtype_show;
81 % if ($qa->{Dropdowns}) {
82 This feature is not available from the "drop down menus" interface.
85 % $prselector->('RouteSearchType');
87 <form action="<% $quri->() |h %>" method="get">
89 <& enter_route, qa=>$qa, dbh=>$dbh, emsg_r=>\$emsg, warningfs_r=>\@warningfs,
90 enterwhat => 'Enter starting point(s)',
91 islandids_r => \@islandids, archipelagoes_r => undef
94 % if ($searchtype == 0) {
96 % $searchtype_show= 'open-ended';
97 % } elsif ($searchtype == 1) {
99 % $searchtype_show= 'circular';
100 % } elsif ($searchtype == 2) {
101 % $searchtype_show= 'specific-destination';
103 Destination (one island only):
104 <& qtextstring, qa => $qa, dbh => $dbh, emsgstore => \$emsg,
105 thingstring => 'deststring',
106 prefix => 'ds', boxopts => 'size=40',
108 print STDERR "ONRESULTS @_\n";
110 my ($canonname, $island, $arch) = @{ $_[0] };
111 die unless defined $island;
120 <&| enter_advrouteopts, qa=>$qa, dbh=>$dbh, routeparams=>$routeparams &>
126 <& qtextstring, qa => $qa, dbh => $dbh, prefix => 'ml',
127 thingstring => 'distance', emsgstore => \$emsg, boxopts => 'size=10',
128 onresults => sub { ($maxdist)= @_; }
132 <input type=submit name=submit value="Search">
133 % my $ours= sub { $_[0] =~ m/^lossperleague|^islandstring|^deststring|^capitalstring|^capacitystring|^minprofitstring|^distance/; };
134 <& "lookup:formhidden", ours => $ours &>
140 <div class="results">
143 my $maxmaxdist= $maxmaxdist[$searchtype];
144 if (!$emsg && $maxdist > $maxmaxdist) {
145 $emsg= "Searching for $searchtype_show routes".
146 " of more than $maxmaxdist leagues is not".
147 " supported, sorry.";
150 print("</div>"), return
151 unless $emsgokorprint->($emsg)
153 and $allargs->{'submit'}
154 and defined $destspec
155 and (defined $routeparams->{MaxMass} or
156 defined $routeparams->{MaxVolume});
158 #---------- prepare island names ----------
160 my $islandname_stmt= $dbh->prepare(<<END);
161 SELECT islandname, archipelago
168 $islandname_stmt->execute($id);
169 my $row= $islandname_stmt->fetchrow_hashref();
170 local $_= $row->{'islandname'};
172 return $_, $row->{'islandname'}, $row->{'archipelago'};
175 #---------- compute the results ----------
177 my @rsargs= ($concur_lim, '-DN');
180 foreach my $k (qw(MaxMass MaxVolume MaxCapital)) {
181 my $v= $routeparams->{$k};
182 push @rsargs, (defined $v ? $v : -1);
184 push @rsargs, defined $routeparams->{LossPerLeaguePct}
185 ? $routeparams->{LossPerLeaguePct}*0.01 : 1e-9;
186 push @rsargs, $routeparams->{MinProfit} // 0;
187 push @rsargs, 'search',$maxdist, $maxcountea,$maxcountea;
188 push @rsargs, $destspec;
189 push @rsargs, @islandids;
191 m/[^-.0-9a-zA-Z]/ and die "$_ $& ?" foreach @rsargs;
193 if ($qa->{'debug'}) {
195 [[ <% "@rsargs" |h %> ]]<br><pre>
200 'nice', sourcebasedir().'/yarrg/routesearch',
201 '-d', dbw_filename($qa->{'Ocean'}),
202 '-C', webdatadir().'/_concur.', '.lock';
204 # touch _concur.0{0,1,2,3,4}.lock
205 # really chgrp www-data _concur.0?.lock
207 my %results; # $results{$ap}{"5 6 9 10"} = { stuff }
209 my $fh= new IO::File;
210 my $child= $fh->open("-|"); defined $child or die $!;
212 my $cpu= BSD::Resource::RLIMIT_CPU;
213 my ($soft,$hard)= getrlimit($cpu);
214 setrlimit($cpu,$maxcpu,$hard) or die $! if $hard<=$maxcpu;
221 if ($qa->{'debug'}) {
226 next unless m/^\s*\@/;
227 if (m/^\@\@\@ concurrency limit exceeded/) {
231 die unless m/^ \@ *\d+ ([ap])\# *\d+ \|.*\| *(\d+)lg *\| *\d+ +(\d+) +(\d+) *\| ([0-9 ]+)$/;
232 my ($ap,$isles) = (uc $1,$5);
233 next if $results{$ap} && keys %{$results{$ap}} >= $maxcountea;
234 my $item= { A => $3, P => $4, Leagues => $2 };
236 foreach (split / /, $isles) {
237 my ($name,$fullname,$arch)= $isleinfo->($_);
240 push @a, $arch unless @a && $a[-1] eq $arch;
242 $item->{Isles}= [ @i ];
243 $item->{Archs}= [ @a ];
244 $item->{Start}= $i[0];
245 $item->{Finish}= $i[-1];
248 for ($i=1; $i < @i-1; $i++) {
249 push @{ $item->{Vias} }, $i[$i];
251 my %linkqf= (%$baseqf, %$queryqf);
252 delete $linkqf{'query'};
253 $linkqf{'routestring'}= join ', ', @fi;
254 $item->{Url}= $quri->(%linkqf);
255 $item->{ArchesString}= join ', ', @a;
256 $item->{ViasString}= join ' ', map { $_.',' } @{ $item->{Vias} };
257 $item->{RouteSortString}= join ', ', @i;
258 $results{$ap}{$isles}= $item;
261 if ($qa->{'debug'}) {
268 die $? if $? != 24; # SIGXCPU but not in POSIX.pm :-/
270 % $someresults->('Search took too long and was terminated');
272 Sorry, but your query resulted in a search that took too long.
273 Searches are limited to <% $maxcpu |h %> seconds of CPU time to
274 avoid them consuming excessive resources on the server system, and to
275 make sure that shorter searches can still happen.
278 Please try a search with a smaller maximum distance, or place more
279 restrictions on the route.
288 % $someresults->('Server too busy');
290 Sorry, but there are already <% $concur_lim |h %> route searches
291 running. We limit the number which can run at once to avoid
292 overloading the server system and to make sure that the rest of the
293 YARRG website still runs quickly.
296 If you submitted several searches and gave up on them (eg by hitting
297 "back" or "stop" in your browser), be aware that that doesn't
298 generally stop the search process at the server end. So it's best to
299 avoid asking for large searches that you're not sure about.
302 Otherwise, please try later. Searches are limited to <% $maxcpu |h %>
303 seconds of CPU time so more processing resources should be available soon.
313 % foreach my $ap (qw(A P)) {
315 <h2>Best routes for total profit</h2>
317 <h2>Best routes for profit per league</h2>
319 <table class="data" rules=groups id="ap<% $ap %>_table">
339 <tr id="ap<% $ap %>_sortrow"><th><th><th><th><th><th><th>
342 % foreach my $isles (sort {
343 % $results{$ap}{$b}{$ap} <=>
344 % $results{$ap}{$a}{$ap}
345 % } keys %{$results{$ap}}) {
346 % my $item= $results{$ap}{$isles};
348 % my $rowid= "r${ap}$isles"; $rowid =~ y/ /_/;
349 % foreach my $k (qw(A P Leagues ArchesString
350 % Start RouteSortString Finish)) {
351 % $sortkeys{$ci}{$rowid}= $item->{$k};
354 <tr class="datarow<% $datarow %>" id="<% $rowid %>">
355 <td align=right><% $item->{A} |h %>
356 <td align=right><% $item->{P} |h %>
357 <td align=right><% $item->{Leagues} |h %>
358 <td align=left><a href="<% $item->{Url} |h %>"><%
359 $item->{ArchesString} |h %></a>
360 <td align=left><% $item->{Start} |h %>,
361 <td align=left><% $item->{ViasString} |h %>
362 <td align=left><% $item->{Finish} |h %>
367 <&| tabsort, table => "ap${ap}_table", sortkeys => "ap${ap}_sortkeys",
368 throw => "ap${ap}_sortrow", rowclass => "datarow", cols => [
369 { DoReverse => 1, Numeric => 1 },
370 { DoReverse => 1, Numeric => 1 },
371 { DoReverse => 1, Numeric => 1 },
377 ap<% $ap %>_sortkeys= <% to_json_protecttags(\%sortkeys) %>;
385 Per league values count each island visited as one
386 (additional) league; the "Dist." column is however the actual distance
387 to be sailed. All profit figures are somewhat approximate; get a
388 complete trading plan for a route for accurate information.