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.
40 $capacitystring => '';
60 my $routeparams= { EmsgRef => \$emsg, SayRequiredCapacity => 1 };
66 <h1>Find most profitable routes and trades</h1>
68 % if ($qa->{Dropdowns}) {
69 This feature is not available from the "drop down menus" interface.
72 <form action="<% $quri->() |h %>" method="get">
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
79 <&| enter_advrouteopts, qa=>$qa, dbh=>$dbh, routeparams=>$routeparams &>
85 <&| qtextstring, qa => $qa, dbh => $dbh, prefix => 'ml',
86 thingstring => 'distance', emsgstore => \$emsg,
87 onresults => sub { ($maxdist)= @_; } &>
92 <input type=submit name=submit value="Go">
93 % my $ours= sub { $_[0] =~ m/^lossperleague|^islandstring|^capitalstring|^capacitystring|^distance/; };
94 <& "lookup:formhidden", ours => $ours &>
101 if (!$emsg && $maxdist > $maxmaxdist) {
102 $emsg= "Searching for routes of more than $maxmaxdist leagues is not".
103 " supported, sorry.";
106 $emsgokorprint->($emsg) or return;
107 @islandids or return;
108 defined $routeparams->{MaxMass} or defined $routeparams->{MaxVolume} or return;
110 #---------- prepare island names ----------
112 my $islandname_stmt= $dbh->prepare(<<END);
113 SELECT islandname, archipelago
120 $islandname_stmt->execute($id);
121 my $row= $islandname_stmt->fetchrow_hashref();
122 local $_= $row->{'islandname'};
124 return $_, $row->{'archipelago'};
127 #---------- compute the results ----------
129 my @rsargs= ($concur_lim, '-DN');
132 foreach my $k (qw(MaxMass MaxVolume MaxCapital)) {
133 my $v= $routeparams->{$k};
134 push @rsargs, (defined $v ? $v : -1);
136 push @rsargs, defined $routeparams->{LossPerLeaguePct}
137 ? $routeparams->{LossPerLeaguePct}*0.01 : 1e-9;
139 push @rsargs, 'search',$maxdist, $maxcountea,$maxcountea, 'any', @islandids;
141 m/[^-.0-9a-zA-Z]/ and die "$_ $& ?" foreach @rsargs;
143 if ($qa->{'debug'}) {
145 [[ <% "@rsargs" |h %> ]]<br><pre>
150 sourcebasedir().'/yarrg/routesearch',
151 '-d', dbw_filename($qa->{'Ocean'}),
152 '-C', webdatadir().'/_concur.', '.lock';
154 # touch _concur.0{0,1,2,3,4}.lock
155 # really chgrp www-data _concur.0?.lock
157 my %results; # $results{$ap}{"5 6 9 10"} = { stuff }
159 my $fh= new IO::File;
160 my $child= $fh->open("-|"); defined $child or die $!;
162 my $cpu= BSD::Resource::RLIMIT_CPU;
163 my ($soft,$hard)= getrlimit($cpu);
164 setrlimit($cpu,$maxcpu,$hard) or die $! if $hard<=$maxcpu;
171 if ($qa->{'debug'}) {
176 next unless m/^\s*\@/;
177 if (m/^\@\@\@ concurrency limit exceeded/) {
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 };
186 foreach (split / /, $isles) {
187 my ($name,$arch)= $isleinfo->($_);
189 push @a, $arch unless @a && $a[-1] eq $arch;
191 $item->{Isles}= [ @i ];
192 $item->{Archs}= [ @a ];
193 $item->{Start}= $i[0];
194 $item->{Finish}= $i[-1];
197 for ($i=1; $i < @i-1; $i++) {
198 push @{ $item->{Vias} }, $i[$i];
200 $results{$ap}{$isles}= $item;
203 if ($qa->{'debug'}) {
209 <h2>Server too busy</h2>
211 Sorry, but there are already <% $concur_lim |h %> route searches
212 running. We limit the number which can run at once to avoid
213 overloading the server system and to make sure that the rest of the
214 YARRG website still runs quickly.
217 If you submitted several searches and gave up on them (eg by hitting
218 `back' or `stop' in your browser), be aware that that doesn't
219 generally stop the search process at the server end. So it's best to
220 avoid asking for large searches that you're not sure about.
223 Otherwise, please try later. Searches are limited to <% $maxcpu |h %>
224 seconds of CPU time so more processing resources should be available soon.
231 % foreach my $ap (qw(A P)) {
232 <h2>ap=<% $ap %></h2>
254 % foreach my $isles (sort {
255 % $results{$ap}{$b}{$ap} <=>
256 % $results{$ap}{$a}{$ap}
257 % } keys %{$results{$ap}}) {
258 % my $item= $results{$ap}{$isles};
259 <tr class="datarow<% $datarow %>">
260 <td align=right><% $item->{A} |h %>
261 <td align=right><% $item->{P} |h %>
262 <td align=right><% $item->{Leagues} |h %>
263 <td align=left><% join ', ', @{ $item->{Archs} } |h %>
264 <td align=left><% $item->{Start} |h %>,
265 <td align=left><% join ' ', map { $_.',' } @{ $item->{Vias} } |h %>
266 <td align=left><% $item->{Finish} |h %>