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 => '';
62 my @maxmaxdist= qw(35 35 60);
67 my $routeparams= { EmsgRef => \$emsg, SayRequiredCapacity => 1 };
74 <h1>Find most profitable routes and trades</h1>
76 % my $searchtype= $ARGS{RouteSearchType};
77 % my $searchtype_show;
79 % if ($qa->{Dropdowns}) {
80 This feature is not available from the "drop down menus" interface.
83 % $prselector->('RouteSearchType');
85 <form action="<% $quri->() |h %>" method="get">
87 <& enter_route, qa=>$qa, dbh=>$dbh, emsg_r=>\$emsg, warningfs_r=>\@warningfs,
88 enterwhat => 'Enter starting point(s)',
89 islandids_r => \@islandids, archipelagoes_r => undef
92 % if ($searchtype == 0) {
94 % $searchtype_show= 'open-ended';
95 % } elsif ($searchtype == 1) {
97 % $searchtype_show= 'circular';
98 % } elsif ($searchtype == 2) {
99 % $searchtype_show= 'specific-destination';
101 Destination (one island only):
102 <& qtextstring, qa => $qa, dbh => $dbh, emsgstore => \$emsg,
103 thingstring => 'deststring',
104 prefix => 'ds', boxopts => 'size=40',
106 print STDERR "ONRESULTS @_\n";
108 my ($canonname, $island, $arch) = @{ $_[0] };
109 die unless defined $island;
118 <&| enter_advrouteopts, qa=>$qa, dbh=>$dbh, routeparams=>$routeparams &>
124 <& qtextstring, qa => $qa, dbh => $dbh, prefix => 'ml',
125 thingstring => 'distance', emsgstore => \$emsg, boxopts => 'size=10',
126 onresults => sub { ($maxdist)= @_; }
130 <input type=submit name=submit value="Search">
131 % my $ours= sub { $_[0] =~ m/^lossperleague|^islandstring|^deststring|^capitalstring|^capacitystring|^minprofitstring|^distance/; };
132 <& "lookup:formhidden", ours => $ours &>
138 <div class="results">
141 my $maxmaxdist= $maxmaxdist[$searchtype];
142 if (!$emsg && $maxdist > $maxmaxdist) {
143 $emsg= "Searching for $searchtype_show routes".
144 " of more than $maxmaxdist leagues is not".
145 " supported, sorry.";
148 print("</div>"), return
149 unless $emsgokorprint->($emsg)
151 and $allargs->{'submit'}
152 and defined $destspec
153 and (defined $routeparams->{MaxMass} or
154 defined $routeparams->{MaxVolume});
156 #---------- prepare island names ----------
158 my $islandname_stmt= $dbh->prepare(<<END);
159 SELECT islandname, archipelago
166 $islandname_stmt->execute($id);
167 my $row= $islandname_stmt->fetchrow_hashref();
168 local $_= $row->{'islandname'};
170 return $_, $row->{'islandname'}, $row->{'archipelago'};
173 #---------- compute the results ----------
175 my @rsargs= ($concur_lim, '-DN');
178 foreach my $k (qw(MaxMass MaxVolume MaxCapital)) {
179 my $v= $routeparams->{$k};
180 push @rsargs, (defined $v ? $v : -1);
182 push @rsargs, defined $routeparams->{LossPerLeaguePct}
183 ? $routeparams->{LossPerLeaguePct}*0.01 : 1e-9;
184 push @rsargs, $routeparams->{MinProfit};
185 push @rsargs, 'search',$maxdist, $maxcountea,$maxcountea;
186 push @rsargs, $destspec;
187 push @rsargs, @islandids;
189 m/[^-.0-9a-zA-Z]/ and die "$_ $& ?" foreach @rsargs;
191 if ($qa->{'debug'}) {
193 [[ <% "@rsargs" |h %> ]]<br><pre>
198 'nice', sourcebasedir().'/yarrg/routesearch',
199 '-d', dbw_filename($qa->{'Ocean'}),
200 '-C', webdatadir().'/_concur.', '.lock';
202 # touch _concur.0{0,1,2,3,4}.lock
203 # really chgrp www-data _concur.0?.lock
205 my %results; # $results{$ap}{"5 6 9 10"} = { stuff }
207 my $fh= new IO::File;
208 my $child= $fh->open("-|"); defined $child or die $!;
210 my $cpu= BSD::Resource::RLIMIT_CPU;
211 my ($soft,$hard)= getrlimit($cpu);
212 setrlimit($cpu,$maxcpu,$hard) or die $! if $hard<=$maxcpu;
219 if ($qa->{'debug'}) {
224 next unless m/^\s*\@/;
225 if (m/^\@\@\@ concurrency limit exceeded/) {
229 die unless m/^ \@ *\d+ ([ap])\# *\d+ \|.*\| *(\d+)lg *\| *\d+ +(\d+) +(\d+) *\| ([0-9 ]+)$/;
230 my ($ap,$isles) = (uc $1,$5);
231 next if $results{$ap} && %{$results{$ap}} >= $maxcountea;
232 my $item= { A => $3, P => $4, Leagues => $2 };
234 foreach (split / /, $isles) {
235 my ($name,$fullname,$arch)= $isleinfo->($_);
238 push @a, $arch unless @a && $a[-1] eq $arch;
240 $item->{Isles}= [ @i ];
241 $item->{Archs}= [ @a ];
242 $item->{Start}= $i[0];
243 $item->{Finish}= $i[-1];
246 for ($i=1; $i < @i-1; $i++) {
247 push @{ $item->{Vias} }, $i[$i];
249 my %linkqf= (%$baseqf, %$queryqf);
250 delete $linkqf{'query'};
251 $linkqf{'routestring'}= join ', ', @fi;
252 $item->{Url}= $quri->(%linkqf);
253 $item->{ArchesString}= join ', ', @a;
254 $item->{ViasString}= join ' ', map { $_.',' } @{ $item->{Vias} };
255 $item->{RouteSortString}= join ', ', @i;
256 $results{$ap}{$isles}= $item;
259 if ($qa->{'debug'}) {
266 die $? if $? != 24; # SIGXCPU but not in POSIX.pm :-/
268 % $someresults->('Search took too long and was terminated');
270 Sorry, but your query resulted in a search that took too long.
271 Searches are limited to <% $maxcpu |h %> seconds of CPU time to
272 avoid them consuming excessive resources on the server system, and to
273 make sure that shorter searches can still happen.
276 Please try a search with a smaller maximum distance, or place more
277 restrictions on the route.
286 % $someresults->('Server too busy');
288 Sorry, but there are already <% $concur_lim |h %> route searches
289 running. We limit the number which can run at once to avoid
290 overloading the server system and to make sure that the rest of the
291 YARRG website still runs quickly.
294 If you submitted several searches and gave up on them (eg by hitting
295 "back" or "stop" in your browser), be aware that that doesn't
296 generally stop the search process at the server end. So it's best to
297 avoid asking for large searches that you're not sure about.
300 Otherwise, please try later. Searches are limited to <% $maxcpu |h %>
301 seconds of CPU time so more processing resources should be available soon.
311 % foreach my $ap (qw(A P)) {
313 <h2>Best routes for total profit</h2>
315 <h2>Best routes for profit per league</h2>
317 <table class="data" rules=groups id="ap<% $ap %>_table">
337 <tr id="ap<% $ap %>_sortrow"><th><th><th><th><th><th><th>
340 % foreach my $isles (sort {
341 % $results{$ap}{$b}{$ap} <=>
342 % $results{$ap}{$a}{$ap}
343 % } keys %{$results{$ap}}) {
344 % my $item= $results{$ap}{$isles};
346 % my $rowid= "r${ap}$isles"; $rowid =~ y/ /_/;
347 % foreach my $k (qw(A P Leagues ArchesString
348 % Start RouteSortString Finish)) {
349 % $sortkeys{$ci}{$rowid}= $item->{$k};
352 <tr class="datarow<% $datarow %>" id="<% $rowid %>">
353 <td align=right><% $item->{A} |h %>
354 <td align=right><% $item->{P} |h %>
355 <td align=right><% $item->{Leagues} |h %>
356 <td align=left><a href="<% $item->{Url} |h %>"><%
357 $item->{ArchesString} |h %></a>
358 <td align=left><% $item->{Start} |h %>,
359 <td align=left><% $item->{ViasString} |h %>
360 <td align=left><% $item->{Finish} |h %>
365 <&| tabsort, table => "ap${ap}_table", sortkeys => "ap${ap}_sortkeys",
366 throw => "ap${ap}_sortrow", rowclass => "datarow", cols => [
367 { DoReverse => 1, Numeric => 1 },
368 { DoReverse => 1, Numeric => 1 },
369 { DoReverse => 1, Numeric => 1 },
375 ap<% $ap %>_sortkeys= <% to_json_protecttags(\%sortkeys) %>;
383 Per league values count each island visited as one
384 (additional) league; the "Dist." column is however the actual distance
385 to be sailed. All profit figures are somewhat approximate; get a
386 complete trading plan for a route for accurate information.