3 This is part of the YARRG website. YARRG is a tool and website
4 for assisting players of Yohoho Puzzle Pirates.
6 Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
7 Copyright (C) 2009 Clare Boothby
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
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.
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.
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/>.
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.
32 This Mason component generates the core of the `commodity' query.
47 my ($commodname,$commodid);
52 <h1>Commodity enquiry</h1>
54 % $prselector->('BuySell');
55 % $prselector->('ShowBlank');
57 %#---------- textbox, user enters route as string ----------
58 % if (!$qa->{Dropdowns}) {
60 Enter commodity (abbreviations are OK):<br>
62 <form action="<% $quri->() |h %>" method="get">
64 <&| qtextstring, qa => $qa, dbh => $dbh,
65 thingstring => 'commodstring', emsgstore => \$emsg,
66 perresult => sub { ($commodname,$commodid)= @_; }
71 % } else { #---------- dropdowns, user selects from menus ----------
75 % } #---------- end of dropdowns, now common middle of page code ----------
77 <input type=submit name=submit value="Go">
78 % my $ours= sub { $_[0] =~ m/^commodstring|^commodid/; };
79 <& "lookup:formhidden", ours => $ours &>
83 %#========== results ==========
86 $emsgokorprint->($emsg) or $commodid=undef;
87 return unless defined $commodid;
90 #---------- actually compute the results and print them ----------
92 foreach my $bs (split /_/, $ARGS{BuySell}) {
93 $bs =~ m/^(buy|sell)$/ or die;
95 my ($ascdesc) = ($bs eq 'buy')
98 my $joinkind= $ARGS{ShowBlank} eq 'show'
99 ? 'LEFT OUTER JOIN' : 'INNER JOIN';
100 my $islands= $dbh->prepare(
101 "SELECT islands.islandid AS islandid, archipelago, islandname,
103 FROM islands $joinkind $bs offers
104 ON islands.islandid == offers.islandid AND commodid == ?
105 GROUP BY islands.islandid,
106 ORDER BY archipelago, islandname"
109 my $offers= $dbh->prepare(
110 "SELECT stallname, price, qty
111 FROM $bs NATURAL JOIN stalls
112 WHERE commodid = ? AND islandid = ?
113 ORDER BY price $ascdesc"
115 # fixme this query is utterly wrong
119 <h2>Offers to <% uc $bs |h %> <% $commodname |h %></h2>
125 <th colspan=3>Quantities available
129 <th>Unique best stall
136 % $islands->execute($commodid);
138 % while ($island= $islands->fetchrow_hashref) {
139 % my $islandid= $island->{'islandid'};
140 % $offers->execute($commodid, $islandid);
141 % my ($offer, $bestprice, $marginal, @beststalls);
142 % my $tqty= $island->{'tqty'};
147 % while ($offer= $offers->fetchrow_hashref) {
148 % my $price= $offer->{'price'};
149 % my $qty= $offer->{'qty'};
150 % length $bestqty or $bestprice= $price;
151 % if ($price == $bestprice) {
153 % push @beststalls, $offer->{'stallname'};
156 % if ($cqty*2 >= $tqty && $median eq '-') {
159 % if ($bestprice*9 <= $price*10 and
160 % $price*10 <= $bestprice*11) {
161 % $approxqty += $qty;
164 % my $nstalls= @beststalls;
165 % $cqty == $tqty or die "$bs $cqty $tqty $commodid $islandid ";
166 <tr> <td><% $island->{'archipelago'} |h %>
167 <td><% $island->{'islandname'} |h %>
168 <td><% $nstalls==0 ? '-' :
169 $nstalls==1 ? $beststalls[0] : "$nstalls offers" |h %>
170 <td><% length $bestqty ? $bestprice : '-' %>