chiark / gitweb /
8937f285e9f9c77e6f3a77585b5471b6bd3de44e
[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 $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 if ($concur_fail) {
208 </%perl>
209 <h2>Server too busy</h2>
210
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.
215 <p>
216
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.
221
222 <p>
223 Otherwise, please try later.  Searches are limited to <% $maxcpu |h %>
224 seconds of CPU time so more processing resources should be available soon.
225
226 <%perl>
227         return;
228 }
229
230 </%perl>
231 % foreach my $ap (qw(A P)) {
232 <h2>ap=<% $ap %></h2>
233 <table rules=groups>
234 <colgroup span=2>
235 <colgroup span=1>
236 <colgroup span=1>
237 <colgroup span=3>
238 <tbody>
239 <tr>
240 <th colspan=2>Profit
241 <th>Dist.
242 <th>Archipelagoes
243 <th colspan=3>Route
244 <tr>
245 <th>Abs.
246 <th>Per.lg.
247 <th>
248 <th>
249 <th>Start
250 <th>Via
251 <th>Finish
252 <tbody>
253 %       my $datarow=0;
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 %>
267 </td>
268 %               $datarow ^= 1;
269 %       } # $isles
270 </table>
271 % } # $ap
272 <%perl>
273
274
275 </%perl>