chiark / gitweb /
e5e7c6b98619c48174328badb9e501623113df47
[ypp-sc-tools.db-test.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 $islandstring => '';
40 $capacitystring => '';
41 $lossperleague => '';
42 $capitalstring => '';
43 $distance => '';
44 $someresults
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->{'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, @a);
186         foreach (split / /, $isles) {
187                 my ($name,$arch)= $isleinfo->($_);
188                 push @i, $name;
189                 push @a, $arch unless @a && $a[-1] eq $arch;
190         }
191         $item->{Isles}= [ @i ];
192         $item->{Archs}= [ @a ];
193         $item->{Start}= $i[0];
194         $item->{Finish}= $i[-1];
195         $item->{Vias}= [ ];
196         my $i;
197         for ($i=1; $i < @i-1; $i++) {
198                 push @{ $item->{Vias} }, $i[$i];
199         }
200         $results{$ap}{$isles}= $item;
201 }
202
203 if ($qa->{'debug'}) {
204         print "</pre>\n";
205 }
206
207 $!=0;
208 if (!close $fh) {
209         die $! if $!;
210         die $? if $? != 24; # SIGXCPU but not in POSIX.pm :-/
211 </%perl>
212 <h2>Search took too long and was terminated</h2>
213
214 Sorry, but your query resulted in a search that took too long.
215 Searches are limited to <% $maxcpu |h %> seconds of CPU time to
216 avoid them consuming excessive resources on the server system, and to
217 make sure that shorter searches can still happen.
218
219 <p>
220 Please try a search with a smaller minimum distance, or place more
221 restrictions on the route.
222
223 <%perl>
224         return;
225 }
226
227 if ($concur_fail) {
228 </%perl>
229 <h2>Server too busy</h2>
230
231 Sorry, but there are already <% $concur_lim |h %> route searches
232 running.  We limit the number which can run at once to avoid
233 overloading the server system and to make sure that the rest of the
234 YARRG website still runs quickly.
235 <p>
236
237 If you submitted several searches and gave up on them (eg by hitting
238 `back' or `stop' in your browser), be aware that that doesn't
239 generally stop the search process at the server end.  So it's best to
240 avoid asking for large searches that you're not sure about.
241
242 <p>
243 Otherwise, please try later.  Searches are limited to <% $maxcpu |h %>
244 seconds of CPU time so more processing resources should be available soon.
245
246 <%perl>
247         return;
248 }
249
250 </%perl>
251 % foreach my $ap (qw(A P)) {
252 %       if ($ap eq 'A') {
253 <h2>Best routes for total profit</h2>
254 %       } else {
255 <h2>Best routes for profit per league</h2>
256 %       }
257 <table rules=groups>
258 <colgroup span=2>
259 <colgroup span=1>
260 <colgroup span=1>
261 <colgroup span=3>
262 <tbody>
263 <tr>
264 <th colspan=2>Profit
265 <th>Dist.
266 <th>Archipelagoes
267 <th colspan=3>Route
268 <tr>
269 <th>Abs.
270 <th>Per.lg.
271 <th>
272 <th>
273 <th>Start
274 <th>Via
275 <th>Finish
276 <tbody>
277 %       my $datarow=0;
278 %       foreach my $isles (sort {
279 %                       $results{$ap}{$b}{$ap} <=>
280 %                       $results{$ap}{$a}{$ap}
281 %               } keys %{$results{$ap}}) {
282 %               my $item= $results{$ap}{$isles};
283 <tr class="datarow<% $datarow %>">
284 <td align=right><% $item->{A} |h %>
285 <td align=right><% $item->{P} |h %>
286 <td align=right><% $item->{Leagues} |h %>
287 <td align=left><% join ', ', @{ $item->{Archs} } |h %>
288 <td align=left><% $item->{Start} |h %>,
289 <td align=left><% join ' ', map { $_.',' } @{ $item->{Vias} } |h %>
290 <td align=left><% $item->{Finish} |h %>
291 </td>
292 %               $datarow ^= 1;
293 %       } # $isles
294 </table>
295 % } # $ap
296
297 <p>
298
299 <h2>Notes</h2>
300
301 Per league values count each island visited as one
302 (additional) league; the `Dist.' column is however the actual distance
303 to be sailed.  All profit figures are somewhat approximate; get a
304 complete trading plan for a route for accurate information.
305
306 <%perl>
307
308
309 </%perl>