chiark / gitweb /
WIP routesearch: actually find the database
[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 $qa= \%ARGS;
56 my $routeparams= { EmsgRef => \$emsg, SayRequiredCapacity => 1 };
57 my $maxdist;
58
59 </%perl>
60
61 <h1>Find most profitable routes and trades</h1>
62
63 % if ($qa->{Dropdowns}) {
64 This feature is not available from the "drop down menus" interface.
65 % } else {
66
67 <form action="<% $quri->() |h %>" method="get">
68
69 <& enter_route, qa=>$qa, dbh=>$dbh, emsg_r=>\$emsg, warningfs_r=>\@warningfs,
70         enterwhat => 'Enter starting point(s)',
71         islandids_r => \@islandids, archipelagoes_r => undef
72  &>
73
74 <&| enter_advrouteopts, qa=>$qa, dbh=>$dbh, routeparams=>$routeparams &>
75 <td>
76 &nbsp;
77 &nbsp;
78 <td>
79  Maximum distance:
80  <&| qtextstring, qa => $qa, dbh => $dbh, prefix => 'ml',
81     thingstring => 'distance', emsgstore => \$emsg,
82     onresults => sub { ($maxdist)= @_; } &>
83    size=10
84  </&>
85 </&>
86
87 <input type=submit name=submit value="Go">
88 % my $ours= sub { $_[0] =~ m/^lossperleague|^islandstring|^capitalstring|^capacitystring|^distance/; };
89 <& "lookup:formhidden", ours => $ours &>
90
91 % }
92
93 </form>
94 <%perl>
95
96 if (!$emsg && $maxdist > 30) {
97         $emsg= "Searching for routes of more than 30 leagues is not".
98                 " supported, sorry.";
99 }
100
101 $emsgokorprint->($emsg) or return;
102 @islandids or return;
103 defined $routeparams->{MaxMass} or defined $routeparams->{MaxVolume} or return;
104
105 #---------- compute the results ----------
106
107 my @rsargs;
108
109 foreach my $k (qw(MaxMass MaxVolume MaxCapital)) {
110         my $v= $routeparams->{$k};
111         push @rsargs, (defined $v ? $v : -1);
112 }
113 push @rsargs, defined $routeparams->{LossPerLeaguePct}
114         ? $routeparams->{LossPerLeaguePct}*0.01 : 1e-9;
115 push @rsargs, qw(search 10 10), $maxdist, 'any', @islandids;
116
117 m/[^-.0-9a-zA-Z]/ and die "$_ $& ?" foreach @rsargs;
118
119 unshift @rsargs, dbw_filename($qa->{'Ocean'});
120 unshift @rsargs, qw(-DN);
121
122 if ($qa->{'debug'}) {
123 </%perl>
124 [[ <% "@rsargs" |h %> ]]<br><pre>
125 <%perl>
126 }
127
128 unshift @rsargs, sourcebasedir().'/yarrg/routesearch';
129
130 my $fh= new IO::File;
131 my $child= $fh->open("-|"); defined $child or die $!;
132 if (!$child) {
133         my $cpu= BSD::Resource::RLIMIT_CPU;
134         my ($soft,$hard)= getrlimit($cpu);
135         my $max=10;
136         setrlimit($cpu,$max,$hard) or die $! if $soft>$max;
137         exec @rsargs;
138         die $!;
139 }
140
141 while (<$fh>) {
142         chomp;
143         if ($qa->{'debug'}) {
144 </%perl>
145 <% $_ |h %>
146 <%perl>
147         }
148 }
149
150 if ($qa->{'debug'}) {
151         print "</pre>\n";
152 }
153
154 </%perl>