<%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 `commodity' query. <%args> $quri $dbh $commodid => undef; $commodstring => ''; $prselector $someresults $emsgokorprint <%perl> my $emsg; my ($commodname,$cmid); my $qa= \%ARGS;

Commodity enquiry

% $prselector->('BuySell'); % $prselector->('ShowBlank');
<& enter_commod, qa => $qa, dbh => $dbh, emsg_r => \$emsg, commodname_r => \$commodname, cmid_r => \$cmid &> % my $ours= sub { $_[0] =~ m/^commodstring|^commodid/; }; <& "lookup:formhidden", ours => $ours &>
%#========== results ========== <%perl> $emsgokorprint->($emsg) or $cmid=undef; return unless defined $cmid; $someresults->(); #---------- actually compute the results and print them ---------- foreach my $bs (split /_/, $ARGS{BuySell}) { $bs =~ m/^(buy|sell)$/ or die; $bs= $1; my ($ascdesc) = ($bs eq 'buy') ? ('DESC') : ('ASC'); my $joinkind= $ARGS{ShowBlank} ? 'LEFT OUTER JOIN' : 'INNER JOIN'; my $islands= $dbh->prepare( "SELECT islands.islandid AS islandid, archipelago, islandname, sum(qty) as tqty FROM islands $joinkind $bs offers ON islands.islandid == offers.islandid AND commodid == ? GROUP BY islands.islandid ORDER BY archipelago, islandname" ); my $offers= $dbh->prepare( "SELECT stallname, price, qty FROM $bs NATURAL JOIN stalls WHERE commodid = ? AND islandid = ? ORDER BY price $ascdesc" );

Offers to <% uc $bs |h %> <% $commodname |h %>

% $islands->execute($cmid); % my $island; % my %ts_sortkeys; % my $rowix= 0; % while ($island= $islands->fetchrow_hashref) { % if (!$rowix) { % } % my $islandid= $island->{'islandid'}; % $offers->execute($cmid, $islandid); % my ($offer, $bestprice, $marginal, @beststalls); % my $tqty= $island->{'tqty'}; % my $cqty= ''; % my $bestqty= ''; % my $approxqty= ''; % my $median= '-'; % while ($offer= $offers->fetchrow_hashref) { % my $price= $offer->{'price'}; % my $qty= $offer->{'qty'}; % length $bestqty or $bestprice= $price; % if ($price == $bestprice) { % $bestqty += $qty; % push @beststalls, $offer->{'stallname'}; % } % $cqty += $qty; % if ($cqty*2 >= $tqty && $median eq '-') { % $median= $price; % } % if ($bestprice*9 <= $price*10 and % $price*10 <= $bestprice*11) { % $approxqty += $qty; % } % } % my $stallname; % % my $rowid= "id_${bs}_$islandid"; % my $s= [ ]; % % $s->[2]= sprintf "%06d", scalar @beststalls; % if (!@beststalls) { % $stallname= '-'; % } elsif (@beststalls==1) { % $stallname= $beststalls[0]; % $s->[2] .= " $stallname"; % } else { % $stallname= sprintf "%d offers", scalar @beststalls; % } % % $cqty == $tqty or die "$bs $cqty $tqty $cmid $islandid "; class="<% 'datarow'.($rowix & 1) %>"> % for my $cix (0..$#$s) { % $ts_sortkeys{$cix}{$rowid}= $s->[$cix]; % } % $rowix++; % } % if ($rowix) {
Prices Quantity at price
Archipelago Island Stall(s) Best Median Best +/-10% Any
<% $s->[0]= $island->{'archipelago'} |h %> <% $s->[1]= $island->{'islandname'} |h %> <% $stallname |h %> <% $s->[3]= (length $bestqty ? $bestprice : '-') %> <% $s->[4]= $median %> <% $s->[5]= $bestqty %> <% $s->[6]= $approxqty %> <% $s->[7]= $cqty %>
<&| tabsort, table => "${bs}_table", sortkeys => "${bs}_sortkeys", throw => "${bs}_table_thr", rowclass => 'datarow', cols => [ {}, {}, { DoReverse => 1 }, { DoReverse => 1, Numeric => 1, MapFn => "ts_Pricemap_${bs}" }, { DoReverse => 1, Numeric => 1, MapFn => "ts_Pricemap_${bs}" }, { DoReverse => 1, Numeric => 1 }, { DoReverse => 1, Numeric => 1 }, { DoReverse => 1, Numeric => 1 }, ] &> <% $bs %>_sortkeys= <% to_json_protecttags(\%ts_sortkeys) %>; function ts_Pricemap_<% $bs %>(price) { if (price=='-') { return <% $bs eq 'buy' ? '-1' : '99999999' %>; } return price; } % } else { No offers. % } <%perl> }