<%doc> This is part of the YARRG website. YARRG is a tool and website for assisting players of Yohoho Puzzle Pirates. Copyright (C) 2009 Ian Jackson Copyright (C) 2009 Clare Boothby YARRG's client code etc. is covered by the ordinary GNU GPL (v3 or later). The YARRG website is covered by the GNU Affero GPL v3 or later, which basically means that every installation of the website will let you download the source. This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . Yohoho and Puzzle Pirates are probably trademarks of Three Rings and are used without permission. This program is not endorsed or sponsored by Three Rings. This Mason component generates the core of the `routesearch' query. <%args> $quri $dbh $islandstring => ''; $capacitystring => ''; $lossperleague => ''; $capitalstring => ''; $distance => ''; $someresults $emsgokorprint <%perl> use BSD::Resource; my $emsg; my @warningfs; my @islandids; my $qa= \%ARGS; my $routeparams= { EmsgRef => \$emsg, SayRequiredCapacity => 1 }; my $maxdist; my $maxcountea=10; my $maxcpu=10;

Find most profitable routes and trades

% if ($qa->{Dropdowns}) { This feature is not available from the "drop down menus" interface. % } else {
<& enter_route, qa=>$qa, dbh=>$dbh, emsg_r=>\$emsg, warningfs_r=>\@warningfs, enterwhat => 'Enter starting point(s)', islandids_r => \@islandids, archipelagoes_r => undef &> <&| enter_advrouteopts, qa=>$qa, dbh=>$dbh, routeparams=>$routeparams &>     Maximum distance: <&| qtextstring, qa => $qa, dbh => $dbh, prefix => 'ml', thingstring => 'distance', emsgstore => \$emsg, onresults => sub { ($maxdist)= @_; } &> size=10 % my $ours= sub { $_[0] =~ m/^lossperleague|^islandstring|^capitalstring|^capacitystring|^distance/; }; <& "lookup:formhidden", ours => $ours &> % }
<%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; #---------- prepare island names ---------- my $islandname_stmt= $dbh->prepare(<execute($id); my $row= $islandname_stmt->fetchrow_hashref(); local $_= $row->{'islandname'}; s/ Island$//; return $_, $row->{'archipelago'}; }; #---------- compute the results ---------- my @rsargs= qw(-DN); 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, 'search',$maxdist, $maxcountea,$maxcountea, 'any', @islandids; m/[^-.0-9a-zA-Z]/ and die "$_ $& ?" foreach @rsargs; if ($qa->{'debug'}) { [[ <% "@rsargs" |h %> ]]
<%perl>
}

unshift @rsargs, sourcebasedir().'/yarrg/routesearch',
	'-d', dbw_filename($qa->{'Ocean'});

my %results; # $results{$ap}{"5 6 9 10"} = { stuff }

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);
	setrlimit($cpu,$maxcpu,$hard) or die $! if $soft>$maxcpu;
	exec @rsargs;
	die $!;
}

while (<$fh>) {
	chomp;
	if ($qa->{'debug'}) {

<% $_ |h %>
<%perl>
	}
	next unless
  m/^ \@ *\d+ ([ap])\# *\d+ \|.*\| *\d+ +(\d+) +(\d+) *\| ([0-9 ]+)$/;
	my ($ap,$isles) = (uc $1,$4);
	next if $results{$ap} && %{$results{$ap}} >= $maxcountea;
	my $item= { A => $2, P => $3 };
	my (@i, @a);
	foreach (split / /, $isles) {
		my ($name,$arch)= $isleinfo->($_);
		push @i, $name;
		push @a, $arch unless @a && $a[-1] eq $arch;
	}
	$item->{Isles}= [ @i ];
	$item->{Archs}= [ @a ];
	$item->{Start}= $i[0];
	$item->{Finish}= $i[-1];
	$item->{Vias}= [ ];
	my $i;
	for ($i=1; $i < @i-1; $i++) {
		push @{ $item->{Vias} }, $i[$i];
	}
	$results{$ap}{$isles}= $item;
}

if ($qa->{'debug'}) {
	print "
\n"; } % foreach my $ap (qw(A P)) {

ap=<% $ap %>

% my $datarow=0; % foreach my $isles (sort { % $results{$ap}{$b}{$ap} <=> % $results{$ap}{$a}{$ap} % } keys %{$results{$ap}}) { % my $item= $results{$ap}{$isles}; % $datarow ^= 1; % } # $isles
Profit Archipelagoes Route
Abs. Per.lg. Start Via Finish
<% $item->{A} |h %> <% $item->{P} |h %> <% join ', ', @{ $item->{Archs} } |h %> <% $item->{Start} |h %>, <% join ' ', map { $_.',' } @{ $item->{Vias} } |h %> <% $item->{Finish} |h %>
% } # $ap <%perl>