chiark / gitweb /
WIP routesearch: actually find the database
[ypp-sc-tools.main.git] / yarrg / web / query_routesearch
index 53128f6483d255723b1ec4df69b2617dbde54387..9accc54bbf2dbf31987948bc2e8e2606457f2ba9 100644 (file)
 <%args>
 $quri
 $dbh
+$islandstring => '';
 $capacitystring => '';
 $lossperleague => '';
 $capitalstring => '';
+$distance => '';
+$someresults
 $emsgokorprint
 </%args>
 
 <%perl>
+use BSD::Resource;
+
 my $emsg;
 my @warningfs;
 my @islandids;
 
 my $qa= \%ARGS;
-my $routeparams= { EmsgRef => \$emsg };
+my $routeparams= { EmsgRef => \$emsg, SayRequiredCapacity => 1 };
 my $maxdist;
+
 </%perl>
 
 <h1>Find most profitable routes and trades</h1>
@@ -67,12 +73,82 @@ This feature is not available from the "drop down menus" interface.
 
 <&| enter_advrouteopts, qa=>$qa, dbh=>$dbh, routeparams=>$routeparams &>
 <td>
- Something:
+&nbsp;
+&nbsp;
+<td>
+ Maximum distance:
  <&| qtextstring, qa => $qa, dbh => $dbh, prefix => 'ml',
-    thingstring => 'capitalstring', emsgstore => \$emsg,
+    thingstring => 'distance', emsgstore => \$emsg,
     onresults => sub { ($maxdist)= @_; } &>
+   size=10
  </&>
 </&>
 
+<input type=submit name=submit value="Go">
+% my $ours= sub { $_[0] =~ m/^lossperleague|^islandstring|^capitalstring|^capacitystring|^distance/; };
+<& "lookup:formhidden", ours => $ours &>
 
 % }
+
+</form>
+<%perl>
+
+if (!$emsg && $maxdist > 30) {
+       $emsg= "Searching for routes of more than 30 leagues is not".
+               " supported, sorry.";
+}
+
+$emsgokorprint->($emsg) or return;
+@islandids or return;
+defined $routeparams->{MaxMass} or defined $routeparams->{MaxVolume} or return;
+
+#---------- compute the results ----------
+
+my @rsargs;
+
+foreach my $k (qw(MaxMass MaxVolume MaxCapital)) {
+       my $v= $routeparams->{$k};
+       push @rsargs, (defined $v ? $v : -1);
+}
+push @rsargs, defined $routeparams->{LossPerLeaguePct}
+       ? $routeparams->{LossPerLeaguePct}*0.01 : 1e-9;
+push @rsargs, qw(search 10 10), $maxdist, 'any', @islandids;
+
+m/[^-.0-9a-zA-Z]/ and die "$_ $& ?" foreach @rsargs;
+
+unshift @rsargs, dbw_filename($qa->{'Ocean'});
+unshift @rsargs, qw(-DN);
+
+if ($qa->{'debug'}) {
+</%perl>
+[[ <% "@rsargs" |h %> ]]<br><pre>
+<%perl>
+}
+
+unshift @rsargs, sourcebasedir().'/yarrg/routesearch';
+
+my $fh= new IO::File;
+my $child= $fh->open("-|"); defined $child or die $!;
+if (!$child) {
+       my $cpu= BSD::Resource::RLIMIT_CPU;
+       my ($soft,$hard)= getrlimit($cpu);
+       my $max=10;
+       setrlimit($cpu,$max,$hard) or die $! if $soft>$max;
+       exec @rsargs;
+       die $!;
+}
+
+while (<$fh>) {
+       chomp;
+       if ($qa->{'debug'}) {
+</%perl>
+<% $_ |h %>
+<%perl>
+       }
+}
+
+if ($qa->{'debug'}) {
+       print "</pre>\n";
+}
+
+</%perl>